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": "0f20c085-66e4-46ac-894a-31c1100ce982",
    "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": {
        "c1243ab4-3125-4810-a826-fcc003f41dd3": {
            "id": "c1243ab4-3125-4810-a826-fcc003f41dd3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "226fa85e-77f9-4eab-977b-4c85e5063c72": {
            "id": "226fa85e-77f9-4eab-977b-4c85e5063c72",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "db524eb5-09ab-44b7-9a58-3b629acb9b94": {
            "id": "db524eb5-09ab-44b7-9a58-3b629acb9b94",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "4d0f2eee-4044-49f2-b815-12bed7359952": {
            "id": "4d0f2eee-4044-49f2-b815-12bed7359952",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "8767b70b-28ce-48bc-bef2-8ccc9f635f1c": {
            "id": "8767b70b-28ce-48bc-bef2-8ccc9f635f1c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "0cc5bbd0-5843-4d7c-994d-95ea49fd937a": {
            "id": "0cc5bbd0-5843-4d7c-994d-95ea49fd937a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "0390a88a-40da-4888-be34-e252904c0670": {
            "id": "0390a88a-40da-4888-be34-e252904c0670",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "9b8956af-0fb7-4451-8aba-7321abb29561": {
            "id": "9b8956af-0fb7-4451-8aba-7321abb29561",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "c1243ab4-3125-4810-a826-fcc003f41dd3",
            "226fa85e-77f9-4eab-977b-4c85e5063c72",
            "db524eb5-09ab-44b7-9a58-3b629acb9b94"
        ],
        "update_warehouse": [
            "4d0f2eee-4044-49f2-b815-12bed7359952"
        ],
        "update_accounting": [
            "8767b70b-28ce-48bc-bef2-8ccc9f635f1c"
        ],
        "update_delivery": [
            "0cc5bbd0-5843-4d7c-994d-95ea49fd937a"
        ],
        "join": [
            "0390a88a-40da-4888-be34-e252904c0670"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "c1243ab4-3125-4810-a826-fcc003f41dd3"
        ],
        "update_accounting": [
            "226fa85e-77f9-4eab-977b-4c85e5063c72"
        ],
        "update_delivery": [
            "db524eb5-09ab-44b7-9a58-3b629acb9b94"
        ],
        "join": [
            "4d0f2eee-4044-49f2-b815-12bed7359952",
            "8767b70b-28ce-48bc-bef2-8ccc9f635f1c",
            "0cc5bbd0-5843-4d7c-994d-95ea49fd937a"
        ],
        "notify_customer": [
            "0390a88a-40da-4888-be34-e252904c0670"
        ],
        "fork": [
            "9b8956af-0fb7-4451-8aba-7321abb29561"
        ]
    },
    "tokens": {
        "19b6e52b-8a7c-4e9c-b3b4-bb981912001a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "19b6e52b-8a7c-4e9c-b3b4-bb981912001a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5a2582b8-336c-4df1-9268-7beee30de71b",
                    "transitionId": "9b8956af-0fb7-4451-8aba-7321abb29561",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014692
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f28c2ea7-ef6c-4d18-9ac9-0712fa4a2e02",
                    "transitionId": "9b8956af-0fb7-4451-8aba-7321abb29561",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014699
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "aefeaf2f-a452-4280-9360-8c962fd74c79",
                    "transitionId": "c1243ab4-3125-4810-a826-fcc003f41dd3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014705
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6fcdc2a9-9d09-412f-ad0f-1423509dc377",
                    "transitionId": "c1243ab4-3125-4810-a826-fcc003f41dd3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014712
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "155c14fe-04e3-4b2e-93d6-73c016945f26",
                    "transitionId": "4d0f2eee-4044-49f2-b815-12bed7359952",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014726
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6ded55d8-b166-4752-8d25-b2e7184c5ccc",
                    "transitionId": "4d0f2eee-4044-49f2-b815-12bed7359952",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15797916014736,
                    "reason": ""
                }
            ]
        },
        "bfec12bf-c6c3-448d-8eae-da78ebba86e4": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "bfec12bf-c6c3-448d-8eae-da78ebba86e4",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "04c95e9f-60d7-49bd-bc1a-7156dd5c9fb9",
                    "transitionId": "226fa85e-77f9-4eab-977b-4c85e5063c72",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014743
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bee21f38-12d0-411d-9459-a6b953b9bd9a",
                    "transitionId": "226fa85e-77f9-4eab-977b-4c85e5063c72",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014767
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ea333099-566c-4eb8-9859-9488c1fe70f3",
                    "transitionId": "8767b70b-28ce-48bc-bef2-8ccc9f635f1c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014773
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c3c704e6-cf85-488d-b1ed-2d794cfd5093",
                    "transitionId": "8767b70b-28ce-48bc-bef2-8ccc9f635f1c",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15797916014781,
                    "reason": ""
                }
            ]
        },
        "2f87fc01-fdc6-4825-bbf3-c7e6fab72695": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "2f87fc01-fdc6-4825-bbf3-c7e6fab72695",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "46ba6b6d-c8d8-45c8-a65d-ea304cd22e07",
                    "transitionId": "db524eb5-09ab-44b7-9a58-3b629acb9b94",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014788
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2782f887-27b9-420a-969c-9e9542c073ef",
                    "transitionId": "db524eb5-09ab-44b7-9a58-3b629acb9b94",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014797
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "11a66feb-9c8d-4678-8e9b-213c3b65977c",
                    "transitionId": "0cc5bbd0-5843-4d7c-994d-95ea49fd937a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014801
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "85ac6046-f9af-4801-a1f0-d15c3918ff03",
                    "transitionId": "0cc5bbd0-5843-4d7c-994d-95ea49fd937a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014807
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4791c413-0dba-4671-ad09-174f657a02c0",
                    "transitionId": "0390a88a-40da-4888-be34-e252904c0670",
                    "weight": 1,
                    "state": "opened",
                    "time": 15797916014812
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e6e27427-f733-438c-9618-539d88231db9",
                    "transitionId": "0390a88a-40da-4888-be34-e252904c0670",
                    "weight": 1,
                    "state": "passed",
                    "time": 15797916014819
                }
            ]
        }
    }
}