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": "34c83c8f-9257-45a9-825d-5550d9a30248",
    "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": {
        "2594ceca-5349-478f-a904-f2103d8ae0b9": {
            "id": "2594ceca-5349-478f-a904-f2103d8ae0b9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3": {
            "id": "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "c5adb808-6fcc-4621-9cf9-b7d5c340b950": {
            "id": "c5adb808-6fcc-4621-9cf9-b7d5c340b950",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0": {
            "id": "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328": {
            "id": "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "1a63aa4b-4094-4ec1-9b84-24ecce0471f0": {
            "id": "1a63aa4b-4094-4ec1-9b84-24ecce0471f0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "4c529de7-19ad-4621-a34b-257eac838523": {
            "id": "4c529de7-19ad-4621-a34b-257eac838523",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "493a020a-23bf-4bcc-9f56-60ee19a21ac1": {
            "id": "493a020a-23bf-4bcc-9f56-60ee19a21ac1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "2594ceca-5349-478f-a904-f2103d8ae0b9",
            "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3",
            "c5adb808-6fcc-4621-9cf9-b7d5c340b950"
        ],
        "update_warehouse": [
            "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0"
        ],
        "update_accounting": [
            "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328"
        ],
        "update_delivery": [
            "1a63aa4b-4094-4ec1-9b84-24ecce0471f0"
        ],
        "join": [
            "4c529de7-19ad-4621-a34b-257eac838523"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "2594ceca-5349-478f-a904-f2103d8ae0b9"
        ],
        "update_accounting": [
            "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3"
        ],
        "update_delivery": [
            "c5adb808-6fcc-4621-9cf9-b7d5c340b950"
        ],
        "join": [
            "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0",
            "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328",
            "1a63aa4b-4094-4ec1-9b84-24ecce0471f0"
        ],
        "notify_customer": [
            "4c529de7-19ad-4621-a34b-257eac838523"
        ],
        "fork": [
            "493a020a-23bf-4bcc-9f56-60ee19a21ac1"
        ]
    },
    "tokens": {
        "cdb87553-18c6-43af-a332-f5e11d11f17a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "cdb87553-18c6-43af-a332-f5e11d11f17a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0da12df3-23ef-49d6-ab3e-61f76a3b5fa9",
                    "transitionId": "493a020a-23bf-4bcc-9f56-60ee19a21ac1",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501897
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "63eefdb6-a520-4b6e-9530-1671e4ded412",
                    "transitionId": "493a020a-23bf-4bcc-9f56-60ee19a21ac1",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501901
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c1186972-5f7d-4217-a618-afa7469e53e5",
                    "transitionId": "2594ceca-5349-478f-a904-f2103d8ae0b9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501905
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a419891f-2179-463f-ac52-0ad7acc82f64",
                    "transitionId": "2594ceca-5349-478f-a904-f2103d8ae0b9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501909
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "153b1fe1-4032-4262-8ba0-c7db2a38d473",
                    "transitionId": "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501912
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "22776510-c799-428a-82d1-0bcb5f5a9164",
                    "transitionId": "4f2b20fa-7e3f-4b60-b19b-0b0c924406a0",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15857752501916,
                    "reason": ""
                }
            ]
        },
        "fa0ce7ab-4111-4c54-ba46-0c43c4b62ab1": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "fa0ce7ab-4111-4c54-ba46-0c43c4b62ab1",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8caf79aa-f9ad-4231-baa0-c454ec891e58",
                    "transitionId": "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501920
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "098622f6-58f1-427e-8c34-a4ae682c17fd",
                    "transitionId": "0c60a6a5-ddbf-4223-8b09-d1219e0afcf3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501924
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7a1488eb-f32a-43fd-a965-fcecfe5be086",
                    "transitionId": "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501927
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c9595a40-ed92-4c57-8063-ba00a58a8f4a",
                    "transitionId": "35d37bc7-0dc7-43ae-b61e-0a9f7cdc2328",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15857752501932,
                    "reason": ""
                }
            ]
        },
        "9cb5cc0e-73c7-4296-9bd6-60db47f15bb8": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "9cb5cc0e-73c7-4296-9bd6-60db47f15bb8",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "02c0760c-bc7e-4952-a83c-14c7358371a5",
                    "transitionId": "c5adb808-6fcc-4621-9cf9-b7d5c340b950",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501935
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "51f9936d-dc21-402a-88fb-46c0d875c1ca",
                    "transitionId": "c5adb808-6fcc-4621-9cf9-b7d5c340b950",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501939
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "44346684-ca1b-4754-a9fa-5fa2d8b36081",
                    "transitionId": "1a63aa4b-4094-4ec1-9b84-24ecce0471f0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501942
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "337b98b3-9a7e-46e1-8eac-571fa5eab9cb",
                    "transitionId": "1a63aa4b-4094-4ec1-9b84-24ecce0471f0",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501946
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c84fa178-59b8-4abb-8dcb-426e4fb83ca0",
                    "transitionId": "4c529de7-19ad-4621-a34b-257eac838523",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857752501949
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4d3ad757-20a5-492f-bd50-50ebd3eaef4a",
                    "transitionId": "4c529de7-19ad-4621-a34b-257eac838523",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857752501953
                }
            ]
        }
    }
}