New in 0.16.2

v0.16.3

Workflow

Parallelization

When a workflow need to do different actions in parallel, it can:

  • dispatch tasks asynchronously
  • dispatch child-workflows
  • run multiple methods in parallel

Asynchronous tasks

Instead of running tasks sequentially, we may want to run some of them asynchronously, as illustrates below:

Asynchronous tasks

This is often the case for tasks for which the returned value is not important, like sending an email.

To dispatch task asynchronously, we also use the stub, created by the newService workflow function from a class interface:

When dispatching a task asynchronously, a Deferred<T> object is created (T being the return type of the task). We can use it to synchronously wait for the completion of taskA if needed:

Asynchronous tasks

Child workflows

If we want to run asynchrously more than a single task, we can use child-worlflows. Dispatching a child-workflow is as easy as dispatching a task. When the child-workflow completes, the return value is sent back to the parent workflow.

The illustration below illustrates this, with a child-workflow of 3 sequential tasks:

Asynchronous tasks

Similarly to tasks, we handle child-workflows through stubs created from their interface by the newWorkflow function.

For example, a distributed (and inefficient) way to calculate the factorial of n is exposed below, using n workflow instances, each of them - excepted the last one - dispatching a child-workflow.

Parallel methods

When we dispatch a child-workflow, we create a new workflow instance. But it is also possible to run multiple methods within the same workflow instance, as illustrated below:

Parallel methods

In this illustration,

  • the methodA was the method used at the workflow start
  • the methodB was dispatched from a client or another workflow but run inside the same instance than methodA.

The main raison to dispatch parallel methods instead of a child-workflow is related to the instance properties:

When multiple methods of the same workflow instance are running in parallel, they share the values of the workflow properties, and are able to read and update them to exchange information.

This can be very useful to get properties or trigger new actions on a running workflow, as described here.

To run a method on a running workflow, we can target

  • a workflow by id using the getWorkflowById function
  • all workflows having a specific tag, using the getWorkflowByTag function Those functions are available both on the Infinitic client and within workflows:

then

To dispatch another method on the same workflow, we can define a stub targeting the very same workflow with:

Previous
Sequential Tasks