HyperdriveJobQueue
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:120
A job queue in a SQL table on D1 or another SQLite, Postgres (Hyperdrive) or MySQL.
With leaseMs, a processing job’s process_at holds its lease’s end, so
the table needs no new column. dequeue() settles expired leases and takes
the next job with statements that each settle or take a job only once:
Postgres skips rows another dequeue() has locked, SQLite runs each
statement under its write lock, and MySQL locks the rows it changes, which
holds only when the client provides transaction(). Without
onLeaseExpired, Postgres does both in one statement; with it, the
callbacks run between the two, so they do not shorten the new lease.
A job already processing without a lease, taken by an earlier version or
by a queue without leaseMs, cannot be told from one whose lease has
expired, so it is due at once.
Type Parameters
Section titled “Type Parameters”T
Implements
Section titled “Implements”JobQueue<T>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new HyperdriveJobQueue<
T>(client,options?):HyperdriveJobQueue<T>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:128
Parameters
Section titled “Parameters”client
Section titled “client”options?
Section titled “options?”HyperdriveJobQueueOptions<T> = {}
Returns
Section titled “Returns”HyperdriveJobQueue<T>
Methods
Section titled “Methods”cleanupTerminal()
Section titled “cleanupTerminal()”cleanupTerminal(
options?):Promise<number>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:401
Removes finished jobs: completed and failed ones by default, or those
with the given statuses, and with olderThan, only those that finished
before it.
Parameters
Section titled “Parameters”options?
Section titled “options?”JobQueueCleanupOptions | JobStatus[]
Returns
Section titled “Returns”Promise<number>
Implementation of
Section titled “Implementation of”clear()
Section titled “clear()”clear():
Promise<void>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:391
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”close()
Section titled “close()”close():
Promise<void>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:442
Returns
Section titled “Returns”Promise<void>
complete()
Section titled “complete()”complete(
jobId,_result?):Promise<void>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:245
Parameters
Section titled “Parameters”string
_result?
Section titled “_result?”any
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”dequeue()
Section titled “dequeue()”dequeue(
options?):Promise<Job<T> |undefined>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:225
Takes the next due job and, with leaseMs, leases it. Jobs whose lease
expired are due again first, or fail when they have no attempts left.
Jobs in options.running are left as they are.
Parameters
Section titled “Parameters”options?
Section titled “options?”JobDequeueOptions = {}
Returns
Section titled “Returns”Promise<Job<T> | undefined>
Implementation of
Section titled “Implementation of”enqueue()
Section titled “enqueue()”enqueue(
type,data,options?):Promise<Job<T>>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:159
Parameters
Section titled “Parameters”string
T
options?
Section titled “options?”{ delay?: number; maxAttempts?: number; metadata?: Record<string, any>; priority?: number; } | undefined
Returns
Section titled “Returns”Promise<Job<T>>
Implementation of
Section titled “Implementation of”fail()
Section titled “fail()”fail(
jobId,error,retry?):Promise<void>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:264
Counts a failed attempt: the job is due again after retry.delayMs when
retries are enabled and attempts are left, and fails otherwise. A
completed job stays completed, even for a worker whose lease expired.
Parameters
Section titled “Parameters”string
string | Error
retry?
Section titled “retry?”JobRetryDirective = ...
Returns
Section titled “Returns”Promise<void>
Implementation of
Section titled “Implementation of”getJob()
Section titled “getJob()”getJob(
jobId):Promise<Job<T> |undefined>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:361
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”Promise<Job<T> | undefined>
Implementation of
Section titled “Implementation of”init()
Section titled “init()”init():
Promise<void>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:141
Returns
Section titled “Returns”Promise<void>
nextDueAt()
Section titled “nextDueAt()”nextDueAt():
Promise<Date|undefined>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:347
When dequeue() next has work: the earliest due time of a pending job
or, with leaseMs, lease expiry of a processing one. A time in the past
means dequeue() has work now, even when it only settles an expired
lease, so call dequeue() rather than checking size(). undefined
when no job is pending or leased.
Returns
Section titled “Returns”Promise<Date | undefined>
Implementation of
Section titled “Implementation of”peek()
Section titled “peek()”peek():
Promise<Job<T> |undefined>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:306
The job dequeue() would take next, including one whose lease expired,
shown as it will be once it is due again. Changes nothing.
Returns
Section titled “Returns”Promise<Job<T> | undefined>
Implementation of
Section titled “Implementation of”remove()
Section titled “remove()”remove(
jobId):Promise<boolean>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:375
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”Promise<boolean>
Implementation of
Section titled “Implementation of”size()
Section titled “size()”size():
Promise<number>
Defined in: packages/messaging/src/adapters/cloudflare/hyperdrive-job-queue.ts:326
How many jobs are due now, including those whose lease expired.
Returns
Section titled “Returns”Promise<number>