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": "f0778499-bcb2-4a29-82f5-4c179d025a29",
    "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": {
        "7455b8f7-f8df-4eb4-a373-ba1bbb90019f": {
            "id": "7455b8f7-f8df-4eb4-a373-ba1bbb90019f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "e27fb622-cde0-4593-b565-9b22057d5512": {
            "id": "e27fb622-cde0-4593-b565-9b22057d5512",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "778c3c6b-35cd-4183-b10a-906b9d9193d2": {
            "id": "778c3c6b-35cd-4183-b10a-906b9d9193d2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "7455b8f7-f8df-4eb4-a373-ba1bbb90019f",
            "e27fb622-cde0-4593-b565-9b22057d5512"
        ]
    },
    "inTransitions": {
        "send_email": [
            "7455b8f7-f8df-4eb4-a373-ba1bbb90019f",
            "778c3c6b-35cd-4183-b10a-906b9d9193d2"
        ],
        "email_sent": [
            "e27fb622-cde0-4593-b565-9b22057d5512"
        ]
    },
    "tokens": {
        "fbe6593e-164e-4750-840a-c94d58b353c7": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "fbe6593e-164e-4750-840a-c94d58b353c7",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "980c5789-55e4-486f-a706-3138d99b0fb1",
                    "transitionId": "778c3c6b-35cd-4183-b10a-906b9d9193d2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823432191057
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "aa7e303f-fd70-4724-96c0-226d76e2621a",
                    "transitionId": "778c3c6b-35cd-4183-b10a-906b9d9193d2",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823432191062
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e58e3cc3-8b65-4a18-8b43-4d7cc6a542bc",
                    "transitionId": "7455b8f7-f8df-4eb4-a373-ba1bbb90019f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823432191066
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1d342a46-aa78-4ac3-8666-c644e9012897",
                    "transitionId": "7455b8f7-f8df-4eb4-a373-ba1bbb90019f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823432191070
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ddecce64-4eb6-4a73-a356-a83271804558",
                    "transitionId": "e27fb622-cde0-4593-b565-9b22057d5512",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823432191074
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f29d0c3b-4107-41f0-b869-17b8d69d92b3",
                    "transitionId": "e27fb622-cde0-4593-b565-9b22057d5512",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823432191077
                }
            ],
            "maxRetries": 2,
            "counter": 2
        }
    }
}