@nest-batch/bullmq 0.2.0 → 0.2.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.
@@ -0,0 +1,441 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: Object.getOwnPropertyDescriptor(all, name).get
9
+ });
10
+ }
11
+ _export(exports, {
12
+ get BULLMQ_QUEUE_NAME () {
13
+ return BULLMQ_QUEUE_NAME;
14
+ },
15
+ get BULLMQ_STRATEGY_NAME () {
16
+ return BULLMQ_STRATEGY_NAME;
17
+ },
18
+ get BullmqRuntime () {
19
+ return BullmqRuntime;
20
+ }
21
+ });
22
+ const _common = require("@nestjs/common");
23
+ const _bullmq = require("bullmq");
24
+ const _core = require("@nest-batch/core");
25
+ const _moduleoptions = require("./module-options");
26
+ function _ts_decorate(decorators, target, key, desc) {
27
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
28
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
29
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
30
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
31
+ }
32
+ function _ts_metadata(k, v) {
33
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
34
+ }
35
+ function _ts_param(paramIndex, decorator) {
36
+ return function(target, key) {
37
+ decorator(target, key, paramIndex);
38
+ };
39
+ }
40
+ const BULLMQ_QUEUE_NAME = 'nest-batch-work';
41
+ const BULLMQ_STRATEGY_NAME = 'bullmq';
42
+ let BullmqRuntime = class BullmqRuntime {
43
+ options;
44
+ repository;
45
+ registry;
46
+ jobExecutor;
47
+ observer;
48
+ /**
49
+ * Strategy name. Distinct from the T17 stub's `'bullmq-stub'`
50
+ * so log lines and boundary reports can tell them apart.
51
+ */ name = BULLMQ_STRATEGY_NAME;
52
+ logger = new _common.Logger(BullmqRuntime.name);
53
+ /** BullMQ queue (producer side). */ queue = null;
54
+ /** BullMQ worker (consumer side). */ worker = null;
55
+ /** BullMQ QueueEvents stream listener. */ queueEvents = null;
56
+ /**
57
+ * Promise-chain lock for the close path. We capture the first
58
+ * `close()` invocation and short-circuit subsequent ones so a
59
+ * stray double-shutdown (Nest calls `OnApplicationShutdown`
60
+ * once, but tests sometimes do their own) does not race the
61
+ * in-flight close.
62
+ */ closePromise = null;
63
+ constructor(options, repository, registry, jobExecutor, observer = new _core.NoopBatchObserver()){
64
+ this.options = options;
65
+ this.repository = repository;
66
+ this.registry = registry;
67
+ this.jobExecutor = jobExecutor;
68
+ this.observer = observer;
69
+ }
70
+ /**
71
+ * Nest lifecycle: spin up the queue, worker, and queue-events
72
+ * after the DI container is fully wired. We do this in
73
+ * `onApplicationBootstrap` (not `onModuleInit`) so every other
74
+ * provider — including user-supplied `JobRepository` overrides —
75
+ * is already instantiated and injectable.
76
+ *
77
+ * Worker startup is gated on `options.autoStartWorker`. The
78
+ * flag exists for launcher-only deployments (e.g. an API
79
+ * service that only enqueues) and for tests that want to
80
+ * exercise the producer side in isolation. When the flag is
81
+ * `false` the queue is still created (so `launch()` can
82
+ * enqueue), but the worker is not started (no consumer means
83
+ * the jobs sit in the queue indefinitely).
84
+ */ onApplicationBootstrap() {
85
+ this.queue = this.buildQueue();
86
+ this.queueEvents = this.buildQueueEvents();
87
+ this.attachQueueEventsBridge();
88
+ if (this.options.autoStartWorker) {
89
+ this.worker = this.buildWorker();
90
+ this.logger.log(`BullmqRuntime started: queue="${BULLMQ_QUEUE_NAME}" ` + `worker=auto, keyPrefix="${this.options.connection.keyPrefix}"`);
91
+ } else {
92
+ this.logger.log(`BullmqRuntime started: queue="${BULLMQ_QUEUE_NAME}" ` + `worker=manual (autoStartWorker=false)`);
93
+ }
94
+ }
95
+ /**
96
+ * Nest lifecycle: close every BullMQ resource in the documented
97
+ * order — workers first (let in-flight jobs finish or be
98
+ * returned to the queue), then events (no new events can
99
+ * arrive once the worker is closed), then queues (the producer
100
+ * is closed last so any pending `add()` calls had a chance to
101
+ * land).
102
+ *
103
+ * Idempotent: a second call to `onApplicationShutdown` (which
104
+ * can happen in tests) short-circuits to the first close's
105
+ * promise rather than racing.
106
+ */ async onApplicationShutdown() {
107
+ if (this.closePromise !== null) {
108
+ return this.closePromise;
109
+ }
110
+ this.closePromise = this.close();
111
+ return this.closePromise;
112
+ }
113
+ // -----------------------------------------------------------------------
114
+ // IExecutionStrategy
115
+ // -----------------------------------------------------------------------
116
+ /**
117
+ * Enqueue a single BullMQ job per step. Returns
118
+ * `{ kind: 'enqueued', queueJobId }` after the producer has
119
+ * acknowledged the enqueue. The execution is fire-and-forget:
120
+ * the launcher resolves the latest persisted `JobExecution`
121
+ * (which is still in `STARTING`/`STARTED` because the executor
122
+ * has not run yet).
123
+ *
124
+ * The canonical `JobExecution` row is created by the launcher
125
+ * via `repository.createExecutionAtomic` BEFORE this method is
126
+ * called (the `executionId` in `ctx` is the result). This
127
+ * strategy does NOT re-create it; doing so would race the
128
+ * launcher's atomic create and break the `SELECT ... FOR
129
+ * UPDATE SKIP LOCKED` invariant.
130
+ *
131
+ * Throws if the producer cannot enqueue (Redis down, key
132
+ * collision, etc.). The launcher re-throws the error to its
133
+ * caller; the `JobExecution` row remains in `STARTING` —
134
+ * the host's recovery path (or a manual cleanup) is
135
+ * responsible for transitioning it.
136
+ */ async launch(job, _params, ctx) {
137
+ if (this.queue === null) {
138
+ throw new Error(`[BullmqRuntime] launch() called before onApplicationBootstrap — ` + 'module is not initialized. Did you forget to import BullmqBatchModule?');
139
+ }
140
+ // T8 (partition orchestration): when the start step declares
141
+ // `partitions.count >= 2`, the strategy enqueues one BullMQ job
142
+ // per partition (each carrying a distinct `partitionIndex`).
143
+ // Otherwise (default, `count === 1`, or absent) it preserves
144
+ // the 0.1.0 "one job per step" behaviour. The validate call
145
+ // surfaces a misconfiguration (e.g. `count <= 0`) at the
146
+ // launcher's boundary so the host's caller sees the failure
147
+ // before the worker is ever asked to process the job.
148
+ const stepId = job.startStepId;
149
+ const startStep = job.steps[stepId];
150
+ const partitions = startStep?.kind === 'chunk' ? startStep.partitions : undefined;
151
+ (0, _core.validatePartitions)(partitions);
152
+ const partitionCount = partitions?.count ?? 1;
153
+ const partitionOrdinals = partitionCount >= 2 ? Array.from({
154
+ length: partitionCount
155
+ }, (_, i)=>i) : [
156
+ undefined
157
+ ];
158
+ const jobOpts = {
159
+ attempts: 3,
160
+ backoff: {
161
+ type: 'exponential',
162
+ delay: 100,
163
+ jitter: 0.5
164
+ },
165
+ removeOnComplete: {
166
+ count: 100,
167
+ age: 3600
168
+ },
169
+ removeOnFail: {
170
+ count: 1000
171
+ }
172
+ };
173
+ let lastQueueJobId = null;
174
+ for (const partitionIndex of partitionOrdinals){
175
+ const payload = {
176
+ executionId: ctx.executionId,
177
+ jobExecutionId: ctx.jobExecutionId,
178
+ jobId: job.id,
179
+ stepId,
180
+ ...partitionIndex !== undefined ? {
181
+ partitionIndex
182
+ } : {}
183
+ };
184
+ const enqueued = await this.queue.add(stepId, payload, jobOpts);
185
+ if (enqueued.id === undefined) {
186
+ // BullMQ returns a job with `id` undefined only when the
187
+ // producer cannot reach Redis and the in-memory buffer
188
+ // (which is disabled by `enableOfflineQueue: false`) is
189
+ // not available. Surface this as a hard error so the
190
+ // launcher propagates the failure.
191
+ throw new Error(`[BullmqRuntime] enqueue returned undefined job id (Redis down?)`);
192
+ }
193
+ const qid = String(enqueued.id);
194
+ lastQueueJobId = qid;
195
+ this.logger.debug(`Enqueued step "${stepId}" for execution ${ctx.executionId}` + (partitionIndex !== undefined ? ` (partition ${partitionIndex}/${partitionCount})` : '') + ` as BullMQ job ${qid}`);
196
+ }
197
+ if (lastQueueJobId === null) {
198
+ // Defensive: the loop above always runs at least once
199
+ // (partitionOrdinals has length >= 1), so this branch is
200
+ // unreachable in practice. Keep the explicit throw so a
201
+ // future refactor cannot quietly enqueue zero jobs.
202
+ throw new Error(`[BullmqRuntime] enqueued zero jobs for execution ${ctx.executionId}`);
203
+ }
204
+ return {
205
+ kind: 'enqueued',
206
+ queueJobId: lastQueueJobId
207
+ };
208
+ }
209
+ // -----------------------------------------------------------------------
210
+ // Construction
211
+ // -----------------------------------------------------------------------
212
+ buildQueue() {
213
+ return new _bullmq.Queue(BULLMQ_QUEUE_NAME, {
214
+ connection: this.producerConnectionOptions(),
215
+ // `defaultJobOptions` is a defence-in-depth measure. The
216
+ // strategy already passes per-call `JobsOptions` (with
217
+ // the T18 retry / remove policy) so this is the fallback
218
+ // for any code path that calls `queue.add` without
219
+ // explicit options. Today the only caller is the strategy.
220
+ defaultJobOptions: {
221
+ attempts: 3,
222
+ backoff: {
223
+ type: 'exponential',
224
+ delay: 100,
225
+ jitter: 0.5
226
+ },
227
+ removeOnComplete: {
228
+ count: 100,
229
+ age: 3600
230
+ },
231
+ removeOnFail: {
232
+ count: 1000
233
+ }
234
+ },
235
+ prefix: this.options.connection.keyPrefix,
236
+ // Skip waiting for the producer connection to become ready
237
+ // before returning from `add`. The fail-fast producer
238
+ // options (see `producerConnectionOptions`) make a dead
239
+ // Redis surface as a synchronous error on the first `add`,
240
+ // which is exactly what the "Redis-down" test asserts.
241
+ skipWaitingForReady: true,
242
+ // BullMQ 5 calls `client.info()` to discover the server
243
+ // version + database type at `Queue` construction time. With
244
+ // `enableOfflineQueue: false` and the ioredis client not
245
+ // yet ready, the call throws `Stream isn't writeable`.
246
+ // `skipVersionCheck: true` short-circuits that probe — the
247
+ // strategy never depends on the version, and a dead Redis
248
+ // still surfaces synchronously on the first `add()` (per
249
+ // the fail-fast contract above).
250
+ skipVersionCheck: true
251
+ });
252
+ }
253
+ buildWorker() {
254
+ return new _bullmq.Worker(BULLMQ_QUEUE_NAME, async (job)=>this.processJob(job.data), {
255
+ connection: this.workerConnectionOptions(),
256
+ prefix: this.options.connection.keyPrefix,
257
+ concurrency: 1
258
+ });
259
+ }
260
+ buildQueueEvents() {
261
+ return new _bullmq.QueueEvents(BULLMQ_QUEUE_NAME, {
262
+ connection: this.workerConnectionOptions(),
263
+ prefix: this.options.connection.keyPrefix
264
+ });
265
+ }
266
+ /**
267
+ * Wire the `QueueEvents` listeners to the configured
268
+ * `BatchObserver`. Each listener swallows observer errors so
269
+ * a slow / failing observer cannot poison the BullMQ event
270
+ * stream.
271
+ */ attachQueueEventsBridge() {
272
+ if (this.queueEvents === null) return;
273
+ this.queueEvents.on('completed', ({ jobId })=>{
274
+ void this.bridgeEvent(_core.BATCH_EVENT.JOB_COMPLETED, {
275
+ queueJobId: jobId,
276
+ kind: 'completed'
277
+ });
278
+ });
279
+ this.queueEvents.on('failed', ({ jobId, failedReason })=>{
280
+ void this.bridgeEvent(_core.BATCH_EVENT.JOB_FAILED, {
281
+ queueJobId: jobId,
282
+ kind: 'failed',
283
+ reason: failedReason
284
+ });
285
+ });
286
+ this.queueEvents.on('stalled', ({ jobId })=>{
287
+ void this.bridgeEvent(_core.BATCH_EVENT.JOB_FAILED, {
288
+ queueJobId: jobId,
289
+ kind: 'stalled'
290
+ });
291
+ });
292
+ }
293
+ async bridgeEvent(type, data) {
294
+ try {
295
+ await this.observer.onEvent({
296
+ type,
297
+ timestamp: new Date(),
298
+ jobExecutionId: String(data['queueJobId'] ?? '<unknown>'),
299
+ data: data
300
+ });
301
+ } catch (err) {
302
+ this.logger.warn(`BatchObserver threw on event ${type}: ${err instanceof Error ? err.message : String(err)}`);
303
+ }
304
+ }
305
+ // -----------------------------------------------------------------------
306
+ // Worker processor — delegated to JobExecutor
307
+ // -----------------------------------------------------------------------
308
+ /**
309
+ * Worker entry point. Loads the canonical `JobExecution` from
310
+ * the repository and the `JobDefinition` from the registry, then
311
+ * hands the work to `JobExecutor.execute`. All batch semantics
312
+ * (step dispatch, chunk loop, skip/retry, checkpoint) live in
313
+ * the executor — this method is a thin bridge.
314
+ */ async processJob(payload) {
315
+ const execution = await this.repository.getJobExecution(payload.executionId);
316
+ if (execution === null) {
317
+ // The DB row is gone. The launcher pre-created it via
318
+ // `createExecutionAtomic`; if it's missing now, the host
319
+ // either deleted it or restored a DB without the row.
320
+ // Surface as a BullMQ-level failure so the technical
321
+ // retry / dead-letter path handles it.
322
+ throw new Error(`[BullmqRuntime] JobExecution ${payload.executionId} not found in repository`);
323
+ }
324
+ const jobDef = this.registry.get(payload.jobId);
325
+ // `JobRegistry.get` throws `JobNotFoundError` if the
326
+ // definition is missing. We let it propagate so BullMQ
327
+ // records the failure and the dead-letter queue catches
328
+ // it (a missing job definition is a misconfiguration that
329
+ // should be loud, not silent).
330
+ await this.jobExecutor.execute(execution, jobDef);
331
+ }
332
+ // -----------------------------------------------------------------------
333
+ // Connection options
334
+ // -----------------------------------------------------------------------
335
+ /**
336
+ * Producer-side connection tuning. The two flags below are
337
+ * the contract the T18 "Redis-down" test depends on:
338
+ *
339
+ * - `enableOfflineQueue: false` — a `Queue.add()` against a
340
+ * dead Redis MUST throw synchronously rather than buffer
341
+ * the command. Without this, BullMQ keeps the command in
342
+ * memory and `add()` returns success, breaking the
343
+ * "fail fast" guarantee.
344
+ * - `maxRetriesPerRequest: 1` — keep the first `add`
345
+ * fast; subsequent reconnects are handled by ioredis
346
+ * itself (we do not want BullMQ to block on retries
347
+ * during the launcher call).
348
+ *
349
+ * BullMQ specifically warns against `maxRetriesPerRequest: null`
350
+ * on the producer, because the producer does not use blocking
351
+ * commands. We use `1` for the same reason.
352
+ */ producerConnectionOptions() {
353
+ return {
354
+ host: this.options.connection.host,
355
+ port: this.options.connection.port,
356
+ password: this.options.connection.password,
357
+ username: this.options.connection.username,
358
+ db: this.options.connection.db,
359
+ ...this.options.connection.tls ? {
360
+ tls: true
361
+ } : {},
362
+ enableOfflineQueue: false,
363
+ maxRetriesPerRequest: 1
364
+ };
365
+ }
366
+ /**
367
+ * Worker-side connection tuning. Two flags that BullMQ
368
+ * *requires* for blocking workers (per the BullMQ docs):
369
+ *
370
+ * - `maxRetriesPerRequest: null` — the worker's
371
+ * `BLPOP` / `BRPOPLPUSH` / `XREADGROUP` commands MUST NOT
372
+ * retry per request. A stalled worker surfaces as a
373
+ * stall, not a connection error.
374
+ * - `enableReadyCheck: false` — the worker should not
375
+ * refuse to start when Redis is in the middle of a
376
+ * failover; ioredis reconnects on its own.
377
+ */ workerConnectionOptions() {
378
+ return {
379
+ host: this.options.connection.host,
380
+ port: this.options.connection.port,
381
+ password: this.options.connection.password,
382
+ username: this.options.connection.username,
383
+ db: this.options.connection.db,
384
+ ...this.options.connection.tls ? {
385
+ tls: true
386
+ } : {},
387
+ maxRetriesPerRequest: null,
388
+ enableReadyCheck: false
389
+ };
390
+ }
391
+ // -----------------------------------------------------------------------
392
+ // Close
393
+ // -----------------------------------------------------------------------
394
+ /**
395
+ * Close all BullMQ resources in the documented order:
396
+ * worker → events → queue. Each step is best-effort: a close
397
+ * error on one resource does not prevent the others from
398
+ * being closed.
399
+ */ async close() {
400
+ if (this.worker !== null) {
401
+ try {
402
+ await this.worker.close();
403
+ } catch (err) {
404
+ this.logger.warn(`Worker close failed: ${err instanceof Error ? err.message : String(err)}`);
405
+ }
406
+ this.worker = null;
407
+ }
408
+ if (this.queueEvents !== null) {
409
+ try {
410
+ await this.queueEvents.close();
411
+ } catch (err) {
412
+ this.logger.warn(`QueueEvents close failed: ${err instanceof Error ? err.message : String(err)}`);
413
+ }
414
+ this.queueEvents = null;
415
+ }
416
+ if (this.queue !== null) {
417
+ try {
418
+ await this.queue.close();
419
+ } catch (err) {
420
+ this.logger.warn(`Queue close failed: ${err instanceof Error ? err.message : String(err)}`);
421
+ }
422
+ this.queue = null;
423
+ }
424
+ }
425
+ };
426
+ BullmqRuntime = _ts_decorate([
427
+ (0, _common.Injectable)(),
428
+ _ts_param(0, (0, _common.Inject)(_moduleoptions.BULLMQ_MODULE_OPTIONS)),
429
+ _ts_param(1, (0, _common.Inject)(_core.JOB_REPOSITORY_TOKEN)),
430
+ _ts_param(4, (0, _common.Optional)()),
431
+ _ts_metadata("design:type", Function),
432
+ _ts_metadata("design:paramtypes", [
433
+ typeof ResolvedBullMqModuleOptions === "undefined" ? Object : ResolvedBullMqModuleOptions,
434
+ typeof JobRepository === "undefined" ? Object : JobRepository,
435
+ typeof _core.JobRegistry === "undefined" ? Object : _core.JobRegistry,
436
+ typeof _core.JobExecutor === "undefined" ? Object : _core.JobExecutor,
437
+ typeof BatchObserver === "undefined" ? Object : BatchObserver
438
+ ])
439
+ ], BullmqRuntime);
440
+
441
+ //# sourceMappingURL=bullmq-runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/bullmq-runtime.ts"],"sourcesContent":["import {\n Inject,\n Injectable,\n Logger,\n OnApplicationBootstrap,\n OnApplicationShutdown,\n Optional,\n} from '@nestjs/common';\nimport { Queue, QueueEvents, Worker, type JobsOptions } from 'bullmq';\n\nimport {\n type IExecutionStrategy,\n type JobDefinition,\n type BatchObserver,\n type JsonValue,\n type JobRepository,\n JOB_REPOSITORY_TOKEN,\n enforcePartitionIndex,\n validatePartitions,\n} from '@nest-batch/core';\nimport { JobExecutor, JobRegistry, NoopBatchObserver, BATCH_EVENT } from '@nest-batch/core';\n\nimport { BULLMQ_MODULE_OPTIONS, type ResolvedBullMqModuleOptions } from './module-options';\n\n/**\n * Payload shape stored in a BullMQ job's `data` field.\n *\n * The strategy enqueues one BullMQ job per step (or per partition,\n * in a future enhancement). The worker reconstructs the\n * `JobExecution` from the repository via `executionId` and the\n * `JobDefinition` from the registry via `jobId`.\n *\n * Why not store the full `JobDefinition` in the payload?\n * - IR is mutable across the host process (decorators / builders\n * may swap providers in tests, hot-reload, etc.). The\n * repository + registry are the canonical sources; the\n * payload carries only the keys needed to look them up.\n * - Storage size — IRs can be large (listeners, resolvers).\n * Redis is transport, not cache; small payloads are cheaper.\n */\nexport interface BullmqJobPayload {\n /** JobExecution id, used to load the canonical execution row. */\n readonly executionId: string;\n /** Mirrors `executionId` today; kept distinct for forward compat. */\n readonly jobExecutionId: string;\n /** JobDefinition id, used to look up the IR from the registry. */\n readonly jobId: string;\n /** Step id (the `name` field of the BullMQ job). */\n readonly stepId: string;\n /**\n * Partition index. Reserved for a future enhancement where a\n * chunk step is split into N partitions and enqueued as N\n * BullMQ jobs. Today the strategy always enqueues one job\n * per step (regardless of chunk size), so the field is\n * `undefined`. Kept in the payload shape so the worker\n * can distinguish \"this is a step\" from \"this is a partition\"\n * without a separate discriminator.\n */\n readonly partitionIndex?: number;\n}\n\n/**\n * The single BullMQ queue name used by the strategy + worker +\n * queue-events. We deliberately do not fan out into per-step\n * queues — that would force the host to pre-declare every step\n * name at compile time, which is at odds with the decorator /\n * builder APIs that discover steps at runtime. A single queue\n * keyed by the step's `name` field is the standard BullMQ pattern\n * (the `name` field discriminates the work).\n *\n * BullMQ 5 rejects queue names that contain a colon (`:`) because\n * it is the path separator in the Redis key layout. We use a\n * hyphen-separated name accordingly.\n */\nexport const BULLMQ_QUEUE_NAME = 'nest-batch-work';\n\n/**\n * Name of the BullMQ strategy. Logged by the bridge for diagnostic\n * purposes and asserted by tests that need to distinguish the\n * real implementation from the T17 stub.\n */\nexport const BULLMQ_STRATEGY_NAME = 'bullmq';\n\n/**\n * Bridge between the BullMQ `Queue` / `Worker` / `QueueEvents` and\n * the `@nest-batch/core` execution pipeline.\n *\n * Responsibilities (T18 contract):\n * 1. Own the producer / worker connection clients with the\n * role-specific tuning (fail-fast producer, blocking worker).\n * 2. Implement the `IExecutionStrategy` contract: `launch()`\n * enqueues a single BullMQ job per step, returns\n * `{ kind: 'enqueued', queueJobId }`. The launch is\n * fire-and-forget — the strategy does NOT block on the\n * worker.\n * 3. Drive the worker lifecycle (`OnApplicationBootstrap` /\n * `OnApplicationShutdown`).\n * 4. Bridge `QueueEvents` `completed` / `failed` / `stalled`\n * into the `BatchObserver` (defaulting to `NoopBatchObserver`).\n * 5. Hand off to `JobExecutor.execute(execution, jobDef)` from\n * inside the worker — Batch Core remains the source of truth\n * for state transitions, skip/retry, checkpoint, restart.\n *\n * Why a single class (not separate `Queue` / `Worker` providers)?\n * - The producer and worker share a `connection` record but\n * carry *different* `ConnectionOptions` (different\n * `maxRetriesPerRequest`, `enableReadyCheck`, ...). Splitting\n * them across providers would force the connection-tuning\n * logic into two places and risk the worker accidentally\n * inheriting the producer's fail-fast config (or vice versa).\n * - Lifecycle is a unit: open producer + worker + events\n * together, close them together in the documented order\n * (workers first, then events, then queues). Centralising\n * this in one class makes the close-order a single source\n * of truth and a single method (`close()`).\n */\n@Injectable()\nexport class BullmqRuntime\n implements IExecutionStrategy, OnApplicationBootstrap, OnApplicationShutdown\n{\n /**\n * Strategy name. Distinct from the T17 stub's `'bullmq-stub'`\n * so log lines and boundary reports can tell them apart.\n */\n readonly name = BULLMQ_STRATEGY_NAME;\n\n private readonly logger = new Logger(BullmqRuntime.name);\n\n /** BullMQ queue (producer side). */\n private queue: Queue | null = null;\n /** BullMQ worker (consumer side). */\n private worker: Worker<BullmqJobPayload> | null = null;\n /** BullMQ QueueEvents stream listener. */\n private queueEvents: QueueEvents | null = null;\n /**\n * Promise-chain lock for the close path. We capture the first\n * `close()` invocation and short-circuit subsequent ones so a\n * stray double-shutdown (Nest calls `OnApplicationShutdown`\n * once, but tests sometimes do their own) does not race the\n * in-flight close.\n */\n private closePromise: Promise<void> | null = null;\n\n constructor(\n @Inject(BULLMQ_MODULE_OPTIONS)\n private readonly options: ResolvedBullMqModuleOptions,\n @Inject(JOB_REPOSITORY_TOKEN)\n private readonly repository: JobRepository,\n private readonly registry: JobRegistry,\n private readonly jobExecutor: JobExecutor,\n @Optional()\n private readonly observer: BatchObserver = new NoopBatchObserver() as BatchObserver,\n ) {}\n\n /**\n * Nest lifecycle: spin up the queue, worker, and queue-events\n * after the DI container is fully wired. We do this in\n * `onApplicationBootstrap` (not `onModuleInit`) so every other\n * provider — including user-supplied `JobRepository` overrides —\n * is already instantiated and injectable.\n *\n * Worker startup is gated on `options.autoStartWorker`. The\n * flag exists for launcher-only deployments (e.g. an API\n * service that only enqueues) and for tests that want to\n * exercise the producer side in isolation. When the flag is\n * `false` the queue is still created (so `launch()` can\n * enqueue), but the worker is not started (no consumer means\n * the jobs sit in the queue indefinitely).\n */\n onApplicationBootstrap(): void {\n this.queue = this.buildQueue();\n this.queueEvents = this.buildQueueEvents();\n this.attachQueueEventsBridge();\n\n if (this.options.autoStartWorker) {\n this.worker = this.buildWorker();\n this.logger.log(\n `BullmqRuntime started: queue=\"${BULLMQ_QUEUE_NAME}\" ` +\n `worker=auto, keyPrefix=\"${this.options.connection.keyPrefix}\"`,\n );\n } else {\n this.logger.log(\n `BullmqRuntime started: queue=\"${BULLMQ_QUEUE_NAME}\" ` +\n `worker=manual (autoStartWorker=false)`,\n );\n }\n }\n\n /**\n * Nest lifecycle: close every BullMQ resource in the documented\n * order — workers first (let in-flight jobs finish or be\n * returned to the queue), then events (no new events can\n * arrive once the worker is closed), then queues (the producer\n * is closed last so any pending `add()` calls had a chance to\n * land).\n *\n * Idempotent: a second call to `onApplicationShutdown` (which\n * can happen in tests) short-circuits to the first close's\n * promise rather than racing.\n */\n async onApplicationShutdown(): Promise<void> {\n if (this.closePromise !== null) {\n return this.closePromise;\n }\n this.closePromise = this.close();\n return this.closePromise;\n }\n\n // -----------------------------------------------------------------------\n // IExecutionStrategy\n // -----------------------------------------------------------------------\n\n /**\n * Enqueue a single BullMQ job per step. Returns\n * `{ kind: 'enqueued', queueJobId }` after the producer has\n * acknowledged the enqueue. The execution is fire-and-forget:\n * the launcher resolves the latest persisted `JobExecution`\n * (which is still in `STARTING`/`STARTED` because the executor\n * has not run yet).\n *\n * The canonical `JobExecution` row is created by the launcher\n * via `repository.createExecutionAtomic` BEFORE this method is\n * called (the `executionId` in `ctx` is the result). This\n * strategy does NOT re-create it; doing so would race the\n * launcher's atomic create and break the `SELECT ... FOR\n * UPDATE SKIP LOCKED` invariant.\n *\n * Throws if the producer cannot enqueue (Redis down, key\n * collision, etc.). The launcher re-throws the error to its\n * caller; the `JobExecution` row remains in `STARTING` —\n * the host's recovery path (or a manual cleanup) is\n * responsible for transitioning it.\n */\n async launch(\n job: JobDefinition,\n _params: Record<string, unknown>,\n ctx: { executionId: string; jobExecutionId: string },\n ): Promise<{ kind: 'enqueued'; queueJobId: string }> {\n if (this.queue === null) {\n throw new Error(\n `[BullmqRuntime] launch() called before onApplicationBootstrap — ` +\n 'module is not initialized. Did you forget to import BullmqBatchModule?',\n );\n }\n // T8 (partition orchestration): when the start step declares\n // `partitions.count >= 2`, the strategy enqueues one BullMQ job\n // per partition (each carrying a distinct `partitionIndex`).\n // Otherwise (default, `count === 1`, or absent) it preserves\n // the 0.1.0 \"one job per step\" behaviour. The validate call\n // surfaces a misconfiguration (e.g. `count <= 0`) at the\n // launcher's boundary so the host's caller sees the failure\n // before the worker is ever asked to process the job.\n const stepId = job.startStepId;\n const startStep = job.steps[stepId];\n const partitions = startStep?.kind === 'chunk' ? startStep.partitions : undefined;\n validatePartitions(partitions);\n const partitionCount = partitions?.count ?? 1;\n const partitionOrdinals: Array<number | undefined> =\n partitionCount >= 2 ? Array.from({ length: partitionCount }, (_, i) => i) : [undefined];\n\n const jobOpts: JobsOptions = {\n attempts: 3,\n backoff: { type: 'exponential', delay: 100, jitter: 0.5 },\n removeOnComplete: { count: 100, age: 3600 },\n removeOnFail: { count: 1000 },\n };\n\n let lastQueueJobId: string | null = null;\n for (const partitionIndex of partitionOrdinals) {\n const payload: BullmqJobPayload = {\n executionId: ctx.executionId,\n jobExecutionId: ctx.jobExecutionId,\n jobId: job.id,\n stepId,\n ...(partitionIndex !== undefined ? { partitionIndex } : {}),\n };\n const enqueued = await this.queue.add(stepId, payload, jobOpts);\n if (enqueued.id === undefined) {\n // BullMQ returns a job with `id` undefined only when the\n // producer cannot reach Redis and the in-memory buffer\n // (which is disabled by `enableOfflineQueue: false`) is\n // not available. Surface this as a hard error so the\n // launcher propagates the failure.\n throw new Error(`[BullmqRuntime] enqueue returned undefined job id (Redis down?)`);\n }\n const qid = String(enqueued.id);\n lastQueueJobId = qid;\n this.logger.debug(\n `Enqueued step \"${stepId}\" for execution ${ctx.executionId}` +\n (partitionIndex !== undefined ? ` (partition ${partitionIndex}/${partitionCount})` : '') +\n ` as BullMQ job ${qid}`,\n );\n }\n if (lastQueueJobId === null) {\n // Defensive: the loop above always runs at least once\n // (partitionOrdinals has length >= 1), so this branch is\n // unreachable in practice. Keep the explicit throw so a\n // future refactor cannot quietly enqueue zero jobs.\n throw new Error(`[BullmqRuntime] enqueued zero jobs for execution ${ctx.executionId}`);\n }\n return { kind: 'enqueued', queueJobId: lastQueueJobId };\n }\n\n // -----------------------------------------------------------------------\n // Construction\n // -----------------------------------------------------------------------\n\n private buildQueue(): Queue {\n return new Queue(BULLMQ_QUEUE_NAME, {\n connection: this.producerConnectionOptions(),\n // `defaultJobOptions` is a defence-in-depth measure. The\n // strategy already passes per-call `JobsOptions` (with\n // the T18 retry / remove policy) so this is the fallback\n // for any code path that calls `queue.add` without\n // explicit options. Today the only caller is the strategy.\n defaultJobOptions: {\n attempts: 3,\n backoff: { type: 'exponential', delay: 100, jitter: 0.5 },\n removeOnComplete: { count: 100, age: 3600 },\n removeOnFail: { count: 1000 },\n },\n prefix: this.options.connection.keyPrefix,\n // Skip waiting for the producer connection to become ready\n // before returning from `add`. The fail-fast producer\n // options (see `producerConnectionOptions`) make a dead\n // Redis surface as a synchronous error on the first `add`,\n // which is exactly what the \"Redis-down\" test asserts.\n skipWaitingForReady: true,\n // BullMQ 5 calls `client.info()` to discover the server\n // version + database type at `Queue` construction time. With\n // `enableOfflineQueue: false` and the ioredis client not\n // yet ready, the call throws `Stream isn't writeable`.\n // `skipVersionCheck: true` short-circuits that probe — the\n // strategy never depends on the version, and a dead Redis\n // still surfaces synchronously on the first `add()` (per\n // the fail-fast contract above).\n skipVersionCheck: true,\n });\n }\n\n private buildWorker(): Worker<BullmqJobPayload> {\n return new Worker<BullmqJobPayload>(\n BULLMQ_QUEUE_NAME,\n async (job) => this.processJob(job.data),\n {\n connection: this.workerConnectionOptions(),\n prefix: this.options.connection.keyPrefix,\n concurrency: 1,\n },\n );\n }\n\n private buildQueueEvents(): QueueEvents {\n return new QueueEvents(BULLMQ_QUEUE_NAME, {\n connection: this.workerConnectionOptions(),\n prefix: this.options.connection.keyPrefix,\n });\n }\n\n /**\n * Wire the `QueueEvents` listeners to the configured\n * `BatchObserver`. Each listener swallows observer errors so\n * a slow / failing observer cannot poison the BullMQ event\n * stream.\n */\n private attachQueueEventsBridge(): void {\n if (this.queueEvents === null) return;\n this.queueEvents.on('completed', ({ jobId }) => {\n void this.bridgeEvent(BATCH_EVENT.JOB_COMPLETED, { queueJobId: jobId, kind: 'completed' });\n });\n this.queueEvents.on('failed', ({ jobId, failedReason }) => {\n void this.bridgeEvent(BATCH_EVENT.JOB_FAILED, {\n queueJobId: jobId,\n kind: 'failed',\n reason: failedReason,\n });\n });\n this.queueEvents.on('stalled', ({ jobId }) => {\n void this.bridgeEvent(BATCH_EVENT.JOB_FAILED, { queueJobId: jobId, kind: 'stalled' });\n });\n }\n\n private async bridgeEvent(\n type: (typeof BATCH_EVENT)[keyof typeof BATCH_EVENT],\n data: Record<string, unknown>,\n ): Promise<void> {\n try {\n await this.observer.onEvent({\n type,\n timestamp: new Date(),\n jobExecutionId: String(data['queueJobId'] ?? '<unknown>'),\n data: data as unknown as JsonValue,\n });\n } catch (err) {\n this.logger.warn(\n `BatchObserver threw on event ${type}: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n\n // -----------------------------------------------------------------------\n // Worker processor — delegated to JobExecutor\n // -----------------------------------------------------------------------\n\n /**\n * Worker entry point. Loads the canonical `JobExecution` from\n * the repository and the `JobDefinition` from the registry, then\n * hands the work to `JobExecutor.execute`. All batch semantics\n * (step dispatch, chunk loop, skip/retry, checkpoint) live in\n * the executor — this method is a thin bridge.\n */\n private async processJob(payload: BullmqJobPayload): Promise<void> {\n const execution = await this.repository.getJobExecution(payload.executionId);\n if (execution === null) {\n // The DB row is gone. The launcher pre-created it via\n // `createExecutionAtomic`; if it's missing now, the host\n // either deleted it or restored a DB without the row.\n // Surface as a BullMQ-level failure so the technical\n // retry / dead-letter path handles it.\n throw new Error(\n `[BullmqRuntime] JobExecution ${payload.executionId} not found in repository`,\n );\n }\n const jobDef = this.registry.get(payload.jobId);\n // `JobRegistry.get` throws `JobNotFoundError` if the\n // definition is missing. We let it propagate so BullMQ\n // records the failure and the dead-letter queue catches\n // it (a missing job definition is a misconfiguration that\n // should be loud, not silent).\n await this.jobExecutor.execute(execution, jobDef);\n }\n\n // -----------------------------------------------------------------------\n // Connection options\n // -----------------------------------------------------------------------\n\n /**\n * Producer-side connection tuning. The two flags below are\n * the contract the T18 \"Redis-down\" test depends on:\n *\n * - `enableOfflineQueue: false` — a `Queue.add()` against a\n * dead Redis MUST throw synchronously rather than buffer\n * the command. Without this, BullMQ keeps the command in\n * memory and `add()` returns success, breaking the\n * \"fail fast\" guarantee.\n * - `maxRetriesPerRequest: 1` — keep the first `add`\n * fast; subsequent reconnects are handled by ioredis\n * itself (we do not want BullMQ to block on retries\n * during the launcher call).\n *\n * BullMQ specifically warns against `maxRetriesPerRequest: null`\n * on the producer, because the producer does not use blocking\n * commands. We use `1` for the same reason.\n */\n private producerConnectionOptions(): Record<string, unknown> {\n return {\n host: this.options.connection.host,\n port: this.options.connection.port,\n password: this.options.connection.password,\n username: this.options.connection.username,\n db: this.options.connection.db,\n ...(this.options.connection.tls ? { tls: true } : {}),\n enableOfflineQueue: false,\n maxRetriesPerRequest: 1,\n };\n }\n\n /**\n * Worker-side connection tuning. Two flags that BullMQ\n * *requires* for blocking workers (per the BullMQ docs):\n *\n * - `maxRetriesPerRequest: null` — the worker's\n * `BLPOP` / `BRPOPLPUSH` / `XREADGROUP` commands MUST NOT\n * retry per request. A stalled worker surfaces as a\n * stall, not a connection error.\n * - `enableReadyCheck: false` — the worker should not\n * refuse to start when Redis is in the middle of a\n * failover; ioredis reconnects on its own.\n */\n private workerConnectionOptions(): Record<string, unknown> {\n return {\n host: this.options.connection.host,\n port: this.options.connection.port,\n password: this.options.connection.password,\n username: this.options.connection.username,\n db: this.options.connection.db,\n ...(this.options.connection.tls ? { tls: true } : {}),\n maxRetriesPerRequest: null,\n enableReadyCheck: false,\n };\n }\n\n // -----------------------------------------------------------------------\n // Close\n // -----------------------------------------------------------------------\n\n /**\n * Close all BullMQ resources in the documented order:\n * worker → events → queue. Each step is best-effort: a close\n * error on one resource does not prevent the others from\n * being closed.\n */\n private async close(): Promise<void> {\n if (this.worker !== null) {\n try {\n await this.worker.close();\n } catch (err) {\n this.logger.warn(\n `Worker close failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n this.worker = null;\n }\n if (this.queueEvents !== null) {\n try {\n await this.queueEvents.close();\n } catch (err) {\n this.logger.warn(\n `QueueEvents close failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n this.queueEvents = null;\n }\n if (this.queue !== null) {\n try {\n await this.queue.close();\n } catch (err) {\n this.logger.warn(`Queue close failed: ${err instanceof Error ? err.message : String(err)}`);\n }\n this.queue = null;\n }\n }\n}\n"],"names":["BULLMQ_QUEUE_NAME","BULLMQ_STRATEGY_NAME","BullmqRuntime","name","logger","Logger","queue","worker","queueEvents","closePromise","options","repository","registry","jobExecutor","observer","NoopBatchObserver","onApplicationBootstrap","buildQueue","buildQueueEvents","attachQueueEventsBridge","autoStartWorker","buildWorker","log","connection","keyPrefix","onApplicationShutdown","close","launch","job","_params","ctx","Error","stepId","startStepId","startStep","steps","partitions","kind","undefined","validatePartitions","partitionCount","count","partitionOrdinals","Array","from","length","_","i","jobOpts","attempts","backoff","type","delay","jitter","removeOnComplete","age","removeOnFail","lastQueueJobId","partitionIndex","payload","executionId","jobExecutionId","jobId","id","enqueued","add","qid","String","debug","queueJobId","Queue","producerConnectionOptions","defaultJobOptions","prefix","skipWaitingForReady","skipVersionCheck","Worker","processJob","data","workerConnectionOptions","concurrency","QueueEvents","on","bridgeEvent","BATCH_EVENT","JOB_COMPLETED","failedReason","JOB_FAILED","reason","onEvent","timestamp","Date","err","warn","message","execution","getJobExecution","jobDef","get","execute","host","port","password","username","db","tls","enableOfflineQueue","maxRetriesPerRequest","enableReadyCheck"],"mappings":";;;;;;;;;;;QA0EaA;eAAAA;;QAOAC;eAAAA;;QAoCAC;eAAAA;;;wBA9GN;wBACsD;sBAWtD;+BAGiE;;;;;;;;;;;;;;;AAoDjE,MAAMF,oBAAoB;AAO1B,MAAMC,uBAAuB;AAoC7B,IAAA,AAAMC,gBAAN,MAAMA;;;;;;IAGX;;;GAGC,GACD,AAASC,OAAOF,qBAAqB;IAEpBG,SAAS,IAAIC,cAAM,CAACH,cAAcC,IAAI,EAAE;IAEzD,kCAAkC,GAClC,AAAQG,QAAsB,KAAK;IACnC,mCAAmC,GACnC,AAAQC,SAA0C,KAAK;IACvD,wCAAwC,GACxC,AAAQC,cAAkC,KAAK;IAC/C;;;;;;GAMC,GACD,AAAQC,eAAqC,KAAK;IAElD,YACE,AACiBC,OAAoC,EACrD,AACiBC,UAAyB,EAC1C,AAAiBC,QAAqB,EACtC,AAAiBC,WAAwB,EACzC,AACiBC,WAA0B,IAAIC,uBAAiB,EAAmB,CACnF;aAPiBL,UAAAA;aAEAC,aAAAA;aACAC,WAAAA;aACAC,cAAAA;aAEAC,WAAAA;IAChB;IAEH;;;;;;;;;;;;;;GAcC,GACDE,yBAA+B;QAC7B,IAAI,CAACV,KAAK,GAAG,IAAI,CAACW,UAAU;QAC5B,IAAI,CAACT,WAAW,GAAG,IAAI,CAACU,gBAAgB;QACxC,IAAI,CAACC,uBAAuB;QAE5B,IAAI,IAAI,CAACT,OAAO,CAACU,eAAe,EAAE;YAChC,IAAI,CAACb,MAAM,GAAG,IAAI,CAACc,WAAW;YAC9B,IAAI,CAACjB,MAAM,CAACkB,GAAG,CACb,CAAC,8BAA8B,EAAEtB,kBAAkB,EA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;YACtBC,kBAAkB;QACpB;IACF;IAEA,0EAA0E;IAC1E,QAAQ;IACR,0EAA0E;IAE1E;;;;;GAKC,GACD,MAAclF,QAAuB;QACnC,IAAI,IAAI,CAACnB,MAAM,KAAK,MAAM;YACxB,IAAI;gBACF,MAAM,IAAI,CAACA,MAAM,CAACmB,KAAK;YACzB,EAAE,OAAOkE,KAAK;gBACZ,IAAI,CAACxF,MAAM,CAACyF,IAAI,CACd,CAAC,qBAAqB,EAAED,eAAe7D,QAAQ6D,IAAIE,OAAO,GAAG3B,OAAOyB,MAAM;YAE9E;YACA,IAAI,CAACrF,MAAM,GAAG;QAChB;QACA,IAAI,IAAI,CAACC,WAAW,KAAK,MAAM;YAC7B,IAAI;gBACF,MAAM,IAAI,CAACA,WAAW,CAACkB,KAAK;YAC9B,EAAE,OAAOkE,KAAK;gBACZ,IAAI,CAACxF,MAAM,CAACyF,IAAI,CACd,CAAC,0BAA0B,EAAED,eAAe7D,QAAQ6D,IAAIE,OAAO,GAAG3B,OAAOyB,MAAM;YAEnF;YACA,IAAI,CAACpF,WAAW,GAAG;QACrB;QACA,IAAI,IAAI,CAACF,KAAK,KAAK,MAAM;YACvB,IAAI;gBACF,MAAM,IAAI,CAACA,KAAK,CAACoB,KAAK;YACxB,EAAE,OAAOkE,KAAK;gBACZ,IAAI,CAACxF,MAAM,CAACyF,IAAI,CAAC,CAAC,oBAAoB,EAAED,eAAe7D,QAAQ6D,IAAIE,OAAO,GAAG3B,OAAOyB,MAAM;YAC5F;YACA,IAAI,CAACtF,KAAK,GAAG;QACf;IACF;AACF"}
@@ -0,0 +1,134 @@
1
+ import { OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
2
+ import { BatchScheduleRegistry, JobLauncher } from '@nest-batch/core';
3
+ import { type ResolvedBullMqModuleOptions } from './module-options';
4
+ /**
5
+ * The single BullMQ queue name used by the schedule service. We
6
+ * intentionally use a DIFFERENT queue from the runtime service's
7
+ * `BULLMQ_QUEUE_NAME` so cron-triggered jobs and ad-hoc
8
+ * `launch()`-triggered jobs are inspectable in isolation (and so
9
+ * the schedule-removal path on shutdown can tear them down
10
+ * without touching the runtime work queue).
11
+ *
12
+ * BullMQ 5 rejects queue names that contain a colon (`:`) because
13
+ * it is the path separator in the key layout. We use a hyphen
14
+ * (`-`) instead, matching the existing `BULLMQ_QUEUE_NAME`
15
+ * convention (`'nest-batch-work'`).
16
+ */
17
+ export declare const BULLMQ_SCHEDULE_QUEUE_NAME = "nest-batch-schedule";
18
+ export interface BullmqSchedulePayload {
19
+ readonly jobId: string;
20
+ readonly scheduleName: string;
21
+ readonly methodName: string;
22
+ }
23
+ /**
24
+ * `BullmqSchedule` — the runtime scheduler for
25
+ * `@BatchScheduled` entries.
26
+ *
27
+ * Lifecycle:
28
+ * 1. `OnApplicationBootstrap` walks the `BatchScheduleRegistry`
29
+ * and, for every entry with `inert: false`, registers a
30
+ * BullMQ repeating job via `queue.upsertJobScheduler(...)`.
31
+ * Entries with `inert: true` are logged and skipped — that
32
+ * is the only place the inert flag is consulted.
33
+ * 2. BullMQ's `upsertJobScheduler` internally fires the
34
+ * schedule at the configured cron time. Each fire enqueues a
35
+ * job into the schedule queue (named after the schedule
36
+ * entry's method). When `autoStartWorker` is `true`, this
37
+ * service also starts a schedule-queue worker that bridges
38
+ * `{ jobId, scheduleName, methodName }` into
39
+ * `JobLauncher.launch(jobId, params)`.
40
+ * 3. `OnApplicationShutdown` removes every installed scheduler
41
+ * (via `queue.removeJobScheduler`) and closes the queue.
42
+ * Removal is best-effort: a partial failure logs a warning
43
+ * but does not block the rest of the shutdown.
44
+ *
45
+ * Why a dedicated service (not a method on `BullmqRuntime`)?
46
+ * - The runtime service is `IExecutionStrategy`-facing; it
47
+ * knows about `JobExecution`, the in-process launch contract,
48
+ * and the worker bridge. Mixing scheduler concerns in would
49
+ * bloat its surface and couple two lifecycles that happen to
50
+ * share a Redis client but are otherwise independent.
51
+ * - The schedule queue needs a different worker contract from
52
+ * the runtime work queue: schedule payloads identify a job to
53
+ * launch, while work payloads identify an already-created
54
+ * execution/step. Keeping the bridge here avoids teaching
55
+ * `BullmqRuntime` a second payload shape.
56
+ * - The schedule service owns its own `Queue` (the schedule
57
+ * queue) so cron jobs are not interleaved with manually-launched
58
+ * jobs. They share the same `keyPrefix` so the host's Redis
59
+ * namespace policy still applies.
60
+ */
61
+ export declare class BullmqSchedule implements OnApplicationBootstrap, OnApplicationShutdown {
62
+ private readonly scheduleRegistry;
63
+ private readonly options;
64
+ private readonly launcher;
65
+ private readonly logger;
66
+ /** BullMQ queue for the scheduler (producer side only). */
67
+ private scheduleQueue;
68
+ /** BullMQ worker that turns schedule fires into real batch launches. */
69
+ private scheduleWorker;
70
+ /**
71
+ * Every schedule key installed during `onApplicationBootstrap`.
72
+ * Tracked so the shutdown path can `removeJobScheduler` for
73
+ * each one deterministically. A `Set` keeps the test assertions
74
+ * order-independent.
75
+ */
76
+ private readonly installedKeys;
77
+ /** Promise-chain lock for the close path. Mirrors the runtime service. */
78
+ private closePromise;
79
+ constructor(scheduleRegistry: BatchScheduleRegistry, options: ResolvedBullMqModuleOptions, launcher: JobLauncher);
80
+ /**
81
+ * Walk the registry and install every non-inert entry as a
82
+ * BullMQ repeating job. Runs AFTER the `BatchBootstrapper` has
83
+ * populated the registry (both hooks are on
84
+ * `OnApplicationBootstrap`, but Nest calls them in
85
+ * provider-registration order; the bootstrapper is registered
86
+ * before this service by `BullmqBatchModule.forRoot()`).
87
+ *
88
+ * Each entry is wrapped in a per-entry `try` so a single bad
89
+ * schedule does not abort the rest of the installation. Bad
90
+ * schedules are logged and skipped — the runtime keeps running
91
+ * for the valid ones.
92
+ */
93
+ onApplicationBootstrap(): void;
94
+ /**
95
+ * Tear down every installed scheduler and close the schedule
96
+ * queue. Idempotent: a second `onApplicationShutdown` short-
97
+ * circuits to the first close's promise.
98
+ */
99
+ onApplicationShutdown(): Promise<void>;
100
+ /**
101
+ * Installed scheduler keys, in insertion order. Exposed for
102
+ * tests and diagnostics. Read-only: callers MUST NOT mutate
103
+ * the returned array.
104
+ */
105
+ installedSchedulerKeys(): readonly string[];
106
+ /**
107
+ * Install a single entry as a BullMQ repeating job. Skips
108
+ * inert entries (the runtime honours the inert flag by NOT
109
+ * calling `upsertJobScheduler` for them). Throws on
110
+ * installation failure so the caller can log + continue.
111
+ */
112
+ private installSchedule;
113
+ /**
114
+ * Build the producer-side BullMQ queue for the scheduler. The
115
+ * connection tuning mirrors the runtime service's producer
116
+ * options: fail-fast on Redis-down (`enableOfflineQueue:
117
+ * false`) and a tight per-request retry budget
118
+ * (`maxRetriesPerRequest: 1`).
119
+ */
120
+ private buildScheduleQueue;
121
+ private buildScheduleWorker;
122
+ private processScheduleFire;
123
+ private producerConnectionOptions;
124
+ private workerConnectionOptions;
125
+ /**
126
+ * Close the schedule queue. `removeJobScheduler` is called
127
+ * first for every installed key so the next run of the host
128
+ * app does not inherit leftover schedulers. Each removal is
129
+ * best-effort: a failure on one key does not prevent the
130
+ * others from being removed.
131
+ */
132
+ private close;
133
+ }
134
+ //# sourceMappingURL=bullmq-schedule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bullmq-schedule.d.ts","sourceRoot":"","sources":["../../src/bullmq-schedule.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,qBAAqB,EACrB,WAAW,EAGZ,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAyB,KAAK,2BAA2B,EAAE,MAAM,kBAAkB,CAAC;AAE3F;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,0BAA0B,wBAAwB,CAAC;AAEhE,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;;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