Synchronization

An example demonstrate how one task could be execute only when other tasks are finished.
For example you have to make sure all your services are updated with new data before you notify customer.

Setup


mkdir ~/pvm-synchronization
cd ~/pvm-synchronization
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php synchronization.php

Result

synchronization.php

fork
update_warehouse
wait for other tasks to be finished
update_accounting
wait for other tasks to be finished
update_delivery
notify_customer

synchronization.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\Exception\InterruptExecutionException;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;

require_once 
__DIR__.'/vendor/autoload.php';

$process = (new ProcessBuilder())
    ->
createNode('fork''print_label')->end()
    ->
createNode('update_warehouse''print_label')->end()
    ->
createNode('update_accounting''print_label')->end()
    ->
createNode('update_delivery''print_label')->end()
    ->
createNode('notify_customer''print_label')->end()
    ->
createNode('join''join')->end()

    ->
createTransition('fork''update_warehouse')->end()
    ->
createTransition('fork''update_accounting')->end()
    ->
createTransition('fork''update_delivery')->end()
    ->
createTransition('update_warehouse''join')->end()
    ->
createTransition('update_accounting''join')->end()
    ->
createTransition('update_delivery''join')->end()
    ->
createTransition('join''notify_customer')->end()
    ->
createStartTransition('fork')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
    
'join' => function (Token $token) {
        static 
$weight 0;
        
$weight += $token->getCurrentTransition()->getWeight();

        if (
$weight === 3) {
            return;
        }

        echo 
'wait for other tasks to be finished'.PHP_EOL;

        throw new 
InterruptExecutionException();
    }
]));

$token $engine->createTokenFor($process->getStartTransition());
$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "904eaec2-bf19-4020-b4e8-3b742537687b",
    "nodes": {
        "fork": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "fork",
            "behavior": "print_label"
        },
        "update_warehouse": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "update_warehouse",
            "behavior": "print_label"
        },
        "update_accounting": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "update_accounting",
            "behavior": "print_label"
        },
        "update_delivery": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "update_delivery",
            "behavior": "print_label"
        },
        "notify_customer": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "notify_customer",
            "behavior": "print_label"
        },
        "join": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "join",
            "behavior": "join"
        }
    },
    "transitions": {
        "8e6c4b16-638f-46aa-94d3-11225cc42f2c": {
            "id": "8e6c4b16-638f-46aa-94d3-11225cc42f2c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "d34a6194-472a-43a6-8875-775b729dbaab": {
            "id": "d34a6194-472a-43a6-8875-775b729dbaab",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "ba86efbc-c4da-4290-a776-4d23ebc0b75f": {
            "id": "ba86efbc-c4da-4290-a776-4d23ebc0b75f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "fd710d18-d1ea-40d3-a06a-633a922ba6e5": {
            "id": "fd710d18-d1ea-40d3-a06a-633a922ba6e5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4": {
            "id": "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "429cbd1e-17f6-427a-a334-25b2e414dee9": {
            "id": "429cbd1e-17f6-427a-a334-25b2e414dee9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67": {
            "id": "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "95df813f-c06c-48b8-a3cc-0cbdfc35c8ec": {
            "id": "95df813f-c06c-48b8-a3cc-0cbdfc35c8ec",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "8e6c4b16-638f-46aa-94d3-11225cc42f2c",
            "d34a6194-472a-43a6-8875-775b729dbaab",
            "ba86efbc-c4da-4290-a776-4d23ebc0b75f"
        ],
        "update_warehouse": [
            "fd710d18-d1ea-40d3-a06a-633a922ba6e5"
        ],
        "update_accounting": [
            "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4"
        ],
        "update_delivery": [
            "429cbd1e-17f6-427a-a334-25b2e414dee9"
        ],
        "join": [
            "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "8e6c4b16-638f-46aa-94d3-11225cc42f2c"
        ],
        "update_accounting": [
            "d34a6194-472a-43a6-8875-775b729dbaab"
        ],
        "update_delivery": [
            "ba86efbc-c4da-4290-a776-4d23ebc0b75f"
        ],
        "join": [
            "fd710d18-d1ea-40d3-a06a-633a922ba6e5",
            "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4",
            "429cbd1e-17f6-427a-a334-25b2e414dee9"
        ],
        "notify_customer": [
            "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67"
        ],
        "fork": [
            "95df813f-c06c-48b8-a3cc-0cbdfc35c8ec"
        ]
    },
    "tokens": {
        "a601e4d0-ab07-4d73-95c9-7a3cb544d397": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a601e4d0-ab07-4d73-95c9-7a3cb544d397",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f25636fb-d4b0-4ffd-9c10-1d917d5be569",
                    "transitionId": "95df813f-c06c-48b8-a3cc-0cbdfc35c8ec",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850796
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "788b9ff2-987c-44ff-831f-fd499259388b",
                    "transitionId": "95df813f-c06c-48b8-a3cc-0cbdfc35c8ec",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850802
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "13a863c2-5665-44f0-ae05-84ffa100ffee",
                    "transitionId": "8e6c4b16-638f-46aa-94d3-11225cc42f2c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850806
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2003573c-440b-4170-8c2e-ac537816d50c",
                    "transitionId": "8e6c4b16-638f-46aa-94d3-11225cc42f2c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850811
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8a7da3d7-9ae7-45e1-964f-0125534bd771",
                    "transitionId": "fd710d18-d1ea-40d3-a06a-633a922ba6e5",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850814
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4cdbbdf1-4b80-4100-870d-cbed19bb3094",
                    "transitionId": "fd710d18-d1ea-40d3-a06a-633a922ba6e5",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15664822850819,
                    "reason": ""
                }
            ]
        },
        "da2b4d14-39b5-4efe-bac5-c04d857c2308": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "da2b4d14-39b5-4efe-bac5-c04d857c2308",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fd7ef782-2862-44a4-9478-822601c14fe5",
                    "transitionId": "d34a6194-472a-43a6-8875-775b729dbaab",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850822
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2e4d74ae-2f81-4fb7-b1bc-f0077c3b405e",
                    "transitionId": "d34a6194-472a-43a6-8875-775b729dbaab",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850828
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8f27012e-3df9-4d3b-a888-acd8aa7b8393",
                    "transitionId": "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850831
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e9fa16c5-112e-4169-9d2c-86e1a9073c8d",
                    "transitionId": "27362e9e-b4e8-4a1e-95a0-95f3fa1c95e4",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15664822850835,
                    "reason": ""
                }
            ]
        },
        "f0719511-3f91-4025-9f8f-ce1d410348c9": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "f0719511-3f91-4025-9f8f-ce1d410348c9",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "449c8d80-1446-40d6-b81b-db4e785e44bb",
                    "transitionId": "ba86efbc-c4da-4290-a776-4d23ebc0b75f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850840
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "333a5ab0-85bf-4184-9064-677b4610a94d",
                    "transitionId": "ba86efbc-c4da-4290-a776-4d23ebc0b75f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850844
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b774eec6-16ca-4508-bdf2-0c141859c927",
                    "transitionId": "429cbd1e-17f6-427a-a334-25b2e414dee9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850847
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8fedbadd-5f3a-40b1-bb9a-b36e883ca6bf",
                    "transitionId": "429cbd1e-17f6-427a-a334-25b2e414dee9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850853
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "06dc7624-29fa-4a61-9bd5-c8b8903a71a7",
                    "transitionId": "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664822850858
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b6c6736c-dcd1-4461-93c4-96cb6e276de7",
                    "transitionId": "c5ec7790-9ac7-4783-bbd3-e7ecbd003a67",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664822850864
                }
            ]
        }
    }
}