You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/workflow.md
+51-126
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ To create a Workflow define its named Jobs.
54
54
55
55
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.
56
56
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.
58
58
59
59
```plain
60
60
<name>: <sync|async>(
@@ -77,7 +77,7 @@ class MyAction extends Action
77
77
}
78
78
```
79
79
80
-
You would be able to write Workflows like this:
80
+
You will be able to write a Workflow like this:
81
81
82
82
```php
83
83
use function Chevere\Workflow\sync;
@@ -127,12 +127,18 @@ workflow(
127
127
```
128
128
129
129
*`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.
131
131
132
132
The graph for this Workflow says that all jobs run one after each other as all jobs are defined using `sync`.
133
133
134
+
```mermaid
135
+
graph TD;
136
+
user-->validate-->meta-->store;
137
+
```
138
+
134
139
```php
135
-
//$workflow->jobs()->graph()->toArray();
140
+
$workflow->jobs()->graph()->toArray();
141
+
// contains
136
142
[
137
143
['user'],
138
144
['validate'],
@@ -155,7 +161,7 @@ run(
155
161
156
162
### With asynchronous jobs
157
163
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.
159
165
160
166
In the example below a Workflow describes an image creation procedure for multiple image sizes.
161
167
@@ -190,10 +196,18 @@ workflow(
190
196
*`variable('image')` declares a [Variable](#variable).
191
197
*`response('thumb', 'filename')` and `response('medium', 'filename')` declares a [Response](#response) reference.
192
198
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
+
```
194
207
195
208
```php
196
-
//$workflow->jobs()->graph()->toArray();
209
+
$workflow->jobs()->graph()->toArray();
210
+
// contains
197
211
[
198
212
['thumb', 'medium', 'poster'],
199
213
['store']
@@ -282,7 +296,7 @@ sync(
282
296
);
283
297
```
284
298
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`.
286
300
287
301
### Conditional running
288
302
@@ -295,7 +309,7 @@ sync(
295
309
)
296
310
->withRunIf(
297
311
variable('compressImage'),
298
-
reference('SomeAction', 'doImageCompress')
312
+
response('SomeAction', 'doImageCompress')
299
313
)
300
314
```
301
315
@@ -326,144 +340,55 @@ Use `getResponse` to retrieve a job response as a `CastArgument` object which ca
326
340
$string = $run->getResponse('myJob')->string();
327
341
```
328
342
329
-
## Code Examples
343
+
## Demo
330
344
331
-
### Hello, world
345
+
See the [demo](https://github.com/chevere/workflow/tree/1.0/demo) directory for a set of examples.
332
346
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
334
348
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.
336
350
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.
353
352
354
353
```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
+
];
359
360
```
360
361
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.
364
363
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).
366
366
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
375
368
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.
399
370
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).
401
372
402
-
```mermaid
403
-
graph TD;
404
-
thumb-->storeThumb;
405
-
poster-->storePoster;
406
-
```
373
+
### Testing Workflow
407
374
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).
409
376
410
377
```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()
417
381
);
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);
425
382
```
426
383
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
432
385
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.
438
387
439
388
```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)
458
393
);
459
394
```
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.
0 commit comments