Skip to content

Commit f29dfeb

Browse files
committed
update docs
1 parent c0b0789 commit f29dfeb

File tree

1 file changed

+51
-126
lines changed

1 file changed

+51
-126
lines changed

packages/workflow.md

+51-126
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ To create a Workflow define its named Jobs.
5454

5555
A [Job](#creating-job) is created by passing an [Action](https://chevere.org/packages/action) and its *expected* run arguments which can be raw values, [Variables](#variable) and/or [Responses](#response) to another job's output.
5656

57-
The syntax for writing Workflow jobs require `name` for job's name, `sync/async` depending on job run method, and named `parameter` bding for each Action run parameter.
57+
The syntax for writing Workflow jobs require `name` for job's name, `sync/async` depending on job run method, and named `parameter` binding for each `Action::main` parameter.
5858

5959
```plain
6060
<name>: <sync|async>(
@@ -77,7 +77,7 @@ class MyAction extends Action
7777
}
7878
```
7979

80-
You would be able to write Workflows like this:
80+
You will be able to write a Workflow like this:
8181

8282
```php
8383
use function Chevere\Workflow\sync;
@@ -127,12 +127,18 @@ workflow(
127127
```
128128

129129
* `variable('payload')` and `variable('file')` declares a [Variable](#variable).
130-
* `response('meta', 'name')` and `reference('user')` declares a [Response](#response) reference.
130+
* `response('meta', 'name')` and `response('user')` declares a [Response](#response) reference.
131131

132132
The graph for this Workflow says that all jobs run one after each other as all jobs are defined using `sync`.
133133

134+
```mermaid
135+
graph TD;
136+
user-->validate-->meta-->store;
137+
```
138+
134139
```php
135-
//$workflow->jobs()->graph()->toArray();
140+
$workflow->jobs()->graph()->toArray();
141+
// contains
136142
[
137143
['user'],
138144
['validate'],
@@ -155,7 +161,7 @@ run(
155161

156162
### With asynchronous jobs
157163

158-
Use function `async` to create an asynchronous job, which runs in parallel non-blocking.
164+
Use function `async` to create an asynchronous job, which runs non-blocking.
159165

160166
In the example below a Workflow describes an image creation procedure for multiple image sizes.
161167

@@ -190,10 +196,18 @@ workflow(
190196
* `variable('image')` declares a [Variable](#variable).
191197
* `response('thumb', 'filename')` and `response('medium', 'filename')` declares a [Response](#response) reference.
192198

193-
The graph for this Workflow says that `thumb` and `medium` run non-blocking. Job `store` runs blocking (another node).
199+
The graph for this Workflow says that `thumb`, `medium` and `poster` run non-blocking in parallel. Job `store` runs blocking (another node).
200+
201+
```mermaid
202+
graph TD;
203+
thumb-->store;
204+
medium-->store;
205+
poster-->store;
206+
```
194207

195208
```php
196-
//$workflow->jobs()->graph()->toArray();
209+
$workflow->jobs()->graph()->toArray();
210+
// contains
197211
[
198212
['thumb', 'medium', 'poster'],
199213
['store']
@@ -282,7 +296,7 @@ sync(
282296
);
283297
```
284298

285-
For the code above, argument `context` will be passed "as-is" (`public`) to `SomeAction`, arguments `role` and `userId` will be dynamic provided. When running the Workflow these arguments will be matched against the Parameters defined at the [run](https://chevere.org/packages/action#run) method for `SomeAction`.
299+
For the code above, argument `context` will be passed "as-is" (`public`) to `SomeAction`, arguments `role` and `userId` will be dynamic provided. When running the Workflow these arguments will be matched against the Parameters defined at the [main method](https://chevere.org/packages/action#mai-method) for `SomeAction`.
286300

287301
### Conditional running
288302

@@ -295,7 +309,7 @@ sync(
295309
)
296310
->withRunIf(
297311
variable('compressImage'),
298-
reference('SomeAction', 'doImageCompress')
312+
response('SomeAction', 'doImageCompress')
299313
)
300314
```
301315

@@ -326,144 +340,55 @@ Use `getResponse` to retrieve a job response as a `CastArgument` object which ca
326340
$string = $run->getResponse('myJob')->string();
327341
```
328342

329-
## Code Examples
343+
## Demo
330344

331-
### Hello, world
345+
See the [demo](https://github.com/chevere/workflow/tree/1.0/demo) directory for a set of examples.
332346

333-
Run live example: `php demo/hello-world.php Rodolfo` - [view source](https://github.com/chevere/workflow/blob/0.9/demo/hello-world.php)
347+
## Debugging
334348

335-
The basic example Workflow defines a greet for a given username. The job `greet` is a named argument and it takes the `GreetAction` plus its run [arguments](https://chevere.org/packages/action#run).
349+
When working with this package you may want to debug the Workflow to ensure that the jobs are declared as expected.
336350

337-
```php
338-
use Chevere\Demo\Actions\Greet;
339-
use function Chevere\Workflow\run;
340-
use function Chevere\Workflow\sync;
341-
use function Chevere\Workflow\variable;
342-
use function Chevere\Workflow\workflow;
343-
344-
$workflow = workflow(
345-
greet: sync(
346-
new Greet(),
347-
username: variable('username'),
348-
),
349-
);
350-
```
351-
352-
Use function `run` to run the Workflow, variables are passed as named arguments.
351+
To debug a Workflow inspect the Jobs graph. It will show the job names and their dependencies for each execution level.
353352

354353
```php
355-
$run = run(
356-
$workflow,
357-
username: 'MyUsername'
358-
);
354+
$workflow->jobs()->graph()->toArray();
355+
[
356+
['job1', 'job2'], // 1st level
357+
['job3', 'job4'], // 2nd level
358+
['job5'], // 3rd level
359+
];
359360
```
360361

361-
### Async example
362-
363-
Run live example: `php demo/image-resize.php` - [view source](https://github.com/chevere/workflow/blob/0.9/demo/image-resize.php)
362+
For each level jobs will run in parallel, but the next level will run after the previous level gets resolved.
364363

365-
For this example Workflow defines an image resize procedure in two sizes. All jobs are defined as async, but as there are dependencies between jobs (see `variable` and `response`) the system resolves a suitable run strategy.
364+
> [!NOTE]
365+
> For runtime debugging is strongly recommended to use a non-blocking debugger like [xrDebug](https://xrdebug.com).
366366
367-
```php
368-
use Chevere\Demo\Actions\ImageResize;
369-
use Chevere\Demo\Actions\StoreFile;
370-
use function Chevere\Workflow\async;
371-
use function Chevere\Workflow\response;
372-
use function Chevere\Workflow\run;
373-
use function Chevere\Workflow\variable;
374-
use function Chevere\Workflow\workflow;
367+
## Testing
375368

376-
$workflow = workflow(
377-
thumb: async(
378-
new ImageResize(),
379-
file: variable('image'),
380-
fit: 'thumbnail',
381-
),
382-
poster: async(
383-
new ImageResize(),
384-
file: variable('image'),
385-
fit: 'poster',
386-
),
387-
storeThumb: async(
388-
new StoreFile(),
389-
file: response('thumb'),
390-
path: variable('savePath'),
391-
),
392-
storePoster: async(
393-
new StoreFile(),
394-
file: response('poster'),
395-
path: variable('savePath'),
396-
)
397-
);
398-
```
369+
Workflow checks on variables, references and any other configuration so you don't have to worry about that.
399370

400-
The graph for the Workflow above shows that `thumb` and `poster` run async, just like `storeThumb` and `storePoster` but the storage jobs run after the first dependency level gets resolved.
371+
Testing the Workflow itself is not necessary as it's just a configuration. What you need to test is the Workflow definition and their Jobs (Actions).
401372

402-
```mermaid
403-
graph TD;
404-
thumb-->storeThumb;
405-
poster-->storePoster;
406-
```
373+
### Testing Workflow
407374

408-
Use function `run` to run the Workflow, variables are passed as named arguments.
375+
For testing a Workflow what you need to assert is the expected Workflow graph (execution order).
409376

410377
```php
411-
use function Chevere\Workflow\run;
412-
413-
$run = run(
414-
$workflow,
415-
image: '/path/to/image-to-upload.png',
416-
savePath: '/path/to/storage/'
378+
assertSame(
379+
$expectedGraph,
380+
$workflow->jobs()->graph()->toArray()
417381
);
418-
419-
// Alternative syntax
420-
$variables = [
421-
'image' => '/path/to/image-to-upload.png',
422-
'savePath' => '/path/to/storage/'
423-
];
424-
$run = run($workflow, ...$variables);
425382
```
426383

427-
Use `getReturn` to retrieve a job response as a `CastArgument` object which can be used to get a typed response.
428-
429-
```php
430-
$thumbFile = $run->getReturn('thumb')->string();
431-
```
384+
### Testing Job
432385

433-
### Conditional jobs
434-
435-
Run live example: `php demo/run-if.php` - [view source](https://github.com/chevere/workflow/blob/0.9/demo/run-if.php)
436-
437-
For this example Workflow defines a greet for a given username, but only if a `sayHello` variable is set to `true`.
386+
For testing a Job what you need to test is the Action that defines that given Job against the response from the Action `main` method.
438387

439388
```php
440-
use Chevere\Demo\Actions\Greet;
441-
use function Chevere\Workflow\run;
442-
use function Chevere\Workflow\sync;
443-
use function Chevere\Workflow\variable;
444-
use function Chevere\Workflow\workflow;
445-
446-
/*
447-
php demo/run-if.php Rodolfo
448-
php demo/run-if.php
449-
*/
450-
451-
$workflow = workflow(
452-
greet: sync(
453-
new Greet(),
454-
username: variable('username'),
455-
)->withRunIf(
456-
variable('sayHello')
457-
),
389+
$action = new MyAction();
390+
assertSame(
391+
$expected,
392+
$action->main(...$arguments)
458393
);
459394
```
460-
461-
Method `withRunIf` accepts one or more `variable` and `response` references. All conditions must be true at the same time for the job to run.
462-
463-
## Debugging Workflow
464-
465-
To debug a Workflow inspect the Jobs graph. It will show the job names and their dependencies for each execution level.
466-
467-
```php
468-
$workflow->jobs()->graph()->toArray();
469-
```

0 commit comments

Comments
 (0)