world.queue

Enqueue workflow runs and create queue handlers for background processing.

The world.queue interface provides access to workflow run queuing and processing. Use it to enqueue runs for background execution and create handlers to process queued items.

Import

import { getWorld } from "workflow/runtime";

const world = getWorld();
const queue = world.queue; 

Methods

getDeploymentId()

Get the current deployment ID. Useful for routing queue messages to the correct deployment.

const deploymentId = await world.queue.getDeploymentId(); 

Returns: string — The current deployment ID

queue()

Enqueue a workflow run for background processing.

const messageId = await world.queue.queue(name, message, opts); 

Parameters:

ParameterTypeDescription
nameValidQueueNameThe queue name
messageobjectThe message payload to enqueue
optsobjectOptional configuration

Returns: MessageId

createQueueHandler()

Create a handler function for processing queued messages.

const handler = world.queue.createQueueHandler(prefix, callback); 

Parameters:

ParameterTypeDescription
prefixstringQueue name prefix to match
callbackfunctionHandler function called for each queued message

Returns: Queue handler function

Examples

Enqueue a Workflow Run for Background Processing

import { getWorld } from "workflow/runtime";

export async function POST(req: Request) {
  const { workflowName, input } = await req.json();

  const world = getWorld();
  const messageId = await world.queue.queue(workflowName, { 
    input,
    priority: "normal",
  }); 

  return Response.json({ messageId });
}

Create a Queue Handler for Processing

import { getWorld } from "workflow/runtime";

const world = getWorld();

const handler = world.queue.createQueueHandler("my-workflows", async (message) => { 
  console.log("Processing:", message);
  // Handle the queued workflow message
}); 

Get Current Deployment ID

import { getWorld } from "workflow/runtime";

const world = getWorld();
const deploymentId = await world.queue.getDeploymentId(); 
console.log("Running on deployment:", deploymentId);