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": "34697e47-5030-4be3-91d4-be0c1d2ea555",
    "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": {
        "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488": {
            "id": "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_warehouse"
        },
        "d7fedad5-f97d-4900-afff-99e08084d6b3": {
            "id": "d7fedad5-f97d-4900-afff-99e08084d6b3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_accounting"
        },
        "cc1002bb-5632-401d-8159-d10268abbffd": {
            "id": "cc1002bb-5632-401d-8159-d10268abbffd",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "fork",
            "to": "update_delivery"
        },
        "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2": {
            "id": "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_warehouse",
            "to": "join"
        },
        "21fcc024-3fc3-4fd6-8b2a-9ef04217362d": {
            "id": "21fcc024-3fc3-4fd6-8b2a-9ef04217362d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_accounting",
            "to": "join"
        },
        "3684dc6d-c115-4948-94c7-d4e12fe6b43f": {
            "id": "3684dc6d-c115-4948-94c7-d4e12fe6b43f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "update_delivery",
            "to": "join"
        },
        "4031704f-ea88-426c-b711-4b545d4f2b8b": {
            "id": "4031704f-ea88-426c-b711-4b545d4f2b8b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "join",
            "to": "notify_customer"
        },
        "361b8175-bf13-454e-9c70-eaae4d0b11c5": {
            "id": "361b8175-bf13-454e-9c70-eaae4d0b11c5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "fork"
        }
    },
    "outTransitions": {
        "fork": [
            "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488",
            "d7fedad5-f97d-4900-afff-99e08084d6b3",
            "cc1002bb-5632-401d-8159-d10268abbffd"
        ],
        "update_warehouse": [
            "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2"
        ],
        "update_accounting": [
            "21fcc024-3fc3-4fd6-8b2a-9ef04217362d"
        ],
        "update_delivery": [
            "3684dc6d-c115-4948-94c7-d4e12fe6b43f"
        ],
        "join": [
            "4031704f-ea88-426c-b711-4b545d4f2b8b"
        ]
    },
    "inTransitions": {
        "update_warehouse": [
            "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488"
        ],
        "update_accounting": [
            "d7fedad5-f97d-4900-afff-99e08084d6b3"
        ],
        "update_delivery": [
            "cc1002bb-5632-401d-8159-d10268abbffd"
        ],
        "join": [
            "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2",
            "21fcc024-3fc3-4fd6-8b2a-9ef04217362d",
            "3684dc6d-c115-4948-94c7-d4e12fe6b43f"
        ],
        "notify_customer": [
            "4031704f-ea88-426c-b711-4b545d4f2b8b"
        ],
        "fork": [
            "361b8175-bf13-454e-9c70-eaae4d0b11c5"
        ]
    },
    "tokens": {
        "4f061e9d-2db5-4e42-8fbe-499cece69d1f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "4f061e9d-2db5-4e42-8fbe-499cece69d1f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ea87ffa4-ff9a-42d9-a5af-c3768f37fce6",
                    "transitionId": "361b8175-bf13-454e-9c70-eaae4d0b11c5",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494682
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c4be0421-b062-488c-97a7-13a141cba360",
                    "transitionId": "361b8175-bf13-454e-9c70-eaae4d0b11c5",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494685
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2f0edea7-8a4e-4445-b959-a7ccdbb78c0c",
                    "transitionId": "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494690
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "63df333d-4535-4796-a7cd-6751c3095301",
                    "transitionId": "4339b1e0-bb11-4b37-9f86-2ec0cd7e9488",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494693
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6b3140c2-76f9-43ec-8143-addd274ae346",
                    "transitionId": "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494696
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cc5627ed-d389-4849-8c0a-294e95db6893",
                    "transitionId": "ab6ecd27-5de6-415b-b4eb-76ab1bba97c2",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15906527494700,
                    "reason": ""
                }
            ]
        },
        "1315f6d5-fa60-450b-b08d-628a65c51f0d": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "1315f6d5-fa60-450b-b08d-628a65c51f0d",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "15b3c396-0d6a-4a04-a3f8-656355029894",
                    "transitionId": "d7fedad5-f97d-4900-afff-99e08084d6b3",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494705
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1d5108f6-f273-4a67-b966-12fa09924f26",
                    "transitionId": "d7fedad5-f97d-4900-afff-99e08084d6b3",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494709
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fbf661be-fd74-4016-981d-c9ceb3aab56c",
                    "transitionId": "21fcc024-3fc3-4fd6-8b2a-9ef04217362d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494711
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "89df657d-606a-4c73-b7d0-9e4fc1b3f074",
                    "transitionId": "21fcc024-3fc3-4fd6-8b2a-9ef04217362d",
                    "weight": 1,
                    "state": "interrupted",
                    "time": 15906527494716,
                    "reason": ""
                }
            ]
        },
        "9bff5960-058d-4119-b843-704f9e2ea8ad": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "9bff5960-058d-4119-b843-704f9e2ea8ad",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ae48f37b-ed5e-4643-bf98-48604f68ff8c",
                    "transitionId": "cc1002bb-5632-401d-8159-d10268abbffd",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494719
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3cc743c5-8baa-472d-8baf-ee8583d7d978",
                    "transitionId": "cc1002bb-5632-401d-8159-d10268abbffd",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494723
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "81c6e377-b2aa-4942-bfe5-9eae96d3c5f4",
                    "transitionId": "3684dc6d-c115-4948-94c7-d4e12fe6b43f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494726
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ff071190-6d5d-465b-bc6f-2339955be16b",
                    "transitionId": "3684dc6d-c115-4948-94c7-d4e12fe6b43f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494729
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6892c362-6548-48d3-a7bb-25807c80cfe0",
                    "transitionId": "4031704f-ea88-426c-b711-4b545d4f2b8b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906527494732
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "89bc8769-a50d-469b-b8ea-d46f65ab5ac3",
                    "transitionId": "4031704f-ea88-426c-b711-4b545d4f2b8b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906527494735
                }
            ]
        }
    }
}