Skip to main content

Task Queues in Node

What is a Task Queue?#

From a high level, we can say that a Task Queue is exactly what the name suggests. It is a "first-in, first-out" queue for Tasks, where a Task is the context needed to execute a chunk of code that alters the "state" of a Workflow.

Task Queues are maintained by the Temporal Server. The Server places Tasks into a Task Queue to schedule, start, cancel, and complete parts of a Workflow and/or Activity, for example. A Worker long polls the Task Queue, hungrily waiting for a Task to become available. The Worker then executes whatever the Task tells the Worker to do.

How to use Task Queues#

In Node, a Task Queue is represented in code by name, as a string. There are 3 places where the name of the Task Queue is supplied by the developer.

  1. When starting a Workflow, you must pass the taskQueue option to the WorkflowClient's createWorkflowHandle() method.
const workflow = workflowClient.createWorkflowHandle(myWorkflow, {  taskQueue: 'my-task-queue',});
const result = await workflow.execute();
  1. When creating a Worker, you must pass the taskQueue option to the Worker.create() function.
const worker = await Worker.create({  workDir: __dirname,  taskQueue: 'my-task-queue',});
  1. Optionally, in Workflow code, when calling an Activity, you can specify the task queue by passing the taskQueue option to createActivityHandle(). If you do not specify a taskQueue, then the Node SDK places Activity Tasks in the same Task Queue as the Workflow Task Queue.
const { greet } = createActivityHandle<typeof activities>({  taskQueue: 'my-other-task-queue',  startToCloseTimeout: '1s',});