Terminating Workflows From A Workflow


A workflow ends when all tasks previously launched are completed and there is nothing left to do according to the workflow's definition.

Once a workflow has completed, there is no way to "resurrect" it. Nevertheless, when we terminate a workflow, tasks that have already been dispatched will still be processed.

# Terminating other workflows

Use this.select function to target other workflows and terminate them from the handle or onEvent generator functions. For example:


module.exports.handle = function* (...) {
  ...
  this.select.workflow().withId("<id>").terminate();
  ...
};

# Terminating itself

The usual way to terminate a workflow is to reach the end of the handle function.

For example:


module.exports.handle = function* (...) {
  ...
  if (this.received) {
    ...
    // nothing more to do
    return;
  }
  // else workflow continues
  ...
};

You can also explicitly self-terminate:


module.exports.handle = function* (...) {
  ...
  if (this.received) {
    // nothing more to do
    yield this.terminate();
  }
  // else workflow continues
  ...
};

There is a difference between these two types of termination: when the handle function reaches its end, the workflow continues to run until all onEvent functions reach their end as well. This could continue for a while if the workflow receives a lot of events.

When using terminate, the workflow is immediately stopped.

WARNING

Do not forget the yield keyword! Without it, the terminate instruction is processed in the background and the workflow continues immediately to the next instructions before being terminated.

This method can be useful to terminate the workflow from the onEvent function or when the handle function delegates its behavior to another method:

module.exports = {
  *handle (...) {
    ...
    yield* this.other(...);
    ...
  },
  *other (...) {
    ...
    yield this.terminate();
  }
}

Returning inside will other function will not end the workflow! this.terminate(); will.