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": "d906ffb1-8cc3-48ff-b0e1-dee8427b4e36",
    "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": {
        "570336a7-2dd1-4f58-a940-99c54f08cd8a": {
            "id": "570336a7-2dd1-4f58-a940-99c54f08cd8a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "e3981952-6913-4cbd-9146-bd85ddcd9f2e": {
            "id": "e3981952-6913-4cbd-9146-bd85ddcd9f2e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c": {
            "id": "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51": {
            "id": "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3": {
            "id": "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "912c2fa4-d032-4713-978d-22073f009e0c": {
            "id": "912c2fa4-d032-4713-978d-22073f009e0c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "e0cab629-190f-458c-9be1-8a3674033beb": {
            "id": "e0cab629-190f-458c-9be1-8a3674033beb",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "41caa9ca-4690-4d9b-aec8-2f9e2bf00f01": {
            "id": "41caa9ca-4690-4d9b-aec8-2f9e2bf00f01",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "570336a7-2dd1-4f58-a940-99c54f08cd8a",
            "e3981952-6913-4cbd-9146-bd85ddcd9f2e",
            "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c"
        ],
        "update_warehouse": [
            "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51"
        ],
        "update_accounting": [
            "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3"
        ],
        "update_delivery": [
            "912c2fa4-d032-4713-978d-22073f009e0c"
        ],
        "join": [
            "e0cab629-190f-458c-9be1-8a3674033beb"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "570336a7-2dd1-4f58-a940-99c54f08cd8a"
        ],
        "update_accounting": [
            "e3981952-6913-4cbd-9146-bd85ddcd9f2e"
        ],
        "update_delivery": [
            "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c"
        ],
        "join": [
            "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51",
            "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3",
            "912c2fa4-d032-4713-978d-22073f009e0c"
        ],
        "notify_customer": [
            "e0cab629-190f-458c-9be1-8a3674033beb"
        ],
        "fork": [
            "41caa9ca-4690-4d9b-aec8-2f9e2bf00f01"
        ]
    },
    "tokens": {
        "f4ebcff1-e794-4137-9243-04f0a24f9b4a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "f4ebcff1-e794-4137-9243-04f0a24f9b4a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "996b0709-24df-4d5e-8052-d797622aa574",
                    "transitionId": "41caa9ca-4690-4d9b-aec8-2f9e2bf00f01",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675005
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "05b76192-1d6e-40e3-86df-f6caedad6606",
                    "transitionId": "41caa9ca-4690-4d9b-aec8-2f9e2bf00f01",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675008
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6eb6f059-a1e5-4bce-adef-3a6c6ac8ba0a",
                    "transitionId": "570336a7-2dd1-4f58-a940-99c54f08cd8a",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675011
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3f847178-9364-45df-b37e-2ee7e370744c",
                    "transitionId": "570336a7-2dd1-4f58-a940-99c54f08cd8a",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675015
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "51f066ca-443c-4b29-9410-a535c769310e",
                    "transitionId": "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675018
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e097e7c3-a184-4fe6-9bbb-204524757508",
                    "transitionId": "9bdea3bf-b5fe-4fe0-8dd0-0dc34ea37e51",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 16070828675022,
                    "reason": ""
                }
            ]
        },
        "910148b9-dfb5-491d-91d8-4b3acf4d56dd": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "910148b9-dfb5-491d-91d8-4b3acf4d56dd",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4733fd23-0abb-4f01-a798-68a3be266ace",
                    "transitionId": "e3981952-6913-4cbd-9146-bd85ddcd9f2e",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675027
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6ba8123c-e9b7-4f4e-8c0a-0ae0c4630098",
                    "transitionId": "e3981952-6913-4cbd-9146-bd85ddcd9f2e",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675030
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e337fded-7b3a-447c-a72d-7d262b0b38ec",
                    "transitionId": "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675033
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ff3892c5-92d5-4a6d-9ea7-7f656bb9127d",
                    "transitionId": "1cd4cfb9-6fde-4610-89f3-6648d5b56fa3",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 16070828675037,
                    "reason": ""
                }
            ]
        },
        "b965bbba-f2a5-433d-959c-9540a3e5d5b2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b965bbba-f2a5-433d-959c-9540a3e5d5b2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "660612fd-0f9d-4d06-8976-4dbc50e818a9",
                    "transitionId": "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675040
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b25f18c5-d70a-4259-8a13-1c4f46ecf42c",
                    "transitionId": "e2e5f428-0ecd-49b3-8ea3-0ebde9f2544c",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675044
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6a5eddef-8b8a-4b46-a46a-94d660988340",
                    "transitionId": "912c2fa4-d032-4713-978d-22073f009e0c",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675047
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "97ee8d12-65bd-4784-aa35-3530f98aa2a3",
                    "transitionId": "912c2fa4-d032-4713-978d-22073f009e0c",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675050
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4a766ed8-7d6e-4f89-aa14-500270f85c1b",
                    "transitionId": "e0cab629-190f-458c-9be1-8a3674033beb",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070828675053
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "26672ab7-e1e4-4795-96f6-7330935d62fd",
                    "transitionId": "e0cab629-190f-458c-9be1-8a3674033beb",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070828675057
                }
            ]
        }
    }
}