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": "491aa1fa-ee37-4b91-9f37-a061be9a8dfd",
    "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": {
        "a6f799ad-fc0c-4a31-8ca9-baa7d4721257": {
            "id": "a6f799ad-fc0c-4a31-8ca9-baa7d4721257",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "1370fcaa-f0d2-4672-895b-7c45dd59c01c": {
            "id": "1370fcaa-f0d2-4672-895b-7c45dd59c01c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "c7bfe081-3412-4134-a12c-4889699b5459": {
            "id": "c7bfe081-3412-4134-a12c-4889699b5459",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "bcc226c6-e424-4d31-8267-0b578e6d21e4": {
            "id": "bcc226c6-e424-4d31-8267-0b578e6d21e4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "006092d4-cb76-4ea4-bb4f-05ce68e80b55": {
            "id": "006092d4-cb76-4ea4-bb4f-05ce68e80b55",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "abb82609-2c0d-4e21-9e55-da9ec7582936": {
            "id": "abb82609-2c0d-4e21-9e55-da9ec7582936",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1": {
            "id": "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "57cea0f6-3ea0-44a0-8ca3-50cc837981b6": {
            "id": "57cea0f6-3ea0-44a0-8ca3-50cc837981b6",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "a6f799ad-fc0c-4a31-8ca9-baa7d4721257",
            "1370fcaa-f0d2-4672-895b-7c45dd59c01c",
            "c7bfe081-3412-4134-a12c-4889699b5459"
        ],
        "update_warehouse": [
            "bcc226c6-e424-4d31-8267-0b578e6d21e4"
        ],
        "update_accounting": [
            "006092d4-cb76-4ea4-bb4f-05ce68e80b55"
        ],
        "update_delivery": [
            "abb82609-2c0d-4e21-9e55-da9ec7582936"
        ],
        "join": [
            "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "a6f799ad-fc0c-4a31-8ca9-baa7d4721257"
        ],
        "update_accounting": [
            "1370fcaa-f0d2-4672-895b-7c45dd59c01c"
        ],
        "update_delivery": [
            "c7bfe081-3412-4134-a12c-4889699b5459"
        ],
        "join": [
            "bcc226c6-e424-4d31-8267-0b578e6d21e4",
            "006092d4-cb76-4ea4-bb4f-05ce68e80b55",
            "abb82609-2c0d-4e21-9e55-da9ec7582936"
        ],
        "notify_customer": [
            "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1"
        ],
        "fork": [
            "57cea0f6-3ea0-44a0-8ca3-50cc837981b6"
        ]
    },
    "tokens": {
        "906589eb-3cf1-49ab-99d3-4080056b67ab": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "906589eb-3cf1-49ab-99d3-4080056b67ab",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9328a146-bbbc-4ae8-9409-5920f68dbea1",
                    "transitionId": "57cea0f6-3ea0-44a0-8ca3-50cc837981b6",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767185
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6675eb23-ccd8-48d4-8e44-4d6a2affe422",
                    "transitionId": "57cea0f6-3ea0-44a0-8ca3-50cc837981b6",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767189
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d6d48b50-bdd9-47f9-9e17-1e395a30ea92",
                    "transitionId": "a6f799ad-fc0c-4a31-8ca9-baa7d4721257",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767192
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c608fba9-35e4-4a45-9bfb-18421bc65949",
                    "transitionId": "a6f799ad-fc0c-4a31-8ca9-baa7d4721257",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767196
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b510bf26-f5cf-4abb-b61c-02e02eab87ce",
                    "transitionId": "bcc226c6-e424-4d31-8267-0b578e6d21e4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767198
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dac3b744-e662-44dd-bf7f-c9af04eb42ab",
                    "transitionId": "bcc226c6-e424-4d31-8267-0b578e6d21e4",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15714988767204,
                    "reason": ""
                }
            ]
        },
        "e2d3db79-2b9d-4144-aa17-e69cff8bb1ad": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "e2d3db79-2b9d-4144-aa17-e69cff8bb1ad",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "90e7a372-111c-4fca-8e28-96d2a6e8320e",
                    "transitionId": "1370fcaa-f0d2-4672-895b-7c45dd59c01c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767207
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "afccfa03-ec08-4ba4-b43e-2f099b3c2656",
                    "transitionId": "1370fcaa-f0d2-4672-895b-7c45dd59c01c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767211
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2c6411b0-91e4-4d22-92fa-cfdbaa76c109",
                    "transitionId": "006092d4-cb76-4ea4-bb4f-05ce68e80b55",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767214
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d8a48847-dcfb-4c9b-8a30-c2f42aa52524",
                    "transitionId": "006092d4-cb76-4ea4-bb4f-05ce68e80b55",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15714988767218,
                    "reason": ""
                }
            ]
        },
        "e1b222de-4449-4426-bc6f-eb80c3a4c8ab": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "e1b222de-4449-4426-bc6f-eb80c3a4c8ab",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b49109ca-fb9b-4245-8931-2454b016d927",
                    "transitionId": "c7bfe081-3412-4134-a12c-4889699b5459",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767222
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0b8e45b8-5940-4a59-83d9-bcea7d80064b",
                    "transitionId": "c7bfe081-3412-4134-a12c-4889699b5459",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767227
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e954acd1-17ea-4015-9b4c-cc0d3f5ba9f0",
                    "transitionId": "abb82609-2c0d-4e21-9e55-da9ec7582936",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767230
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4710429a-8002-4f01-8695-0c272195d497",
                    "transitionId": "abb82609-2c0d-4e21-9e55-da9ec7582936",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767234
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fe2c2a10-8f02-405d-90dd-2ba7d757095c",
                    "transitionId": "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714988767237
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db8ab6c6-0754-445c-96aa-35ca41e4ffe6",
                    "transitionId": "b2ac63c1-ddcf-4488-bb38-087b4e4e77d1",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714988767241
                }
            ]
        }
    }
}