Runner 0.3.0 · Library for PHP

Flows that run, wait and resume

Runner runs flows: blocks of steps that share one context. A step can check a condition, call a PHP function, run another block and come back, jump, or pause. A paused flow picks up later, even in another process, right after the step where it stopped. Use it for orders and approvals, data jobs, or code generators.

Free software (GPL 3) · PHP 8.1 or newer · one file, no dependencies

What a flow is

Blocks of steps, written in YAML

A flow is a YAML file. The first block is where the flow starts. Each step does its work through small PHP files that return a function, and every step reads and writes the same context: a PHP array.

A step can have these keys. They run in this order:

  1. condition If it is false, the rest of the step is skipped.
  2. context Values merged into the context.
  3. call Runs another block, then comes back to this step.
  4. activity A PHP function. What it returns is kept under the step's name.
  5. jump Goes to another step and does not come back.
  6. pause Stops the run. The next run continues after this step.

flows/order.yml

Flow flows/order.yml
id: order
description: Compute an order total, pause once for review, then print a receipt
blocks:
  main:
    steps:
      total:
        activity: compute_total
      review:
        call: review.check
      receipt:
        activity: format_receipt
  review:
    steps:
      check:
        condition: needs_review
        context:
          reviewed: true
        pause: true

functions/compute_total.php

Activity functions/compute_total.php
<?php

declare(strict_types=1);

// The returned value is stored in $context["total"] (the step id).
return function (array &$context): float {
    $total = 0.0;
    foreach ($context["items"] as $item) {
        $total += $item["price"] * $item["qty"];
    }

    return $total;
};

functions/needs_review.php

Condition functions/needs_review.php
<?php

declare(strict_types=1);

return fn (array &$context): bool => $context["total"] >= 20 && empty($context["reviewed"]);

functions/format_receipt.php

Activity functions/format_receipt.php
<?php

declare(strict_types=1);

return fn (array &$context): string =>
    sprintf("Total: %.2f%s", $context["total"], empty($context["reviewed"]) ? "" : " (reviewed)");

A real run

Watch it pause, then resume in a new process

Runner 0.3.0 ran this flow twice from its command line, with items of 10 × 2 and 5 × 1. The first run paused for review. The second run, a new PHP process with the saved context, finished the flow. Below is what Runner recorded, event by event. Move with Previous and Next.

  1. run.start Run 1 starts with the context you give it.
  2. block.enter Enters block main, the first block.
  3. step.start Step main.total starts.
  4. activity.end Activity compute_total returns 25.0, kept as total.
  5. step.start Step main.review starts.
  6. call call review.check: runs that block, then comes back here.
  7. block.enter Enters block review, called.
  8. step.start Step review.check starts.
  9. condition Condition needs_review is true, so the step goes on.
  10. context.update The step sets reviewed = true.
  11. pause pause: the run stops here. Saved: where it is, and where to come back to (main.review).
  12. run.end Run 1 ends: paused (exit code 3).
  13. run.start Run 2 starts, in a new PHP process, from the saved context.
  14. block.enter Resumes in block review, where it paused.
  15. step.skip Skips review.check: it ran before the pause.
  16. call.return Block review ends. Back to main.review.
  17. block.enter Back in block main.
  18. step.skip Skips main.total: it ran before the pause.
  19. step.start Continues step main.review, after its call.
  20. step.start Step main.receipt starts.
  21. activity.end Activity format_receipt returns "Total: 25.00 (reviewed)", kept as receipt.
  22. run.end Run 2 ends: done (exit code 0).
Context after this event

Simulation

Try a flow without running its functions

Since 0.3.0, Runner can simulate a flow. A test double you declare answers each condition and activity, so no function file is loaded. The flow itself runs on Runner's normal engine, with the same calls, jumps and pauses.

Here is an invoice flow. The order is large, so it pauses for approval. Then the first charge is declined: the retry step jumps back to charge, and the second charge goes through.

The real functions would write payments.log and outbox.log. After the simulation, neither file exists. The events match the ones Runner checks in its own tests.

  • A double can return a value, give a different answer on each call, fail, or pause.
  • Limits on steps, transfers and seconds stop a simulation that would never end.
  • Values with names like password or token are hidden in the trace.
Test doubles doubles.json
{
    "description": "Large order: pauses for approval, the first charge is declined, the retry is captured",
    "doubles": {
        "orders:validate_order": {"returns": true},
        "billing:compute_total": {"returns": 1250.0},
        "needs_approval": {"returns": true},
        "billing:charge_card": {"sequence": [
            {"returns": {"status": "declined"}, "context": {"last_charge": "declined"}},
            {"returns": {"status": "captured"}, "context": {"last_charge": "captured"}}
        ]},
        "payment_declined": {"sequence": [{"returns": true}, {"returns": false}]},
        "send_receipt": {"returns": "receipt sent"}
    },
    "budget": {"steps": 200, "transfers": 50, "seconds": 5}
}

  1. run.start Run 1 starts with the context you give it.
  2. block.enter Enters block main, the first block.
  3. step.start Step main.validated starts.
  4. activity.end Activity orders:validate_order returns true, kept as validated. A test double gave this answer.
  5. step.start Step main.total starts.
  6. activity.end Activity billing:compute_total returns 1250.0, kept as total. A test double gave this answer.
  7. step.start Step main.approval starts.
  8. call call approval.check: runs that block, then comes back here.
  9. block.enter Enters block approval, called.
  10. step.start Step approval.check starts.
  11. condition Condition needs_approval is true, so the step goes on.
  12. context.update The step sets approval_requested = true.
  13. pause pause: the run stops here. Saved: where it is, and where to come back to (main.approval).
  14. run.end Run 1 ends: paused (exit code 3).
  15. run.start Run 2 starts, in a new PHP process, from the saved context.
  16. block.enter Resumes in block approval, where it paused.
  17. step.skip Skips approval.check: it ran before the pause.
  18. call.return Block approval ends. Back to main.approval.
  19. block.enter Back in block main.
  20. step.skip Skips main.validated: it ran before the pause.
  21. step.skip Skips main.total: it ran before the pause.
  22. step.start Continues step main.approval, after its call.
  23. step.start Step main.payment starts.
  24. call call payment.charge: runs that block, then comes back here.
  25. block.enter Enters block payment, called.
  26. step.start Step payment.charge starts.
  27. activity.end Activity billing:charge_card returns {"status": "declined"}, kept as charge. A test double gave this answer.
  28. step.start Step payment.retry starts.
  29. condition Condition payment_declined is true, so the step goes on.
  30. jump jump payment.charge: goes there and does not come back.
  31. block.enter Enters block payment again, by the jump.
  32. step.start Step payment.charge starts.
  33. activity.end Activity billing:charge_card returns {"status": "captured"}, kept as charge. A test double gave this answer.
  34. step.start Step payment.retry starts.
  35. condition Condition payment_declined is false, so the rest of the step is skipped.
  36. call.return Block payment ends. Back to main.payment.
  37. step.start Step main.receipt starts.
  38. activity.end Activity send_receipt returns "receipt sent", kept as receipt. A test double gave this answer.
  39. run.end Run 2 ends: done (exit code 0).
Context after this event

What it is for

Any work that happens in steps

Runner runs the steps and keeps them in order. Your application decides where to keep a paused context and when to run it again: Runner is not a queue or a scheduler.

condition, pause, jump

Business processes

Approvals that wait for a person, payments that try again, orders that move through stages.

pause, resume

Long jobs

Work that stops and continues in another request or process, without repeating the steps that already ran.

call, activity

Generators

Load a model, check it, render it with Div, check the result. divide's example generator is a Runner flow.

Runner in divide

See the flow, edit it, replay a simulation

divide works on the same YAML file. Its Flow Designer draws the blocks as lanes, the steps as cards, and every call and jump as a line. You edit the flow on the board, and each change is an edit of the YAML.

Runner does the work: divide asks Runner to check, run and simulate the flow. divide adds the view and the editing.

divide's Flow Designer: four blocks as lanes, their steps as cards, and the lines of their calls and jumps
Who does what
TaskRunner, the enginedivide, the IDE
The flowReads the YAML and compiles it to PHP. Rejects unknown keys.Draws it in the Flow Designer and edits the YAML. Shows Runner's verdict.
RunningRuns it, pauses it, and resumes it from a saved context.Ctrl+F9 asks Runner to run it, and keeps the context so you can resume.
SimulationAnswers functions with test doubles and records every event.Shift+F9 asks Runner to simulate, then replays the events on the board, forwards and backwards.
FunctionsFinds each function file, by name and module.Lists them, makes new ones, and opens them in the PHP editor.
One function, line by lineNot part of Runner.Function Mock Simulation runs one function's real PHP code, with doubles only for what it asks the context for. It is not a sandbox.
In divide: Runner simulates the flow, and divide replays the events on the board.

Under the hood

Small on purpose

One file, one class

No runtime dependencies. Use it from PHP, or from the terminal with the command line it comes with.

YAML becomes PHP

A flow compiles to a real PHP function. You can run it or save its source, which comes out the same every time.

Tested

On PHP 8.1 to 8.5, on every change. It follows the Sequential Flow Model.

Your code, trusted

Flows, conditions and activities are PHP code you trust. Runner does not sandbox them.

Install

Your first paused flow in three steps

A complete example. You need PHP 8.1 or newer and Composer.

1. Create a project with Runner

In an empty folder.

mkdir hello-runner
cd hello-runner
composer require divengine/runner:0.3.0

2. Add four files

flows/hello.yml:

id: hello
blocks:
  main:
    steps:
      greeting:
        activity: greet
      wait:
        pause: true
      shout:
        activity: shout

functions/greet.php:

<?php
return fn (array &$context): string =>
    "Hello " . $context["name"];

functions/shout.php:

<?php
return fn (array &$context): string =>
    strtoupper($context["greeting"]) . "!";

input.json:

{"name": "Ada"}

3. Run, then resume

The first run stops at wait and saves the context:

php vendor/divengine/runner/bin/run.php flows/hello.yml --context input.json --root functions --save-context state.json
paused

The second run continues after it:

php vendor/divengine/runner/bin/run.php flows/hello.yml --context state.json --save-context state.json
done

What the last step left in the context:

php -r 'echo json_decode(file_get_contents("state.json"), true)["shout"], PHP_EOL;'
HELLO ADA!

Prefer an IDE? divide runs, draws and simulates Runner flows. Every example on this page, with the commands to run it again with Runner 0.3.0, is in the site's repository.