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.
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
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": "69f8b15b-bad9-430a-8295-242e5cb48354", "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": { "53b74b1a-9db1-4f59-b30c-27bbb63396c7": { "id": "53b74b1a-9db1-4f59-b30c-27bbb63396c7", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "fork", "to": "update_warehouse" }, "0306977e-a465-4f8e-89e9-9672198a4b66": { "id": "0306977e-a465-4f8e-89e9-9672198a4b66", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "fork", "to": "update_accounting" }, "4ac04735-4649-4a68-a53c-0a51e2a451fb": { "id": "4ac04735-4649-4a68-a53c-0a51e2a451fb", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "fork", "to": "update_delivery" }, "e45b48ca-9bad-4cdb-9ead-f06db74681cf": { "id": "e45b48ca-9bad-4cdb-9ead-f06db74681cf", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "update_warehouse", "to": "join" }, "229adf6b-44a0-4a70-a6cc-84fedf4df99b": { "id": "229adf6b-44a0-4a70-a6cc-84fedf4df99b", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "update_accounting", "to": "join" }, "26bf69d6-b027-4aa8-9da7-ddab61276671": { "id": "26bf69d6-b027-4aa8-9da7-ddab61276671", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "update_delivery", "to": "join" }, "8e4dbb6f-3556-4010-9778-7d15cd075eeb": { "id": "8e4dbb6f-3556-4010-9778-7d15cd075eeb", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "join", "to": "notify_customer" }, "92d0b972-3c46-4c86-84db-ae1e53f94410": { "id": "92d0b972-3c46-4c86-84db-ae1e53f94410", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "to": "fork" } }, "outTransitions": { "fork": [ "53b74b1a-9db1-4f59-b30c-27bbb63396c7", "0306977e-a465-4f8e-89e9-9672198a4b66", "4ac04735-4649-4a68-a53c-0a51e2a451fb" ], "update_warehouse": [ "e45b48ca-9bad-4cdb-9ead-f06db74681cf" ], "update_accounting": [ "229adf6b-44a0-4a70-a6cc-84fedf4df99b" ], "update_delivery": [ "26bf69d6-b027-4aa8-9da7-ddab61276671" ], "join": [ "8e4dbb6f-3556-4010-9778-7d15cd075eeb" ] }, "inTransitions": { "update_warehouse": [ "53b74b1a-9db1-4f59-b30c-27bbb63396c7" ], "update_accounting": [ "0306977e-a465-4f8e-89e9-9672198a4b66" ], "update_delivery": [ "4ac04735-4649-4a68-a53c-0a51e2a451fb" ], "join": [ "e45b48ca-9bad-4cdb-9ead-f06db74681cf", "229adf6b-44a0-4a70-a6cc-84fedf4df99b", "26bf69d6-b027-4aa8-9da7-ddab61276671" ], "notify_customer": [ "8e4dbb6f-3556-4010-9778-7d15cd075eeb" ], "fork": [ "92d0b972-3c46-4c86-84db-ae1e53f94410" ] }, "tokens": { "5c6b5aea-8cbc-48a9-81da-4af631a94638": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "5c6b5aea-8cbc-48a9-81da-4af631a94638", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "022f74b5-8226-4dfa-8189-98c99067befe", "transitionId": "92d0b972-3c46-4c86-84db-ae1e53f94410", "weight": 1, "state": "opened", "time": 16107350316896 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "b0aa11e4-034d-429c-a5aa-1b8e53ecbcee", "transitionId": "92d0b972-3c46-4c86-84db-ae1e53f94410", "weight": 1, "state": "passed", "time": 16107350316900 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "24132ece-7fde-4562-ab4b-d611b743147c", "transitionId": "53b74b1a-9db1-4f59-b30c-27bbb63396c7", "weight": 1, "state": "opened", "time": 16107350316903 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "0f35e344-6370-462b-b475-8f208ae1dbfd", "transitionId": "53b74b1a-9db1-4f59-b30c-27bbb63396c7", "weight": 1, "state": "passed", "time": 16107350316909 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "9396c4fc-032d-4d62-a15a-56e9fd0552f8", "transitionId": "e45b48ca-9bad-4cdb-9ead-f06db74681cf", "weight": 1, "state": "opened", "time": 16107350316912 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "687e4e41-a06a-48d5-a71b-d2741f9d1a19", "transitionId": "e45b48ca-9bad-4cdb-9ead-f06db74681cf", "weight": 1, "state": "interrupted", "time": 16107350316916, "reason": "" } ] }, "7c7471aa-a2e6-4b9b-a6ba-aecf16df174c": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "7c7471aa-a2e6-4b9b-a6ba-aecf16df174c", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "a9200365-3ccd-41c2-bb63-e16b3c3d718b", "transitionId": "0306977e-a465-4f8e-89e9-9672198a4b66", "weight": 1, "state": "opened", "time": 16107350316920 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "a946e6ef-9141-4a04-a3b6-e45dfebc34ad", "transitionId": "0306977e-a465-4f8e-89e9-9672198a4b66", "weight": 1, "state": "passed", "time": 16107350316923 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "3077723f-b119-4c15-89d6-3ccd477683c2", "transitionId": "229adf6b-44a0-4a70-a6cc-84fedf4df99b", "weight": 1, "state": "opened", "time": 16107350316927 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "34365936-ddce-4c2a-849c-f19d63fcc1d9", "transitionId": "229adf6b-44a0-4a70-a6cc-84fedf4df99b", "weight": 1, "state": "interrupted", "time": 16107350316930, "reason": "" } ] }, "a7d6835a-4932-4a13-aef0-2005320b83ae": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "a7d6835a-4932-4a13-aef0-2005320b83ae", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "599ec02a-53aa-49c1-990e-06d6fc279481", "transitionId": "4ac04735-4649-4a68-a53c-0a51e2a451fb", "weight": 1, "state": "opened", "time": 16107350316934 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "af796fd1-b411-42c2-8565-ae8435c3beb2", "transitionId": "4ac04735-4649-4a68-a53c-0a51e2a451fb", "weight": 1, "state": "passed", "time": 16107350316938 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "b258d46c-616d-4abe-be95-283ed8abe76c", "transitionId": "26bf69d6-b027-4aa8-9da7-ddab61276671", "weight": 1, "state": "opened", "time": 16107350316941 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "2747bc3e-95f9-4440-b9e6-62081edb1a41", "transitionId": "26bf69d6-b027-4aa8-9da7-ddab61276671", "weight": 1, "state": "passed", "time": 16107350316944 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "a97c3389-0ec5-4282-8eab-292fd3d6b2ba", "transitionId": "8e4dbb6f-3556-4010-9778-7d15cd075eeb", "weight": 1, "state": "opened", "time": 16107350316947 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "f57ad853-686e-4391-bc57-9e852796139b", "transitionId": "8e4dbb6f-3556-4010-9778-7d15cd075eeb", "weight": 1, "state": "passed", "time": 16107350316950 } ] } } }