Designing Workflows
The process automation component gives control over which actions to perform and when. While this can significantly improve performance it does put more emphasis on the design of the workflows.
Prioritize events
While not always applicable, events are the most granular change drivers. They result in the least latency and cause very little overhead. This is why these should always be preferred over other triggers where possible.
The workflow below shows how events can be used to manage a lot of common use-cases. This state-machine depicts a
dataElement which may only be processed once some validation passes. The validation will first be triggered OnCreate.
If validation succeeds it will become 'Ready', otherwise it will revert to 'Created'. Validate is then only re-run if
the element is modified, causing the OnModify trigger to fire. The OnTransition trigger ensures Process is started
as soon as the element is transitioned to 'Ready'.
Note how this workflow doesn't require any timers to operate, but will still catch all moments of change instantly.
Scheduled task execution
Events won't cover all possible use-cases, especially with time as a change driver. Some tasks may need to run periodically to act on external changes, or some elements may simply need to wait for a certain timestamp before progressing. For these type of tasks we introduced the Schedule trigger. It is specific to a single transition and utilizes a cron-like syntax to define when it should run.
For more information on how to configure these see the triggers page.
Job attempts
Not all business processes are stable. An external service may be unreachable, or your server may be overloaded. To handle these transient errors process-automation will automatically retry failed jobs.
Retries give the failed state a special property: an element in the failed state can still be picked up by a later attempt. When using the failed state as a business error, these retries may be unexpected. Business errors should be explicitly modeled as a (successful) task outcome.
The job.attempts option can be used to configure how many times a job is attempted. Configuring the option as 1 disables retries altogether.
Job priority
With the workflow model, a priority can be configured using the job.priority option. This can be applied to both
workflows and transitions. The default priority for all workflows is 'normal', other options are 'low', 'medium', 'high' and 'critical'.
Assigning a priority to the workflow itself will change its priority over other workflows. This can be useful if some tasks are more important to execute quickly than others. Priority can also be used to create a more 'greedy' execution order, by giving the first transition a lower priority than the rest of the transitions.
Transition recovery
It can happen a transition didn't complete correctly, which means it is stuck in its interim state. To remedy this the process automation component will perform a recovery on elements stuck in an interim state. If the transition defines a recovery state it will move the element to this state. Otherwise, it will move to the failed state.
Recovery is performed by the WorkerNodes and runs:
- at startup, when a WorkerNode starts without other active WorkerNodes to join (for example after a restart of the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution.);
- when the leading WorkerNode is lost, detected through the heartbeat of the remaining WorkerNodes;
- every day at a fixed time, when a
DailyRecoveryPolicyis configured.
Recovery is separate from retries. A retry handles a task that failed, while recovery handles a transition that never finished.
Recovery state
The recovery state can explicitly be configured on the Transition.
<workflow>
<name>MyElementFlow</name>
<transitions>
<transition>
<name>Make</name>
<!-- other fields omitted for brevity -->
<recoveryState>mycomp::MyElementFlow::MakeFailed</recoveryState>
</transition>
</transitions>
</workflow>
It is possible to use the begin state as the recovery state with one restriction. The executed task must be transactional. In order for recovery to the begin state to be valid, the task must not have performed any side effects. Having a transaction around a task at least ensures there were no database modifications as a result of a failed task.
You still need to make sure no other side effects were performed within the task. For example when making a request to an external api, a compensating request may be needed in order to cancel the initial request.
Providing task context
There are several ways to adjust the context of any running transition/task. Process automation will provide a
net.democritus.processautomation.context.WorkflowProcessingContext for any transition that runs. This can be used to
make the distinction between parts of the code executed by a workflow or through any other cause.
Additionally, there are two options on transitions to modify the context:
TransitionSelect the implementation of the task that should be used. Will be added to the BasicProcessingContext.
If no implementation is specified, the default implementation will be used.
<options>
<executeTask.implementation>secure</executeTask.implementation>
</options>
Transition parameters
A transition normally receives only its target element. When a trigger needs to hand the task something extra, the transition can declare the type of that extra value.
The value itself is supplied per invocation, not in the model. A trigger passes it when submitting the transition, and the task reads it back from the context:
Optional<MyParameters> params = ValueParameterContext.retrieve(context, MyParameters.class);
See receiving transition parameters for the task side, and custom triggers for supplying the value.
The class must be serializable, since the value travels with the job on the queue.
This replaces the target.class.custom option on TaskElement. The MigrateCustomTarget transmutation moves existing
models over.
Long-running transitions
The goal of transitions is to be small and apply incremental change. Some business requirements lead you to do a lot of
work in a single transaction. This is generally discouraged, so try to implement a better approach, but this can be the only
viable solution in some scenarios. In case a transition specifically should run longer than the default timeout, it can
be overridden using the transition.timeout.seconds option.