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": "51ac17d9-4593-488e-b5d5-5cda2b55017b",
    "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": {
        "a5047a86-f17e-4b85-bcb2-81646f2f7720": {
            "id": "a5047a86-f17e-4b85-bcb2-81646f2f7720",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "74669694-be83-4bb1-b4d5-5b44b3c49c59": {
            "id": "74669694-be83-4bb1-b4d5-5b44b3c49c59",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "c871f38a-f9a5-43ee-bc14-297ba54ec6eb": {
            "id": "c871f38a-f9a5-43ee-bc14-297ba54ec6eb",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "dc1d02ea-9104-413c-a25c-41c80a894581": {
            "id": "dc1d02ea-9104-413c-a25c-41c80a894581",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "9a954ae8-e30d-45c2-8b89-6dce65635df5": {
            "id": "9a954ae8-e30d-45c2-8b89-6dce65635df5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "f0c209d2-572e-45f2-b05c-012f1390e9ca": {
            "id": "f0c209d2-572e-45f2-b05c-012f1390e9ca",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "a96e1b54-2edf-472a-ac31-b0c7f9783ab8": {
            "id": "a96e1b54-2edf-472a-ac31-b0c7f9783ab8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "64d1c25b-00d7-4a3c-9925-5293ab1570c3": {
            "id": "64d1c25b-00d7-4a3c-9925-5293ab1570c3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "a5047a86-f17e-4b85-bcb2-81646f2f7720",
            "74669694-be83-4bb1-b4d5-5b44b3c49c59",
            "c871f38a-f9a5-43ee-bc14-297ba54ec6eb"
        ],
        "update_warehouse": [
            "dc1d02ea-9104-413c-a25c-41c80a894581"
        ],
        "update_accounting": [
            "9a954ae8-e30d-45c2-8b89-6dce65635df5"
        ],
        "update_delivery": [
            "f0c209d2-572e-45f2-b05c-012f1390e9ca"
        ],
        "join": [
            "a96e1b54-2edf-472a-ac31-b0c7f9783ab8"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "a5047a86-f17e-4b85-bcb2-81646f2f7720"
        ],
        "update_accounting": [
            "74669694-be83-4bb1-b4d5-5b44b3c49c59"
        ],
        "update_delivery": [
            "c871f38a-f9a5-43ee-bc14-297ba54ec6eb"
        ],
        "join": [
            "dc1d02ea-9104-413c-a25c-41c80a894581",
            "9a954ae8-e30d-45c2-8b89-6dce65635df5",
            "f0c209d2-572e-45f2-b05c-012f1390e9ca"
        ],
        "notify_customer": [
            "a96e1b54-2edf-472a-ac31-b0c7f9783ab8"
        ],
        "fork": [
            "64d1c25b-00d7-4a3c-9925-5293ab1570c3"
        ]
    },
    "tokens": {
        "9d50c9fa-607a-4d25-b174-ad79ea97a80a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "9d50c9fa-607a-4d25-b174-ad79ea97a80a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4876a801-77a8-4e22-a397-1229454d2604",
                    "transitionId": "64d1c25b-00d7-4a3c-9925-5293ab1570c3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007579
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "78d5de1d-cf33-4d33-af74-ad5c1b1ac6ed",
                    "transitionId": "64d1c25b-00d7-4a3c-9925-5293ab1570c3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007583
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d18f464b-3f06-47da-a2f2-4cdadf2fca0b",
                    "transitionId": "a5047a86-f17e-4b85-bcb2-81646f2f7720",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007587
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1e8e7584-2ff0-49ba-825f-8045a369287e",
                    "transitionId": "a5047a86-f17e-4b85-bcb2-81646f2f7720",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007591
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "adb9ab0a-bc10-44bd-9fb5-e2ff1e8da8e9",
                    "transitionId": "dc1d02ea-9104-413c-a25c-41c80a894581",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007594
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2a71fecc-00f5-4ebb-bb06-c89ac039f405",
                    "transitionId": "dc1d02ea-9104-413c-a25c-41c80a894581",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15968798007598,
                    "reason": ""
                }
            ]
        },
        "7e46a8e1-7d2c-4c6f-9aa8-dec7b1806fcc": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7e46a8e1-7d2c-4c6f-9aa8-dec7b1806fcc",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2698e38b-19e5-43b3-9c21-1ba52e6c6f6a",
                    "transitionId": "74669694-be83-4bb1-b4d5-5b44b3c49c59",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007601
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4f3d8c1e-dce7-4afa-921b-5a40255b9b5b",
                    "transitionId": "74669694-be83-4bb1-b4d5-5b44b3c49c59",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007606
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f7c7c15f-8b85-42b5-bbfd-4162b814da85",
                    "transitionId": "9a954ae8-e30d-45c2-8b89-6dce65635df5",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007609
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3e91d85e-ec09-405e-b624-9e95679dcb92",
                    "transitionId": "9a954ae8-e30d-45c2-8b89-6dce65635df5",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15968798007612,
                    "reason": ""
                }
            ]
        },
        "db3df69c-71a4-40fc-b4c3-882336b634d7": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "db3df69c-71a4-40fc-b4c3-882336b634d7",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b89ad7bf-2217-41f3-a85b-2d4b44a5f90b",
                    "transitionId": "c871f38a-f9a5-43ee-bc14-297ba54ec6eb",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007616
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "472a85a4-10b2-4235-bbf9-64ce9c0d0661",
                    "transitionId": "c871f38a-f9a5-43ee-bc14-297ba54ec6eb",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007619
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "012c10bc-ec01-46e6-82e4-794148b3bfee",
                    "transitionId": "f0c209d2-572e-45f2-b05c-012f1390e9ca",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007623
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "650bb872-f046-4a01-8420-a85405c9b5c1",
                    "transitionId": "f0c209d2-572e-45f2-b05c-012f1390e9ca",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007627
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d2415f95-72d3-4fd0-aa82-1dcfe59d5a99",
                    "transitionId": "a96e1b54-2edf-472a-ac31-b0c7f9783ab8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968798007629
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cc6e6095-fbe2-4593-85f1-fcec575483ea",
                    "transitionId": "a96e1b54-2edf-472a-ac31-b0c7f9783ab8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968798007633
                }
            ]
        }
    }
}