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": "761ce02f-41b9-4d7b-94c1-22a82b75748e",
    "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": {
        "00c2f619-bdc9-4c37-aef2-45a2d27c1f21": {
            "id": "00c2f619-bdc9-4c37-aef2-45a2d27c1f21",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2": {
            "id": "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "4bdc6600-bf16-4371-8404-ef6ec449dd29": {
            "id": "4bdc6600-bf16-4371-8404-ef6ec449dd29",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "f251e40e-64e4-4fd7-a457-e563430ab62f": {
            "id": "f251e40e-64e4-4fd7-a457-e563430ab62f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "5114e6ff-e244-46f3-b107-a4529bba276b": {
            "id": "5114e6ff-e244-46f3-b107-a4529bba276b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "78da6155-ace0-4045-a91e-2698c573b004": {
            "id": "78da6155-ace0-4045-a91e-2698c573b004",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c": {
            "id": "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "8c4d1ae7-4425-4438-8742-7ed6cc420eac": {
            "id": "8c4d1ae7-4425-4438-8742-7ed6cc420eac",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "00c2f619-bdc9-4c37-aef2-45a2d27c1f21",
            "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2",
            "4bdc6600-bf16-4371-8404-ef6ec449dd29"
        ],
        "update_warehouse": [
            "f251e40e-64e4-4fd7-a457-e563430ab62f"
        ],
        "update_accounting": [
            "5114e6ff-e244-46f3-b107-a4529bba276b"
        ],
        "update_delivery": [
            "78da6155-ace0-4045-a91e-2698c573b004"
        ],
        "join": [
            "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "00c2f619-bdc9-4c37-aef2-45a2d27c1f21"
        ],
        "update_accounting": [
            "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2"
        ],
        "update_delivery": [
            "4bdc6600-bf16-4371-8404-ef6ec449dd29"
        ],
        "join": [
            "f251e40e-64e4-4fd7-a457-e563430ab62f",
            "5114e6ff-e244-46f3-b107-a4529bba276b",
            "78da6155-ace0-4045-a91e-2698c573b004"
        ],
        "notify_customer": [
            "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c"
        ],
        "fork": [
            "8c4d1ae7-4425-4438-8742-7ed6cc420eac"
        ]
    },
    "tokens": {
        "f54c143b-7609-4927-92ca-e730bc666745": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "f54c143b-7609-4927-92ca-e730bc666745",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "45bc61bf-35b2-4bfa-8bfe-34e6b311c816",
                    "transitionId": "8c4d1ae7-4425-4438-8742-7ed6cc420eac",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378799
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0712ee5d-af06-442a-bd57-3a0810e16a31",
                    "transitionId": "8c4d1ae7-4425-4438-8742-7ed6cc420eac",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378803
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f1884554-2ec3-4ffb-b635-e4dcfe9c49a6",
                    "transitionId": "00c2f619-bdc9-4c37-aef2-45a2d27c1f21",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378806
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ddde97e6-5f37-4c18-ac64-a55f1b6214c4",
                    "transitionId": "00c2f619-bdc9-4c37-aef2-45a2d27c1f21",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378809
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "37894d3d-351a-4eff-97ac-08dd6e126ca8",
                    "transitionId": "f251e40e-64e4-4fd7-a457-e563430ab62f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378812
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "09a469a9-515f-4380-b219-5f1de5b688ef",
                    "transitionId": "f251e40e-64e4-4fd7-a457-e563430ab62f",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15736955378817,
                    "reason": ""
                }
            ]
        },
        "25496245-21e1-4276-acd0-09104aebeb3c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "25496245-21e1-4276-acd0-09104aebeb3c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "943c2d46-f157-46d9-ad5b-6c553354c13b",
                    "transitionId": "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378821
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "19057073-7610-4479-a8c1-87695ba48282",
                    "transitionId": "1c3d4b63-fcd6-449f-bc71-0d5beec9aca2",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378825
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2df22c4b-0f39-4e59-ae3c-c2d4fcdf0863",
                    "transitionId": "5114e6ff-e244-46f3-b107-a4529bba276b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378828
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4c5c8395-7169-4875-95e1-0668990175f6",
                    "transitionId": "5114e6ff-e244-46f3-b107-a4529bba276b",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15736955378831,
                    "reason": ""
                }
            ]
        },
        "664ea1f1-f444-43be-9366-0f636f6920e8": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "664ea1f1-f444-43be-9366-0f636f6920e8",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6b889a0c-344d-4a87-8bc7-f586e3421d77",
                    "transitionId": "4bdc6600-bf16-4371-8404-ef6ec449dd29",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378835
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9b8d9d1f-8211-43e4-bf58-59422e46885a",
                    "transitionId": "4bdc6600-bf16-4371-8404-ef6ec449dd29",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378839
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2bb32b9c-4acc-4ce9-844c-3ef29721f149",
                    "transitionId": "78da6155-ace0-4045-a91e-2698c573b004",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378841
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c9dfea35-8b70-4eb9-a8e7-744fb2f69e55",
                    "transitionId": "78da6155-ace0-4045-a91e-2698c573b004",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378844
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "62acd9c7-f44d-4c39-8622-10877d2ff186",
                    "transitionId": "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15736955378847
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "61399f9e-1afa-4110-ad13-813f201d6125",
                    "transitionId": "4d19f4dd-90bc-4f2b-9de0-827c7ea5111c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15736955378851
                }
            ]
        }
    }
}