Cycle

The example shows how some nodes could be executed several times in a cycle.
For example you want to send an email but something went wrong and the server is not available and you want to retry the operation.

Setup


mkdir ~/pvm-cycle
cd ~/pvm-cycle
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php cycle.php

Result

cycle.php

failed to send email
email is sent

cycle.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;

require_once 
__DIR__.'/vendor/autoload.php';


$process = (new ProcessBuilder())
    ->
createNode('send_email''send_email')->end()
    ->
createNode('email_sent''email_sent')->end()

    ->
createTransition('send_email''send_email''retry')->end()
    ->
createTransition('send_email''email_sent')->end()
    ->
createStartTransition('send_email')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'email_sent' => function(Token $token) {
        echo 
'email is sent'.PHP_EOL;
    },
    
'send_email' => function(Token $token) {
        
$counter $token->getValue('counter'0);
        
$counter++;

        
$token->setValue('counter'$counter);

        try {
            if (
$counter $token->getValue('maxRetries')) {
                throw new \
LogicException('Mail server is down');
            }
        } catch (\
Throwable $e) {
            echo 
'failed to send email'.PHP_EOL;
            return 
'retry';
        }
    },
]));

$token $engine->createTokenFor($process->getStartTransition());
$token->setValue('maxRetries'rand(24));

$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "86e647e2-40dc-4950-9679-8b9b46fdaa55",
    "nodes": {
        "send_email": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "send_email",
            "behavior": "send_email"
        },
        "email_sent": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "email_sent",
            "behavior": "email_sent"
        }
    },
    "transitions": {
        "5b77c056-b140-4db2-a183-774fe46ed719": {
            "id": "5b77c056-b140-4db2-a183-774fe46ed719",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "66fe047b-38a7-4756-8a55-c0c8ea719154": {
            "id": "66fe047b-38a7-4756-8a55-c0c8ea719154",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "688151a5-3816-467f-9974-2658edac52d8": {
            "id": "688151a5-3816-467f-9974-2658edac52d8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "5b77c056-b140-4db2-a183-774fe46ed719",
            "66fe047b-38a7-4756-8a55-c0c8ea719154"
        ]
    },
    "inTransitions": {
        "send_email": [
            "5b77c056-b140-4db2-a183-774fe46ed719",
            "688151a5-3816-467f-9974-2658edac52d8"
        ],
        "email_sent": [
            "66fe047b-38a7-4756-8a55-c0c8ea719154"
        ]
    },
    "tokens": {
        "2c7b17d8-4485-45f9-a1a4-424f6c405d3f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "2c7b17d8-4485-45f9-a1a4-424f6c405d3f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "222d432d-674c-4658-9302-b1a9d98cd76e",
                    "transitionId": "688151a5-3816-467f-9974-2658edac52d8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559357008576
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f48dd5c2-ca1a-4350-84c7-ff2e3e5a2301",
                    "transitionId": "688151a5-3816-467f-9974-2658edac52d8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559357008586
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dfb9ae40-0be3-4f8a-838f-bf9b3b9e7894",
                    "transitionId": "5b77c056-b140-4db2-a183-774fe46ed719",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559357008591
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "809809a6-9ad7-444b-8dcb-7001314cd7f4",
                    "transitionId": "5b77c056-b140-4db2-a183-774fe46ed719",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559357008598
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "21e193f7-cfd1-4162-99dc-0027414818ed",
                    "transitionId": "66fe047b-38a7-4756-8a55-c0c8ea719154",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559357008603
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d460584b-10db-4a3a-bd52-2cd2bec58816",
                    "transitionId": "66fe047b-38a7-4756-8a55-c0c8ea719154",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559357008606
                }
            ],
            "maxRetries": 2,
            "counter": 2
        }
    }
}