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": "c52ee014-207a-453a-80a8-3a534f3f7646",
    "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": {
        "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a": {
            "id": "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "0a65cae1-1357-40f6-8635-bb6c374395cd": {
            "id": "0a65cae1-1357-40f6-8635-bb6c374395cd",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "54939498-c53e-44c0-87ae-2d6afb51df4d": {
            "id": "54939498-c53e-44c0-87ae-2d6afb51df4d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "a179b183-65d0-45f9-8af9-f374337f9d59": {
            "id": "a179b183-65d0-45f9-8af9-f374337f9d59",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8": {
            "id": "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "26436b6e-bb9e-41b8-b485-b3e1c8a2e159": {
            "id": "26436b6e-bb9e-41b8-b485-b3e1c8a2e159",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "d462297b-26f2-4fa4-a95d-377183e96cbe": {
            "id": "d462297b-26f2-4fa4-a95d-377183e96cbe",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "4458aafb-5c61-4be4-9b8f-5e4822106464": {
            "id": "4458aafb-5c61-4be4-9b8f-5e4822106464",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a",
            "0a65cae1-1357-40f6-8635-bb6c374395cd",
            "54939498-c53e-44c0-87ae-2d6afb51df4d"
        ],
        "update_warehouse": [
            "a179b183-65d0-45f9-8af9-f374337f9d59"
        ],
        "update_accounting": [
            "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8"
        ],
        "update_delivery": [
            "26436b6e-bb9e-41b8-b485-b3e1c8a2e159"
        ],
        "join": [
            "d462297b-26f2-4fa4-a95d-377183e96cbe"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a"
        ],
        "update_accounting": [
            "0a65cae1-1357-40f6-8635-bb6c374395cd"
        ],
        "update_delivery": [
            "54939498-c53e-44c0-87ae-2d6afb51df4d"
        ],
        "join": [
            "a179b183-65d0-45f9-8af9-f374337f9d59",
            "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8",
            "26436b6e-bb9e-41b8-b485-b3e1c8a2e159"
        ],
        "notify_customer": [
            "d462297b-26f2-4fa4-a95d-377183e96cbe"
        ],
        "fork": [
            "4458aafb-5c61-4be4-9b8f-5e4822106464"
        ]
    },
    "tokens": {
        "84e03841-1b47-4bbf-bcd5-6a8e71b47fca": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "84e03841-1b47-4bbf-bcd5-6a8e71b47fca",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f84292dc-020d-42dd-acd8-4c157e2706d4",
                    "transitionId": "4458aafb-5c61-4be4-9b8f-5e4822106464",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042652
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cc41352c-2ad4-455a-959f-4ec491b68403",
                    "transitionId": "4458aafb-5c61-4be4-9b8f-5e4822106464",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042656
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b6f4b0ff-2814-4fc3-aaea-db09ed7c81b4",
                    "transitionId": "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042660
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5e41440c-fa14-4722-bae9-e990b1f65ac0",
                    "transitionId": "c2e9f28f-3182-4be2-a152-f3fdb8e9f25a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042663
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8fd6b4c6-4c56-42c9-b029-b1dcd478a6b8",
                    "transitionId": "a179b183-65d0-45f9-8af9-f374337f9d59",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042667
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "62ca083c-825f-430f-9191-a640e998f24c",
                    "transitionId": "a179b183-65d0-45f9-8af9-f374337f9d59",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15479179042671,
                    "reason": ""
                }
            ]
        },
        "7ce494fe-f12a-4e7e-ad2c-fe4500f86866": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7ce494fe-f12a-4e7e-ad2c-fe4500f86866",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8b27b01a-cdc0-4ed2-94ff-a010a1faca01",
                    "transitionId": "0a65cae1-1357-40f6-8635-bb6c374395cd",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042674
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b8d09ace-b22f-435b-8aa8-b2bd14214325",
                    "transitionId": "0a65cae1-1357-40f6-8635-bb6c374395cd",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042679
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1082e2b8-9f44-44b9-b560-30e98cb94e95",
                    "transitionId": "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042682
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bbea3bae-67d3-444b-bacd-0347584abf2f",
                    "transitionId": "c5d6b44e-e334-4d37-aba8-9ee6493bbaf8",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15479179042685,
                    "reason": ""
                }
            ]
        },
        "3caa77b8-f24b-4eaf-bce7-759326517efb": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "3caa77b8-f24b-4eaf-bce7-759326517efb",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1823ef6b-db4f-42d4-b2de-343e6a2f142c",
                    "transitionId": "54939498-c53e-44c0-87ae-2d6afb51df4d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042689
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "76e18f3e-3406-40be-b979-8aec81f9db78",
                    "transitionId": "54939498-c53e-44c0-87ae-2d6afb51df4d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042693
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "94fac83c-f9b5-4b97-96fe-e053880ffdc6",
                    "transitionId": "26436b6e-bb9e-41b8-b485-b3e1c8a2e159",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042696
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "52261a9b-ed96-4882-8a59-71e7781b7da9",
                    "transitionId": "26436b6e-bb9e-41b8-b485-b3e1c8a2e159",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042700
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d629c384-1685-4dcf-947b-35b2170ba5ef",
                    "transitionId": "d462297b-26f2-4fa4-a95d-377183e96cbe",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479179042703
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9d06ab29-81e3-4daf-be13-fbfc298f1709",
                    "transitionId": "d462297b-26f2-4fa4-a95d-377183e96cbe",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479179042708
                }
            ]
        }
    }
}