title: Enqueue Workflows from External Applications impact: HIGH impactDescription: Enables external services to submit work to DBOS queues tags: client, enqueue, external, queue
Enqueue Workflows from External Applications
Use dbos.Enqueue with a client to submit workflows from outside your DBOS application. Since the Client runs externally, workflow and queue metadata must be specified explicitly by name, and the result type is given as the type parameter.
Incorrect (trying to use RunWorkflow from external code):
// RunWorkflow requires a full DBOS context with registered workflows
dbos.RunWorkflow(ctx, processTask, "data", dbos.WithQueue(queue))
Correct (using Enqueue with a client):
client, err := dbos.NewClient(context.Background(), dbos.ClientConfig{
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
})
if err != nil {
log.Fatal(err)
}
defer dbos.Shutdown(client, 10*time.Second)
// Basic enqueue - specify workflow and queue by name, result type as type parameter
handle, err := dbos.Enqueue[string](client, "task_queue", "processTask", "task-data")
if err != nil {
log.Fatal(err)
}
// Wait for the result
result, err := handle.GetResult()
Enqueue with options:
handle, err := dbos.Enqueue[string](client, "task_queue", "processTask", "task-data",
dbos.WithEnqueueWorkflowID("custom-id"),
dbos.WithEnqueueDeduplicationID("unique-id"),
dbos.WithEnqueuePriority(10),
dbos.WithEnqueueTimeout(5*time.Minute),
dbos.WithEnqueueQueuePartitionKey("user-123"),
dbos.WithEnqueueApplicationVersion("2.0.0"),
)
Enqueue options:
WithEnqueueWorkflowID: Custom workflow IDWithEnqueueWorkflowIDReusePolicy: What to do if that workflow ID already exists —WorkflowIDReusePolicyReturnExisting(default, returns a handle to the existing workflow) orWorkflowIDReusePolicyReject(returns an error matchingdbos.ErrWorkflowIDInUsewithout enqueueing anything). See pattern-idempotency.mdWithEnqueueDeduplicationID: Prevent duplicate enqueuesWithEnqueueDeduplicationPolicy: How a colliding deduplication ID is handled —DeduplicationPolicyReject(default, returns aQueueDeduplicatederror) orDeduplicationPolicyReturnExisting(returns a handle to the existing workflow)WithEnqueuePriority: Queue priority (lower = higher priority)WithEnqueueTimeout: Workflow timeoutWithEnqueueQueuePartitionKey: Partition key for partitioned queuesWithEnqueueApplicationVersion: Override application version. When unset, a client leaves the version unset and the workflow is dequeued at the owning application’s latest registered versionWithEnqueueApplicationName: The application that owns, dequeues, and runs the enqueued workflow (defaults to the enqueueing handle’s own application). See advanced-shared-database.mdWithEnqueueDelay: Delay execution; the workflow stays inDELAYEDstatus until the delay expiresWithEnqueueClassName: Class/namespace name, required when enqueueing to Python, TypeScript, or Java targetsWithEnqueueConfigName: Config/instance name, required when the target workflow is registered on a configured instance (Go’sWithInstance, Python/TypeScript/Java class instances)WithEnqueueAuthenticatedUser,WithEnqueueAssumedRole,WithEnqueueAuthenticatedRoles: Attach authentication metadata (the workflow reads the user back withdbos.GetAuthenticatedUser(ctx))WithEnqueueTransaction(tx): Enqueue on a transaction you own (apgx.Tx,*sql.Tx, ordbos.Txon the DBOS system database), so the enqueue commits atomically with your own writes
The workflow name must match the registered name or custom name set with WithWorkflowName during registration.
Enqueue atomically with your own writes:
tx, err := pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
tx.Exec(ctx, "INSERT INTO orders (id) VALUES ($1)", orderID)
handle, err := dbos.Enqueue[string](client, "task_queue", "processOrder", orderID,
dbos.WithEnqueueTransaction(tx))
if err != nil {
return err // the transaction is aborted: roll back, do not retry on it
}
if err := tx.Commit(ctx); err != nil {
return err // until this commits, the workflow does not exist
}
result, err := handle.GetResult() // only after the commit
You own the transaction: Enqueue neither commits nor rolls back, and the handle does not resolve until you commit. Not available inside a workflow (where an enqueue is checkpointed as a step) nor with DeduplicationPolicyReturnExisting.
Always call dbos.Shutdown(client, timeout) when done.
Reference: DBOS Client Enqueue