A transformer should behave like a pure function:
- the same input (and options) always gives the same output
- it has no side effect: no database write, no file, no API call, no state kept between two calls
This makes transformers predictable, reusable in any process, and trivial to unit test. Anything with a side effect (reading, writing, calling a remote service) belongs in a task. If a transformation is expensive and deterministic, wrap it in the CachedTransformer instead of adding a cache in your own transformer.
Prefer composing existing transformers (mapping, callback, cast, implode, ...) and, when the same chain is used
in several places, declare it once as a generic transformer.
Task services should be shared: false: a task often keeps state in its properties (an
open file, a buffer, a counter...). A shared service would share this state between two tasks using the same service
in a process (rejected when the container is compiled), or between two executions of a process in the same PHP
process (sub-processes, workers, tests). See service declaration.
In a long-running worker (Messenger, UI bundle), the resolved process configurations are released between two messages
(ProcessConfigurationRegistry::reset(), tagged kernel.reset), with their task instances and states (inputs,
outputs, aggregated data): a task must not rely on data kept from a previous message.
A process with dozens of tasks is hard to read, to debug and to test. Split it into small processes that do one thing (read a file, import one item, export a batch...) and chain them with the ProcessExecutorTask (see subprocesses):
- each sub-process can be executed and tested on its own, with an
entry_pointfor its input and anend_pointfor its output - sub-processes that should not be launched directly can be marked
public: false - the same small process can be reused by several parent processes, or parallelized with the ProcessLauncherTask
Use description and help on processes and tasks: they are displayed by cleverage:process:list and
cleverage:process:help.
Process codes are global to the application, and so are transformer codes. Prefix them with your project or domain
(e.g. app.catalog.import_products, app_vat) to avoid conflicts with processes and transformers provided by
bundles. Organizing configuration files like the codes (e.g. config/packages/process/app/catalog/import_products.yaml)
makes them easy to find.
- Keep the global
default_error_strategy: stop(the default), so any unexpected error stops the process instead of silently dropping data. - Set
error_strategy: skiponly on the tasks where an item can fail without compromising the others (e.g. the transformation of one line of a file), and tune theirlog_level. - Plug an error branch (
error_outputs) on those tasks to keep track of rejected items (write them to a file, log them...), rather than losing them.
See errors and skips.
- Stream data with iterable tasks (CSV/JSON stream/line readers, Doctrine iterators...) instead of loading whole collections: only one item at a time goes through the process.
- Use blocking tasks (aggregators) only when you really need the whole data set, and store only what you need in them. Prefer accumulators that write as they receive data (like the CsvWriterTask).
- When you need batches (e.g. for bulk database writes), use the SimpleBatchTask rather than an aggregator.
- Measure: see the memory usage cookbook, and always check memory in the
prodenvironment (thedevenvironment keeps logs and debug data in memory).
Nothing prevents two executions of the same process from running at the same time: two cleverage:process:execute
commands (a cron entry whose previous run is not over), or executions launched from a user interface, a scheduler or
several Messenger workers. Both executions then read the same data and write to the same places: a file written to a
fixed path (csv_writer without {unique_token}, file_mover) is overwritten or truncated by the other execution, and
the database writes of both executions mix.
When a process must not run twice at the same time:
- make its outputs unique (
{unique_token}in the file paths:{date_time}has a resolution of one second, two executions started in the same second get the same path) and its writes idempotent; - or make it exclusive (
exclusive: true, with symfony/lock): another execution fails while it is running.
When a task has several outputs (or selected branches), each output
is fully processed one after the other, in the configured order, but this is an implementation detail. If a task
needs the result of another branch, make the dependency explicit in the graph (e.g. with a blocking task or an
InputAggregatorTask) rather than relying on the order in which branches are
executed.
Values that change between executions (a file name, a date, a limit...) should not be hard-coded in the process
configuration: pass them as contextual values (-c key:value) and use
{{ key }} placeholders in the task options. The context may come from untrusted users and is logged: keep it out of
the options interpreted by the tasks (shell command, SQL...) and do not put secrets in it (see
contextual values).
The classes meant to be extended are the abstract ones (AbstractConfigurableTask, AbstractIterableOutputTask,
AbstractCsvTask, Logger\AbstractLogger...), the interfaces and traits, the exceptions, and a few file tasks used as
a base by other tasks (FileReaderTask, LineReaderTask, FolderBrowserTask, CsvReaderTask, InputCsvReaderTask
and Filesystem\CsvResource).
The other classes are final: the engine (ProcessManager, the registries, ContextualOptionResolver), the models
(ProcessState, the configurations, the events) and the concrete tasks and transformers. Instead of extending them:
- write your own task (extending
AbstractConfigurableTask) or transformer, and inject the services of the bundle you need; - decorate a service having an
interface (
ProcessManagerInterface, the registries; notContextualOptionResolver), or listen to the process events; - chain existing tasks and transformers in the process configuration.
The classes marked @internal (the dependency injection compiler passes, the commands, the event listeners, the Monolog
processors, ProcessLogger, TaskLogger and PhpFunctionProvider) are not part of the public API: they can change in
any version.