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": "7e2e4602-cff8-454d-91a8-d09e34b9ac45",
    "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": {
        "6f484ceb-898c-421d-aef1-65fcdab3e847": {
            "id": "6f484ceb-898c-421d-aef1-65fcdab3e847",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "11326687-a34e-427e-bad4-b9229261ea77": {
            "id": "11326687-a34e-427e-bad4-b9229261ea77",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "7def8910-3014-4e4f-88f7-de8d20d5aec5": {
            "id": "7def8910-3014-4e4f-88f7-de8d20d5aec5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "1ecea168-f8db-4a57-9c6f-8ee792b31b47": {
            "id": "1ecea168-f8db-4a57-9c6f-8ee792b31b47",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "59ce1683-1446-45ae-b4fd-d182f19e3aaf": {
            "id": "59ce1683-1446-45ae-b4fd-d182f19e3aaf",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "9d3abbd5-a248-47f0-9066-51e4abaf8d4f": {
            "id": "9d3abbd5-a248-47f0-9066-51e4abaf8d4f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2": {
            "id": "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "6551c886-4366-42b0-afc3-4ef6088d420f": {
            "id": "6551c886-4366-42b0-afc3-4ef6088d420f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "6f484ceb-898c-421d-aef1-65fcdab3e847",
            "11326687-a34e-427e-bad4-b9229261ea77",
            "7def8910-3014-4e4f-88f7-de8d20d5aec5"
        ],
        "update_warehouse": [
            "1ecea168-f8db-4a57-9c6f-8ee792b31b47"
        ],
        "update_accounting": [
            "59ce1683-1446-45ae-b4fd-d182f19e3aaf"
        ],
        "update_delivery": [
            "9d3abbd5-a248-47f0-9066-51e4abaf8d4f"
        ],
        "join": [
            "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "6f484ceb-898c-421d-aef1-65fcdab3e847"
        ],
        "update_accounting": [
            "11326687-a34e-427e-bad4-b9229261ea77"
        ],
        "update_delivery": [
            "7def8910-3014-4e4f-88f7-de8d20d5aec5"
        ],
        "join": [
            "1ecea168-f8db-4a57-9c6f-8ee792b31b47",
            "59ce1683-1446-45ae-b4fd-d182f19e3aaf",
            "9d3abbd5-a248-47f0-9066-51e4abaf8d4f"
        ],
        "notify_customer": [
            "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2"
        ],
        "fork": [
            "6551c886-4366-42b0-afc3-4ef6088d420f"
        ]
    },
    "tokens": {
        "c52d6421-b510-4a94-9c61-7cb66f9a0d39": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "c52d6421-b510-4a94-9c61-7cb66f9a0d39",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5bf231c4-0227-4eef-aca2-42804c0e5f8d",
                    "transitionId": "6551c886-4366-42b0-afc3-4ef6088d420f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002266
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "af3457dc-c492-4642-ac29-3668464f49bc",
                    "transitionId": "6551c886-4366-42b0-afc3-4ef6088d420f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002272
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2a0fbe7e-0858-46c9-ae00-3f72a1504b31",
                    "transitionId": "6f484ceb-898c-421d-aef1-65fcdab3e847",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002275
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8621d481-c8f3-4688-ba4c-cb5c596ee8be",
                    "transitionId": "6f484ceb-898c-421d-aef1-65fcdab3e847",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002279
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c395d7ad-63a9-4dc4-8721-054fc4cbc5d7",
                    "transitionId": "1ecea168-f8db-4a57-9c6f-8ee792b31b47",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002283
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7a21ab55-54fb-4198-b46e-f31e92b7ba64",
                    "transitionId": "1ecea168-f8db-4a57-9c6f-8ee792b31b47",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15559364002288,
                    "reason": ""
                }
            ]
        },
        "40c37a3c-5ab2-4555-abc7-33112c599bf7": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "40c37a3c-5ab2-4555-abc7-33112c599bf7",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1a8d1e0f-f3c6-4c84-9033-039651839370",
                    "transitionId": "11326687-a34e-427e-bad4-b9229261ea77",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002293
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d539df57-94d5-44f5-91bc-118ae576f74b",
                    "transitionId": "11326687-a34e-427e-bad4-b9229261ea77",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002298
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3b5f395c-048d-49a8-8256-8f56a7e7250f",
                    "transitionId": "59ce1683-1446-45ae-b4fd-d182f19e3aaf",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002300
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0bd75d2d-6a3e-41d0-8409-3bcf1cd77efa",
                    "transitionId": "59ce1683-1446-45ae-b4fd-d182f19e3aaf",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15559364002304,
                    "reason": ""
                }
            ]
        },
        "20e22471-8aed-41de-938f-4ef8c99daf41": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "20e22471-8aed-41de-938f-4ef8c99daf41",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f549c8c4-3bc5-499c-8747-69708ac8460f",
                    "transitionId": "7def8910-3014-4e4f-88f7-de8d20d5aec5",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002307
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "15b84d55-3417-47e9-a67f-a24d760317ce",
                    "transitionId": "7def8910-3014-4e4f-88f7-de8d20d5aec5",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002311
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "06086ca8-0b3c-4c8f-9aa0-82185adad465",
                    "transitionId": "9d3abbd5-a248-47f0-9066-51e4abaf8d4f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002313
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d42d2479-4967-4e06-a2fb-aa254bea0e28",
                    "transitionId": "9d3abbd5-a248-47f0-9066-51e4abaf8d4f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002316
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e6f07742-645f-4603-9b6e-9a6f248f3f77",
                    "transitionId": "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559364002319
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9e8ba3bb-dfb2-4891-b796-3a9b63836ea2",
                    "transitionId": "feedaed6-9c70-45bb-a1d6-0ed3e5e112d2",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559364002324
                }
            ]
        }
    }
}