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": "5f62055b-d379-4487-b7d1-3746bf754894",
    "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": {
        "24c61567-7696-4799-b9fa-6e3ea0d104e8": {
            "id": "24c61567-7696-4799-b9fa-6e3ea0d104e8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "4d5b0110-b246-4f73-971f-e41bac398b9a": {
            "id": "4d5b0110-b246-4f73-971f-e41bac398b9a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "b5f28f57-c90c-4184-be50-0842a383ec8f": {
            "id": "b5f28f57-c90c-4184-be50-0842a383ec8f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "eac95771-69d1-4cba-819a-2fd6f97cdaf2": {
            "id": "eac95771-69d1-4cba-819a-2fd6f97cdaf2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "9eebcab0-fdb5-44b4-a695-41e1d393cd4f": {
            "id": "9eebcab0-fdb5-44b4-a695-41e1d393cd4f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "962c9f94-8c53-40be-bcaf-b96ec2e7361f": {
            "id": "962c9f94-8c53-40be-bcaf-b96ec2e7361f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "e285e2d6-2a80-450a-97b5-c42e4809959e": {
            "id": "e285e2d6-2a80-450a-97b5-c42e4809959e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "6a7c9e3c-4878-448b-8336-75c0f9847731": {
            "id": "6a7c9e3c-4878-448b-8336-75c0f9847731",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "24c61567-7696-4799-b9fa-6e3ea0d104e8",
            "4d5b0110-b246-4f73-971f-e41bac398b9a",
            "b5f28f57-c90c-4184-be50-0842a383ec8f"
        ],
        "update_warehouse": [
            "eac95771-69d1-4cba-819a-2fd6f97cdaf2"
        ],
        "update_accounting": [
            "9eebcab0-fdb5-44b4-a695-41e1d393cd4f"
        ],
        "update_delivery": [
            "962c9f94-8c53-40be-bcaf-b96ec2e7361f"
        ],
        "join": [
            "e285e2d6-2a80-450a-97b5-c42e4809959e"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "24c61567-7696-4799-b9fa-6e3ea0d104e8"
        ],
        "update_accounting": [
            "4d5b0110-b246-4f73-971f-e41bac398b9a"
        ],
        "update_delivery": [
            "b5f28f57-c90c-4184-be50-0842a383ec8f"
        ],
        "join": [
            "eac95771-69d1-4cba-819a-2fd6f97cdaf2",
            "9eebcab0-fdb5-44b4-a695-41e1d393cd4f",
            "962c9f94-8c53-40be-bcaf-b96ec2e7361f"
        ],
        "notify_customer": [
            "e285e2d6-2a80-450a-97b5-c42e4809959e"
        ],
        "fork": [
            "6a7c9e3c-4878-448b-8336-75c0f9847731"
        ]
    },
    "tokens": {
        "af70b56c-2501-4bf7-8320-267ae498fad3": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "af70b56c-2501-4bf7-8320-267ae498fad3",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3c981fc9-4d11-4c23-992c-f9bc0c592886",
                    "transitionId": "6a7c9e3c-4878-448b-8336-75c0f9847731",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008765
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b13f13fa-3de0-4824-92ec-5009733ea06a",
                    "transitionId": "6a7c9e3c-4878-448b-8336-75c0f9847731",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008769
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c5f48695-a81a-40cc-8eca-156c53cd183e",
                    "transitionId": "24c61567-7696-4799-b9fa-6e3ea0d104e8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008772
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2e57beb0-8af7-4bbe-8883-52fdda3fa363",
                    "transitionId": "24c61567-7696-4799-b9fa-6e3ea0d104e8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008776
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "84ed4d29-534b-4a4f-a2da-4f83012f4682",
                    "transitionId": "eac95771-69d1-4cba-819a-2fd6f97cdaf2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008779
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a4e7b223-334d-4202-a438-652445aeb47c",
                    "transitionId": "eac95771-69d1-4cba-819a-2fd6f97cdaf2",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15691118008785,
                    "reason": ""
                }
            ]
        },
        "4a945254-66f5-4632-ad0c-8f52f693e41f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "4a945254-66f5-4632-ad0c-8f52f693e41f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "105c67ce-2bc5-4bb2-8e73-4af629ab4842",
                    "transitionId": "4d5b0110-b246-4f73-971f-e41bac398b9a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008790
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "09e17318-74af-4aee-977e-bf6c20cc6f68",
                    "transitionId": "4d5b0110-b246-4f73-971f-e41bac398b9a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008796
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "445c1621-7eef-436c-bb5b-93f963390add",
                    "transitionId": "9eebcab0-fdb5-44b4-a695-41e1d393cd4f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008800
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1780f8e7-7d6d-4aaf-985c-375ba25e9e95",
                    "transitionId": "9eebcab0-fdb5-44b4-a695-41e1d393cd4f",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15691118008804,
                    "reason": ""
                }
            ]
        },
        "798364c8-430a-489f-bd0f-da22bd2bc3df": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "798364c8-430a-489f-bd0f-da22bd2bc3df",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bb19b7a8-f58b-4317-8fd1-8f91252dfcca",
                    "transitionId": "b5f28f57-c90c-4184-be50-0842a383ec8f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008810
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "02215f48-ec49-4586-98dc-e8455a057421",
                    "transitionId": "b5f28f57-c90c-4184-be50-0842a383ec8f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008815
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bb95eef7-a324-438e-abbb-e107c55d4ee8",
                    "transitionId": "962c9f94-8c53-40be-bcaf-b96ec2e7361f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008819
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "86c30e0a-a053-41fb-9f95-8209f7ad461d",
                    "transitionId": "962c9f94-8c53-40be-bcaf-b96ec2e7361f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008825
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1525b434-d83b-4ee3-8a56-01f119ecc57d",
                    "transitionId": "e285e2d6-2a80-450a-97b5-c42e4809959e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691118008830
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "80d6d3b1-9b0d-43b1-8290-b15d5dcd5a1b",
                    "transitionId": "e285e2d6-2a80-450a-97b5-c42e4809959e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691118008836
                }
            ]
        }
    }
}