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": "0f703660-b0bd-4130-b06a-613536162772",
    "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": {
        "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236": {
            "id": "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "b1fd13fd-884c-4f67-80ad-14a4600f44b4": {
            "id": "b1fd13fd-884c-4f67-80ad-14a4600f44b4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "2ab2e51f-8717-4667-86db-ab362ea3b398": {
            "id": "2ab2e51f-8717-4667-86db-ab362ea3b398",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "98bd5ee9-951d-43ba-878f-d6805db428f3": {
            "id": "98bd5ee9-951d-43ba-878f-d6805db428f3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "031260e4-a211-41ab-99c0-764858ca88d1": {
            "id": "031260e4-a211-41ab-99c0-764858ca88d1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "33f5362d-1dad-48aa-b16b-914ef3aa434d": {
            "id": "33f5362d-1dad-48aa-b16b-914ef3aa434d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "294641e9-c41d-4384-b0c7-fa6063cf8889": {
            "id": "294641e9-c41d-4384-b0c7-fa6063cf8889",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "04da8630-9d23-4ec8-a43a-0d6a2fc8f453": {
            "id": "04da8630-9d23-4ec8-a43a-0d6a2fc8f453",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236",
            "b1fd13fd-884c-4f67-80ad-14a4600f44b4",
            "2ab2e51f-8717-4667-86db-ab362ea3b398"
        ],
        "update_warehouse": [
            "98bd5ee9-951d-43ba-878f-d6805db428f3"
        ],
        "update_accounting": [
            "031260e4-a211-41ab-99c0-764858ca88d1"
        ],
        "update_delivery": [
            "33f5362d-1dad-48aa-b16b-914ef3aa434d"
        ],
        "join": [
            "294641e9-c41d-4384-b0c7-fa6063cf8889"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236"
        ],
        "update_accounting": [
            "b1fd13fd-884c-4f67-80ad-14a4600f44b4"
        ],
        "update_delivery": [
            "2ab2e51f-8717-4667-86db-ab362ea3b398"
        ],
        "join": [
            "98bd5ee9-951d-43ba-878f-d6805db428f3",
            "031260e4-a211-41ab-99c0-764858ca88d1",
            "33f5362d-1dad-48aa-b16b-914ef3aa434d"
        ],
        "notify_customer": [
            "294641e9-c41d-4384-b0c7-fa6063cf8889"
        ],
        "fork": [
            "04da8630-9d23-4ec8-a43a-0d6a2fc8f453"
        ]
    },
    "tokens": {
        "30d6a93d-1b3f-46ae-805c-e7efe58fdefa": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "30d6a93d-1b3f-46ae-805c-e7efe58fdefa",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "69b4835a-5a65-4991-9bba-92740854fd15",
                    "transitionId": "04da8630-9d23-4ec8-a43a-0d6a2fc8f453",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175084
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "426f289f-0dd5-43f3-98f1-23ea8c1c1464",
                    "transitionId": "04da8630-9d23-4ec8-a43a-0d6a2fc8f453",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175091
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4b416729-544f-43a9-b0b7-c96bfd83285d",
                    "transitionId": "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175097
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4a372cb3-ddb6-4f3e-a4bb-1a6070c39db7",
                    "transitionId": "cda3ea14-ee38-4b1e-ad90-bf6ff1da7236",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175103
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f9d3567f-d981-4c5e-9fa6-72f40f797984",
                    "transitionId": "98bd5ee9-951d-43ba-878f-d6805db428f3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175108
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ed235690-47a8-46c3-a87c-212fb50716f8",
                    "transitionId": "98bd5ee9-951d-43ba-878f-d6805db428f3",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15508402175116,
                    "reason": ""
                }
            ]
        },
        "5a1736cf-fba4-4364-b81b-62caaff3f696": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "5a1736cf-fba4-4364-b81b-62caaff3f696",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2c67ebe0-2a8e-409f-bb36-acd15a685dcf",
                    "transitionId": "b1fd13fd-884c-4f67-80ad-14a4600f44b4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175120
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "491efe71-6aa2-4d59-910c-5bb21f0c1bcb",
                    "transitionId": "b1fd13fd-884c-4f67-80ad-14a4600f44b4",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175124
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0cc2f528-ac9e-4495-b5cd-2200b41e5c10",
                    "transitionId": "031260e4-a211-41ab-99c0-764858ca88d1",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175127
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f0e6c208-3543-43e4-98ce-ffb3c7048fd9",
                    "transitionId": "031260e4-a211-41ab-99c0-764858ca88d1",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15508402175130,
                    "reason": ""
                }
            ]
        },
        "55c627ce-4f9c-49b3-9f1a-5d74edd9812a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "55c627ce-4f9c-49b3-9f1a-5d74edd9812a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "702b07ec-4b66-4835-b2d0-7737ec593401",
                    "transitionId": "2ab2e51f-8717-4667-86db-ab362ea3b398",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175134
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4373a774-1310-4ef0-a013-1fbfb1435ce5",
                    "transitionId": "2ab2e51f-8717-4667-86db-ab362ea3b398",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175141
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e7d84305-b650-498d-8589-9da0e513ad58",
                    "transitionId": "33f5362d-1dad-48aa-b16b-914ef3aa434d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175146
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4b7687f5-ed67-44c9-b576-aef6299ff42d",
                    "transitionId": "33f5362d-1dad-48aa-b16b-914ef3aa434d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175151
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "79170c02-f707-4752-8829-88a9b8dd3035",
                    "transitionId": "294641e9-c41d-4384-b0c7-fa6063cf8889",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508402175156
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d6651c69-f542-4c63-88bb-58dc11860d58",
                    "transitionId": "294641e9-c41d-4384-b0c7-fa6063cf8889",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508402175161
                }
            ]
        }
    }
}