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": "7421cac0-a030-421e-97be-02408188853d",
    "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": {
        "d1e11762-132d-40e9-8eed-9ad09ffff0bc": {
            "id": "d1e11762-132d-40e9-8eed-9ad09ffff0bc",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "3d4f3b2f-97b1-4346-b67f-2f456cf8455f": {
            "id": "3d4f3b2f-97b1-4346-b67f-2f456cf8455f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "01928273-e081-444c-8b82-622b5cc2a530": {
            "id": "01928273-e081-444c-8b82-622b5cc2a530",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "61a7bae6-e26e-4939-b8b1-bde243db6d98": {
            "id": "61a7bae6-e26e-4939-b8b1-bde243db6d98",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "248f4a79-78c0-4c08-99c2-7a68745af918": {
            "id": "248f4a79-78c0-4c08-99c2-7a68745af918",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "803cc741-a2a9-4a41-a63a-80f28c74b21b": {
            "id": "803cc741-a2a9-4a41-a63a-80f28c74b21b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "e92caf11-20d1-497b-a3e8-54caa0720dc3": {
            "id": "e92caf11-20d1-497b-a3e8-54caa0720dc3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "0052f9ba-b0a7-45ae-9d04-dfe146551b5e": {
            "id": "0052f9ba-b0a7-45ae-9d04-dfe146551b5e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "d1e11762-132d-40e9-8eed-9ad09ffff0bc",
            "3d4f3b2f-97b1-4346-b67f-2f456cf8455f",
            "01928273-e081-444c-8b82-622b5cc2a530"
        ],
        "update_warehouse": [
            "61a7bae6-e26e-4939-b8b1-bde243db6d98"
        ],
        "update_accounting": [
            "248f4a79-78c0-4c08-99c2-7a68745af918"
        ],
        "update_delivery": [
            "803cc741-a2a9-4a41-a63a-80f28c74b21b"
        ],
        "join": [
            "e92caf11-20d1-497b-a3e8-54caa0720dc3"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "d1e11762-132d-40e9-8eed-9ad09ffff0bc"
        ],
        "update_accounting": [
            "3d4f3b2f-97b1-4346-b67f-2f456cf8455f"
        ],
        "update_delivery": [
            "01928273-e081-444c-8b82-622b5cc2a530"
        ],
        "join": [
            "61a7bae6-e26e-4939-b8b1-bde243db6d98",
            "248f4a79-78c0-4c08-99c2-7a68745af918",
            "803cc741-a2a9-4a41-a63a-80f28c74b21b"
        ],
        "notify_customer": [
            "e92caf11-20d1-497b-a3e8-54caa0720dc3"
        ],
        "fork": [
            "0052f9ba-b0a7-45ae-9d04-dfe146551b5e"
        ]
    },
    "tokens": {
        "0deab30e-3fca-4743-a38e-aadfedbc4f7c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0deab30e-3fca-4743-a38e-aadfedbc4f7c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "390844e1-ef7f-42ca-878e-6de6f2399297",
                    "transitionId": "0052f9ba-b0a7-45ae-9d04-dfe146551b5e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671371
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "571bdba3-4551-40dc-9bed-33f5a6bfb73f",
                    "transitionId": "0052f9ba-b0a7-45ae-9d04-dfe146551b5e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671376
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "82fbe72b-e4ec-4d6e-9785-6a6206d3ad34",
                    "transitionId": "d1e11762-132d-40e9-8eed-9ad09ffff0bc",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671379
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "75b1b319-acf9-4b47-9955-cf3c8cece4e8",
                    "transitionId": "d1e11762-132d-40e9-8eed-9ad09ffff0bc",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671383
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4bf328c9-c01d-492e-9c24-d51e6a8639f7",
                    "transitionId": "61a7bae6-e26e-4939-b8b1-bde243db6d98",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671386
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cc98abde-c8d7-43fe-a4be-471c713adcbc",
                    "transitionId": "61a7bae6-e26e-4939-b8b1-bde243db6d98",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15421494671391,
                    "reason": ""
                }
            ]
        },
        "8e72734f-fecd-4783-8a35-1d7759415565": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8e72734f-fecd-4783-8a35-1d7759415565",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d113668c-8342-4318-b068-f568fc467567",
                    "transitionId": "3d4f3b2f-97b1-4346-b67f-2f456cf8455f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671395
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a383bdc5-cc6b-49ad-a260-413c536d31d3",
                    "transitionId": "3d4f3b2f-97b1-4346-b67f-2f456cf8455f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671399
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c678ee35-c6d7-4af4-b87f-0dbd49c9cd4d",
                    "transitionId": "248f4a79-78c0-4c08-99c2-7a68745af918",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671402
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0f208995-6166-4113-9cd8-c1e701ef081c",
                    "transitionId": "248f4a79-78c0-4c08-99c2-7a68745af918",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15421494671405,
                    "reason": ""
                }
            ]
        },
        "7bbf5e3c-b96f-4881-89ed-d5436aaebad6": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7bbf5e3c-b96f-4881-89ed-d5436aaebad6",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "645ae05b-e5a6-4248-8e68-240c57053ff3",
                    "transitionId": "01928273-e081-444c-8b82-622b5cc2a530",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671409
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bcf6eb4b-0643-4efb-a656-a5e7ab725efb",
                    "transitionId": "01928273-e081-444c-8b82-622b5cc2a530",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671413
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8f6366db-212b-4257-acd2-1d8402392bd5",
                    "transitionId": "803cc741-a2a9-4a41-a63a-80f28c74b21b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671415
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "92bdfac3-25d7-4d78-90d6-9bab9e9e3679",
                    "transitionId": "803cc741-a2a9-4a41-a63a-80f28c74b21b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671419
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db85a33e-8789-42e0-85e2-90095700ee26",
                    "transitionId": "e92caf11-20d1-497b-a3e8-54caa0720dc3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421494671421
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "698e9bad-cf76-4e07-bfa2-2606e8b7613f",
                    "transitionId": "e92caf11-20d1-497b-a3e8-54caa0720dc3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421494671425
                }
            ]
        }
    }
}