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": "66e9bbdc-034e-4923-8987-c21832c94efd",
    "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": {
        "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8": {
            "id": "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "da4be5c6-d0cb-40e4-9c35-4106a14f1c98": {
            "id": "da4be5c6-d0cb-40e4-9c35-4106a14f1c98",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "013f4ea0-8970-46ee-a9ff-71edf9c469d0": {
            "id": "013f4ea0-8970-46ee-a9ff-71edf9c469d0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8",
            "da4be5c6-d0cb-40e4-9c35-4106a14f1c98"
        ]
    },
    "inTransitions": {
        "send_email": [
            "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8",
            "013f4ea0-8970-46ee-a9ff-71edf9c469d0"
        ],
        "email_sent": [
            "da4be5c6-d0cb-40e4-9c35-4106a14f1c98"
        ]
    },
    "tokens": {
        "77896a9a-1c4c-4459-bab7-137261cbabd2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "77896a9a-1c4c-4459-bab7-137261cbabd2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "51a2f10d-8caf-40ce-9f9d-333da923926f",
                    "transitionId": "013f4ea0-8970-46ee-a9ff-71edf9c469d0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664812974756
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e3cb2773-a697-4186-988e-c258bece4ed2",
                    "transitionId": "013f4ea0-8970-46ee-a9ff-71edf9c469d0",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664812974762
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e803335b-85cb-4e71-837f-1e08d86f5ee7",
                    "transitionId": "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664812974768
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c78851c2-cb44-400f-b49d-02566ae9012d",
                    "transitionId": "0b5a30dd-1a56-4ac9-9ebf-c7db6cdd03d8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664812974774
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "89fc9335-baa8-458b-b7a8-816d17d113d8",
                    "transitionId": "da4be5c6-d0cb-40e4-9c35-4106a14f1c98",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664812974780
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7b1f588b-88f4-4ee5-9c87-9ac07904e688",
                    "transitionId": "da4be5c6-d0cb-40e4-9c35-4106a14f1c98",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664812974788
                }
            ],
            "maxRetries": 2,
            "counter": 2
        }
    }
}