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": "fcafa354-d5e7-4da9-a7cc-a8499dc9eb5d",
    "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": {
        "490caa81-e666-445a-a638-2fa9a7f7a9b7": {
            "id": "490caa81-e666-445a-a638-2fa9a7f7a9b7",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "39434703-832e-431b-8041-5e8286b85ed0": {
            "id": "39434703-832e-431b-8041-5e8286b85ed0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "88b9f54e-57c1-4cb0-8b34-47a98271bb36": {
            "id": "88b9f54e-57c1-4cb0-8b34-47a98271bb36",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "25a4e822-fab1-457c-a4ff-79a06485abdc": {
            "id": "25a4e822-fab1-457c-a4ff-79a06485abdc",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "dc158c75-41a1-40a6-9746-926b41ded7f4": {
            "id": "dc158c75-41a1-40a6-9746-926b41ded7f4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "d8fc387f-1706-486d-a8b5-b02e476c2274": {
            "id": "d8fc387f-1706-486d-a8b5-b02e476c2274",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "90205053-585c-4e73-a90a-d93e7a4e9c50": {
            "id": "90205053-585c-4e73-a90a-d93e7a4e9c50",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "77ed8a20-c886-46cc-bde6-215ca1c70e3d": {
            "id": "77ed8a20-c886-46cc-bde6-215ca1c70e3d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "490caa81-e666-445a-a638-2fa9a7f7a9b7",
            "39434703-832e-431b-8041-5e8286b85ed0",
            "88b9f54e-57c1-4cb0-8b34-47a98271bb36"
        ],
        "update_warehouse": [
            "25a4e822-fab1-457c-a4ff-79a06485abdc"
        ],
        "update_accounting": [
            "dc158c75-41a1-40a6-9746-926b41ded7f4"
        ],
        "update_delivery": [
            "d8fc387f-1706-486d-a8b5-b02e476c2274"
        ],
        "join": [
            "90205053-585c-4e73-a90a-d93e7a4e9c50"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "490caa81-e666-445a-a638-2fa9a7f7a9b7"
        ],
        "update_accounting": [
            "39434703-832e-431b-8041-5e8286b85ed0"
        ],
        "update_delivery": [
            "88b9f54e-57c1-4cb0-8b34-47a98271bb36"
        ],
        "join": [
            "25a4e822-fab1-457c-a4ff-79a06485abdc",
            "dc158c75-41a1-40a6-9746-926b41ded7f4",
            "d8fc387f-1706-486d-a8b5-b02e476c2274"
        ],
        "notify_customer": [
            "90205053-585c-4e73-a90a-d93e7a4e9c50"
        ],
        "fork": [
            "77ed8a20-c886-46cc-bde6-215ca1c70e3d"
        ]
    },
    "tokens": {
        "11f62245-bbf1-4faa-883c-1eb56572847d": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "11f62245-bbf1-4faa-883c-1eb56572847d",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "baacf5d1-585d-487b-9674-fffc5b2d29ac",
                    "transitionId": "77ed8a20-c886-46cc-bde6-215ca1c70e3d",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624286
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "358e9246-7362-4261-b944-cd404a692f70",
                    "transitionId": "77ed8a20-c886-46cc-bde6-215ca1c70e3d",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624290
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "605b0886-b4af-4b75-92e4-ff376f125fda",
                    "transitionId": "490caa81-e666-445a-a638-2fa9a7f7a9b7",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624293
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "59042255-0fdf-4057-88f1-97130a66f80b",
                    "transitionId": "490caa81-e666-445a-a638-2fa9a7f7a9b7",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624296
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "35a0ac5b-f94f-409c-be60-f710b2354685",
                    "transitionId": "25a4e822-fab1-457c-a4ff-79a06485abdc",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624299
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9c973da5-d21c-4490-86d5-d5c07a69c7bb",
                    "transitionId": "25a4e822-fab1-457c-a4ff-79a06485abdc",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 16031647624303,
                    "reason": ""
                }
            ]
        },
        "25f87c23-0af2-4425-b916-ca016e57c6a6": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "25f87c23-0af2-4425-b916-ca016e57c6a6",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e7edcea7-a314-4b7c-a231-d4f534295620",
                    "transitionId": "39434703-832e-431b-8041-5e8286b85ed0",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624307
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cad748e5-db3a-4aa5-b437-caa67f7a5dc9",
                    "transitionId": "39434703-832e-431b-8041-5e8286b85ed0",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624311
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0b11cac6-ac5d-495b-b04a-3850513cd4b1",
                    "transitionId": "dc158c75-41a1-40a6-9746-926b41ded7f4",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624314
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b20c69a3-30a9-426a-99cb-e39b3fcc8b58",
                    "transitionId": "dc158c75-41a1-40a6-9746-926b41ded7f4",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 16031647624318,
                    "reason": ""
                }
            ]
        },
        "7a2f7f96-9285-4a1e-a061-5fef612baf10": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7a2f7f96-9285-4a1e-a061-5fef612baf10",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db02a486-771d-4cce-9c3e-b105e98dddfe",
                    "transitionId": "88b9f54e-57c1-4cb0-8b34-47a98271bb36",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624321
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bbbcd156-033e-4524-81e4-bd89772875b0",
                    "transitionId": "88b9f54e-57c1-4cb0-8b34-47a98271bb36",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624325
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6587a81e-20ff-41b3-8d68-85a9859cf1d7",
                    "transitionId": "d8fc387f-1706-486d-a8b5-b02e476c2274",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624327
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "edda07f4-f5d7-4f39-9b9c-5de9195890c5",
                    "transitionId": "d8fc387f-1706-486d-a8b5-b02e476c2274",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624331
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0e91bebb-2466-40ab-92fc-387978b573ce",
                    "transitionId": "90205053-585c-4e73-a90a-d93e7a4e9c50",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031647624333
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "49a8e64b-fd27-47be-ac62-748550fea536",
                    "transitionId": "90205053-585c-4e73-a90a-d93e7a4e9c50",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031647624337
                }
            ]
        }
    }
}