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": "020ab647-667b-40b9-95e7-987b0d872e66",
    "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": {
        "045f4add-275f-4413-975f-affbf9ed5bed": {
            "id": "045f4add-275f-4413-975f-affbf9ed5bed",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "e0829da5-f615-437f-aae2-53060a5dba79": {
            "id": "e0829da5-f615-437f-aae2-53060a5dba79",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "ae035cd5-e8c7-4b58-894b-c1d5e1efe964": {
            "id": "ae035cd5-e8c7-4b58-894b-c1d5e1efe964",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "3df97e1d-420b-4910-855a-4dbfc9f57fad": {
            "id": "3df97e1d-420b-4910-855a-4dbfc9f57fad",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf": {
            "id": "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "a5893744-d4fb-4080-a534-3495ac1adfc3": {
            "id": "a5893744-d4fb-4080-a534-3495ac1adfc3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "ea4fa5c6-a7e2-43d9-aa93-77147a4de873": {
            "id": "ea4fa5c6-a7e2-43d9-aa93-77147a4de873",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "95d0214f-c10e-474d-a2af-68af6fc1567b": {
            "id": "95d0214f-c10e-474d-a2af-68af6fc1567b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "045f4add-275f-4413-975f-affbf9ed5bed",
            "e0829da5-f615-437f-aae2-53060a5dba79",
            "ae035cd5-e8c7-4b58-894b-c1d5e1efe964"
        ],
        "update_warehouse": [
            "3df97e1d-420b-4910-855a-4dbfc9f57fad"
        ],
        "update_accounting": [
            "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf"
        ],
        "update_delivery": [
            "a5893744-d4fb-4080-a534-3495ac1adfc3"
        ],
        "join": [
            "ea4fa5c6-a7e2-43d9-aa93-77147a4de873"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "045f4add-275f-4413-975f-affbf9ed5bed"
        ],
        "update_accounting": [
            "e0829da5-f615-437f-aae2-53060a5dba79"
        ],
        "update_delivery": [
            "ae035cd5-e8c7-4b58-894b-c1d5e1efe964"
        ],
        "join": [
            "3df97e1d-420b-4910-855a-4dbfc9f57fad",
            "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf",
            "a5893744-d4fb-4080-a534-3495ac1adfc3"
        ],
        "notify_customer": [
            "ea4fa5c6-a7e2-43d9-aa93-77147a4de873"
        ],
        "fork": [
            "95d0214f-c10e-474d-a2af-68af6fc1567b"
        ]
    },
    "tokens": {
        "dc032c6d-6e99-4520-969b-56264f52207f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "dc032c6d-6e99-4520-969b-56264f52207f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "63cb4e27-d684-4c53-b700-589740d0a4ad",
                    "transitionId": "95d0214f-c10e-474d-a2af-68af6fc1567b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706151
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3ae53faa-5faf-4fec-8969-8887a3e083c5",
                    "transitionId": "95d0214f-c10e-474d-a2af-68af6fc1567b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706155
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f245ad4c-dddf-4beb-b1f9-b9b798dd69a8",
                    "transitionId": "045f4add-275f-4413-975f-affbf9ed5bed",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706158
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "545d2303-f89f-4ee2-bfb8-d1b1f63c1b12",
                    "transitionId": "045f4add-275f-4413-975f-affbf9ed5bed",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706162
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7658b2d6-68fd-4482-b37b-31212d3ce61b",
                    "transitionId": "3df97e1d-420b-4910-855a-4dbfc9f57fad",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706165
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "307938d7-7e82-4c13-b5aa-065a6c2e9fd2",
                    "transitionId": "3df97e1d-420b-4910-855a-4dbfc9f57fad",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15606694706170,
                    "reason": ""
                }
            ]
        },
        "8055f392-f8b0-44c3-b393-b499667feba2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8055f392-f8b0-44c3-b393-b499667feba2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db994256-209e-4607-98bd-5445e5658967",
                    "transitionId": "e0829da5-f615-437f-aae2-53060a5dba79",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706175
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e887aba0-094e-4e67-9985-bee7d7b0d1cf",
                    "transitionId": "e0829da5-f615-437f-aae2-53060a5dba79",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706180
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c879ed1a-00fd-44db-b242-80cfefa59f77",
                    "transitionId": "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706184
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b70a4f15-880a-4261-8cdd-b5d4cd6b787c",
                    "transitionId": "1b325a8e-06e0-44f0-9196-78e4bf5a9bdf",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15606694706188,
                    "reason": ""
                }
            ]
        },
        "148c9627-bb8d-4df2-9c13-bb8580a47cd2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "148c9627-bb8d-4df2-9c13-bb8580a47cd2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f8ba120f-bf78-466e-9360-9537e574953a",
                    "transitionId": "ae035cd5-e8c7-4b58-894b-c1d5e1efe964",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706191
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d1f3fd49-557b-4c82-9f19-6c3ea4ac2c65",
                    "transitionId": "ae035cd5-e8c7-4b58-894b-c1d5e1efe964",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706195
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "36946607-3e0a-4c48-a718-7eb13de866b9",
                    "transitionId": "a5893744-d4fb-4080-a534-3495ac1adfc3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706198
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9d6b6d81-d6e0-493e-85ae-0ca71a4e127a",
                    "transitionId": "a5893744-d4fb-4080-a534-3495ac1adfc3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706202
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2fba25f5-e2cf-457d-b3d3-fc12f8379fe8",
                    "transitionId": "ea4fa5c6-a7e2-43d9-aa93-77147a4de873",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606694706205
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1092d3d5-f2fb-47f5-b74c-8005b6fb9ab8",
                    "transitionId": "ea4fa5c6-a7e2-43d9-aa93-77147a4de873",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606694706209
                }
            ]
        }
    }
}