|
| 1 | +RouterTask |
| 2 | +========== |
| 3 | + |
| 4 | +Sends each input to the [conditional branches](../../04-advanced_workflow.md#conditional-branches) of the task whose |
| 5 | +conditions it matches: the first matching branch, or all of them, else a default branch. Use it to dispatch the items |
| 6 | +between several processings (e.g. the lines of a file by type, the responses of an API by status); to drop the items |
| 7 | +that do not match, use the [FilterTask](filter_task.md). |
| 8 | + |
| 9 | +The conditions are given by branch name in the `conditions` option, the tasks of each branch in the `branches` key of |
| 10 | +the task configuration (see [task definition](../02-task_definition.md)). Each branch of the task must have conditions |
| 11 | +or be the `default` branch, and each branch of the options must be a branch of the task: otherwise the process fails |
| 12 | +at the initialization of the task, before any input. |
| 13 | + |
| 14 | +Task reference |
| 15 | +-------------- |
| 16 | + |
| 17 | +* **Service**: `CleverAge\ProcessBundle\Task\RouterTask` |
| 18 | +* **Code**: `router` (`task: router`) |
| 19 | + |
| 20 | +Accepted inputs |
| 21 | +--------------- |
| 22 | + |
| 23 | +`any`: the conditions of [ConditionTrait](../traits/condition_trait.md) read the values of an `array` or an `object` |
| 24 | +with the Symfony PropertyAccessor (a non-readable property is `null`, use the empty path `''` for a scalar input); an |
| 25 | +`expression` gets the input as the `data` variable. |
| 26 | + |
| 27 | +Possible outputs |
| 28 | +---------------- |
| 29 | + |
| 30 | +`any`: the input, unchanged, sent to the `outputs` of the task and to the tasks of the selected branches. |
| 31 | + |
| 32 | +Error output: the input, unchanged, when it matches no branch and there is no `default` branch (logged as a warning on |
| 33 | +the `cleverage_process_task` channel). An error while checking a condition (e.g. an expression calling a method of an |
| 34 | +array, a missing array key) makes the task fail: its `error_strategy` applies, the input never goes to the default |
| 35 | +branch. Both go to the same error outputs: use a `default` branch to keep the inputs of no branch apart from the errors. |
| 36 | + |
| 37 | +Options |
| 38 | +------- |
| 39 | + |
| 40 | +| Code | Type | Required | Default | Description | |
| 41 | +|--------------|-------------------------|:--------:|---------|--------------------------------------------------------------------------------------| |
| 42 | +| `conditions` | `array` | **X** | | Branch name => conditions of the branch (see below); all of them must hold | |
| 43 | +| `mode` | `string` | | `first` | `first`: the first matching branch, in the order of `conditions`; `all`: all of them | |
| 44 | +| `default` | `string`, `int`, `null` | | `null` | Branch of the inputs matching no branch; `null`: they go to the error outputs | |
| 45 | + |
| 46 | +The conditions of a branch are the options of [ConditionTrait](../traits/condition_trait.md) (`match`, `not_match`, |
| 47 | +`empty`, `not_empty`, `match_regexp`, `not_match_regexp`) and `expression`, a Symfony |
| 48 | +[ExpressionLanguage](https://symfony.com/doc/current/components/expression_language.html) expression evaluated with the |
| 49 | +input as `data` (the bundle's `cleverage_process.expression_language` service, with its `preg_match` function, as in |
| 50 | +the [RulesTransformer](../transformers/rules_transformer.md)); its result is cast to `bool`. A branch without conditions |
| 51 | +(`~`) matches every input; another value than a mapping or `~` is rejected. An expression gives the alternatives |
| 52 | +(`or`) the other conditions cannot express. A missing array key in an expression is an error: give a default value |
| 53 | +when the key may be missing (`(data["total"] ?? 0) > 1000`). |
| 54 | + |
| 55 | +Do not put a `{{ key }}` contextual value in an `expression`: it is replaced before the expression is parsed, so a value |
| 56 | +given by the context (command line, scheduler, UI form) would become expression code and change the routing. Read the |
| 57 | +values in the input (`data`) instead. |
| 58 | + |
| 59 | +Examples |
| 60 | +-------- |
| 61 | + |
| 62 | +* Dispatch the lines of a catalog file by type, ignore the comments, report the unknown types |
| 63 | + |
| 64 | +```yaml |
| 65 | +# Task configuration level |
| 66 | +route: |
| 67 | + task: router |
| 68 | + options: |
| 69 | + conditions: |
| 70 | + product: { match: { '[type]': product } } |
| 71 | + category: { match: { '[type]': category } } |
| 72 | + comment: { match: { '[type]': comment } } |
| 73 | + outputs: [count] # every line |
| 74 | + branches: |
| 75 | + product: [write_product] |
| 76 | + category: [write_category] |
| 77 | + comment: [] # ignored |
| 78 | + error_outputs: [report] # an unknown type, with a warning |
| 79 | +``` |
| 80 | +
|
| 81 | +* Route the responses of an API by status code, with a default branch; flag the big orders as well (mode `all`) |
| 82 | + |
| 83 | +```yaml |
| 84 | +# Task configuration level |
| 85 | +route_status: |
| 86 | + task: router |
| 87 | + options: |
| 88 | + conditions: |
| 89 | + 200: { match: { '[status]': 200 } } |
| 90 | + 404: { match: { '[status]': 404 } } |
| 91 | + default: failed |
| 92 | + branches: |
| 93 | + 200: [update_stock] |
| 94 | + 404: [disable_product] |
| 95 | + failed: [alert] |
| 96 | +
|
| 97 | +route_order: |
| 98 | + task: router |
| 99 | + options: |
| 100 | + mode: all |
| 101 | + conditions: |
| 102 | + erp: ~ # every order |
| 103 | + big_order: { expression: 'data["total"] > 1000 or data["vip"]' } |
| 104 | + branches: |
| 105 | + erp: [export_erp] |
| 106 | + big_order: [notify_sales] |
| 107 | +``` |
| 108 | + |
| 109 | +See the [conditional branches](../../04-advanced_workflow.md#conditional-branches) for the behaviour of the branches |
| 110 | +(blocking tasks, end point, no join with an [InputAggregatorTask](input_aggregator_task.md)), and the |
| 111 | +[recipe](../../cookbooks/router.md) replacing a cascade of filters. |
0 commit comments