DBOS Methods & Variables
DBOS Methods
DBOS.startWorkflow
static startWorkflow<Args extends unknown[], Return>(
target: (...args: Args) => Promise<Return>,
params?: StartWorkflowParams,
): (...args: Args) => Promise<WorkflowHandle<Return>>;
interface StartWorkflowParams {
workflowID?: string;
queueName?: string;
timeoutMS?: number | null;
enqueueOptions?: EnqueueOptions;
}
export interface EnqueueOptions {
deduplicationID?: string;
priority?: number;
queuePartitionKey?: string;
applicationVersion?: string;
}
Start a workflow in the background and return a handle to it.
Optionally enqueue it on a DBOS queue.
The DBOS.startWorkflow method resolves after the workflow is durably started; at this point the workflow is guaranteed to run to completion even if the app is interrupted.
Example syntax:
async function example(input: number) {
// Call steps
}
const exampleWorkflow = DBOS.registerWorkflow(example);
const input = 10;
const handle = await DBOS.startWorkflow(exampleWorkflow)(input);
Or if using decorators:
export class Example {
@DBOS.workflow()
static async exampleWorkflow(input: number) {
// Call steps
}
}
const input = 10;
const handle = await DBOS.startWorkflow(Example).exampleWorkflow(input);
Parameters:
- target: The workflow to start.
- workflowID: An ID to assign to the workflow. If not specified, a random UUID is generated.
- queueName: The name of the queue on which to enqueue this workflow, if any.
- timeoutMS: The timeout of this workflow in milliseconds.
- enqueueOptions:
- deduplicationID: At any given time, only one workflow with a specific deduplication ID can be enqueued in the specified queue. If a workflow with a deduplication ID is currently enqueued or actively executing (status
ENQUEUEDorPENDING), subsequent workflow enqueue attempt with the same deduplication ID in the same queue will raise aDBOSQueueDuplicatedErrorexception. - priority: The priority of the enqueued workflow in the specified queue. Workflows with the same priority are dequeued in FIFO (first in, first out) order. Priority values can range from
1to2,147,483,647, where a low number indicates a higher priority. Workflows without assigned priorities have the highest priority and are dequeued before workflows with assigned priorities. - queuePartitionKey: The queue partition in which to enqueue this workflow. Use if and only if the queue is partitioned (
partitionQueue: true). In partitioned queues, all flow control (including concurrency and rate limits) is applied to individual partitions instead of the queue as a whole. - applicationVersion: The application version of the workflow to enqueue. The workflow may only be dequeued by processes running that version. Defaults to the current application version.
- deduplicationID: At any given time, only one workflow with a specific deduplication ID can be enqueued in the specified queue. If a workflow with a deduplication ID is currently enqueued or actively executing (status
DBOS.waitFirst
static async waitFirst(
handles: WorkflowHandle<unknown>[]
): Promise<WorkflowHandle<unknown>>
Wait for any one of the given workflow handles to complete and return the first completed handle. This is useful when you have multiple concurrent workflows and want to process results as they complete.
Parameters:
- handles: A non-empty array of workflow handles to wait on. Throws an error if the array is empty.
See the queue tutorial for an example.
DBOS.send
DBOS.send<T>(
destinationID: string,
message: T,
topic?: string,
idempotencyKey?: string,
options?: SendOptions
): Promise<void>
Send a message to the workflow identified by destinationID.
Messages can optionally be associated with a topic.
Parameters:
- destinationID: The workflow to which to send the message.
- message: The message to send. Must be serializable.
- topic: A topic with which to associate the message. Messages are enqueued per-topic on the receiver.
- idempotencyKey: If an idempotency key is set, the message will only be sent once no matter how many times
DBOS.sendis called with this key. - options.serializationType: The serialization format to use for this message.
DBOS.recv
recv<T>(
topic?: string,
timeoutSeconds?: number // Default: 60 seconds
): Promise<T | null>
Receive and return a message sent to this workflow.
Can only be called from within a workflow.
Messages are dequeued first-in, first-out from a queue associated with the topic.
Calls to recv wait for the next message in the queue, returning null if the wait times out. If timeoutSeconds is not specified, a 60-second timeout is used.
If no topic is specified, recv can only access messages sent without a topic.
Parameters:
- topic: A topic queue on which to wait.
- timeoutSeconds: A timeout in seconds. If the wait times out, return
null.
Returns:
- The first message enqueued on the input topic, or
nullif the wait times out.
DBOS.setEvent
DBOS.setEvent<T>(
key: string,
value: T,
options?: SetEventOptions
): Promise<void>
Create and associate with this workflow an event with key key and value value.
If the event already exists, update its value.
Can only be called from within a workflow.
Parameters:
- key: The key of the event.
- value: The value of the event. Must be serializable.
- options.serializationType: The serialization format to use for this event.
DBOS.getEvent
DBOS.getEvent<T>(
workflowID: string,
key: string,
timeoutSeconds?: number // Default: 60 seconds
): Promise<T | null>
Retrieve the latest value of an event published by the workflow identified by workflowID to the key key.
If the event does not yet exist, wait for it to be published, returning null if the wait times out. If timeoutSeconds is not specified, a 60-second timeout is used.
Parameters:
- workflowID: The identifier of the workflow whose events to retrieve.
- key: The key of the event to retrieve.
- timeoutSeconds: A timeout in seconds. If the wait times out, return
null.