velocious 1.0.519 → 1.0.520

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 (46) hide show
  1. package/README.md +34 -0
  2. package/build/background-jobs/json-socket.js +12 -0
  3. package/build/background-jobs/main.js +78 -23
  4. package/build/background-jobs/store.js +87 -5
  5. package/build/background-jobs/types.js +4 -2
  6. package/build/background-jobs/worker.js +113 -23
  7. package/build/configuration-types.js +11 -2
  8. package/build/configuration.js +1 -1
  9. package/build/environment-handlers/node/cli/commands/generate/base-models.js +1 -1
  10. package/build/jobs/prune-terminal-background-jobs.js +67 -0
  11. package/build/src/background-jobs/json-socket.d.ts +12 -0
  12. package/build/src/background-jobs/json-socket.d.ts.map +1 -1
  13. package/build/src/background-jobs/json-socket.js +13 -1
  14. package/build/src/background-jobs/main.d.ts +18 -9
  15. package/build/src/background-jobs/main.d.ts.map +1 -1
  16. package/build/src/background-jobs/main.js +73 -26
  17. package/build/src/background-jobs/store.d.ts +16 -0
  18. package/build/src/background-jobs/store.d.ts.map +1 -1
  19. package/build/src/background-jobs/store.js +75 -5
  20. package/build/src/background-jobs/types.d.ts +14 -3
  21. package/build/src/background-jobs/types.d.ts.map +1 -1
  22. package/build/src/background-jobs/types.js +5 -3
  23. package/build/src/background-jobs/worker.d.ts +55 -7
  24. package/build/src/background-jobs/worker.d.ts.map +1 -1
  25. package/build/src/background-jobs/worker.js +108 -22
  26. package/build/src/configuration-types.d.ts +35 -4
  27. package/build/src/configuration-types.d.ts.map +1 -1
  28. package/build/src/configuration-types.js +12 -3
  29. package/build/src/configuration.d.ts +4 -2
  30. package/build/src/configuration.d.ts.map +1 -1
  31. package/build/src/configuration.js +2 -2
  32. package/build/src/environment-handlers/node/cli/commands/generate/base-models.js +2 -2
  33. package/build/src/jobs/prune-terminal-background-jobs.d.ts +23 -0
  34. package/build/src/jobs/prune-terminal-background-jobs.d.ts.map +1 -0
  35. package/build/src/jobs/prune-terminal-background-jobs.js +61 -0
  36. package/build/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +1 -1
  38. package/src/background-jobs/json-socket.js +12 -0
  39. package/src/background-jobs/main.js +78 -23
  40. package/src/background-jobs/store.js +87 -5
  41. package/src/background-jobs/types.js +4 -2
  42. package/src/background-jobs/worker.js +113 -23
  43. package/src/configuration-types.js +11 -2
  44. package/src/configuration.js +1 -1
  45. package/src/environment-handlers/node/cli/commands/generate/base-models.js +1 -1
  46. package/src/jobs/prune-terminal-background-jobs.js +67 -0
package/README.md CHANGED
@@ -2131,6 +2131,40 @@ await MyJob.performLaterWithOptions({
2131
2131
 
2132
2132
  If a handed-off job does not report back within 2 hours, it is marked orphaned and re-queued if retries remain.
2133
2133
 
2134
+ Workers also send periodic heartbeats (and use TCP keepalive) so the main can drop a wedged or half-open worker that never fires a socket `close` and release its leases, and job slots are freed independently of durable, background result reporting so a transient outage can't wedge a worker or lose a completion. See [docs/background-jobs.md](docs/background-jobs.md#worker-liveness).
2135
+
2136
+ ## Queues
2137
+
2138
+ Give a job a queue with `static queue = "..."` (or a `{queue}` job option; the option wins) and cap how many of that queue's jobs run in flight across the whole cluster under `backgroundJobs.queues`:
2139
+
2140
+ ```js
2141
+ backgroundJobs: {
2142
+ queues: {
2143
+ builds: {maxConcurrent: 100}, // I/O-bound: can run well above the core count
2144
+ default: {maxConcurrent: 8} // CPU-bound: keep near the core count
2145
+ }
2146
+ }
2147
+ ```
2148
+
2149
+ A job with no queue runs on `"default"`; a queue with no cap is unlimited. Caps are enforced through the durable per-key concurrency mechanism (the reserved `queue:<name>` key), hold regardless of how many worker processes run, and are reconciled against the existing backlog on startup when you change them. Scheduled jobs honor a job's `static queue` too. See [docs/background-jobs.md](docs/background-jobs.md#queues-per-queue-concurrency-caps).
2150
+
2151
+ ## Retention
2152
+
2153
+ Terminal `background_jobs` rows are not deleted automatically unless you configure retention, so a busy app otherwise grows the table indefinitely. Set `backgroundJobs.retention` to prune old terminal rows:
2154
+
2155
+ ```js
2156
+ backgroundJobs: {
2157
+ retention: {
2158
+ completedTtlMs: 7 * 24 * 60 * 60 * 1000, // default: 7 days (null/0 disables)
2159
+ failedTtlMs: 30 * 24 * 60 * 60 * 1000, // default: 30 days for failed/orphaned (null/0 disables)
2160
+ batchSize: 1000, // default: 1000 rows per delete batch
2161
+ sweepIntervalMs: 60 * 60 * 1000 // default: 1 hour
2162
+ }
2163
+ }
2164
+ ```
2165
+
2166
+ `background-jobs-main` registers a built-in `velocious:prune-terminal-background-jobs` job on the scheduler when retention is enabled, so pruning runs as an ordinary scheduled/queued job (it needs a worker, appears in the job tables, and is bounded to one non-overlapping run). See [docs/background-jobs.md](docs/background-jobs.md#retention-pruning-old-job-rows).
2167
+
2134
2168
  ## Dashboard
2135
2169
 
2136
2170
  Velocious ships a mountable read-only HTTP API for inspecting jobs (queued, running, completed, failed, orphaned and scheduled), similar in spirit to `sidekiq-web`. Mount it in your routes file the way `Sidekiq::Web` is mounted in Rails:
@@ -30,6 +30,18 @@ export default class JsonSocket extends EventEmitter {
30
30
  * Narrows the runtime value to the documented type.
31
31
  * @type {boolean} */
32
32
  this.acceptsInlineJobs = true
33
+ /**
34
+ * Whether this worker advertised heartbeat support in its hello. Only
35
+ * heartbeat-capable workers are subject to the main's stale-liveness
36
+ * eviction; a legacy worker (e.g. mid rolling deploy) is exempt so its
37
+ * active leases are not released while it is still running them.
38
+ * @type {boolean} */
39
+ this.supportsHeartbeat = false
40
+ /**
41
+ * Last time (ms) the main saw any message from this worker socket; used by
42
+ * the main's liveness sweep to drop a wedged/silent worker.
43
+ * @type {number | undefined} */
44
+ this.lastSeenAt = undefined
33
45
  this.buffer = ""
34
46
  this.socket.setEncoding("utf8")
35
47
  this.socket.on("data", (chunk) => this._onData(String(chunk)))
@@ -5,6 +5,7 @@ import JsonSocket from "./json-socket.js"
5
5
  import BackgroundJobsScheduler from "./scheduler.js"
6
6
  import BackgroundJobsStore from "./store.js"
7
7
  import Logger from "../logger.js"
8
+ import PruneTerminalBackgroundJobsJob from "../jobs/prune-terminal-background-jobs.js"
8
9
 
9
10
  /**
10
11
  * Channel used by `background-jobs-main` to coordinate dispatch wake-ups
@@ -21,6 +22,10 @@ const DISPATCH_CHANNEL = "velocious-background-jobs-dispatch"
21
22
  * scheduled-job timer here and re-arm when it expires.
22
23
  */
23
24
  const MAX_TIMER_MS = 2_147_483_647 // ~24.8 days
25
+ /** A worker silent (no heartbeat/ready/report) longer than this is dropped. */
26
+ const WORKER_STALE_TIMEOUT_MS = 60000
27
+ /** How often the main scans workers for staleness. */
28
+ const WORKER_LIVENESS_SWEEP_MS = 15000
24
29
  /**
25
30
  * WorkerExecutionModeCapability type.
26
31
  * @typedef {object} WorkerExecutionModeCapability
@@ -46,8 +51,10 @@ export default class BackgroundJobsMain {
46
51
  * @param {import("../configuration.js").default} args.configuration - Configuration.
47
52
  * @param {string} [args.host] - Hostname.
48
53
  * @param {number} [args.port] - Port.
54
+ * @param {number} [args.workerStaleTimeoutMs] - Override how long a silent worker may go before being dropped (default 60000ms).
55
+ * @param {number} [args.workerLivenessSweepMs] - Override how often stale workers are swept for (default 15000ms).
49
56
  */
50
- constructor({configuration, host, port}) {
57
+ constructor({configuration, host, port, workerStaleTimeoutMs, workerLivenessSweepMs}) {
51
58
  this.configuration = configuration
52
59
  const config = configuration.getBackgroundJobsConfig()
53
60
  this.host = host || config.host
@@ -55,6 +62,10 @@ export default class BackgroundJobsMain {
55
62
  this.dispatchStrategy = config.dispatchStrategy
56
63
  this.pollIntervalMs = config.pollIntervalMs
57
64
  this.retention = config.retention
65
+ // A worker that stops sending anything (heartbeat/ready/report) for this
66
+ // long is treated as wedged/dead: its leases are released and it is dropped.
67
+ this.workerStaleTimeoutMs = typeof workerStaleTimeoutMs === "number" && workerStaleTimeoutMs >= 1 ? workerStaleTimeoutMs : WORKER_STALE_TIMEOUT_MS
68
+ this.workerLivenessSweepMs = typeof workerLivenessSweepMs === "number" && workerLivenessSweepMs >= 1 ? workerLivenessSweepMs : WORKER_LIVENESS_SWEEP_MS
58
69
  this.store = new BackgroundJobsStore({configuration, databaseIdentifier: config.databaseIdentifier})
59
70
  this.logger = new Logger(this)
60
71
  /**
@@ -91,8 +102,8 @@ export default class BackgroundJobsMain {
91
102
  this._orphanTimer = undefined
92
103
  /**
93
104
  * Narrows the runtime value to the documented type.
94
- * @type {ReturnType<typeof setTimeout> | undefined} */
95
- this._retentionTimer = undefined
105
+ * @type {ReturnType<typeof setInterval> | undefined} */
106
+ this._workerStaleTimer = undefined
96
107
  /**
97
108
  * Narrows the runtime value to the documented type.
98
109
  * @type {BackgroundJobsScheduler | undefined} */
@@ -144,9 +155,9 @@ export default class BackgroundJobsMain {
144
155
  void this._sweepOrphans()
145
156
  }, 60000)
146
157
 
147
- this._retentionTimer = setInterval(() => {
148
- void this._sweepRetention()
149
- }, this.retention.sweepIntervalMs)
158
+ this._workerStaleTimer = setInterval(() => {
159
+ void this._sweepStaleWorkers()
160
+ }, this.workerLivenessSweepMs)
150
161
 
151
162
  this.scheduler = new BackgroundJobsScheduler({
152
163
  configuration: this.configuration,
@@ -154,7 +165,10 @@ export default class BackgroundJobsMain {
154
165
  await this.store.enqueue({
155
166
  jobName: jobClass.jobName(),
156
167
  args,
157
- options
168
+ // Fold in the job class's static `queue` (as performLater* do) so a
169
+ // scheduled job with `static queue = "..."` lands on its queue and
170
+ // honors the configured cap without every schedule repeating it.
171
+ options: jobClass._withQueue(options)
158
172
  })
159
173
  this._notifyEnqueued()
160
174
  await this._drain()
@@ -162,6 +176,16 @@ export default class BackgroundJobsMain {
162
176
  })
163
177
  await this.scheduler.start()
164
178
 
179
+ // Retention pruning runs as an ordinary scheduled job on the normal
180
+ // scheduler (so it is visible in the job tables and dispatched to a
181
+ // worker), rather than a hidden in-process timer. Skipped when retention
182
+ // is disabled. The scheduler owns the timer, so scheduler.stop() clears it.
183
+ const retentionSchedule = PruneTerminalBackgroundJobsJob.scheduleConfiguration(this.retention)
184
+
185
+ if (retentionSchedule) {
186
+ this.scheduler.scheduleJob({jobConfiguration: retentionSchedule, jobKey: "velociousPruneTerminalBackgroundJobs"})
187
+ }
188
+
165
189
  // Startup catch-up: drain anything that was waiting before this
166
190
  // process came up. In beacon mode this is also the safety net for
167
191
  // races between attaching the connect listener and the initial
@@ -206,12 +230,12 @@ export default class BackgroundJobsMain {
206
230
  if (this._scheduledTimer) clearTimeout(this._scheduledTimer)
207
231
  if (this._errorRetryTimer) clearTimeout(this._errorRetryTimer)
208
232
  if (this._orphanTimer) clearInterval(this._orphanTimer)
209
- if (this._retentionTimer) clearInterval(this._retentionTimer)
233
+ if (this._workerStaleTimer) clearInterval(this._workerStaleTimer)
210
234
  this._pollTimer = undefined
211
235
  this._scheduledTimer = undefined
212
236
  this._errorRetryTimer = undefined
213
237
  this._orphanTimer = undefined
214
- this._retentionTimer = undefined
238
+ this._workerStaleTimer = undefined
215
239
  }
216
240
 
217
241
  /**
@@ -394,6 +418,8 @@ export default class BackgroundJobsMain {
394
418
  if (message.role === "worker") {
395
419
  jsonSocket.workerId = message.workerId
396
420
  jsonSocket.supportsHandoffIdReporting = message.supportsHandoffIdReporting === true
421
+ jsonSocket.supportsHeartbeat = message.supportsHeartbeat === true
422
+ jsonSocket.lastSeenAt = Date.now()
397
423
  this.workers.add(jsonSocket)
398
424
  this.workerHandoffs.set(jsonSocket, new Map())
399
425
  }
@@ -422,6 +448,14 @@ export default class BackgroundJobsMain {
422
448
  * @returns {void}
423
449
  */
424
450
  _handleWorkerSocketMessage({jsonSocket, message}) {
451
+ // Any message from the worker proves it is alive; the liveness sweep uses
452
+ // this to detect a wedged/silent worker.
453
+ jsonSocket.lastSeenAt = Date.now()
454
+
455
+ if (message?.type === "heartbeat") {
456
+ return
457
+ }
458
+
425
459
  if (message?.type === "ready") {
426
460
  this._handleWorkerReady({jsonSocket, message})
427
461
  return
@@ -1098,22 +1132,43 @@ export default class BackgroundJobsMain {
1098
1132
  }
1099
1133
 
1100
1134
  /**
1101
- * Deletes terminal job rows past their retention window so the jobs table
1102
- * does not grow unbounded. Runs on its own interval; failures are logged and
1103
- * retried on the next tick.
1104
- * @returns {Promise<void>} - Resolves after one retention sweep.
1135
+ * Drops workers that have gone silent past `workerStaleTimeoutMs` (no
1136
+ * heartbeat, ready, or report). A wedged worker keeps its socket open, so the
1137
+ * `close`-based cleanup never fires and its in-flight leases — and the whole
1138
+ * queue stay stuck until a human notices. Releasing the lost worker's
1139
+ * leases lets its jobs run elsewhere and stops dispatch to it; the worker's
1140
+ * own process lifecycle is the supervisor's concern.
1141
+ * @returns {Promise<void>} - Resolves after the sweep.
1105
1142
  */
1106
- async _sweepRetention() {
1107
- try {
1108
- const deleted = await this.store.pruneTerminalJobs({
1109
- completedTtlMs: this.retention.completedTtlMs,
1110
- failedTtlMs: this.retention.failedTtlMs,
1111
- batchSize: this.retention.batchSize
1112
- })
1143
+ async _sweepStaleWorkers() {
1144
+ if (this._stopped) return
1113
1145
 
1114
- if (deleted > 0) this.logger.warn(() => ["Pruned terminal background jobs", deleted])
1115
- } catch (error) {
1116
- this.logger.error(() => ["Failed to prune terminal jobs:", error])
1146
+ const cutoff = Date.now() - this.workerStaleTimeoutMs
1147
+ /** @type {JsonSocket[]} */
1148
+ const stale = []
1149
+
1150
+ for (const worker of this.workers) {
1151
+ // Only evict heartbeat-capable workers. A legacy worker (e.g. one from the
1152
+ // previous release during a rolling deploy) never heartbeats, so evicting
1153
+ // it on silence would wrongly release the leases of a job it is still
1154
+ // running. Its disconnect is still handled by the socket `close` path.
1155
+ if (!worker.supportsHeartbeat) continue
1156
+
1157
+ const lastSeenAt = typeof worker.lastSeenAt === "number" ? worker.lastSeenAt : 0
1158
+
1159
+ if (lastSeenAt <= cutoff) stale.push(worker)
1160
+ }
1161
+
1162
+ for (const worker of stale) {
1163
+ this.logger.warn(() => ["Dropping stale background jobs worker", {workerId: worker.workerId, lastSeenAt: worker.lastSeenAt}])
1164
+
1165
+ try {
1166
+ worker.close()
1167
+ } catch {
1168
+ // Already closing; the lease release below is what matters.
1169
+ }
1170
+
1171
+ await this._handleWorkerSocketClosed(worker)
1117
1172
  }
1118
1173
  }
1119
1174
  }
@@ -51,6 +51,7 @@ export default class BackgroundJobsStore {
51
51
  this.databaseIdentifier = databaseIdentifier
52
52
  this.logger = new Logger(this)
53
53
  this._readyPromise = null
54
+ this._queueConcurrencyReconciled = false
54
55
  }
55
56
 
56
57
  /**
@@ -101,8 +102,26 @@ export default class BackgroundJobsStore {
101
102
  const argsJson = JSON.stringify(args || [])
102
103
  const queue = this._normalizeQueue(options)
103
104
  const concurrency = this._resolveConcurrency(options, queue)
105
+ /** @type {string} */
106
+ let resultJobId = jobId
104
107
 
105
108
  await this._withDb(async (db) => {
109
+ if (options?.deduplicateWhileQueued && concurrency?.concurrencyKey) {
110
+ const existing = await db
111
+ .newQuery()
112
+ .from(JOBS_TABLE)
113
+ .select("id")
114
+ .where({status: "queued", concurrency_key: concurrency.concurrencyKey})
115
+ .limit(1)
116
+ .results()
117
+
118
+ if (existing[0]) {
119
+ resultJobId = String(/** @type {Record<string, ?>} */ (existing[0]).id)
120
+
121
+ return
122
+ }
123
+ }
124
+
106
125
  if (concurrency) {
107
126
  if (concurrency.queueDerived) {
108
127
  await this._ensureQueueConcurrencyKey(db, concurrency)
@@ -130,7 +149,7 @@ export default class BackgroundJobsStore {
130
149
  })
131
150
  })
132
151
 
133
- return jobId
152
+ return resultJobId
134
153
  }
135
154
 
136
155
  /**
@@ -648,6 +667,7 @@ export default class BackgroundJobsStore {
648
667
  if (alreadyApplied && await db.tableExists(JOBS_TABLE)) {
649
668
  await this._ensureJobsTableColumns(db)
650
669
  await this._ensureConcurrencyTable(db)
670
+ await this._reconcileQueueConcurrency(db)
651
671
  await this._reconcileConcurrency(db)
652
672
  return
653
673
  }
@@ -655,6 +675,7 @@ export default class BackgroundJobsStore {
655
675
  await this._applyMigrations(db)
656
676
  await this._ensureJobsTableColumns(db)
657
677
  await this._ensureConcurrencyTable(db)
678
+ await this._reconcileQueueConcurrency(db)
658
679
  await this._reconcileConcurrency(db)
659
680
 
660
681
  if (alreadyApplied) return
@@ -834,16 +855,15 @@ export default class BackgroundJobsStore {
834
855
  * @returns {Promise<void>} - Resolves when ensured.
835
856
  */
836
857
  async _ensureQueueColumn(db) {
837
- const table = await db.getTableByNameOrFail(JOBS_TABLE)
838
-
839
- if (await table.getColumnByName("queue")) return
840
-
841
858
  const lockName = `${MIGRATION_SCOPE}:queue_column`
842
859
  const acquired = await db.acquireAdvisoryLock(lockName)
843
860
 
844
861
  if (!acquired) throw new Error("Failed to acquire background jobs queue schema lock")
845
862
 
846
863
  try {
864
+ // SQL Server schema reads can deadlock with a concurrent ALTER TABLE, so
865
+ // acquire the lock before inspecting the column rather than only
866
+ // protecting the mutation (mirrors the concurrency-column migration).
847
867
  db.clearSchemaCache()
848
868
  const lockedTable = await db.getTableByNameOrFail(JOBS_TABLE)
849
869
 
@@ -1101,6 +1121,9 @@ export default class BackgroundJobsStore {
1101
1121
  if (typeof key !== "string" || key.length === 0 || !Number.isInteger(cap) || Number(cap) <= 0) {
1102
1122
  throw new Error("background job concurrencyKey and maxConcurrency must be paired; concurrencyKey must be non-empty and maxConcurrency must be a positive integer")
1103
1123
  }
1124
+ if (key.startsWith(QUEUE_CONCURRENCY_KEY_PREFIX)) {
1125
+ throw new Error(`background job concurrencyKey must not start with the reserved "${QUEUE_CONCURRENCY_KEY_PREFIX}" prefix, which is reserved for queue-derived concurrency caps`)
1126
+ }
1104
1127
  return {concurrencyKey: key, maxConcurrency: Number(cap)}
1105
1128
  }
1106
1129
 
@@ -1277,6 +1300,65 @@ export default class BackgroundJobsStore {
1277
1300
  )
1278
1301
  }
1279
1302
 
1303
+ /**
1304
+ * Reconciles queue-derived concurrency with the current configuration, once
1305
+ * per process. Enqueue only consults config for new jobs, so a cap added,
1306
+ * removed, or changed while a backlog exists otherwise leaves persisted rows
1307
+ * stale: pre-cap jobs keep a null key and bypass the cap, post-removal jobs
1308
+ * stay capped under a now-unconfigured key, and a changed numeric cap stays
1309
+ * stale until the next enqueue. Bring the durable state in line with config:
1310
+ * sync each configured queue's stored cap, adopt not-yet-keyed non-terminal
1311
+ * jobs onto their queue key, and release non-terminal jobs from queue keys
1312
+ * whose queue is no longer capped. Runs before {@link _reconcileConcurrency}
1313
+ * so the rebuilt active counts reflect the adopted/released keys.
1314
+ * @param {import("../database/drivers/base.js").default} db - Database connection.
1315
+ * @returns {Promise<void>} - Resolves when reconciled.
1316
+ */
1317
+ async _reconcileQueueConcurrency(db) {
1318
+ if (this._queueConcurrencyReconciled) return
1319
+ if (!(await db.tableExists(CONCURRENCY_TABLE))) return
1320
+
1321
+ const queuesConfig = this.configuration.getBackgroundJobsConfig().queues || {}
1322
+ const jobsTable = db.quoteTable(JOBS_TABLE)
1323
+ const keyColumn = db.quoteColumn("concurrency_key")
1324
+ const capColumn = db.quoteColumn("max_concurrency")
1325
+ const queueColumn = db.quoteColumn("queue")
1326
+ const nonTerminal = `${db.quoteColumn("status")} IN (${db.quote("queued")}, ${db.quote("handed_off")})`
1327
+ /** @type {Set<string>} */
1328
+ const cappedQueues = new Set()
1329
+
1330
+ for (const queue of Object.keys(queuesConfig)) {
1331
+ const cap = this._queueMaxConcurrency(queue)
1332
+
1333
+ if (cap === null) continue
1334
+
1335
+ cappedQueues.add(queue)
1336
+ const concurrencyKey = `${QUEUE_CONCURRENCY_KEY_PREFIX}${queue}`
1337
+
1338
+ await this._ensureQueueConcurrencyKey(db, {concurrencyKey, maxConcurrency: cap})
1339
+ await db.query(
1340
+ `UPDATE ${jobsTable} SET ${keyColumn} = ${db.quote(concurrencyKey)}, ${capColumn} = ${Number(cap)} ` +
1341
+ `WHERE ${queueColumn} = ${db.quote(queue)} AND ${keyColumn} IS NULL AND ${nonTerminal}`
1342
+ )
1343
+ }
1344
+
1345
+ const concurrencyRows = await db.newQuery().from(CONCURRENCY_TABLE).select("concurrency_key").results()
1346
+
1347
+ for (const row of concurrencyRows) {
1348
+ const concurrencyKey = String(/** @type {Record<string, ?>} */ (row).concurrency_key)
1349
+
1350
+ if (!concurrencyKey.startsWith(QUEUE_CONCURRENCY_KEY_PREFIX)) continue
1351
+ if (cappedQueues.has(concurrencyKey.slice(QUEUE_CONCURRENCY_KEY_PREFIX.length))) continue
1352
+
1353
+ await db.query(
1354
+ `UPDATE ${jobsTable} SET ${keyColumn} = NULL, ${capColumn} = NULL ` +
1355
+ `WHERE ${keyColumn} = ${db.quote(concurrencyKey)} AND ${nonTerminal}`
1356
+ )
1357
+ }
1358
+
1359
+ this._queueConcurrencyReconciled = true
1360
+ }
1361
+
1280
1362
  /**
1281
1363
  * Runs normalize number.
1282
1364
  * @param {?} value - Input value.
@@ -16,6 +16,7 @@
16
16
  * @property {string} [queue] - Queue name. Defaults to `"default"`. When the queue has a configured cap in `backgroundJobs.queues`, that cap is enforced cluster-wide.
17
17
  * @property {string} [concurrencyKey] - Opaque non-empty key used to share a concurrency cap. Overrides any queue-derived cap.
18
18
  * @property {number} [maxConcurrency] - Positive integer cap; must be paired with `concurrencyKey`.
19
+ * @property {boolean} [deduplicateWhileQueued] - When true (requires `concurrencyKey`), skip the enqueue if a still-queued job with the same `concurrencyKey` already exists, returning that job's id. Keeps an interval-scheduled recurring job (e.g. retention pruning) from piling up redundant queued rows when it runs slower than its interval or no worker is free.
19
20
  */
20
21
  /**
21
22
  * @typedef {object} BackgroundJobPayload
@@ -65,9 +66,10 @@
65
66
  * @typedef {"worker" | "client" | "reporter"} BackgroundJobSocketRole
66
67
  */
67
68
  /**
68
- * @typedef {{type: "hello", role: BackgroundJobSocketRole, supportsHandoffIdReporting?: boolean, workerId?: string}} BackgroundJobHelloMessage
69
+ * @typedef {{type: "hello", role: BackgroundJobSocketRole, supportsHandoffIdReporting?: boolean, supportsHeartbeat?: boolean, workerId?: string}} BackgroundJobHelloMessage
69
70
  * @typedef {{type: "ready", acceptsForked?: boolean, acceptsInline?: boolean, acceptsSpawned?: boolean}} BackgroundJobReadyMessage
70
71
  * @typedef {{type: "draining"}} BackgroundJobDrainingMessage
72
+ * @typedef {{type: "heartbeat", workerId?: string}} BackgroundJobHeartbeatMessage
71
73
  * @typedef {{type: "enqueue", jobName: string, args?: Array<?>, options?: BackgroundJobOptions}} BackgroundJobEnqueueMessage
72
74
  * @typedef {{type: "enqueued", jobId: string}} BackgroundJobEnqueuedMessage
73
75
  * @typedef {{type: "enqueue-error", error?: string}} BackgroundJobEnqueueErrorMessage
@@ -78,7 +80,7 @@
78
80
  * @typedef {{type: "job-update-error", jobId: string, error?: string}} BackgroundJobUpdateErrorMessage
79
81
  */
80
82
  /**
81
- * @typedef {BackgroundJobHelloMessage | BackgroundJobReadyMessage | BackgroundJobDrainingMessage | BackgroundJobEnqueueMessage | BackgroundJobEnqueuedMessage | BackgroundJobEnqueueErrorMessage | BackgroundJobJobMessage | BackgroundJobCompleteMessage | BackgroundJobFailedMessage | BackgroundJobUpdatedMessage | BackgroundJobUpdateErrorMessage} BackgroundJobSocketMessage
83
+ * @typedef {BackgroundJobHelloMessage | BackgroundJobReadyMessage | BackgroundJobDrainingMessage | BackgroundJobHeartbeatMessage | BackgroundJobEnqueueMessage | BackgroundJobEnqueuedMessage | BackgroundJobEnqueueErrorMessage | BackgroundJobJobMessage | BackgroundJobCompleteMessage | BackgroundJobFailedMessage | BackgroundJobUpdatedMessage | BackgroundJobUpdateErrorMessage} BackgroundJobSocketMessage
82
84
  */
83
85
 
84
86
  export const nothing = {}