@orieljs/queue 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +9 -0
- package/dist/index.d.ts +663 -0
- package/dist/index.js +1977 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { OrielError, ModelNotFoundError, Job, ClassRegistry, Class, ModelInstance, QueuePayload, Container, QueueJob, Queue as Queue$1, QueueManager as QueueManager$1, DatabaseConnection, FailedJobProvider, FailedJob, ConfigRepository, Batch as Batch$1, BatchRepository, PendingBatchOptions, Application, ConsoleKernel } from '@orieljs/contracts';
|
|
2
|
+
import { Redis } from 'ioredis';
|
|
3
|
+
import * as _orieljs_support from '@orieljs/support';
|
|
4
|
+
import { ServiceProvider } from '@orieljs/support';
|
|
5
|
+
|
|
6
|
+
/** Base for queue errors: internal failures, reported and rendered as a 500 (kind 'Server'). */
|
|
7
|
+
declare class QueueError extends Error implements OrielError {
|
|
8
|
+
readonly kind: "Server";
|
|
9
|
+
readonly status = 500;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
}
|
|
12
|
+
/** A job (or a value inside it) can't be turned into a payload. Thrown at dispatch time. */
|
|
13
|
+
declare class JobSerializationError extends QueueError {
|
|
14
|
+
readonly name = "JobSerializationError";
|
|
15
|
+
}
|
|
16
|
+
declare class JobClassNotFoundError extends QueueError {
|
|
17
|
+
readonly className: string;
|
|
18
|
+
readonly name = "JobClassNotFoundError";
|
|
19
|
+
constructor(className: string);
|
|
20
|
+
}
|
|
21
|
+
/** A model stored in a payload no longer exists. Implements the ModelNotFoundError contract. */
|
|
22
|
+
declare class MissingModelError extends Error implements ModelNotFoundError {
|
|
23
|
+
readonly model: string;
|
|
24
|
+
readonly ids: unknown[];
|
|
25
|
+
readonly kind: "ModelNotFound";
|
|
26
|
+
readonly status = 404;
|
|
27
|
+
readonly name = "MissingModelError";
|
|
28
|
+
constructor(model: string, ids: unknown[]);
|
|
29
|
+
}
|
|
30
|
+
declare class MaxAttemptsExceededError extends QueueError {
|
|
31
|
+
readonly job: string;
|
|
32
|
+
readonly name = "MaxAttemptsExceededError";
|
|
33
|
+
constructor(job: string);
|
|
34
|
+
}
|
|
35
|
+
declare class TimeoutExceededError extends QueueError {
|
|
36
|
+
readonly job: string;
|
|
37
|
+
readonly name = "TimeoutExceededError";
|
|
38
|
+
constructor(job: string);
|
|
39
|
+
}
|
|
40
|
+
/** Used when fail() is called without an error. */
|
|
41
|
+
declare class ManuallyFailedError extends QueueError {
|
|
42
|
+
readonly name = "ManuallyFailedError";
|
|
43
|
+
constructor();
|
|
44
|
+
}
|
|
45
|
+
declare class UnknownQueueConnectionError extends QueueError {
|
|
46
|
+
readonly connection: string;
|
|
47
|
+
readonly name = "UnknownQueueConnectionError";
|
|
48
|
+
constructor(connection: string);
|
|
49
|
+
}
|
|
50
|
+
declare class UnknownQueueDriverError extends QueueError {
|
|
51
|
+
readonly driver: string;
|
|
52
|
+
readonly name = "UnknownQueueDriverError";
|
|
53
|
+
constructor(driver: string);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Symbol.for keys shared with @orieljs/bus (which declares the same keys; the two packages never import each other).
|
|
58
|
+
* Symbol-keyed properties are never serialized into a payload's data.
|
|
59
|
+
*/
|
|
60
|
+
/** The running QueueJob, set on the restored job instance by CallQueuedHandler. */
|
|
61
|
+
declare const QUEUE_JOB: unique symbol;
|
|
62
|
+
/** A ChainSpec, set on the first job of a chain by Bus. createPayload() turns it into payload.chain & co. */
|
|
63
|
+
declare const CHAIN: unique symbol;
|
|
64
|
+
/** A UniqueLockSpec, set by Bus after acquiring a unique-job lock. createPayload() copies it to payload.uniqueLock. */
|
|
65
|
+
declare const UNIQUE_LOCK: unique symbol;
|
|
66
|
+
interface ChainSpec {
|
|
67
|
+
jobs: Job[];
|
|
68
|
+
connection?: string;
|
|
69
|
+
queue?: string;
|
|
70
|
+
catch: Job[];
|
|
71
|
+
}
|
|
72
|
+
interface UniqueLockSpec {
|
|
73
|
+
key: string;
|
|
74
|
+
/** Cache store name (undefined = the default store). */
|
|
75
|
+
store?: string;
|
|
76
|
+
untilProcessing: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** JSON-safe encoded value. Tagged objects carry `__oriel`. */
|
|
80
|
+
type Encoded = null | boolean | number | string | Encoded[] | {
|
|
81
|
+
[key: string]: Encoded;
|
|
82
|
+
};
|
|
83
|
+
/** Structural model check: what the ModelInstance contract guarantees. */
|
|
84
|
+
declare function isModel(value: unknown): value is ModelInstance;
|
|
85
|
+
/**
|
|
86
|
+
* The registry name of a class, registering it under its own name the first time. A name collision (two different
|
|
87
|
+
* classes sharing `constructor.name`) is reported as a JobSerializationError naming the class, so the caller can
|
|
88
|
+
* register it explicitly under a namespaced name.
|
|
89
|
+
*/
|
|
90
|
+
declare function classNameOf(registry: ClassRegistry, cls: Class<unknown>): string;
|
|
91
|
+
/**
|
|
92
|
+
* Encodes a value for a payload. Models become `{ __oriel: 'model', class, key }`; other class instances keep their
|
|
93
|
+
* class name and fields; Date, Map, Set and byte arrays (any ArrayBuffer view, as base64) are tagged. Functions,
|
|
94
|
+
* symbols, bigints, unsaved models and cycles throw.
|
|
95
|
+
*/
|
|
96
|
+
declare function serializeValue(value: unknown, registry: ClassRegistry, path?: string, seen?: Set<object>): Encoded | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* The inverse of serializeValue(). Byte arrays come back as Uint8Array. Models are re-fetched by key without global
|
|
99
|
+
* scopes (Laravel's newQueryForRestoration), so a soft-deleted model still reaches the job; a missing one throws
|
|
100
|
+
* MissingModelError.
|
|
101
|
+
*/
|
|
102
|
+
declare function restoreValue(value: Encoded, registry: ClassRegistry): Promise<unknown>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The payload Oriel stores: the QueuePayload contract (which carries retryUntil and failOnTimeout) plus the
|
|
106
|
+
* missing-model flag and the chain / unique-lock bookkeeping. `data` holds the job's encoded own fields.
|
|
107
|
+
*/
|
|
108
|
+
interface QueueJobPayload extends QueuePayload {
|
|
109
|
+
data: Record<string, Encoded>;
|
|
110
|
+
deleteWhenMissingModels?: boolean;
|
|
111
|
+
chain?: QueueJobPayload[];
|
|
112
|
+
chainConnection?: string;
|
|
113
|
+
chainQueue?: string;
|
|
114
|
+
chainCatch?: QueueJobPayload[];
|
|
115
|
+
uniqueLock?: UniqueLockSpec;
|
|
116
|
+
/** A batch's then/catch/finally callback: it carries the batchId but isn't one of the batch's jobs. */
|
|
117
|
+
batchCallback?: boolean;
|
|
118
|
+
}
|
|
119
|
+
declare function normalizeBackoff(backoff: unknown): number[] | undefined;
|
|
120
|
+
/** Builds the payload for a job instance. Registers the job's class (and nested classes) in the ClassRegistry. */
|
|
121
|
+
declare function createPayload(job: Job, registry: ClassRegistry): QueueJobPayload;
|
|
122
|
+
/** Restores the job instance from a payload, without calling its constructor. Models are re-fetched. */
|
|
123
|
+
declare function restoreJob<T extends Job = Job>(payload: QueuePayload, registry: ClassRegistry): Promise<T>;
|
|
124
|
+
/** JSON with `attempts` as the first key, so the redis Lua scripts can bump it without re-encoding the payload. */
|
|
125
|
+
declare function encodePayload(payload: QueuePayload): string;
|
|
126
|
+
declare function decodePayload(raw: string): QueueJobPayload;
|
|
127
|
+
|
|
128
|
+
type Next = (job: Job) => Promise<unknown>;
|
|
129
|
+
/** Job middleware: a function or an object with handle(job, next), like Laravel's job middleware classes. */
|
|
130
|
+
type JobMiddleware = ((job: Job, next: Next) => unknown) | {
|
|
131
|
+
handle(job: Job, next: Next): unknown;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Laravel's CallQueuedHandler: restores the job from its payload and runs it through its middleware, then does the
|
|
135
|
+
* chain / batch / unique-lock bookkeeping. failed() is the other half, called by BaseJob.fail().
|
|
136
|
+
*/
|
|
137
|
+
declare class CallQueuedHandler {
|
|
138
|
+
private readonly container;
|
|
139
|
+
constructor(container: Container);
|
|
140
|
+
call(job: BaseJob): Promise<void>;
|
|
141
|
+
/** Called by BaseJob.fail() after the job was deleted. */
|
|
142
|
+
failed(job: BaseJob, error: unknown): Promise<void>;
|
|
143
|
+
/** Dispatches a callback job (chain catch, batch then/catch/finally) through the Bus, or runs it inline. */
|
|
144
|
+
dispatchCallback(callback: Job): Promise<void>;
|
|
145
|
+
private handleModelNotFound;
|
|
146
|
+
private dispatchThroughMiddleware;
|
|
147
|
+
private dispatchNextJobInChain;
|
|
148
|
+
private recordSuccessfulBatchJob;
|
|
149
|
+
private releaseUniqueLock;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What every queue job needs from its driver. */
|
|
153
|
+
interface JobContext {
|
|
154
|
+
/** The container the driver was built with (the application). Used for FailedJobProvider and the handler. */
|
|
155
|
+
container: Container;
|
|
156
|
+
handler: CallQueuedHandler;
|
|
157
|
+
connectionName: string;
|
|
158
|
+
}
|
|
159
|
+
/** Laravel's Illuminate\Queue\Jobs\Job: state flags, fire(), fail(), and payload accessors. */
|
|
160
|
+
declare abstract class BaseJob implements QueueJob {
|
|
161
|
+
protected readonly context: JobContext;
|
|
162
|
+
protected readonly queue: string;
|
|
163
|
+
protected deleted: boolean;
|
|
164
|
+
protected released: boolean;
|
|
165
|
+
protected failedFlag: boolean;
|
|
166
|
+
constructor(context: JobContext, queue: string);
|
|
167
|
+
abstract getJobId(): string;
|
|
168
|
+
abstract payload(): QueueJobPayload;
|
|
169
|
+
attempts(): number;
|
|
170
|
+
getQueue(): string;
|
|
171
|
+
getConnectionName(): string;
|
|
172
|
+
uuid(): string;
|
|
173
|
+
/** The job's display name (its class name unless it defines displayName()). */
|
|
174
|
+
resolveName(): string;
|
|
175
|
+
maxTries(): number | undefined;
|
|
176
|
+
backoff(): number[] | undefined;
|
|
177
|
+
timeout(): number | undefined;
|
|
178
|
+
retryUntil(): number | undefined;
|
|
179
|
+
shouldFailOnTimeout(): boolean;
|
|
180
|
+
delete(): Promise<void>;
|
|
181
|
+
release(_delaySeconds?: number): Promise<void>;
|
|
182
|
+
isDeleted(): boolean;
|
|
183
|
+
isReleased(): boolean;
|
|
184
|
+
isDeletedOrReleased(): boolean;
|
|
185
|
+
hasFailed(): boolean;
|
|
186
|
+
markAsFailed(): void;
|
|
187
|
+
/** Runs the job through CallQueuedHandler. */
|
|
188
|
+
fire(): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Marks the job as failed, deletes it, runs the handler's failure bookkeeping (unique lock, batch, chain catch,
|
|
191
|
+
* the job's failed() hook) and stores it with the FailedJobProvider. A job that was already deleted is ignored.
|
|
192
|
+
*/
|
|
193
|
+
fail(error?: unknown): Promise<void>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The sync driver's job: runs once, in the dispatching process. release() only marks it (nothing to requeue). */
|
|
197
|
+
declare class SyncJob extends BaseJob {
|
|
198
|
+
private readonly raw;
|
|
199
|
+
constructor(context: JobContext, raw: QueueJobPayload, queue: string);
|
|
200
|
+
getJobId(): string;
|
|
201
|
+
payload(): QueueJobPayload;
|
|
202
|
+
attempts(): number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Laravel's WithoutOverlapping job middleware: only one job with the same key runs at a time (a cache lock).
|
|
207
|
+
* A job that finds the lock taken is released back onto the queue after `releaseAfter` seconds (default 0), or
|
|
208
|
+
* dropped after dontRelease().
|
|
209
|
+
*/
|
|
210
|
+
declare class WithoutOverlapping {
|
|
211
|
+
key: string | number;
|
|
212
|
+
releaseAfter: number | undefined;
|
|
213
|
+
expiresAfter: number;
|
|
214
|
+
prefix: string;
|
|
215
|
+
shareKey: boolean;
|
|
216
|
+
constructor(key?: string | number, releaseAfter?: number | undefined, expiresAfter?: number);
|
|
217
|
+
releaseAfterSeconds(seconds: number): this;
|
|
218
|
+
dontRelease(): this;
|
|
219
|
+
/** Lock expiry in seconds, so a crashed worker can't hold the lock forever. 0 (the default) = no expiry. */
|
|
220
|
+
expireAfter(seconds: number): this;
|
|
221
|
+
withPrefix(prefix: string): this;
|
|
222
|
+
/** Share the lock across job classes that use the same key. */
|
|
223
|
+
shared(): this;
|
|
224
|
+
getLockKey(job: Job): string;
|
|
225
|
+
handle(job: Job, next: (job: Job) => Promise<unknown>): Promise<unknown>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Shared driver behaviour (Laravel's Illuminate\Queue\Queue): payload creation, queue names, the job context. */
|
|
229
|
+
declare abstract class QueueBase implements Queue$1 {
|
|
230
|
+
protected readonly container: Container;
|
|
231
|
+
protected readonly defaultQueue: string;
|
|
232
|
+
protected connectionName: string;
|
|
233
|
+
protected readonly handler: CallQueuedHandler;
|
|
234
|
+
constructor(container: Container, defaultQueue?: string);
|
|
235
|
+
setConnectionName(name: string): this;
|
|
236
|
+
getConnectionName(): string;
|
|
237
|
+
/** The queue name to use: the given one, else the connection's default. */
|
|
238
|
+
getQueue(queue?: string): string;
|
|
239
|
+
createPayload(job: Job): QueueJobPayload;
|
|
240
|
+
push(job: Job, queue?: string): Promise<string>;
|
|
241
|
+
later(delaySeconds: number, job: Job, queue?: string): Promise<string>;
|
|
242
|
+
abstract pushRaw(payload: QueuePayload, queue?: string, delaySeconds?: number): Promise<string>;
|
|
243
|
+
abstract pop(queue?: string): Promise<QueueJob | undefined>;
|
|
244
|
+
abstract size(queue?: string): Promise<number>;
|
|
245
|
+
abstract clear(queue?: string): Promise<number>;
|
|
246
|
+
/** Closes network connections (redis). No-op by default. */
|
|
247
|
+
disconnect(): Promise<void>;
|
|
248
|
+
/**
|
|
249
|
+
* A row whose payload can't be decoded can never run: record it as failed (with a minimal payload holding the raw
|
|
250
|
+
* text) and report the original error. If the failed-job provider throws, that error propagates so the caller
|
|
251
|
+
* keeps the row (it is retried after retry_after) instead of losing the payload. With no provider bound the row
|
|
252
|
+
* is only reported, like Laravel. A reporter failure never blocks the move.
|
|
253
|
+
*/
|
|
254
|
+
protected failCorrupt(queue: string, raw: string, error: unknown): Promise<void>;
|
|
255
|
+
protected jobContext(): JobContext;
|
|
256
|
+
/** Unix seconds, like Laravel's availableAt()/currentTime(). */
|
|
257
|
+
protected currentTime(): number;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Runs jobs immediately, in the dispatching process (delays are ignored). The payload is still built and the job
|
|
262
|
+
* restored from it, so sync behaves like a real worker. A failure is stored with the FailedJobProvider and rethrown.
|
|
263
|
+
*/
|
|
264
|
+
declare class SyncQueue extends QueueBase {
|
|
265
|
+
pushRaw(payload: QueuePayload, queue?: string): Promise<string>;
|
|
266
|
+
pop(): Promise<QueueJob | undefined>;
|
|
267
|
+
size(): Promise<number>;
|
|
268
|
+
clear(): Promise<number>;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Discards every job (Laravel's `null` driver). */
|
|
272
|
+
declare class NullQueue extends QueueBase {
|
|
273
|
+
push(_job: Job): Promise<string>;
|
|
274
|
+
later(_delaySeconds: number, _job: Job): Promise<string>;
|
|
275
|
+
pushRaw(): Promise<string>;
|
|
276
|
+
pop(): Promise<QueueJob | undefined>;
|
|
277
|
+
size(): Promise<number>;
|
|
278
|
+
clear(): Promise<number>;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
type ConnectionConfig = Record<string, unknown>;
|
|
282
|
+
/** Laravel's QueueManager: builds connections from `queue.connections`, caches them, delegates to the default. */
|
|
283
|
+
declare class QueueManager implements QueueManager$1 {
|
|
284
|
+
private readonly container;
|
|
285
|
+
private readonly connections;
|
|
286
|
+
private readonly drivers;
|
|
287
|
+
constructor(container: Container);
|
|
288
|
+
connection(name?: string): Queue$1;
|
|
289
|
+
/**
|
|
290
|
+
* Registers a custom driver (`queue.connections.<name>.driver`). The driver's `pop()` must return instances of the
|
|
291
|
+
* exported `BaseJob` subclasses (or subclasses of `BaseJob`): the Worker relies on their methods.
|
|
292
|
+
*/
|
|
293
|
+
extend(driver: string, factory: (config: ConnectionConfig) => Queue$1): void;
|
|
294
|
+
getDefaultDriver(): string;
|
|
295
|
+
setDefaultDriver(name: string): void;
|
|
296
|
+
getConnectionConfig(name: string): ConnectionConfig | undefined;
|
|
297
|
+
/** Whether the connection has been built already. */
|
|
298
|
+
connected(name?: string): boolean;
|
|
299
|
+
/** Disconnects and forgets one connection (default: the default one). */
|
|
300
|
+
disconnect(name?: string): Promise<void>;
|
|
301
|
+
/** Disconnects every built connection. */
|
|
302
|
+
disconnectAll(): Promise<void>;
|
|
303
|
+
push(job: Job, queue?: string): Promise<string>;
|
|
304
|
+
later(delaySeconds: number, job: Job, queue?: string): Promise<string>;
|
|
305
|
+
pushRaw(payload: QueuePayload, queue?: string, delaySeconds?: number): Promise<string>;
|
|
306
|
+
pop(queue?: string): Promise<QueueJob | undefined>;
|
|
307
|
+
size(queue?: string): Promise<number>;
|
|
308
|
+
clear(queue?: string): Promise<number>;
|
|
309
|
+
private resolve;
|
|
310
|
+
private config;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** A row of the `jobs` table (Laravel 11 schema). Times are Unix seconds. */
|
|
314
|
+
interface DatabaseJobRecord {
|
|
315
|
+
id: number | string;
|
|
316
|
+
queue: string;
|
|
317
|
+
payload: string;
|
|
318
|
+
attempts: number;
|
|
319
|
+
reserved_at: number | null;
|
|
320
|
+
available_at: number;
|
|
321
|
+
created_at: number;
|
|
322
|
+
}
|
|
323
|
+
/** A reserved row. attempts() comes from the row, not the payload. */
|
|
324
|
+
declare class DatabaseJob extends BaseJob {
|
|
325
|
+
private readonly database;
|
|
326
|
+
private readonly record;
|
|
327
|
+
private readonly decoded;
|
|
328
|
+
constructor(context: JobContext, database: DatabaseQueue, record: DatabaseJobRecord, queue: string);
|
|
329
|
+
getJobId(): string;
|
|
330
|
+
payload(): QueueJobPayload;
|
|
331
|
+
attempts(): number;
|
|
332
|
+
getJobRecord(): DatabaseJobRecord;
|
|
333
|
+
/** Deletes the reserved row and inserts a fresh one, keeping the attempt count (Laravel's deleteAndRelease). */
|
|
334
|
+
release(delaySeconds?: number): Promise<void>;
|
|
335
|
+
delete(): Promise<void>;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
interface DatabaseQueueOptions {
|
|
339
|
+
table?: string;
|
|
340
|
+
queue?: string;
|
|
341
|
+
/** Seconds before a reserved job is considered lost and handed out again (Laravel's retry_after, default 90). */
|
|
342
|
+
retryAfter?: number;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Laravel's DatabaseQueue on the `jobs` table. Reservation is a `SELECT … FOR UPDATE` plus a conditional UPDATE on
|
|
346
|
+
* the row's attempts and reserved_at, so a row can never be reserved twice, even on SQLite.
|
|
347
|
+
*/
|
|
348
|
+
declare class DatabaseQueue extends QueueBase {
|
|
349
|
+
private readonly database;
|
|
350
|
+
private readonly table;
|
|
351
|
+
private readonly retryAfter;
|
|
352
|
+
constructor(container: Container, database: () => DatabaseConnection, options?: DatabaseQueueOptions);
|
|
353
|
+
pushRaw(payload: QueuePayload, queue?: string, delaySeconds?: number): Promise<string>;
|
|
354
|
+
pop(queue?: string): Promise<QueueJob | undefined>;
|
|
355
|
+
size(queue?: string): Promise<number>;
|
|
356
|
+
clear(queue?: string): Promise<number>;
|
|
357
|
+
deleteReserved(id: string): Promise<void>;
|
|
358
|
+
deleteAndRelease(queue: string, job: DatabaseJob, delaySeconds: number): Promise<void>;
|
|
359
|
+
private pushToDatabase;
|
|
360
|
+
/** The oldest available row, or the oldest reservation older than retry_after, whichever has the lower id. */
|
|
361
|
+
private getNextAvailableJob;
|
|
362
|
+
/** Reserves the row only if nobody else changed it since we read it. */
|
|
363
|
+
private markJobAsReserved;
|
|
364
|
+
/** Job ids are stored as numbers; getJobId() hands them out as strings. */
|
|
365
|
+
private key;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Forgets failed jobs (queue.failed.driver = 'null'). */
|
|
369
|
+
declare class NullFailedJobProvider implements FailedJobProvider {
|
|
370
|
+
log(): Promise<string>;
|
|
371
|
+
all(): Promise<FailedJob[]>;
|
|
372
|
+
find(): Promise<FailedJob | undefined>;
|
|
373
|
+
forget(): Promise<boolean>;
|
|
374
|
+
flush(): Promise<void>;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** `YYYY-MM-DD HH:MM:SS` in UTC: a value every driver accepts for a timestamp column. */
|
|
378
|
+
declare function formatTimestamp(date: Date): string;
|
|
379
|
+
/** Like PHP's (string) $exception: the stack when there is one. */
|
|
380
|
+
declare function describeError(error: unknown): string;
|
|
381
|
+
/** Laravel's DatabaseUuidFailedJobProvider (`database-uuids`): failed jobs are identified by the payload uuid. */
|
|
382
|
+
declare class DatabaseFailedJobProvider implements FailedJobProvider {
|
|
383
|
+
private readonly database;
|
|
384
|
+
private readonly table;
|
|
385
|
+
constructor(database: () => DatabaseConnection, table?: string);
|
|
386
|
+
log(connection: string, queue: string, payload: QueuePayload, error: unknown): Promise<string>;
|
|
387
|
+
all(): Promise<FailedJob[]>;
|
|
388
|
+
find(id: string): Promise<FailedJob | undefined>;
|
|
389
|
+
forget(id: string): Promise<boolean>;
|
|
390
|
+
flush(): Promise<void>;
|
|
391
|
+
private toFailedJob;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Text of Laravel 11's `0001_01_01_000002_create_jobs_table` migration, for the skeleton's
|
|
396
|
+
* `database/migrations/0001_01_01_000002_create_jobs_table.ts`. Database owns migrations, so Queue ships the text
|
|
397
|
+
* (the C2 skeleton step writes it); it is not compiled here.
|
|
398
|
+
*/
|
|
399
|
+
declare const JOBS_TABLE_MIGRATION = "import { Migration, Schema } from '@orieljs/framework'\n\nexport default class extends Migration {\n async up() {\n await Schema.create('jobs', (t) => {\n t.id()\n t.string('queue').index()\n t.longText('payload')\n t.unsignedTinyInteger('attempts')\n t.unsignedInteger('reserved_at').nullable()\n t.unsignedInteger('available_at')\n t.unsignedInteger('created_at')\n })\n\n await Schema.create('job_batches', (t) => {\n t.string('id').primary()\n t.string('name')\n t.integer('total_jobs')\n t.integer('pending_jobs')\n t.integer('failed_jobs')\n t.longText('failed_job_ids')\n t.mediumText('options').nullable()\n t.integer('cancelled_at').nullable()\n t.integer('created_at')\n t.integer('finished_at').nullable()\n })\n\n await Schema.create('failed_jobs', (t) => {\n t.id()\n t.string('uuid').unique()\n t.text('connection')\n t.text('queue')\n t.longText('payload')\n t.longText('exception')\n t.timestamp('failed_at').useCurrent()\n })\n }\n\n async down() {\n await Schema.dropIfExists('jobs')\n await Schema.dropIfExists('job_batches')\n await Schema.dropIfExists('failed_jobs')\n }\n}\n";
|
|
400
|
+
|
|
401
|
+
/** A job popped from redis. `reserved` is the exact member stored in the `:reserved` set (attempts already bumped). */
|
|
402
|
+
declare class RedisJob extends BaseJob {
|
|
403
|
+
private readonly redis;
|
|
404
|
+
private readonly job;
|
|
405
|
+
private readonly reserved;
|
|
406
|
+
private readonly decoded;
|
|
407
|
+
constructor(context: JobContext, redis: RedisQueue, job: string, reserved: string, queue: string);
|
|
408
|
+
getJobId(): string;
|
|
409
|
+
payload(): QueueJobPayload;
|
|
410
|
+
/** The payload as it was pushed (before the attempt was counted). */
|
|
411
|
+
getRawBody(): string;
|
|
412
|
+
getReservedJob(): string;
|
|
413
|
+
delete(): Promise<void>;
|
|
414
|
+
release(delaySeconds?: number): Promise<void>;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
interface RedisQueueOptions {
|
|
418
|
+
queue?: string;
|
|
419
|
+
/** Seconds before a reserved job is handed out again (Laravel's retry_after, default 90). */
|
|
420
|
+
retryAfter?: number;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Client config for the redis driver: a prebuilt client, a URL, or ioredis options (`host`, `port`, `password`,
|
|
424
|
+
* `db`, …). Either inline on the queue connection or a `database.redis.<name>` entry (see resolveRedisConfig()).
|
|
425
|
+
*/
|
|
426
|
+
interface RedisConnectionConfig {
|
|
427
|
+
client?: Redis;
|
|
428
|
+
url?: string;
|
|
429
|
+
[option: string]: unknown;
|
|
430
|
+
}
|
|
431
|
+
/** An ioredis client. With a URL, the other options (such as `db`) apply where the URL doesn't set them. */
|
|
432
|
+
declare function makeRedisClient(config: RedisConnectionConfig): Redis;
|
|
433
|
+
/**
|
|
434
|
+
* The client config for a redis queue connection. An inline `client` or `url` wins; otherwise, as in Laravel, the
|
|
435
|
+
* `database.redis.<connection ?? 'default'>` entry (merged by Database). The connection's own inline ioredis
|
|
436
|
+
* options are used only when that entry doesn't exist.
|
|
437
|
+
*/
|
|
438
|
+
declare function resolveRedisConfig(config: RedisConnectionConfig, appConfig: ConfigRepository): RedisConnectionConfig;
|
|
439
|
+
/** Laravel's RedisQueue: a list per queue plus `:delayed`/`:reserved` sorted sets, moved around with Lua. */
|
|
440
|
+
declare class RedisQueue extends QueueBase {
|
|
441
|
+
private readonly connect;
|
|
442
|
+
private client;
|
|
443
|
+
private readonly retryAfter;
|
|
444
|
+
constructor(container: Container, connect: () => Redis, options?: RedisQueueOptions);
|
|
445
|
+
redis(): Redis;
|
|
446
|
+
/** The list key for a queue name: `queues:<name>`. */
|
|
447
|
+
key(queue?: string): string;
|
|
448
|
+
pushRaw(payload: QueuePayload, queue?: string, delaySeconds?: number): Promise<string>;
|
|
449
|
+
pop(queue?: string): Promise<QueueJob | undefined>;
|
|
450
|
+
size(queue?: string): Promise<number>;
|
|
451
|
+
clear(queue?: string): Promise<number>;
|
|
452
|
+
deleteReserved(queue: string, job: RedisJob): Promise<void>;
|
|
453
|
+
deleteAndRelease(queue: string, job: RedisJob, delaySeconds: number): Promise<void>;
|
|
454
|
+
disconnect(): Promise<void>;
|
|
455
|
+
/** Unix seconds, with milliseconds as the fraction (sorted-set scores). */
|
|
456
|
+
private now;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** The `options` column: callbacks are stored as payloads. */
|
|
460
|
+
interface StoredBatchOptions {
|
|
461
|
+
allowFailures: boolean;
|
|
462
|
+
connection?: string;
|
|
463
|
+
queue?: string;
|
|
464
|
+
then: QueueJobPayload[];
|
|
465
|
+
catch: QueueJobPayload[];
|
|
466
|
+
finally: QueueJobPayload[];
|
|
467
|
+
}
|
|
468
|
+
interface BatchAttributes {
|
|
469
|
+
id: string;
|
|
470
|
+
name: string;
|
|
471
|
+
totalJobs: number;
|
|
472
|
+
pendingJobs: number;
|
|
473
|
+
failedJobs: number;
|
|
474
|
+
failedJobIds: string[];
|
|
475
|
+
options: StoredBatchOptions;
|
|
476
|
+
createdAt: Date;
|
|
477
|
+
cancelledAt?: Date;
|
|
478
|
+
finishedAt?: Date;
|
|
479
|
+
}
|
|
480
|
+
/** A snapshot of a batch row (Laravel's Illuminate\Bus\Batch). Call fresh() for current numbers. */
|
|
481
|
+
declare class Batch implements Batch$1 {
|
|
482
|
+
private readonly repository;
|
|
483
|
+
readonly id: string;
|
|
484
|
+
readonly name: string;
|
|
485
|
+
readonly totalJobs: number;
|
|
486
|
+
readonly pendingJobs: number;
|
|
487
|
+
readonly failedJobs: number;
|
|
488
|
+
readonly failedJobIds: string[];
|
|
489
|
+
readonly options: StoredBatchOptions;
|
|
490
|
+
readonly createdAt: Date;
|
|
491
|
+
readonly cancelledAt: Date | undefined;
|
|
492
|
+
readonly finishedAt: Date | undefined;
|
|
493
|
+
constructor(repository: BatchRepository, attributes: BatchAttributes);
|
|
494
|
+
processedJobs(): number;
|
|
495
|
+
/** Percentage of processed jobs, 0–100. */
|
|
496
|
+
progress(): number;
|
|
497
|
+
finished(): boolean;
|
|
498
|
+
cancelled(): boolean;
|
|
499
|
+
hasFailures(): boolean;
|
|
500
|
+
allowsFailures(): boolean;
|
|
501
|
+
cancel(): Promise<void>;
|
|
502
|
+
delete(): Promise<void>;
|
|
503
|
+
fresh(): Promise<Batch$1 | undefined>;
|
|
504
|
+
toJSON(): Record<string, unknown>;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** Laravel's DatabaseBatchRepository plus Batch::recordSuccessfulJob/recordFailedJob. */
|
|
508
|
+
declare class DatabaseBatchRepository implements BatchRepository {
|
|
509
|
+
private readonly container;
|
|
510
|
+
private readonly database;
|
|
511
|
+
private readonly table;
|
|
512
|
+
private readonly handler;
|
|
513
|
+
constructor(container: Container, database: () => DatabaseConnection, table?: string);
|
|
514
|
+
store(options: PendingBatchOptions): Promise<Batch>;
|
|
515
|
+
find(id: string): Promise<Batch | undefined>;
|
|
516
|
+
incrementTotalJobs(id: string, amount: number): Promise<void>;
|
|
517
|
+
recordSuccessfulJob(id: string, jobUuid: string): Promise<void>;
|
|
518
|
+
recordFailedJob(id: string, jobUuid: string, _error: unknown): Promise<void>;
|
|
519
|
+
markAsFinished(id: string): Promise<void>;
|
|
520
|
+
cancel(id: string): Promise<void>;
|
|
521
|
+
delete(id: string): Promise<void>;
|
|
522
|
+
/** Read-modify-write of the counters under a row lock (Laravel's updateAtomicValues). */
|
|
523
|
+
private updateAtomicValues;
|
|
524
|
+
/** Restores each callback job, gives it the batch id, marks it as a callback (so it isn't counted) and dispatches it. */
|
|
525
|
+
private invoke;
|
|
526
|
+
private toBatch;
|
|
527
|
+
private now;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** `queue:work` options (Laravel's WorkerOptions). Seconds unless noted. */
|
|
531
|
+
interface WorkerOptions {
|
|
532
|
+
name: string;
|
|
533
|
+
/** Seconds to wait before retrying a failed job: one number or a per-attempt list. */
|
|
534
|
+
backoff: number | number[] | string;
|
|
535
|
+
/** MB of V8 heap after which the worker stops (exit 12). 0 disables the check. */
|
|
536
|
+
memory: number;
|
|
537
|
+
/** Max seconds a job may run. 0 disables it. A job's own `timeout` wins. */
|
|
538
|
+
timeout: number;
|
|
539
|
+
/** Seconds to sleep when no job is available. */
|
|
540
|
+
sleep: number;
|
|
541
|
+
/** Attempts before a job fails, unless the job sets `tries`. 0 = unlimited. */
|
|
542
|
+
maxTries: number;
|
|
543
|
+
stopWhenEmpty: boolean;
|
|
544
|
+
/** Stop after this many jobs. 0 = no limit. */
|
|
545
|
+
maxJobs: number;
|
|
546
|
+
/** Stop after this many seconds. 0 = no limit. */
|
|
547
|
+
maxTime: number;
|
|
548
|
+
/** Seconds to rest between jobs. */
|
|
549
|
+
rest: number;
|
|
550
|
+
}
|
|
551
|
+
declare const defaultWorkerOptions: () => WorkerOptions;
|
|
552
|
+
/** Hooks for output (the queue:work command prints RUNNING / DONE / FAIL lines with them). */
|
|
553
|
+
interface WorkerListener {
|
|
554
|
+
processing?(job: QueueJob): void;
|
|
555
|
+
processed?(job: QueueJob, milliseconds: number): void;
|
|
556
|
+
failed?(job: QueueJob, error: unknown): void;
|
|
557
|
+
exception?(error: unknown, job?: QueueJob): void;
|
|
558
|
+
}
|
|
559
|
+
declare const RESTART_CACHE_KEY = "oriel:queue:restart";
|
|
560
|
+
/**
|
|
561
|
+
* Laravel's Illuminate\Queue\Worker: pops jobs, runs each in a fresh container scope, applies tries / backoff /
|
|
562
|
+
* retryUntil / timeout, and stops on a signal, a restart broadcast, memory, --max-jobs, --max-time or an empty queue.
|
|
563
|
+
* A job's error never crashes the loop (spec §7): it's reported and the job is released or failed. Nor does an
|
|
564
|
+
* unhandled rejection leaked from a job's handle(): it's reported the same way, and the job (if not already failed)
|
|
565
|
+
* is failed with it.
|
|
566
|
+
*/
|
|
567
|
+
declare class Worker {
|
|
568
|
+
private readonly manager;
|
|
569
|
+
private readonly container;
|
|
570
|
+
private readonly listener;
|
|
571
|
+
static readonly EXIT_SUCCESS = 0;
|
|
572
|
+
static readonly EXIT_ERROR = 1;
|
|
573
|
+
static readonly EXIT_MEMORY_LIMIT = 12;
|
|
574
|
+
shouldQuit: boolean;
|
|
575
|
+
private timedOut;
|
|
576
|
+
private sleeping;
|
|
577
|
+
private currentJob;
|
|
578
|
+
/** F2: the promise handling the most recently leaked unhandled rejection, awaited by process() before it decides
|
|
579
|
+
* whether the job succeeded, so failing it is never racing the "processed" event. */
|
|
580
|
+
private pendingRejection;
|
|
581
|
+
constructor(manager: QueueManager$1, container: Container, listener?: WorkerListener);
|
|
582
|
+
/** Runs until a stop condition; returns the exit code. `queue` may be a comma-separated priority list. */
|
|
583
|
+
daemon(connectionName: string, queue: string, options: WorkerOptions): Promise<number>;
|
|
584
|
+
/**
|
|
585
|
+
* `--once`: processes the next job, or sleeps when there is none. Laravel's runNextJob doesn't listen for stop
|
|
586
|
+
* signals, but spec §7 still binds here: the unhandled-rejection guard (F2) is installed for the duration of the
|
|
587
|
+
* single run, through the same helper listenForSignals() uses, so a leaked rejection fails the job instead of
|
|
588
|
+
* crashing the process. Returns EXIT_ERROR when the job timed out, EXIT_SUCCESS otherwise.
|
|
589
|
+
*/
|
|
590
|
+
runNextJob(connectionName: string, queue: string, options: WorkerOptions): Promise<number>;
|
|
591
|
+
/** Stops after the current job (SIGTERM/SIGINT/SIGQUIT call this). */
|
|
592
|
+
stop(): void;
|
|
593
|
+
/** Registers graceful-stop signal handlers and the unhandled-rejection guard; returns a function that removes them. */
|
|
594
|
+
listenForSignals(): () => void;
|
|
595
|
+
/** F2: installs the process-wide unhandledRejection guard; returns a function that removes it. */
|
|
596
|
+
private guardUnhandledRejections;
|
|
597
|
+
/** Processes one job: max-attempts check, fire (with timeout), and failure handling. `_connectionName` is unused (kept for Laravel's signature). */
|
|
598
|
+
process(_connectionName: string, job: QueueJob, options: WorkerOptions): Promise<void>;
|
|
599
|
+
sleep(seconds: number): Promise<void>;
|
|
600
|
+
/** Runs one job in its own scope. Nothing it throws escapes: an error from the failure handling itself (a failed()
|
|
601
|
+
* hook, the failed-job provider, a release) is reported like any other, as Laravel's Worker::runJob does. */
|
|
602
|
+
private runJob;
|
|
603
|
+
private getNextJob;
|
|
604
|
+
private fireWithTimeout;
|
|
605
|
+
/** Releases or fails the job. The job's error is reported even when handling it throws (runJob reports that too). */
|
|
606
|
+
private handleJobException;
|
|
607
|
+
private markJobAsFailedIfAlreadyExceedsMaxAttempts;
|
|
608
|
+
private markJobAsFailedIfWillExceedMaxAttempts;
|
|
609
|
+
private failJob;
|
|
610
|
+
private calculateBackoff;
|
|
611
|
+
private stopIfNecessary;
|
|
612
|
+
memoryExceeded(limitMb: number): boolean;
|
|
613
|
+
private queueShouldRestart;
|
|
614
|
+
private getTimestampOfLastQueueRestart;
|
|
615
|
+
/** Reports through the ExceptionHandler when bound, and tells the listener. Never throws. */
|
|
616
|
+
private report;
|
|
617
|
+
/**
|
|
618
|
+
* Spec §7: an unhandled rejection leaked from a job's handle() must not crash the worker. It's reported through
|
|
619
|
+
* the same path as any other job error, and the job currently running — if there is one, and it hasn't already
|
|
620
|
+
* failed — is failed with it, exactly as handleJobException fails a job.
|
|
621
|
+
*/
|
|
622
|
+
private handleUnhandledRejection;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** The app directories whose classes travel inside wrapper jobs (queued listeners, mailables, notifications). */
|
|
626
|
+
declare const WORKER_DISCOVERY_DIRECTORIES: readonly ["Events", "Listeners", "Mail", "Notifications"];
|
|
627
|
+
/**
|
|
628
|
+
* Imports every module under `directory` (recursively) and registers each exported class that passes `filter`
|
|
629
|
+
* (every class by default) under its own name, so a worker process can restore objects it never created itself.
|
|
630
|
+
* Returns the registered names. A missing directory registers nothing. A name already taken by a different class
|
|
631
|
+
* (classNameOf) rejects with a JobSerializationError naming the class and suggesting an explicit namespaced
|
|
632
|
+
* registration, instead of surfacing the ClassRegistry's bare collision error.
|
|
633
|
+
*/
|
|
634
|
+
declare function discoverClasses(directory: string, registry: ClassRegistry, filter?: (cls: Class<unknown>) => boolean): Promise<string[]>;
|
|
635
|
+
/** Registers the exported classes under `directory` that have a handle() method (app/Jobs). */
|
|
636
|
+
declare function discoverJobs(directory: string, registry: ClassRegistry): Promise<string[]>;
|
|
637
|
+
|
|
638
|
+
interface QueueCommandContext {
|
|
639
|
+
app: Pick<Application, 'isProduction' | 'appPath'>;
|
|
640
|
+
container: Container;
|
|
641
|
+
}
|
|
642
|
+
/** Laravel's queue:work options from the parsed command options. */
|
|
643
|
+
declare function workerOptionsFrom(option: (name: string) => unknown): WorkerOptions;
|
|
644
|
+
/** The queue:* commands, registered as closure commands (PC1). */
|
|
645
|
+
declare function registerQueueCommands(kernel: ConsoleKernel, context: QueueCommandContext): void;
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Laravel 11's config/queue.php defaults (without env() lookups, which belong in the app's config/queue.ts).
|
|
649
|
+
* `connection`/`database` left undefined mean "the default database connection".
|
|
650
|
+
*/
|
|
651
|
+
declare function queueConfigDefaults(): Record<string, unknown>;
|
|
652
|
+
|
|
653
|
+
/** `Queue.push(job)`, `Queue.connection('redis').size()`, … on the bound QueueManager. */
|
|
654
|
+
declare const Queue: _orieljs_support.Facade<QueueManager$1, object>;
|
|
655
|
+
|
|
656
|
+
/** Builds the provider for `queue.failed.driver`: database-uuids / database (needs DatabaseManager) or null. */
|
|
657
|
+
declare function makeFailedJobProvider(container: Container): FailedJobProvider;
|
|
658
|
+
declare class QueueServiceProvider extends ServiceProvider {
|
|
659
|
+
register(): void;
|
|
660
|
+
boot(): void;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
export { BaseJob, Batch, type BatchAttributes, CHAIN, CallQueuedHandler, type ChainSpec, type ConnectionConfig, DatabaseBatchRepository, DatabaseFailedJobProvider, DatabaseJob, type DatabaseJobRecord, DatabaseQueue, type DatabaseQueueOptions, type Encoded, JOBS_TABLE_MIGRATION, JobClassNotFoundError, type JobContext, type JobMiddleware, JobSerializationError, ManuallyFailedError, MaxAttemptsExceededError, MissingModelError, NullFailedJobProvider, NullQueue, QUEUE_JOB, Queue, QueueBase, type QueueCommandContext, QueueError, type QueueJobPayload, QueueManager, QueueServiceProvider, RESTART_CACHE_KEY, type RedisConnectionConfig, RedisJob, RedisQueue, type RedisQueueOptions, type StoredBatchOptions, SyncJob, SyncQueue, TimeoutExceededError, UNIQUE_LOCK, type UniqueLockSpec, UnknownQueueConnectionError, UnknownQueueDriverError, WORKER_DISCOVERY_DIRECTORIES, WithoutOverlapping, Worker, type WorkerListener, type WorkerOptions, classNameOf, createPayload, decodePayload, defaultWorkerOptions, describeError, discoverClasses, discoverJobs, encodePayload, formatTimestamp, isModel, makeFailedJobProvider, makeRedisClient, normalizeBackoff, queueConfigDefaults, registerQueueCommands, resolveRedisConfig, restoreJob, restoreValue, serializeValue, workerOptionsFrom };
|