Skip to main content

@flow-state-dev/bullmq

Interfaces​

BullmqConnectionOptions​

Extended by​

Properties​

connection​
connection: string | RedisOptions;

ioredis connection (URL string or options object).

prefix?​
prefix?: string;

BullMQ key prefix for multi-tenant namespacing. Default "fsd".


BullmqJobQueueAdapter​

The adapter handed to createFlowState({ jobQueue }), with the underlying queue/runtime exposed for admin consoles (Bull Board) and direct enqueueAction use.

Extends​

  • JobQueueAdapter

Properties​

leaseBackend?​
readonly leaseBackend?: ConcurrencyLeaseBackend;

An ordered-lease backend shared by every process of the deployment, for the engine's concurrency arbiter to keep its lines in.

Supplied by an adapter whose runs land in several processes, so a session's queue / reject / hold / defer policy holds across all of them. The arbiter keeps the policy; the backend only orders places on a key. With one, a delivery into an existing session is accepted and carries its place on the job (DispatchEnvelope.leasePlace), which the adapter's processing side waits on and gives back.

Absent → each process arbitrates only its own in-process runs, work handed to the queue is not arbitrated, and a delivery into an existing session is refused external-flow-runner.

Inherited from​

JobQueueAdapter.leaseBackend

mode​
readonly mode: JobQueueMode;

Which sides this process runs. Default "colocated".

Overrides​

JobQueueAdapter.mode

queue​
readonly queue: Queue;

Underlying BullMQ queue — e.g. for Bull Board.

runtime​
readonly runtime: BullmqRuntime;

The composed runtime handle (enqueueAction, createJobProcessor, close).

Methods​

close()?​
close?(): Promise<void>;

Release backend resources (queues, connections). Called by dispose().

Returns​

Promise<void>

Inherited from​

JobQueueAdapter.close

createRunner()​
createRunner(runtime): FlowRunner;

Build the enqueue side (enqueue + live-event bridge). Installed as the router's FlowRunner unless mode is "process-only".

Parameters​
runtime​

FlowStateRuntime

Returns​

FlowRunner

Inherited from​

JobQueueAdapter.createRunner

startProcessing()​
startProcessing(runtime): ProcessingHandle;

Start the processing side against the resolved runtime. Called during runtime init unless mode is "enqueue-only". The returned handle is closed by FlowState.dispose() before store adapters are disposed, so in-flight jobs drain against open stores.

Parameters​
runtime​

FlowStateRuntime

Returns​

ProcessingHandle

Inherited from​

JobQueueAdapter.startProcessing


BullmqJobQueueOptions​

Extends​

Properties​

channelPrefix?​
channelPrefix?: string;

Redis pub/sub channel prefix for the stream bridge. Default "fsd:stream".

concurrency?​
concurrency?: number;

Concurrent jobs per processing process. Default 2.

connection​
connection: string | RedisOptions;

ioredis connection (URL string or options object).

Inherited from​

BullmqConnectionOptions.connection

leaseMs?​
leaseMs?: number;

How long a concurrency place lives past its last renewal (ms). A process that stops renewing (it died, or lost Redis) frees the session's key within this; a running holder that cannot renew is stopped at half of it. Default 10000.

lockDuration?​
lockDuration?: number;

Job lock duration (ms). Default 300000 — LLM calls are slow.

mode?​
mode?: JobQueueMode;

Which sides this process runs: "colocated" (default — enqueue and process in one process), "enqueue-only" (web process; a separate processing container consumes the queue), or "process-only" (dedicated processing process; call flowstate.ready() to start consuming).

prefix?​
prefix?: string;

BullMQ key prefix for multi-tenant namespacing. Default "fsd".

Inherited from​

BullmqConnectionOptions.prefix

queueName?​
queueName?: string;

Queue name. Default "fsd-flows".

retry?​
retry?: RetryConfig;

Retry/backoff/DLQ config applied to enqueued jobs and the job processor.


BullmqRuntime​

Handle returned by createBullmqRuntime.

Properties​

queue​
readonly queue: Queue;

The underlying BullMQ Queue instance (useful for monitoring tools like Bull Board).

Methods​

close()​
close(): Promise<void>;

Close all connections, queues and job processors. Wire to SIGTERM/SIGINT.

Returns​

Promise<void>

createJobProcessor()​
createJobProcessor(deps): Worker;

Construct a Worker that processes flow-run jobs by calling runAction.

Parameters​
deps​

JobProcessorDeps

Returns​

Worker

enqueueAction()​
enqueueAction(data, opts?): Promise<string>;

Enqueue a flow action for durable, off-request execution. Returns the BullMQ job id.

Parameters​
data​

FlowJobData

opts?​

EnqueueOptions

Returns​

Promise<string>


CreateBullmqRuntimeOptions​

Extends​

Properties​

connection​
connection: string | RedisOptions;

ioredis connection (URL string or options object).

Inherited from​

BullmqConnectionOptions.connection

prefix?​
prefix?: string;

BullMQ key prefix for multi-tenant namespacing. Default "fsd".

Inherited from​

BullmqConnectionOptions.prefix

queueName?​
queueName?: string;
retry?​
retry?: RetryConfig;

CreateJobQueueRunnerOptions​

Properties​

bridge​
bridge: StreamBridge;
queue​
queue: Queue;
retryConfig?​
retryConfig?: RetryConfig;

CreateRedisLeaseBackendOptions​

Extends​

Properties​

connection​
connection: string | RedisOptions;

ioredis connection (URL string or options object).

Inherited from​

BullmqConnectionOptions.connection

leaseMs?​
leaseMs?: number;

How long a place lives past its last renewal. Default DEFAULT_LEASE_MS.

prefix?​
prefix?: string;

BullMQ key prefix for multi-tenant namespacing. Default "fsd".

Inherited from​

BullmqConnectionOptions.prefix

queue​
queue: Queue;

The queue whose jobs hold the places, read to reconcile an expired place.


CreateRedisStreamBridgeOptions​

Properties​

channelPrefix?​
channelPrefix?: string;

Redis channel prefix. Default "fsd:stream".

connection​
connection: string | RedisOptions;

ioredis connection (URL string or options object).


EnqueueOptions​

Properties​

delay?​
delay?: number;

Delay before processing (ms).

jobId?​
jobId?: string;

Custom BullMQ jobId for deduplication.

priority?​
priority?: number;

BullMQ job priority (lower = higher priority).

retry?​
retry?: RetryConfig;

Override default retry config for this job.


FlowJobData​

Properties​

actionName​
actionName: string;
attachments?​
attachments?: FileContent[];

The person's files, as references the action route checked and claimed. == null → none, as on an older job.

flowKind​
flowKind: string;
input​
input: unknown;
leasePlace?​
leasePlace?: LeasePlace | null;

The run's place on its concurrency key, taken by the dispatching process before the enqueue. The worker runs the job only in the place's turn and gives it back when the job is done for good. Absent on a job with no arbitrated key, and on a job from a release before places existed; both run as they always did.

leaseTurn?​
leaseTurn?: JobLeaseTurn | null;

How the job reaches its turn when not by waiting in line for leasePlace (queue, reject); see JobLeaseTurn.

== null → leasePlace waits its turn with the queue budget.

leaseWait?​
leaseWait?:
| {
attempt: number;
firstCheckAt: number;
}
| null;

The job's wait for its turn so far, carried across requeues: when it first checked (the wait budget counts from there, not from the enqueue) and how many checks it has made (the backoff grows with them).

metadata?​
metadata?: Record<string, unknown>;
orgId?​
orgId?: string;
requestId?​
requestId?: string;
sessionId?​
sessionId?: string;
source?​
source?: string;
tenantId?​
tenantId?: string;
userId​
userId: string;

JobLeaseBackend​

A lease backend a queue worker can re-take a place on for a job that already exists. Any ConcurrencyLeaseBackend fits; one that ignores jobId loses only the reconciliation of that place.

Extends​

  • ConcurrencyLeaseBackend

Extended by​

Properties​

leaseMs?​
readonly leaseMs?: number;

How long a place lives past its last renewal, for a backend whose places expire. With it, a running holder that cannot renew is stopped at half the lease, so it has ended before another process can take the key. Without it, only a renewal that answers false stops a holder.

Inherited from​

ConcurrencyLeaseBackend.leaseMs

Methods​

giveBack()​
giveBack(place): Promise<void>;

Remove the place. Idempotent. Wakes the next waiter on the key.

Parameters​
place​

LeasePlace

Returns​

Promise<void>

Inherited from​

ConcurrencyLeaseBackend.giveBack

isMyTurn()​
isMyTurn(place): Promise<boolean | "missing">;

True when this place is the first live place on its key. "missing" when the key's line no longer has the place at all: a backend whose places expire dropped it. The request behind it did not go away, so the engine takes a new place for it, at the back of the line. A backend that answers only true / false never re-admits a waiter.

Parameters​
place​

LeasePlace

Returns​

Promise<boolean | "missing">

Inherited from​

ConcurrencyLeaseBackend.isMyTurn

renew()​
renew(place): Promise<boolean | void>;

Extend the place's lease, for a backend whose places expire. The engine's arbiter calls it every 2 seconds (more often under a short leaseMs) for as long as its process holds the place: from take, through the dispatch's writes, its wait for a turn and its run, until it gives the place back or hands it to a job (whose worker then renews it). A lease a few times longer than 2 seconds survives one missed renewal.

Answers false when the place is gone, so it can no longer be kept; a running holder is then stopped. Any other answer, undefined included, means the place was kept.

Parameters​
place​

LeasePlace

Returns​

Promise<boolean | void>

Inherited from​

ConcurrencyLeaseBackend.renew

take()​
take(input): Promise<LeaseTakeResult>;

Append a place to the key's line, or with ifEmpty claim only a free key. Returns the current holder's request id instead when ifEmpty finds the key held.

Not idempotent: each call appends a new place, even for a request id already on the line. The engine takes once per dispatch.

Parameters​
input​

JobLeaseTakeInput

Returns​

Promise<LeaseTakeResult>

Overrides​

ConcurrencyLeaseBackend.take


RedisLeaseBackend​

The Redis backend, with its connection to close.

Extends​

Properties​

leaseMs​
readonly leaseMs: number;

How long a place lives past its last renewal, for a backend whose places expire. With it, a running holder that cannot renew is stopped at half the lease, so it has ended before another process can take the key. Without it, only a renewal that answers false stops a holder.

Overrides​

JobLeaseBackend.leaseMs

Methods​

close()​
close(): Promise<void>;

Close this backend's Redis connection.

Returns​

Promise<void>

giveBack()​
giveBack(place): Promise<void>;

Remove the place. Idempotent. Wakes the next waiter on the key.

Parameters​
place​

LeasePlace

Returns​

Promise<void>

Inherited from​

JobLeaseBackend.giveBack

isMyTurn()​
isMyTurn(place): Promise<boolean | "missing">;

True when this place is the first live place on its key. "missing" when the key's line no longer has the place at all: a backend whose places expire dropped it. The request behind it did not go away, so the engine takes a new place for it, at the back of the line. A backend that answers only true / false never re-admits a waiter.

Parameters​
place​

LeasePlace

Returns​

Promise<boolean | "missing">

Inherited from​

JobLeaseBackend.isMyTurn

renew()​
renew(place): Promise<boolean | void>;

Extend the place's lease, for a backend whose places expire. The engine's arbiter calls it every 2 seconds (more often under a short leaseMs) for as long as its process holds the place: from take, through the dispatch's writes, its wait for a turn and its run, until it gives the place back or hands it to a job (whose worker then renews it). A lease a few times longer than 2 seconds survives one missed renewal.

Answers false when the place is gone, so it can no longer be kept; a running holder is then stopped. Any other answer, undefined included, means the place was kept.

Parameters​
place​

LeasePlace

Returns​

Promise<boolean | void>

Inherited from​

JobLeaseBackend.renew

take()​
take(input): Promise<LeaseTakeResult>;

Append a place to the key's line, or with ifEmpty claim only a free key. Returns the current holder's request id instead when ifEmpty finds the key held.

Not idempotent: each call appends a new place, even for a request id already on the line. The engine takes once per dispatch.

Parameters​
input​

JobLeaseTakeInput

Returns​

Promise<LeaseTakeResult>

Inherited from​

JobLeaseBackend.take


ResolvedConnection​

Properties​

connection​
connection: RedisOptions;
prefix​
prefix: string;

RetryConfig​

Properties​

attempts?​
attempts?: number;

Max attempts (including initial). Default 3.

backoff?​
backoff?: object;

Backoff strategy. Default exponential 1000ms jitter 0.5.

delay​
delay: number;
jitter?​
jitter?: number;
type​
type: "exponential" | "fixed";
deadLetter?​
deadLetter?:
| boolean
| {
queueName: string;
};

Dead-letter queue. Default false; true maps to "<queue>-dlq".

removeOnComplete?​
removeOnComplete?:
| boolean
| {
age?: number;
count?: number;
};

Completed job cleanup. Default { age: 3600, count: 1000 }.

removeOnFail?​
removeOnFail?:
| boolean
| {
age?: number;
count?: number;
};

Failed job cleanup. Default { age: 86400 }.

Type Aliases​

JobLeaseTakeInput​

type JobLeaseTakeInput = LeaseTakeInput & object;

What a worker may add to a take: the job that already exists for the request.

Type Declaration​

jobId?​
jobId?: string;

The job the place is for. Default: leaseJobId of the new place.


JobLeaseTurn​

type JobLeaseTurn =
| LeaseTurn
| {
kind: "claimed";
}
| {
kind: "behind";
};

How a job reaches its turn, as the dispatching host set it (DispatchEnvelope.leaseTurn) or as the worker moved it on since:

  • { kind: "now" } — a hold run: runs at once under leasePlace, even after a lost place is taken again at the back.
  • { kind: "when-free", key } — a defer run that has not claimed key yet. It claims it once nothing holds or waits on it.
  • { kind: "claimed" } — a defer run that claimed its free key and holds leasePlace. It runs at once while it keeps that place; one that lost it goes back to when-free.
  • { kind: "behind" } — a defer run that ran out of patience and lined up at the back: it waits for leasePlace's turn with no time budget.

Variables​

DEFAULT_LEASE_MS​

const DEFAULT_LEASE_MS: 10000 = 10000;

How long a place lives past its last renewal, by default. Holders renew every 2 seconds and a waiter's requeue delay is capped at 2 seconds, so ten seconds survives a few missed renewals, and a holder that cannot renew is stopped at five.

Functions​

bullmqJobQueue()​

function bullmqJobQueue(options): BullmqJobQueueAdapter;

Build a BullMQ execution-backend adapter for createFlowState.

createFlowState hands both sides the same resolved { registry, stores, runtimeConfig } the router uses, so processed jobs' writes are always visible to streaming, refresh, and the devtool — there is no way to wire a mismatched store registry through this path.

Parameters​

options​

BullmqJobQueueOptions

Returns​

BullmqJobQueueAdapter


createBullmqRuntime()​

function createBullmqRuntime(options): BullmqRuntime;

Creates the BullMQ runtime: a queue for enqueuing flow-run jobs, a factory for constructing job processors, and a close hook for graceful shutdown.

Parameters​

options​

CreateBullmqRuntimeOptions

Returns​

BullmqRuntime


createJobQueueRunner()​

function createJobQueueRunner(options): FlowRunner;

Creates a FlowRunner that enqueues flow-run jobs to a BullMQ queue and bridges live events back to the caller via a StreamBridge.

Parameters​

options​

CreateJobQueueRunnerOptions

Returns​

FlowRunner


createRedisLeaseBackend()​

function createRedisLeaseBackend(options): RedisLeaseBackend;

Build the Redis lease backend over the deployment's connection, reading queue to reconcile an expired place.

Parameters​

options​

CreateRedisLeaseBackendOptions

Returns​

RedisLeaseBackend


createRedisStreamBridge()​

function createRedisStreamBridge(options): StreamBridge;

Creates a Redis pub/sub StreamBridge. Each requestId gets its own channel pair: one for events and one for abort signals.

Parameters​

options​

CreateRedisStreamBridgeOptions

Returns​

StreamBridge


leaseJobId()​

function leaseJobId(place): string;

The BullMQ job id a place's job is enqueued under, unless it names another.

Parameters​

place​

LeasePlace

Returns​

string


resolveDlqName()​

function resolveDlqName(baseQueueName, config?): string | null;

Parameters​

baseQueueName​

string

config?​

RetryConfig

Returns​

string | null


resolveProcessorConnection()​

function resolveProcessorConnection(opts): ResolvedConnection;

Parameters​

opts​

BullmqConnectionOptions

Returns​

ResolvedConnection


resolveProducerConnection()​

function resolveProducerConnection(opts): ResolvedConnection;

Parameters​

opts​

BullmqConnectionOptions

Returns​

ResolvedConnection


toJobOptions()​

function toJobOptions(config?): Partial<JobsOptions>;

Parameters​

config?​

RetryConfig

Returns​

Partial<JobsOptions>

References​

createFlowJobProcessor​

Re-exports createFlowJobProcessor


createJobProcessor​

Re-exports createJobProcessor


CreateJobProcessorOptions​

Re-exports CreateJobProcessorOptions


JobProcessorDeps​

Re-exports JobProcessorDeps