queue-jobs-worker 1.0.4 → 2.0.0

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.
Files changed (58) hide show
  1. package/README.md +754 -538
  2. package/dist/index.d.ts +546 -29
  3. package/dist/index.js +1279 -2410
  4. package/dist/scripts/claim-job.lua +117 -0
  5. package/dist/scripts/clear-queue.lua +42 -0
  6. package/dist/scripts/list-jobs.lua +50 -0
  7. package/dist/scripts/remove-job.lua +41 -0
  8. package/dist/scripts/save-job.lua +52 -0
  9. package/dist/scripts/update-job.lua +183 -0
  10. package/package.json +91 -110
  11. package/CHANGELOG.md +0 -160
  12. package/LICENSE +0 -21
  13. package/assets/queue-jobs-worker-demo.mp4 +0 -0
  14. package/assets/queue-jobs-worker-github.png +0 -0
  15. package/dist/core/backoff.d.ts +0 -24
  16. package/dist/core/backoff.d.ts.map +0 -1
  17. package/dist/core/client.d.ts +0 -95
  18. package/dist/core/client.d.ts.map +0 -1
  19. package/dist/core/id.d.ts +0 -9
  20. package/dist/core/id.d.ts.map +0 -1
  21. package/dist/core/index.d.ts +0 -7
  22. package/dist/core/index.d.ts.map +0 -1
  23. package/dist/core/job.d.ts +0 -75
  24. package/dist/core/job.d.ts.map +0 -1
  25. package/dist/core/queue.d.ts +0 -70
  26. package/dist/core/queue.d.ts.map +0 -1
  27. package/dist/core/worker.d.ts +0 -65
  28. package/dist/core/worker.d.ts.map +0 -1
  29. package/dist/events/emitter.d.ts +0 -24
  30. package/dist/events/emitter.d.ts.map +0 -1
  31. package/dist/index.cjs +0 -2529
  32. package/dist/index.cjs.map +0 -1
  33. package/dist/index.d.ts.map +0 -1
  34. package/dist/index.js.map +0 -1
  35. package/dist/storage/in-memory.adapter.d.ts +0 -33
  36. package/dist/storage/in-memory.adapter.d.ts.map +0 -1
  37. package/dist/storage/index.d.ts +0 -5
  38. package/dist/storage/index.d.ts.map +0 -1
  39. package/dist/storage/mysql.adapter.d.ts +0 -38
  40. package/dist/storage/mysql.adapter.d.ts.map +0 -1
  41. package/dist/storage/postgres.adapter.d.ts +0 -38
  42. package/dist/storage/postgres.adapter.d.ts.map +0 -1
  43. package/dist/storage/redis.adapter.d.ts +0 -45
  44. package/dist/storage/redis.adapter.d.ts.map +0 -1
  45. package/dist/types/client.types.d.ts +0 -41
  46. package/dist/types/client.types.d.ts.map +0 -1
  47. package/dist/types/events.types.d.ts +0 -22
  48. package/dist/types/events.types.d.ts.map +0 -1
  49. package/dist/types/index.d.ts +0 -10
  50. package/dist/types/index.d.ts.map +0 -1
  51. package/dist/types/job.types.d.ts +0 -97
  52. package/dist/types/job.types.d.ts.map +0 -1
  53. package/dist/types/queue.types.d.ts +0 -43
  54. package/dist/types/queue.types.d.ts.map +0 -1
  55. package/dist/types/storage.types.d.ts +0 -138
  56. package/dist/types/storage.types.d.ts.map +0 -1
  57. package/dist/types/worker.types.d.ts +0 -25
  58. package/dist/types/worker.types.d.ts.map +0 -1
@@ -1,70 +0,0 @@
1
- /**
2
- * Queue
3
- *
4
- * Represents an independent stream of jobs. Applications use a Queue to:
5
- * - Enqueue jobs (with optional scheduling, priority, retry config).
6
- * - Register processors for job types.
7
- * - Create workers that consume jobs from this queue.
8
- * - Query job state and counts.
9
- *
10
- * Queue-level configuration overrides client defaults.
11
- * Job-level options override queue configuration where applicable.
12
- */
13
- import type { StorageAdapter } from "../types/storage.types.js";
14
- import type { JobOptions, JobStatus } from "../types/job.types.js";
15
- import type { QueueOptions } from "../types/queue.types.js";
16
- import type { WorkerOptions, Processor } from "../types/worker.types.js";
17
- import type { ClientDefaults } from "../types/client.types.js";
18
- import { Job } from "./job.js";
19
- import { Worker } from "./worker.js";
20
- import type { QueueEventEmitter } from "../events/emitter.js";
21
- type ResolvedDefaults = Required<ClientDefaults>;
22
- export declare class Queue<TPayload = unknown> {
23
- /** The name of this queue — unique within a QueueClient. */
24
- readonly name: string;
25
- private readonly storage;
26
- private readonly emitter;
27
- private readonly resolvedConfig;
28
- private readonly clientDefaults;
29
- /** Registered processors keyed by job type. */
30
- private readonly processors;
31
- /** Active worker instances created by this queue. */
32
- private readonly workers;
33
- constructor(name: string, storage: StorageAdapter, emitter: QueueEventEmitter, options: QueueOptions | undefined, defaults: ResolvedDefaults);
34
- /**
35
- * Add a new job to the queue.
36
- *
37
- * @param type - Job type string, must match a registered processor.
38
- * @param payload - Arbitrary serialisable payload (never logged by default).
39
- * @param options - Per-job overrides (attempts, delay, priority, etc.).
40
- */
41
- enqueue(type: string, payload: TPayload, options?: JobOptions): Promise<Job<TPayload>>;
42
- /**
43
- * Register a processor function for a given job type.
44
- *
45
- * Only one processor per type per queue is supported.
46
- * Registering a second processor for the same type replaces the first.
47
- */
48
- process<P = TPayload>(type: string, processor: Processor<P>): void;
49
- /**
50
- * Create and start a Worker that consumes jobs from this queue.
51
- *
52
- * Worker-level `concurrency` overrides queue-level concurrency.
53
- */
54
- createWorker(options?: WorkerOptions): Worker;
55
- /** Fetch a single job by its ID. Returns null if not found or wrong queue. */
56
- getJob(jobId: string): Promise<Job<TPayload> | null>;
57
- /** Fetch jobs from this queue, optionally filtered by status. */
58
- getJobs(status?: JobStatus, limit?: number, offset?: number): Promise<Job<TPayload>[]>;
59
- /** Get job counts by status for this queue. */
60
- getJobCounts(): Promise<Record<JobStatus, number>>;
61
- /**
62
- * Gracefully stop all workers attached to this queue.
63
- * Called automatically by QueueClient.close().
64
- */
65
- close(): Promise<void>;
66
- /** Return all active worker instances on this queue. */
67
- getWorkers(): readonly Worker[];
68
- }
69
- export {};
70
- //# sourceMappingURL=queue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/core/queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EAAE,Y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IACH,YAAY,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IAoB7C,8EAA8E;IACxE,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;IAM1D,iEAAiE;IAC3D,OAAO,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,KAAK,SAAM,EAAE,MAAM,SAAI,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;IAUpF,+CAA+C;IACzC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAQxD;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,wDAAwD;IACxD,UAAU,IAAI,SAAS,MAAM,EAAE;CAGhC"}
@@ -1,65 +0,0 @@
1
- /**
2
- * Worker
3
- *
4
- * Claims jobs from a queue and executes registered processors concurrently.
5
- *
6
- * Key guarantees:
7
- * - At-most `concurrency` jobs run simultaneously.
8
- * - Job claiming is delegated to the storage adapter's atomic `claim()` call.
9
- * - Failed jobs are requeued (with backoff) or moved to the DLQ.
10
- * - Stalled jobs (expired locks from crashed workers) are recovered
11
- * periodically.
12
- * - Graceful shutdown: stop claiming, wait for active jobs, release any
13
- * locks that could not be finished within shutdownTimeout.
14
- * - Cron jobs: re-enqueued immediately after each successful execution.
15
- */
16
- import type { StorageAdapter } from "../types/storage.types.js";
17
- import type { WorkerOptions, WorkerStatus, Processor } from "../types/worker.types.js";
18
- import type { QueueOptions } from "../types/queue.types.js";
19
- import type { ClientDefaults } from "../types/client.types.js";
20
- import type { QueueEventEmitter } from "../events/emitter.js";
21
- type ResolvedDefaults = Required<ClientDefaults>;
22
- export declare class Worker {
23
- /** Unique identifier for this worker instance. */
24
- readonly id: string;
25
- private readonly queueName;
26
- private readonly storage;
27
- private readonly emitter;
28
- private readonly processors;
29
- private readonly config;
30
- private _status;
31
- private activeCount;
32
- /**
33
- * Tracks job IDs currently being processed so we can release their locks
34
- * when graceful shutdown times out before they finish.
35
- */
36
- private readonly activeJobIds;
37
- private pollTimer;
38
- private stalledTimer;
39
- /** Resolves when all active jobs finish during shutdown. */
40
- private drainResolve;
41
- constructor(queueName: string, storage: StorageAdapter, emitter: QueueEventEmitter, processors: Map<string, Processor<unknown>>, workerOptions: WorkerOptions, queueOptions: QueueOptions, defaults: ResolvedDefaults);
42
- get status(): WorkerStatus;
43
- /** Start polling for jobs. */
44
- start(): void;
45
- /**
46
- * Gracefully stop the worker.
47
- *
48
- * 1. Stop accepting new jobs.
49
- * 2. Wait up to `shutdownTimeout` ms for active jobs to finish.
50
- * 3. Release locks on any jobs that did not finish in time so another
51
- * worker can reclaim them.
52
- * 4. Emit stopped event.
53
- */
54
- stop(): Promise<void>;
55
- private schedulePoll;
56
- private poll;
57
- private claimNext;
58
- private executeJob;
59
- private enqueueCronNext;
60
- private handleFailure;
61
- private scheduleStallCheck;
62
- private recoverStalledJobs;
63
- }
64
- export {};
65
- //# sourceMappingURL=worker.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/core/worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACvF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAI/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,KAAK,gBAAgB,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC;AAyBjD,qBAAa,MAAM;IACjB,kDAAkD;IAClD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoB;IAC5C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAkC;IAC7D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;IAE1D,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,WAAW,CAAK;IAExB;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAqB;IAElD,OAAO,CAAC,SAAS,CAA+B;IAChD,OAAO,CAAC,YAAY,CAA+B;IAEnD,4DAA4D;IAC5D,OAAO,CAAC,YAAY,CAA6B;gBAG/C,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,iBAAiB,EAC1B,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC,EAC3C,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,YAAY,EAC1B,QAAQ,EAAE,gBAAgB;IAc5B,IAAI,MAAM,IAAI,YAAY,CAEzB;IAED,8BAA8B;IAC9B,KAAK,IAAI,IAAI;IAab;;;;;;;;OAQG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAqD3B,OAAO,CAAC,YAAY;YAQN,IAAI;YAsBJ,SAAS;YAyCT,UAAU;YAqFV,eAAe;YA4Df,aAAa;IA4C3B,OAAO,CAAC,kBAAkB;YAQZ,kBAAkB;CAkBjC"}
@@ -1,24 +0,0 @@
1
- /**
2
- * QueueEventEmitter
3
- *
4
- * A strongly-typed, lightweight event emitter built on top of Node.js
5
- * EventEmitter. All queue/worker/job lifecycle events flow through here.
6
- *
7
- * Using typed generics ensures that each event name maps to its exact
8
- * argument tuple — no runtime guessing.
9
- */
10
- import type { QueueEvents } from "../types/events.types.js";
11
- type EventKey = keyof QueueEvents;
12
- type EventArgs<K extends EventKey> = QueueEvents[K];
13
- export declare class QueueEventEmitter {
14
- private readonly emitter;
15
- constructor();
16
- on<K extends EventKey>(event: K, listener: (...args: EventArgs<K>) => void): this;
17
- once<K extends EventKey>(event: K, listener: (...args: EventArgs<K>) => void): this;
18
- off<K extends EventKey>(event: K, listener: (...args: EventArgs<K>) => void): this;
19
- emit<K extends EventKey>(event: K, ...args: EventArgs<K>): boolean;
20
- removeAllListeners(event?: EventKey): this;
21
- listenerCount(event: EventKey): number;
22
- }
23
- export {};
24
- //# sourceMappingURL=emitter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../src/events/emitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAM5D,KAAK,QAAQ,GAAG,MAAM,WAAW,CAAC;AAClC,KAAK,SAAS,CAAC,CAAC,SAAS,QAAQ,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAEpD,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;;IAavC,EAAE,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,IAAI;IAKjF,IAAI,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,IAAI;IAKnF,GAAG,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,IAAI;IASlF,IAAI,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO;IAQlE,kBAAkB,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,IAAI;IAS1C,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;CAGvC"}