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": "e98c448d-9439-4b05-a4c6-4e1006405a6c",
    "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": {
        "8b6c3642-013c-4ce2-99ac-f84ad326ba23": {
            "id": "8b6c3642-013c-4ce2-99ac-f84ad326ba23",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "bb361ff6-60c4-4cb7-9424-c99aba5bcb89": {
            "id": "bb361ff6-60c4-4cb7-9424-c99aba5bcb89",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d": {
            "id": "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "66e37d51-997d-4c38-ac8d-b03406754546": {
            "id": "66e37d51-997d-4c38-ac8d-b03406754546",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9": {
            "id": "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "dd349639-d445-475f-8519-90ed44f5b1c9": {
            "id": "dd349639-d445-475f-8519-90ed44f5b1c9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "9634d527-3787-46b5-84eb-9720f97d0745": {
            "id": "9634d527-3787-46b5-84eb-9720f97d0745",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "3a9987f1-21e8-4889-ae35-53f10a2f13e4": {
            "id": "3a9987f1-21e8-4889-ae35-53f10a2f13e4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "8b6c3642-013c-4ce2-99ac-f84ad326ba23",
            "bb361ff6-60c4-4cb7-9424-c99aba5bcb89",
            "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d"
        ],
        "update_warehouse": [
            "66e37d51-997d-4c38-ac8d-b03406754546"
        ],
        "update_accounting": [
            "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9"
        ],
        "update_delivery": [
            "dd349639-d445-475f-8519-90ed44f5b1c9"
        ],
        "join": [
            "9634d527-3787-46b5-84eb-9720f97d0745"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "8b6c3642-013c-4ce2-99ac-f84ad326ba23"
        ],
        "update_accounting": [
            "bb361ff6-60c4-4cb7-9424-c99aba5bcb89"
        ],
        "update_delivery": [
            "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d"
        ],
        "join": [
            "66e37d51-997d-4c38-ac8d-b03406754546",
            "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9",
            "dd349639-d445-475f-8519-90ed44f5b1c9"
        ],
        "notify_customer": [
            "9634d527-3787-46b5-84eb-9720f97d0745"
        ],
        "fork": [
            "3a9987f1-21e8-4889-ae35-53f10a2f13e4"
        ]
    },
    "tokens": {
        "5b57e88a-4dac-4ed1-9e00-cda022bfc453": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "5b57e88a-4dac-4ed1-9e00-cda022bfc453",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7032d774-e129-45a9-b96d-bff19b877996",
                    "transitionId": "3a9987f1-21e8-4889-ae35-53f10a2f13e4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787149
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7a665e86-9e28-4250-9a4b-151dad3f2a9b",
                    "transitionId": "3a9987f1-21e8-4889-ae35-53f10a2f13e4",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787153
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "030821f0-0536-4494-950c-4a9ff1954984",
                    "transitionId": "8b6c3642-013c-4ce2-99ac-f84ad326ba23",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787157
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2602c92a-cc3a-49a8-8a8a-845c71f2b5d7",
                    "transitionId": "8b6c3642-013c-4ce2-99ac-f84ad326ba23",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787160
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c5ed3b30-ae4f-494e-918f-bb5db7cc3922",
                    "transitionId": "66e37d51-997d-4c38-ac8d-b03406754546",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787163
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5b510476-54b9-4696-b786-84fe0903c02f",
                    "transitionId": "66e37d51-997d-4c38-ac8d-b03406754546",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15450767787168,
                    "reason": ""
                }
            ]
        },
        "0e8d2087-6adc-4201-807a-3b678ae027e3": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0e8d2087-6adc-4201-807a-3b678ae027e3",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "78c4b66a-5dc0-414f-ae0a-66b77a605d4e",
                    "transitionId": "bb361ff6-60c4-4cb7-9424-c99aba5bcb89",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787172
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "35cec3ef-f4bd-4102-ad66-00910cf21b65",
                    "transitionId": "bb361ff6-60c4-4cb7-9424-c99aba5bcb89",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787176
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "56e26ffe-eb60-45db-b4a6-3f64323b64af",
                    "transitionId": "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787178
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b4efb954-a2db-4884-83b1-f482e711d89f",
                    "transitionId": "8b377e9d-5f1e-4b26-8e2a-47c30fd74dc9",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15450767787182,
                    "reason": ""
                }
            ]
        },
        "d7e87fc5-68a9-4a10-86b9-dfc0eaa63616": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "d7e87fc5-68a9-4a10-86b9-dfc0eaa63616",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "befd0dbf-443c-458d-8330-d01bddf1ad0e",
                    "transitionId": "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787185
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ded67a84-a7a5-4411-bd41-ae20ebbc5a44",
                    "transitionId": "cd4011dc-1dcc-47bd-bfff-b35acd6f8d6d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787189
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "30ea1ccb-4fe9-457d-9478-d4c82de2cf1a",
                    "transitionId": "dd349639-d445-475f-8519-90ed44f5b1c9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787192
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0b5275db-6423-4180-90a9-3c4f9a0a0462",
                    "transitionId": "dd349639-d445-475f-8519-90ed44f5b1c9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787195
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2be46048-43e4-4649-b1c7-a3de4b0817f9",
                    "transitionId": "9634d527-3787-46b5-84eb-9720f97d0745",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450767787198
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "10544b83-b54d-4659-b85a-f0fba960f3e9",
                    "transitionId": "9634d527-3787-46b5-84eb-9720f97d0745",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450767787201
                }
            ]
        }
    }
}