Dispatching Tasks & Workflows From A Workflow


# Dispatching a task

# Sequential processing

Within a workflow, a task can be dispatched from the handle or onEvent generator functions using:

module.exports.handle = function* (...) {
  ...
  const output = yield this.run.task("<task name>", ...<task input>);
  ...
};

When processing the workflow, this instruction means the workflow must stop and process the task <task name> with <task input> data. Once this task is completed by an Agent (and only then), the workflow resumes and set output with the return value of this task.

For example, with:

const a = yield this.run.task("taskA", ...inputA);
const b = yield this.run.task("taskB", ...inputB);            

taskB will be dispatched only after completion of taskA. After completion of taskB, a and b will contain the returned value of taskA and taskB respectively.

WARNING

Task input as well as returned value are serialized and deserialized using JSON.stringify and JSON.parse. So, those values should not contain any class, function, or recursive structure, that would be lost or would throw an error during this process.

# Background processing

If you omit the yieldkeyword:

module.exports.handle = function* (...input) {
  ...
  const job = this.run.task("<task name>", ...<task input>);
  ...
};

The same task will be dispatched, but the workflow will continue processing without waiting for the task completion. So job will be a simple object containing the generated id of the dispatched task:

{
  id: "<uuid>",
}

For example, with:

this.run.task("taskA", ...inputA);
const b = yield this.run.task("taskB", ...inputB);            

taskA and taskB will be dispatched at the same time, but the workflow will only wait for the completion of taskB before continuing.

# Parallel processing

In order to launch multiple tasks in parallel:

const [a, b, c] = yield this.run.task(
    ["taskA", ...inputA],
    ["taskB", ...inputB],
    ["taskC", ...inputC]
);
yield this.run.task("taskD", ...inputD);

Here, taskA, taskB and taskC will be processed in parallel. The workflow will wait for all tasks to complete before dispatching taskD. The output is the array of all returned values.

# Dispatching a child workflow

Within a workflow, a child workflow can be dispatched from the handle or onEvent generator functions using:

module.exports.handle = function* (...) {
  ...
  this.run.workflow("<workflow name>", ...<workflow input>);
  ...
};

The syntax above will dispatch a child workflow in the background: the workflow will continue without waiting for the completion of this child workflow.

WARNING

Currently, you can NOT use yield keyword before this.run.workflow() to indicate that child workflow should complete before continuing the parent workflow processing.

If you want to implement a sequential child workflow, look at this workaround.