velocious 1.0.519 → 1.0.521
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/README.md +36 -0
- package/build/background-jobs/json-socket.js +12 -0
- package/build/background-jobs/main.js +78 -23
- package/build/background-jobs/store.js +127 -5
- package/build/background-jobs/types.js +4 -2
- package/build/background-jobs/worker.js +113 -23
- package/build/configuration-types.js +29 -6
- package/build/configuration.js +1 -1
- package/build/environment-handlers/node/cli/commands/generate/base-models.js +1 -1
- package/build/jobs/prune-terminal-background-jobs.js +67 -0
- package/build/src/background-jobs/json-socket.d.ts +12 -0
- package/build/src/background-jobs/json-socket.d.ts.map +1 -1
- package/build/src/background-jobs/json-socket.js +13 -1
- package/build/src/background-jobs/main.d.ts +18 -9
- package/build/src/background-jobs/main.d.ts.map +1 -1
- package/build/src/background-jobs/main.js +73 -26
- package/build/src/background-jobs/store.d.ts +30 -0
- package/build/src/background-jobs/store.d.ts.map +1 -1
- package/build/src/background-jobs/store.js +110 -5
- package/build/src/background-jobs/types.d.ts +14 -3
- package/build/src/background-jobs/types.d.ts.map +1 -1
- package/build/src/background-jobs/types.js +5 -3
- package/build/src/background-jobs/worker.d.ts +55 -7
- package/build/src/background-jobs/worker.d.ts.map +1 -1
- package/build/src/background-jobs/worker.js +108 -22
- package/build/src/configuration-types.d.ts +71 -11
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +30 -7
- package/build/src/configuration.d.ts +4 -2
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +2 -2
- package/build/src/environment-handlers/node/cli/commands/generate/base-models.js +2 -2
- package/build/src/jobs/prune-terminal-background-jobs.d.ts +23 -0
- package/build/src/jobs/prune-terminal-background-jobs.d.ts.map +1 -0
- package/build/src/jobs/prune-terminal-background-jobs.js +61 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/background-jobs/json-socket.js +12 -0
- package/src/background-jobs/main.js +78 -23
- package/src/background-jobs/store.js +127 -5
- package/src/background-jobs/types.js +4 -2
- package/src/background-jobs/worker.js +113 -23
- package/src/configuration-types.js +29 -6
- package/src/configuration.js +1 -1
- package/src/environment-handlers/node/cli/commands/generate/base-models.js +1 -1
- package/src/jobs/prune-terminal-background-jobs.js +67 -0
|
@@ -12,6 +12,10 @@ import {fileURLToPath} from "node:url"
|
|
|
12
12
|
/** Grace period after SIGTERM before a lingering process runner is SIGKILLed. */
|
|
13
13
|
const FORKED_CHILD_SIGKILL_GRACE_MS = 5000
|
|
14
14
|
const FORKED_RUNNER_ENTRY_PATH = fileURLToPath(new URL("./forked-runner-child.js", import.meta.url))
|
|
15
|
+
/** How often the worker sends a liveness heartbeat to the main. */
|
|
16
|
+
const HEARTBEAT_INTERVAL_MS = 15000
|
|
17
|
+
/** TCP keepalive so a half-open connection to the main surfaces as a close. */
|
|
18
|
+
const SOCKET_KEEPALIVE_MS = 10000
|
|
15
19
|
/**
|
|
16
20
|
* Execution modes.
|
|
17
21
|
* @type {import("./types.js").BackgroundJobExecutionMode[]} */
|
|
@@ -27,8 +31,9 @@ export default class BackgroundJobsWorker {
|
|
|
27
31
|
* @param {number} [args.maxConcurrentForkedJobs] - Override the process runner concurrency cap from `configuration.getBackgroundJobsConfig()`.
|
|
28
32
|
* @param {number} [args.maxConcurrentInlineJobs] - Override the inline-job concurrency cap from `configuration.getBackgroundJobsConfig()`.
|
|
29
33
|
* @param {number} [args.forkedChildSigkillGraceMs] - Override the grace period between SIGTERM and SIGKILL when reaping lingering process runners on stop.
|
|
34
|
+
* @param {number} [args.heartbeatIntervalMs] - Override the liveness heartbeat interval (default 15000ms).
|
|
30
35
|
*/
|
|
31
|
-
constructor({configuration, host, port, maxConcurrentForkedJobs, maxConcurrentInlineJobs, forkedChildSigkillGraceMs} = {}) {
|
|
36
|
+
constructor({configuration, host, port, maxConcurrentForkedJobs, maxConcurrentInlineJobs, forkedChildSigkillGraceMs, heartbeatIntervalMs} = {}) {
|
|
32
37
|
/**
|
|
33
38
|
* Narrows the runtime value to the documented type.
|
|
34
39
|
* @type {Promise<import("../configuration.js").default>} */
|
|
@@ -74,6 +79,21 @@ export default class BackgroundJobsWorker {
|
|
|
74
79
|
: FORKED_CHILD_SIGKILL_GRACE_MS
|
|
75
80
|
this.shouldStop = false
|
|
76
81
|
this.workerId = randomUUID()
|
|
82
|
+
this.heartbeatIntervalMs = typeof heartbeatIntervalMs === "number" && heartbeatIntervalMs >= 1
|
|
83
|
+
? heartbeatIntervalMs
|
|
84
|
+
: HEARTBEAT_INTERVAL_MS
|
|
85
|
+
/**
|
|
86
|
+
* Narrows the runtime value to the documented type.
|
|
87
|
+
* @type {ReturnType<typeof setInterval> | undefined} */
|
|
88
|
+
this._heartbeatTimer = undefined
|
|
89
|
+
/**
|
|
90
|
+
* In-flight job-result reports to the main. Reporting is decoupled from the
|
|
91
|
+
* job/child slot (freeing the slot never waits on a report) and retried
|
|
92
|
+
* durably, so a transient main/DB outage cannot leak slots or lose a
|
|
93
|
+
* terminal report. Tracked so a graceful `stop()` can drain them.
|
|
94
|
+
* @type {Set<Promise<void>>}
|
|
95
|
+
*/
|
|
96
|
+
this.inflightReports = new Set()
|
|
77
97
|
/**
|
|
78
98
|
* Narrows the runtime value to the documented type.
|
|
79
99
|
* @type {JsonSocket | undefined} */
|
|
@@ -154,6 +174,7 @@ export default class BackgroundJobsWorker {
|
|
|
154
174
|
async stop({timeoutMs} = {}) {
|
|
155
175
|
if (this.shouldStop) return
|
|
156
176
|
this.shouldStop = true
|
|
177
|
+
this._stopHeartbeat()
|
|
157
178
|
|
|
158
179
|
// Announce drain so main stops dispatching but keeps the connection
|
|
159
180
|
// open until we close it ourselves below.
|
|
@@ -168,6 +189,9 @@ export default class BackgroundJobsWorker {
|
|
|
168
189
|
await this._drainInflight(this.inflightInlineJobs, timeoutMs)
|
|
169
190
|
await this._drainInflight(this.inflightProcessJobs, timeoutMs)
|
|
170
191
|
await this._terminateProcessChildren()
|
|
192
|
+
// Give in-flight result reports (now decoupled from job slots) a bounded
|
|
193
|
+
// chance to land before the socket closes.
|
|
194
|
+
await this._drainInflight(this.inflightReports, timeoutMs)
|
|
171
195
|
|
|
172
196
|
if (this.jsonSocket) this.jsonSocket.close()
|
|
173
197
|
if (this.configuration) {
|
|
@@ -238,6 +262,7 @@ export default class BackgroundJobsWorker {
|
|
|
238
262
|
const host = this.host || config.host
|
|
239
263
|
const port = typeof this.port === "number" ? this.port : config.port
|
|
240
264
|
const socket = net.createConnection({host, port})
|
|
265
|
+
socket.setKeepAlive(true, SOCKET_KEEPALIVE_MS)
|
|
241
266
|
const jsonSocket = new JsonSocket(socket)
|
|
242
267
|
this.jsonSocket = jsonSocket
|
|
243
268
|
|
|
@@ -256,16 +281,51 @@ export default class BackgroundJobsWorker {
|
|
|
256
281
|
})
|
|
257
282
|
|
|
258
283
|
jsonSocket.on("close", () => {
|
|
284
|
+
this._stopHeartbeat()
|
|
259
285
|
if (this.shouldStop) return
|
|
260
286
|
setTimeout(() => { void this._connect() }, 1000)
|
|
261
287
|
})
|
|
262
288
|
|
|
263
289
|
socket.on("connect", () => {
|
|
264
|
-
jsonSocket.send({type: "hello", role: "worker", supportsHandoffIdReporting: true, workerId: this.workerId})
|
|
290
|
+
jsonSocket.send({type: "hello", role: "worker", supportsHandoffIdReporting: true, supportsHeartbeat: true, workerId: this.workerId})
|
|
265
291
|
this._sendReadyIfRunning()
|
|
292
|
+
this._startHeartbeat()
|
|
266
293
|
})
|
|
267
294
|
}
|
|
268
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Sends periodic liveness heartbeats to the main so a wedged or silent worker
|
|
298
|
+
* can be detected and dropped there (its leases released) instead of freezing
|
|
299
|
+
* the queue until a human notices.
|
|
300
|
+
* @returns {void}
|
|
301
|
+
*/
|
|
302
|
+
_startHeartbeat() {
|
|
303
|
+
this._stopHeartbeat()
|
|
304
|
+
|
|
305
|
+
this._heartbeatTimer = setInterval(() => {
|
|
306
|
+
if (this.shouldStop || !this.jsonSocket) return
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
this.jsonSocket.send({type: "heartbeat", workerId: this.workerId})
|
|
310
|
+
} catch {
|
|
311
|
+
// Socket is closing/closed; the close handler drives reconnect.
|
|
312
|
+
}
|
|
313
|
+
}, this.heartbeatIntervalMs)
|
|
314
|
+
|
|
315
|
+
if (typeof this._heartbeatTimer.unref === "function") this._heartbeatTimer.unref()
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Stops the liveness heartbeat timer.
|
|
320
|
+
* @returns {void}
|
|
321
|
+
*/
|
|
322
|
+
_stopHeartbeat() {
|
|
323
|
+
if (this._heartbeatTimer) {
|
|
324
|
+
clearInterval(this._heartbeatTimer)
|
|
325
|
+
this._heartbeatTimer = undefined
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
269
329
|
/**
|
|
270
330
|
* Runs handle job.
|
|
271
331
|
* @param {import("./types.js").BackgroundJobPayload} payload - Payload.
|
|
@@ -394,9 +454,12 @@ export default class BackgroundJobsWorker {
|
|
|
394
454
|
* @returns {Promise<void>} - Resolves when complete (success or failure reported).
|
|
395
455
|
*/
|
|
396
456
|
async _runInlineJobAndReport(payload) {
|
|
457
|
+
// Report in the background so freeing this inline slot never waits on the
|
|
458
|
+
// report. Reporting is durable (retried until it lands), so a transient
|
|
459
|
+
// main/DB outage neither wedges the slot nor loses the terminal result.
|
|
397
460
|
try {
|
|
398
461
|
await this._runJobInline(payload)
|
|
399
|
-
|
|
462
|
+
this._reportJobResultInBackground({
|
|
400
463
|
jobId: payload.id,
|
|
401
464
|
status: "completed",
|
|
402
465
|
handoffId: payload.handoffId,
|
|
@@ -404,7 +467,7 @@ export default class BackgroundJobsWorker {
|
|
|
404
467
|
workerId: payload.workerId || this.workerId
|
|
405
468
|
})
|
|
406
469
|
} catch (error) {
|
|
407
|
-
|
|
470
|
+
this._reportJobResultInBackground({
|
|
408
471
|
jobId: payload.id,
|
|
409
472
|
status: "failed",
|
|
410
473
|
error,
|
|
@@ -522,10 +585,10 @@ export default class BackgroundJobsWorker {
|
|
|
522
585
|
_waitForForkedChild({child, payload}) {
|
|
523
586
|
return new Promise((resolve) => {
|
|
524
587
|
child.once("exit", (code, signal) => {
|
|
525
|
-
|
|
588
|
+
this._handleForkedChildExit({child, code, signal, payload, resolve})
|
|
526
589
|
})
|
|
527
590
|
child.once("error", (error) => {
|
|
528
|
-
|
|
591
|
+
this._handleForkedChildError({child, error, payload, resolve})
|
|
529
592
|
})
|
|
530
593
|
})
|
|
531
594
|
}
|
|
@@ -538,22 +601,22 @@ export default class BackgroundJobsWorker {
|
|
|
538
601
|
* @param {keyof typeof import("node:os").constants.signals | null} args.signal - Exit signal.
|
|
539
602
|
* @param {import("./types.js").BackgroundJobPayload & {id: string}} args.payload - Payload.
|
|
540
603
|
* @param {(value: void) => void} args.resolve - Promise resolver.
|
|
541
|
-
* @returns {
|
|
604
|
+
* @returns {void}
|
|
542
605
|
*/
|
|
543
|
-
|
|
606
|
+
_handleForkedChildExit({child, code, signal, payload, resolve}) {
|
|
544
607
|
this.inflightProcessChildren.delete(child)
|
|
545
608
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
609
|
+
// Free the worker slot as soon as the child is gone — never gate it on the
|
|
610
|
+
// failure report. A hung/slow report must not leak the slot; enough leaked
|
|
611
|
+
// slots drive `acceptsForked` to false and silently wedge the worker.
|
|
612
|
+
resolve(undefined)
|
|
613
|
+
|
|
614
|
+
if (this._forkedChildExitedCleanly({code, signal})) return
|
|
550
615
|
|
|
551
|
-
|
|
616
|
+
this._reportForkedChildFailure({
|
|
552
617
|
payload,
|
|
553
618
|
error: new Error(`Forked background job runner exited before reporting: code=${code} signal=${signal || "none"}`)
|
|
554
619
|
})
|
|
555
|
-
|
|
556
|
-
resolve(undefined)
|
|
557
620
|
}
|
|
558
621
|
|
|
559
622
|
/**
|
|
@@ -574,13 +637,14 @@ export default class BackgroundJobsWorker {
|
|
|
574
637
|
* @param {Error} args.error - Child process error.
|
|
575
638
|
* @param {import("./types.js").BackgroundJobPayload & {id: string}} args.payload - Payload.
|
|
576
639
|
* @param {(value: void) => void} args.resolve - Promise resolver.
|
|
577
|
-
* @returns {
|
|
640
|
+
* @returns {void}
|
|
578
641
|
*/
|
|
579
|
-
|
|
642
|
+
_handleForkedChildError({child, error, payload, resolve}) {
|
|
580
643
|
this.inflightProcessChildren.delete(child)
|
|
581
|
-
|
|
582
|
-
await this._reportForkedChildFailure({payload, error})
|
|
644
|
+
// Free the slot first (see _handleForkedChildExit) — reporting is best-effort.
|
|
583
645
|
resolve(undefined)
|
|
646
|
+
console.error("Background jobs forked runner error:", error)
|
|
647
|
+
this._reportForkedChildFailure({payload, error})
|
|
584
648
|
}
|
|
585
649
|
|
|
586
650
|
/**
|
|
@@ -595,7 +659,7 @@ export default class BackgroundJobsWorker {
|
|
|
595
659
|
child.send({type: "job", payload})
|
|
596
660
|
} catch (error) {
|
|
597
661
|
child.kill("SIGTERM")
|
|
598
|
-
|
|
662
|
+
this._reportForkedChildFailure({payload, error})
|
|
599
663
|
}
|
|
600
664
|
}
|
|
601
665
|
|
|
@@ -604,10 +668,10 @@ export default class BackgroundJobsWorker {
|
|
|
604
668
|
* @param {object} args - Options.
|
|
605
669
|
* @param {import("./types.js").BackgroundJobPayload & {id: string}} args.payload - Payload.
|
|
606
670
|
* @param {?} args.error - Error.
|
|
607
|
-
* @returns {
|
|
671
|
+
* @returns {void}
|
|
608
672
|
*/
|
|
609
|
-
|
|
610
|
-
|
|
673
|
+
_reportForkedChildFailure({payload, error}) {
|
|
674
|
+
this._reportJobResultInBackground({
|
|
611
675
|
jobId: payload.id,
|
|
612
676
|
status: "failed",
|
|
613
677
|
error,
|
|
@@ -682,4 +746,30 @@ export default class BackgroundJobsWorker {
|
|
|
682
746
|
console.error("Background job status reporting failed:", reportError)
|
|
683
747
|
}
|
|
684
748
|
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Fires a durable job-result report without blocking the caller (so freeing a
|
|
752
|
+
* job/child slot never waits on the report). The report is tracked so a
|
|
753
|
+
* graceful `stop()` can drain in-flight reports before closing the socket.
|
|
754
|
+
* @param {object} args - Options.
|
|
755
|
+
* @param {string} args.jobId - Job id.
|
|
756
|
+
* @param {"completed" | "failed"} args.status - Status.
|
|
757
|
+
* @param {?} [args.error] - Error.
|
|
758
|
+
* @param {string} [args.handoffId] - Handoff lease id.
|
|
759
|
+
* @param {number} [args.handedOffAtMs] - Handed off timestamp.
|
|
760
|
+
* @param {string} [args.workerId] - Worker id.
|
|
761
|
+
* @returns {void}
|
|
762
|
+
*/
|
|
763
|
+
_reportJobResultInBackground({jobId, status, error, handoffId, handedOffAtMs, workerId}) {
|
|
764
|
+
/**
|
|
765
|
+
* Defines report.
|
|
766
|
+
* @type {Promise<void>} */
|
|
767
|
+
let report
|
|
768
|
+
|
|
769
|
+
report = this._reportJobResult({jobId, status, error, handoffId, handedOffAtMs, workerId}).finally(() => {
|
|
770
|
+
this.inflightReports.delete(report)
|
|
771
|
+
})
|
|
772
|
+
|
|
773
|
+
this.inflightReports.add(report)
|
|
774
|
+
}
|
|
685
775
|
}
|
|
@@ -164,15 +164,29 @@
|
|
|
164
164
|
* allowed to keep in flight. Default: `4`. This is a per-worker safety cap;
|
|
165
165
|
* for workload-shaped limits use per-queue caps (`queues`) instead, which are
|
|
166
166
|
* enforced cluster-wide and are immune to duplicate worker processes.
|
|
167
|
-
* @property {Record<string, {maxConcurrent?: number}>} [queues] - Per-queue
|
|
168
|
-
* concurrency caps, Sidekiq-style. A job declares its queue (static
|
|
169
|
-
* the job class, or the `queue` enqueue option; defaults to `"default"`), and
|
|
167
|
+
* @property {Record<string, {maxConcurrent?: number, priority?: number}>} [queues] - Per-queue
|
|
168
|
+
* concurrency caps and dispatch priorities, Sidekiq-style. A job declares its queue (static
|
|
169
|
+
* `queue` on the job class, or the `queue` enqueue option; defaults to `"default"`), and
|
|
170
170
|
* `queues[name].maxConcurrent` bounds how many jobs from that queue may be
|
|
171
171
|
* in flight across the whole cluster (enforced via durable per-key
|
|
172
172
|
* concurrency, so it holds regardless of how many worker processes run).
|
|
173
173
|
* Size each queue to its workload: I/O-bound queues (e.g. build runners
|
|
174
174
|
* waiting on remote Docker servers) can run far above the core count, while
|
|
175
|
-
* CPU-bound queues should stay near it.
|
|
175
|
+
* CPU-bound queues should stay near it. `queues[name].priority` (default `0`)
|
|
176
|
+
* makes the main process dispatch higher-priority queues before lower-priority
|
|
177
|
+
* ones regardless of enqueue order, so a small time-critical queue can never be
|
|
178
|
+
* starved by a flood of low-priority work sharing a worker pool. Unlike
|
|
179
|
+
* Sidekiq's strict queue ordering, priority composes with the per-queue caps:
|
|
180
|
+
* a higher-priority queue that is already at its `maxConcurrent` is skipped and
|
|
181
|
+
* dispatch falls through to the next eligible lower-priority job, so a busy
|
|
182
|
+
* high-priority queue does not block everything else. This fallthrough is a
|
|
183
|
+
* property of the queue-derived cap; a job that supplies its own explicit
|
|
184
|
+
* `concurrencyKey`/`maxConcurrency` bypasses the queue cap entirely (an
|
|
185
|
+
* explicit key always wins — see above) and is bounded only by that key, so it
|
|
186
|
+
* is not held back by the queue's cap and priority simply orders it normally.
|
|
187
|
+
* Priorities may be any number, including negative to sink a queue below the
|
|
188
|
+
* default. Jobs within the same priority keep FIFO (`scheduled_at`, then
|
|
189
|
+
* `created_at`) order. Default: `{}` (no queue caps, all queues priority `0`).
|
|
176
190
|
* @property {BackgroundJobsDispatchStrategy} [dispatchStrategy] - How the main process
|
|
177
191
|
* detects new work. Defaults to `"beacon"` (event-driven). Set to `"polling"`
|
|
178
192
|
* to restore the legacy fixed-interval poll.
|
|
@@ -194,8 +208,17 @@
|
|
|
194
208
|
* jobs older than this many ms. `null` or `<= 0` disables (keeps them for
|
|
195
209
|
* debugging). Default: `2592000000` (30 days).
|
|
196
210
|
* @property {number} [batchSize] - Rows deleted per batch. Default: `1000`.
|
|
197
|
-
* @property {number} [sweepIntervalMs] - How often the
|
|
198
|
-
*
|
|
211
|
+
* @property {number} [sweepIntervalMs] - How often the retention sweep runs.
|
|
212
|
+
* Default: `3600000` (1 hour).
|
|
213
|
+
*/
|
|
214
|
+
/**
|
|
215
|
+
* Fully-resolved retention config as returned by `getBackgroundJobsConfig()`
|
|
216
|
+
* (every field defaulted), as opposed to the partial user-provided input.
|
|
217
|
+
* @typedef {object} ResolvedBackgroundJobsRetentionConfiguration
|
|
218
|
+
* @property {number | null} completedTtlMs - Resolved completed-job TTL in ms (`null` disables).
|
|
219
|
+
* @property {number | null} failedTtlMs - Resolved failed/orphaned TTL in ms (`null` disables).
|
|
220
|
+
* @property {number} batchSize - Resolved delete batch size.
|
|
221
|
+
* @property {number} sweepIntervalMs - Resolved sweep interval in ms.
|
|
199
222
|
*/
|
|
200
223
|
|
|
201
224
|
/**
|
package/src/configuration.js
CHANGED
|
@@ -1260,7 +1260,7 @@ export default class VelociousConfiguration {
|
|
|
1260
1260
|
|
|
1261
1261
|
/**
|
|
1262
1262
|
* Runs get background jobs config.
|
|
1263
|
-
* @returns {Required<import("./configuration-types.js").BackgroundJobsConfiguration>} - Background jobs configuration.
|
|
1263
|
+
* @returns {Required<import("./configuration-types.js").BackgroundJobsConfiguration> & {retention: import("./configuration-types.js").ResolvedBackgroundJobsRetentionConfiguration}} - Background jobs configuration.
|
|
1264
1264
|
*/
|
|
1265
1265
|
getBackgroundJobsConfig() {
|
|
1266
1266
|
const envHost = process.env.VELOCIOUS_BACKGROUND_JOBS_HOST
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import BackgroundJobsStore from "../background-jobs/store.js"
|
|
4
|
+
import Configuration from "../configuration.js"
|
|
5
|
+
import VelociousJob from "../background-jobs/job.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Built-in job that prunes terminal `background_jobs` rows past their retention
|
|
9
|
+
* window so the table does not grow unbounded. The main process registers this
|
|
10
|
+
* on the normal background-jobs scheduler when `backgroundJobs.retention` is
|
|
11
|
+
* enabled, so it runs as an ordinary scheduled/queued job — visible in the job
|
|
12
|
+
* tables and dispatched to a worker — rather than a hidden in-process timer.
|
|
13
|
+
* @augments {VelociousJob<[]>}
|
|
14
|
+
*/
|
|
15
|
+
export default class PruneTerminalBackgroundJobsJob extends VelociousJob {
|
|
16
|
+
/**
|
|
17
|
+
* Reserved job name that an application job cannot shadow. The registry loads
|
|
18
|
+
* app `src/jobs` first and skips duplicate built-in names, so if this used the
|
|
19
|
+
* default class-name identity an app class named `PruneTerminalBackgroundJobsJob`
|
|
20
|
+
* would be dispatched instead. A `:`-namespaced name can never collide with a
|
|
21
|
+
* default (class-name) identity, since class names cannot contain `:`.
|
|
22
|
+
* @returns {string} - Reserved job name.
|
|
23
|
+
*/
|
|
24
|
+
static jobName() {
|
|
25
|
+
return "velocious:prune-terminal-background-jobs"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Builds the scheduler configuration for this job from a resolved retention
|
|
30
|
+
* config, or returns `null` when retention is fully disabled (nothing to
|
|
31
|
+
* prune, so nothing to schedule). `maxConcurrency: 1` keeps runs from
|
|
32
|
+
* overlapping, and `deduplicateWhileQueued` stops the interval scheduler from
|
|
33
|
+
* piling up redundant queued rows when a prune is slow or no worker is free.
|
|
34
|
+
* @param {import("../configuration-types.js").ResolvedBackgroundJobsRetentionConfiguration} retention - Resolved retention config.
|
|
35
|
+
* @returns {import("../configuration-types.js").ScheduledBackgroundJobConfiguration | null} - Scheduler config for the prune job, or null when retention is disabled.
|
|
36
|
+
*/
|
|
37
|
+
static scheduleConfiguration(retention) {
|
|
38
|
+
const prunesCompleted = typeof retention.completedTtlMs === "number" && retention.completedTtlMs > 0
|
|
39
|
+
const prunesFailed = typeof retention.failedTtlMs === "number" && retention.failedTtlMs > 0
|
|
40
|
+
|
|
41
|
+
if (!prunesCompleted && !prunesFailed) {
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
class: this,
|
|
47
|
+
every: retention.sweepIntervalMs,
|
|
48
|
+
options: {concurrencyKey: "velocious-prune-terminal-background-jobs", maxConcurrency: 1, deduplicateWhileQueued: true}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Prunes terminal job rows past their retention window.
|
|
54
|
+
* @returns {Promise<void>}
|
|
55
|
+
*/
|
|
56
|
+
async perform() {
|
|
57
|
+
const configuration = Configuration.current()
|
|
58
|
+
const config = configuration.getBackgroundJobsConfig()
|
|
59
|
+
const store = new BackgroundJobsStore({configuration, databaseIdentifier: config.databaseIdentifier})
|
|
60
|
+
|
|
61
|
+
await store.pruneTerminalJobs({
|
|
62
|
+
completedTtlMs: config.retention.completedTtlMs,
|
|
63
|
+
failedTtlMs: config.retention.failedTtlMs,
|
|
64
|
+
batchSize: config.retention.batchSize
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
}
|