Skip to content

Latest commit

 

History

History
111 lines (96 loc) · 4.81 KB

router.md

File metadata and controls

111 lines (96 loc) · 4.81 KB

Dispatching items with a router

This recipe dispatches the lines of a single file between several processings, according to their content: a typical partner file mixing products, categories and comments.

type;code;label;total
product;P001;Chair;10
category;C01;Furniture;
product;P002;Table;2500
comment;;File of 2026-12-03;
promo;X1;Black Friday;

Before the RouterTask, this was done with a cascade of FilterTask, each rejected line going to the next filter through error_outputs:

is_product:
    task: filter
    options: { match: { '[type]': product } }
    outputs: [write_product]
    error_outputs: [is_category]     # "else", through the error channel
is_category:
    task: filter
    options: { match: { '[type]': category } }
    outputs: [write_category]        # the other lines are lost without trace
  • The next filters and their tasks are an error branch (shown as such by cleverage:process:help).
  • A failure of is_product with error_strategy: skip sends the line to is_category, as if it were not a product.
  • Each new type adds a filter, and a line of no known type is silently dropped.

With a router, the dispatch is a single task, the lines of no known type are reported, and the errors stay errors:

cleverage_process:
    configurations:
        app.import_catalog:
            description: 'Import the catalog file of the partner'
            tasks:
                read:
                    task: csv_reader
                    options:
                        file_path: '%kernel.project_dir%/var/data/catalog.csv'
                        delimiter: ';'
                    outputs: [route]

                route:
                    task: router
                    options:
                        mode: all
                        conditions:
                            product:  { match: { '[type]': product } }
                            category: { match: { '[type]': category } }
                            comment:  { match: { '[type]': comment } }
                            big:      { expression: 'data["type"] == "product" and data["total"] > 1000' }
                        default: unknown         # a line of no known type (e.g. promo)
                    outputs: [count]             # every line
                    branches:
                        product:  [write_product]
                        category: [write_category]
                        comment:  []             # ignored
                        big:      [log_big]
                        unknown:  [log_unknown]

                count:
                    task: stat_counter

                write_product:
                    task: csv_writer
                    options:
                        file_path: '%kernel.project_dir%/var/exports/products.csv'

                write_category:
                    task: csv_writer
                    options:
                        file_path: '%kernel.project_dir%/var/exports/categories.csv'

                log_big:
                    task: logger
                    options:
                        level: notice
                        message: 'Big product'

                log_unknown:
                    task: logger
                    options:
                        level: warning
                        message: 'Unknown line type'

How it works:

  • CsvReaderTask is iterable: each line goes through the router before the next one is read.
  • The RouterTask sends every line to its outputs (the StatCounterTask), and to the tasks of the branches it matches. With mode: all, the line P002 goes to write_product and log_big; with the default mode: first, only the first matching branch (in the order of conditions) would get it.
  • The comment branch has no task: the comments are dropped on purpose, without warning.
  • The line promo matches no branch: it goes to the default branch, log_unknown. Without a default branch, it would go to the error_outputs of the router, with a warning, mixed with the errors of the router (e.g. a missing key in an expression, with error_strategy: skip): a default branch keeps them apart.
  • cleverage:process:help app.import_catalog shows the branch leading to each task (write_product [branch route.product]), in the main branch.

Keep the FilterTask to drop the items that do not match (e.g. the lines without sku): a router is meant to dispatch them. The branches of a router cannot be joined back with an InputAggregatorTask, see conditional branches.