Skip to content

Latest commit

 

History

History
111 lines (91 loc) · 5.23 KB

router_task.md

File metadata and controls

111 lines (91 loc) · 5.23 KB

RouterTask

Sends each input to the conditional branches of the task whose conditions it matches: the first matching branch, or all of them, else a default branch. Use it to dispatch the items between several processings (e.g. the lines of a file by type, the responses of an API by status); to drop the items that do not match, use the FilterTask.

The conditions are given by branch name in the conditions option, the tasks of each branch in the branches key of the task configuration (see task definition). Each branch of the task must have conditions or be the default branch, and each branch of the options must be a branch of the task: otherwise the process fails at the initialization of the task, before any input.

Task reference

  • Service: CleverAge\ProcessBundle\Task\RouterTask
  • Code: router (task: router)

Accepted inputs

any: the conditions of ConditionTrait read the values of an array or an object with the Symfony PropertyAccessor (a non-readable property is null, use the empty path '' for a scalar input); an expression gets the input as the data variable.

Possible outputs

any: the input, unchanged, sent to the outputs of the task and to the tasks of the selected branches.

Error output: the input, unchanged, when it matches no branch and there is no default branch (logged as a warning on the cleverage_process_task channel). An error while checking a condition (e.g. an expression calling a method of an array, a missing array key) makes the task fail: its error_strategy applies, the input never goes to the default branch. Both go to the same error outputs: use a default branch to keep the inputs of no branch apart from the errors.

Options

Code Type Required Default Description
conditions array X Branch name => conditions of the branch (see below); all of them must hold
mode string first first: the first matching branch, in the order of conditions; all: all of them
default string, int, null null Branch of the inputs matching no branch; null: they go to the error outputs

The conditions of a branch are the options of ConditionTrait (match, not_match, empty, not_empty, match_regexp, not_match_regexp) and expression, a Symfony ExpressionLanguage expression evaluated with the input as data (the bundle's cleverage_process.expression_language service, with its preg_match function, as in the RulesTransformer); its result is cast to bool. A branch without conditions (~) matches every input; another value than a mapping or ~ is rejected. An expression gives the alternatives (or) the other conditions cannot express. A missing array key in an expression is an error: give a default value when the key may be missing ((data["total"] ?? 0) > 1000).

Do not put a {{ key }} contextual value in an expression: it is replaced before the expression is parsed, so a value given by the context (command line, scheduler, UI form) would become expression code and change the routing. Read the values in the input (data) instead.

Examples

  • Dispatch the lines of a catalog file by type, ignore the comments, report the unknown types
# Task configuration level
route:
  task: router
  options:
    conditions:
      product:  { match: { '[type]': product } }
      category: { match: { '[type]': category } }
      comment:  { match: { '[type]': comment } }
  outputs: [count]                 # every line
  branches:
    product:  [write_product]
    category: [write_category]
    comment:  []                   # ignored
  error_outputs: [report]          # an unknown type, with a warning
  • Route the responses of an API by status code, with a default branch; flag the big orders as well (mode all)
# Task configuration level
route_status:
  task: router
  options:
    conditions:
      200: { match: { '[status]': 200 } }
      404: { match: { '[status]': 404 } }
    default: failed
  branches:
    200: [update_stock]
    404: [disable_product]
    failed: [alert]

route_order:
  task: router
  options:
    mode: all
    conditions:
      erp: ~                                                   # every order
      big_order: { expression: 'data["total"] > 1000 or data["vip"]' }
  branches:
    erp: [export_erp]
    big_order: [notify_sales]

See the conditional branches for the behaviour of the branches (blocking tasks, end point, no join with an InputAggregatorTask), and the recipe replacing a cascade of filters.