velocious 1.0.731 → 1.0.733
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 +1 -1
- package/build/background-jobs/local-store.js +86 -6
- package/build/background-jobs/queue-priority-order.js +57 -1
- package/build/background-jobs/store.js +89 -3
- package/build/src/background-jobs/local-store.d.ts +37 -0
- package/build/src/background-jobs/local-store.d.ts.map +1 -1
- package/build/src/background-jobs/local-store.js +78 -7
- package/build/src/background-jobs/queue-priority-order.d.ts +47 -0
- package/build/src/background-jobs/queue-priority-order.d.ts.map +1 -1
- package/build/src/background-jobs/queue-priority-order.js +54 -2
- package/build/src/background-jobs/store.d.ts +28 -0
- package/build/src/background-jobs/store.d.ts.map +1 -1
- package/build/src/background-jobs/store.js +80 -4
- package/package.json +1 -1
- package/src/background-jobs/local-store.js +86 -6
- package/src/background-jobs/queue-priority-order.js +57 -1
- package/src/background-jobs/store.js +89 -3
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
rescheduledBackgroundJobAtMs,
|
|
22
22
|
retryDelayMs
|
|
23
23
|
} from "./job-semantics.js"
|
|
24
|
-
import {buildQueuePriorityOrderSql} from "./queue-priority-order.js"
|
|
24
|
+
import {buildLaneFairnessOrderSql, buildQueuePriorityOrderSql, laneConcurrencyKey} from "./queue-priority-order.js"
|
|
25
25
|
|
|
26
26
|
export const LOCAL_BACKGROUND_JOBS_TABLE = "velocious_local_background_jobs"
|
|
27
27
|
export const LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE = "velocious_local_background_job_concurrency"
|
|
@@ -67,7 +67,7 @@ const EXPECTED_JOB_COLUMNS = [
|
|
|
67
67
|
"child_pid",
|
|
68
68
|
"runner_hostname"
|
|
69
69
|
]
|
|
70
|
-
const EXPECTED_CONCURRENCY_COLUMNS = ["concurrency_key", "max_concurrency", "active_count"]
|
|
70
|
+
const EXPECTED_CONCURRENCY_COLUMNS = ["concurrency_key", "max_concurrency", "active_count", "last_claimed_at_ms"]
|
|
71
71
|
const EXPECTED_SCHEDULE_KEY_COLUMNS = ["schedule_key", "job_id"]
|
|
72
72
|
/** @type {WeakMap<import("../configuration.js").default, Map<string, Promise<void>>>} */
|
|
73
73
|
const deduplicatedEnqueueChains = new WeakMap()
|
|
@@ -228,6 +228,7 @@ export default class LocalBackgroundJobsStore {
|
|
|
228
228
|
await db.createTable(this._concurrencyTableData())
|
|
229
229
|
changed = true
|
|
230
230
|
} else {
|
|
231
|
+
if (await this._ensureConcurrencyColumns(db)) changed = true
|
|
231
232
|
await this._assertColumns(db, LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE, EXPECTED_CONCURRENCY_COLUMNS)
|
|
232
233
|
}
|
|
233
234
|
|
|
@@ -258,12 +259,35 @@ export default class LocalBackgroundJobsStore {
|
|
|
258
259
|
* @returns {Promise<boolean>} - Whether a column was added.
|
|
259
260
|
*/
|
|
260
261
|
async _ensureJobColumns(db) {
|
|
262
|
+
return await this._ensureMissingColumns(db, LOCAL_BACKGROUND_JOBS_TABLE, this._jobsTableData())
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Idempotently adds the concurrency-table columns an existing local table is missing,
|
|
267
|
+
* so an upgraded framework keeps its live counters instead of failing the assertion.
|
|
268
|
+
* @param {import("../database/drivers/base.js").default} db - Local SQLite connection.
|
|
269
|
+
* @returns {Promise<boolean>} - Whether a column was added.
|
|
270
|
+
*/
|
|
271
|
+
async _ensureConcurrencyColumns(db) {
|
|
272
|
+
return await this._ensureMissingColumns(db, LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE, this._concurrencyTableData())
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Idempotently adds columns from the current table definition that an existing local
|
|
277
|
+
* table is missing, so an upgraded framework finds a compatible schema instead of
|
|
278
|
+
* failing the column assertion.
|
|
279
|
+
* @param {import("../database/drivers/base.js").default} db - Local SQLite connection.
|
|
280
|
+
* @param {string} tableName - Table to upgrade.
|
|
281
|
+
* @param {TableData} definition - Current definition whose columns are required.
|
|
282
|
+
* @returns {Promise<boolean>} - Whether a column was added.
|
|
283
|
+
*/
|
|
284
|
+
async _ensureMissingColumns(db, tableName, definition) {
|
|
261
285
|
db.clearSchemaCache()
|
|
262
|
-
const table = await db.getTableByNameOrFail(
|
|
263
|
-
const tableData = new TableData(
|
|
286
|
+
const table = await db.getTableByNameOrFail(tableName)
|
|
287
|
+
const tableData = new TableData(tableName)
|
|
264
288
|
let added = false
|
|
265
289
|
|
|
266
|
-
for (const column of
|
|
290
|
+
for (const column of definition.getColumns()) {
|
|
267
291
|
if (await table.getColumnByName(column.getName())) continue
|
|
268
292
|
if (column.getPrimaryKey()) continue
|
|
269
293
|
|
|
@@ -355,6 +379,7 @@ export default class LocalBackgroundJobsStore {
|
|
|
355
379
|
table.string("concurrency_key", {null: false, primaryKey: true})
|
|
356
380
|
table.integer("max_concurrency", {null: false})
|
|
357
381
|
table.integer("active_count", {null: false})
|
|
382
|
+
table.bigint("last_claimed_at_ms", {null: true})
|
|
358
383
|
return table
|
|
359
384
|
}
|
|
360
385
|
|
|
@@ -913,7 +938,17 @@ export default class LocalBackgroundJobsStore {
|
|
|
913
938
|
`${concurrencyTable}.${db.quoteColumn("active_count")} < ${concurrencyTable}.${db.quoteColumn("max_concurrency")}))`
|
|
914
939
|
)
|
|
915
940
|
|
|
916
|
-
if (priorityOrder)
|
|
941
|
+
if (priorityOrder) {
|
|
942
|
+
query = query.order(`${priorityOrder} DESC`)
|
|
943
|
+
|
|
944
|
+
// The aging term levels every aged lane onto the same ceiling rank, so at the
|
|
945
|
+
// ceiling the configured priority no longer separates lanes and the claim would
|
|
946
|
+
// fall through to `scheduled_at_ms` — permanently won by whichever lane holds the
|
|
947
|
+
// deepest aged backlog. Break that tie by lane service instead.
|
|
948
|
+
const laneFairnessOrder = this._laneFairnessOrderSql(db, nowMs)
|
|
949
|
+
|
|
950
|
+
if (laneFairnessOrder) query = query.order(laneFairnessOrder)
|
|
951
|
+
}
|
|
917
952
|
|
|
918
953
|
const rows = await query
|
|
919
954
|
.order("scheduled_at_ms ASC")
|
|
@@ -1005,6 +1040,8 @@ export default class LocalBackgroundJobsStore {
|
|
|
1005
1040
|
return null
|
|
1006
1041
|
}
|
|
1007
1042
|
|
|
1043
|
+
await this._recordLaneClaim(db, job.queue, handedOffAtMs)
|
|
1044
|
+
|
|
1008
1045
|
return {handedOffAtMs, handoffId}
|
|
1009
1046
|
}))
|
|
1010
1047
|
}
|
|
@@ -1348,6 +1385,32 @@ export default class LocalBackgroundJobsStore {
|
|
|
1348
1385
|
return affectedRows === 1
|
|
1349
1386
|
}
|
|
1350
1387
|
|
|
1388
|
+
/**
|
|
1389
|
+
* Stamps the instant a lane received a claim so {@link _laneFairnessOrderSql} can break the
|
|
1390
|
+
* aging-ceiling tie by lane service rather than by backlog age. The lane is the queue: a job
|
|
1391
|
+
* claimed under an explicit `concurrencyKey` still consumed its queue's turn, and leaving that
|
|
1392
|
+
* claim unstamped made the tie-break term equal the row's own age again — which the deepest
|
|
1393
|
+
* aged backlog won every cycle.
|
|
1394
|
+
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1395
|
+
* @param {string | null} queue - Queue of the job that just claimed.
|
|
1396
|
+
* @param {number} claimedAtMs - Claim instant.
|
|
1397
|
+
* @returns {Promise<void>} - Resolves when the lane counter is stamped.
|
|
1398
|
+
*/
|
|
1399
|
+
async _recordLaneClaim(db, queue, claimedAtMs) {
|
|
1400
|
+
const normalizedQueue = queue || DEFAULT_BACKGROUND_JOB_QUEUE
|
|
1401
|
+
const queues = this.configuration.getBackgroundJobsConfig().queues || {}
|
|
1402
|
+
const cap = queues[normalizedQueue]?.maxConcurrent
|
|
1403
|
+
|
|
1404
|
+
if (!Number.isInteger(cap) || Number(cap) <= 0) return
|
|
1405
|
+
|
|
1406
|
+
const concurrencyKey = laneConcurrencyKey(normalizedQueue)
|
|
1407
|
+
await this._ensureConcurrency(db, {concurrencyKey, maxConcurrency: Number(cap), queueDerived: true})
|
|
1408
|
+
|
|
1409
|
+
const table = db.quoteTable(LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE)
|
|
1410
|
+
|
|
1411
|
+
await db.query(`UPDATE ${table} SET ${db.quoteColumn("last_claimed_at_ms")} = ${Number(claimedAtMs)} WHERE ${db.quoteColumn("concurrency_key")} = ${db.quote(concurrencyKey)}`)
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1351
1414
|
/**
|
|
1352
1415
|
* Releases one slot for a concurrency key.
|
|
1353
1416
|
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
@@ -1412,6 +1475,23 @@ export default class LocalBackgroundJobsStore {
|
|
|
1412
1475
|
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs})
|
|
1413
1476
|
}
|
|
1414
1477
|
|
|
1478
|
+
/**
|
|
1479
|
+
* Builds the lane-fairness tie-break for the queued-job claim. See
|
|
1480
|
+
* {@link buildLaneFairnessOrderSql} for the ranking contract.
|
|
1481
|
+
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1482
|
+
* @param {number} nowMs - Dispatch instant.
|
|
1483
|
+
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
1484
|
+
*/
|
|
1485
|
+
_laneFairnessOrderSql(db, nowMs) {
|
|
1486
|
+
return buildLaneFairnessOrderSql({
|
|
1487
|
+
configuration: this.configuration,
|
|
1488
|
+
db,
|
|
1489
|
+
nowMs,
|
|
1490
|
+
jobsTable: db.quoteTable(LOCAL_BACKGROUND_JOBS_TABLE),
|
|
1491
|
+
concurrencyTable: db.quoteTable(LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE)
|
|
1492
|
+
})
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1415
1495
|
/**
|
|
1416
1496
|
* Checks whether a persisted handoff owns the supplied acknowledgement fence.
|
|
1417
1497
|
* @param {import("./types.js").BackgroundJobRow | null} job - Persisted job.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import {DEFAULT_BACKGROUND_JOB_QUEUE} from "./job-semantics.js"
|
|
3
|
+
import {DEFAULT_BACKGROUND_JOB_QUEUE, QUEUE_CONCURRENCY_KEY_PREFIX} from "./job-semantics.js"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* How many milliseconds of eligible waiting cancel one unit of `queues[name].priority`.
|
|
@@ -94,3 +94,59 @@ export function buildQueuePriorityOrderSql({configuration, db, nowMs}) {
|
|
|
94
94
|
`CASE WHEN ${scheduledAtColumn} <= (${db.quote(nowMs)} - ${agingCap}) THEN ${agingCap} ` +
|
|
95
95
|
`ELSE (${db.quote(nowMs)} - ${scheduledAtColumn}) END)`
|
|
96
96
|
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The counter row that represents a lane: the queue's own concurrency counter. An explicit
|
|
100
|
+
* `concurrencyKey` groups unrelated jobs, so it never names a lane.
|
|
101
|
+
* @param {string} queue - Normalized queue name.
|
|
102
|
+
* @returns {string} - Queue-derived concurrency key.
|
|
103
|
+
*/
|
|
104
|
+
export function laneConcurrencyKey(queue) {
|
|
105
|
+
return `${QUEUE_CONCURRENCY_KEY_PREFIX}${queue}`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Builds the lane-fairness tie-break term for the queued-job claim: how long the candidate's
|
|
110
|
+
* own lane has gone without receiving a claim, longest-starved-first.
|
|
111
|
+
*
|
|
112
|
+
* Reaching the aging ceiling is not the same as winning the claim. Because every lane converges
|
|
113
|
+
* on the same ceiling, all aged lanes tie, and a tie broken by `scheduled_at_ms` is won
|
|
114
|
+
* permanently by whichever lane holds the deepest aged backlog: a lane with one fresh row is
|
|
115
|
+
* only served when the backlog lane reaches its own `maxConcurrent` cap, which never happens
|
|
116
|
+
* when that cap is far above what the fleet can actually run. Ordering by time-since-last-claim
|
|
117
|
+
* makes the ceiling tie alternate between starved lanes.
|
|
118
|
+
*
|
|
119
|
+
* The lane is the queue, not the candidate's concurrency counter. Most of a busy lane's rows can
|
|
120
|
+
* sit under explicit per-group keys (TensorBuzz groups its `builds` backlog by
|
|
121
|
+
* `cleanup-build-run-output-<build group id>`), and those keys are deliberately not stamped —
|
|
122
|
+
* reading them would make the term equal the row's own age again and hand the tie back to the
|
|
123
|
+
* deepest backlog, which is the monopoly this term exists to break. Every row of a queue
|
|
124
|
+
* therefore reads its queue's counter, which keeps the term constant within a lane and leaves
|
|
125
|
+
* intra-lane order untouched.
|
|
126
|
+
*
|
|
127
|
+
* A queue with no counter row (no configured `maxConcurrent`, or a queue name the configuration
|
|
128
|
+
* does not declare) falls back to the row's own eligible wait, which reproduces the previous
|
|
129
|
+
* `scheduled_at_ms ASC` ordering exactly.
|
|
130
|
+
*
|
|
131
|
+
* Returns null when aging is opted out (`queuePriorityAgingUnitMs: null`): opting out asks for
|
|
132
|
+
* strict queue-priority ordering, so no time-based reordering is applied.
|
|
133
|
+
* @param {object} args - Options.
|
|
134
|
+
* @param {import("../configuration.js").default} args.configuration - Active configuration.
|
|
135
|
+
* @param {import("../database/drivers/base.js").default} args.db - Connection.
|
|
136
|
+
* @param {number} args.nowMs - Dispatch instant.
|
|
137
|
+
* @param {string} args.jobsTable - Quoted jobs table name.
|
|
138
|
+
* @param {string} args.concurrencyTable - Quoted concurrency counter table name.
|
|
139
|
+
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
140
|
+
*/
|
|
141
|
+
export function buildLaneFairnessOrderSql({configuration, db, nowMs, jobsTable, concurrencyTable}) {
|
|
142
|
+
if (queuePriorityAgingUnitMs(configuration) === null) return null
|
|
143
|
+
|
|
144
|
+
const queues = configuration.getBackgroundJobsConfig().queues || {}
|
|
145
|
+
const queueExpression = `COALESCE(${jobsTable}.${db.quoteColumn("queue")}, ${db.quote(DEFAULT_BACKGROUND_JOB_QUEUE)})`
|
|
146
|
+
const laneKeyCase = `CASE ${queueExpression} ${Object.keys(queues).map((queue) => `WHEN ${db.quote(queue)} THEN ${db.quote(laneConcurrencyKey(queue))}`).join(" ")} ELSE NULL END`
|
|
147
|
+
const lastClaimedLookup =
|
|
148
|
+
`(SELECT ${concurrencyTable}.${db.quoteColumn("last_claimed_at_ms")} FROM ${concurrencyTable} ` +
|
|
149
|
+
`WHERE ${concurrencyTable}.${db.quoteColumn("concurrency_key")} = ${laneKeyCase})`
|
|
150
|
+
|
|
151
|
+
return `(${Number(nowMs)} - COALESCE(${lastClaimedLookup}, ${jobsTable}.${db.quoteColumn("scheduled_at_ms")})) DESC`
|
|
152
|
+
}
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
rescheduledBackgroundJobAtMs,
|
|
28
28
|
retryDelayMs
|
|
29
29
|
} from "./job-semantics.js"
|
|
30
|
-
import {buildQueuePriorityOrderSql} from "./queue-priority-order.js"
|
|
30
|
+
import {buildLaneFairnessOrderSql, buildQueuePriorityOrderSql, laneConcurrencyKey} from "./queue-priority-order.js"
|
|
31
31
|
import {
|
|
32
32
|
MAIL_DELIVERY_OPERATIONS_TABLE,
|
|
33
33
|
mailDeliveryOperationForJob,
|
|
@@ -1218,7 +1218,17 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1218
1218
|
if (scheduledAtOperator === "<=") {
|
|
1219
1219
|
const priorityOrder = this._queuePriorityOrderSql(db, now)
|
|
1220
1220
|
|
|
1221
|
-
if (priorityOrder)
|
|
1221
|
+
if (priorityOrder) {
|
|
1222
|
+
query = query.order(`${priorityOrder} DESC`)
|
|
1223
|
+
|
|
1224
|
+
// The aging term levels every aged lane onto the same ceiling rank, so at the
|
|
1225
|
+
// ceiling the configured priority no longer separates lanes and the claim would
|
|
1226
|
+
// fall through to `scheduled_at_ms` — permanently won by whichever lane holds the
|
|
1227
|
+
// deepest aged backlog. Break that tie by lane service instead.
|
|
1228
|
+
const laneFairnessOrder = this._laneFairnessOrderSql(db, now)
|
|
1229
|
+
|
|
1230
|
+
if (laneFairnessOrder) query = query.order(laneFairnessOrder)
|
|
1231
|
+
}
|
|
1222
1232
|
|
|
1223
1233
|
// Job-level priority applies inside the queue ranking above. A retry is
|
|
1224
1234
|
// stored at RETRY_BACKGROUND_JOB_PRIORITY (-1), so it sorts behind every
|
|
@@ -1259,6 +1269,23 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1259
1269
|
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs})
|
|
1260
1270
|
}
|
|
1261
1271
|
|
|
1272
|
+
/**
|
|
1273
|
+
* Builds the lane-fairness tie-break for the queued-job claim. See
|
|
1274
|
+
* {@link buildLaneFairnessOrderSql} for the ranking contract.
|
|
1275
|
+
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1276
|
+
* @param {number} nowMs - Dispatch instant.
|
|
1277
|
+
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
1278
|
+
*/
|
|
1279
|
+
_laneFairnessOrderSql(db, nowMs) {
|
|
1280
|
+
return buildLaneFairnessOrderSql({
|
|
1281
|
+
configuration: this.configuration,
|
|
1282
|
+
db,
|
|
1283
|
+
nowMs,
|
|
1284
|
+
jobsTable: db.quoteTable(JOBS_TABLE),
|
|
1285
|
+
concurrencyTable: db.quoteTable(CONCURRENCY_TABLE)
|
|
1286
|
+
})
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1262
1289
|
/**
|
|
1263
1290
|
* Reads the per-lane dispatch boundary for every queue holding eligible queued work:
|
|
1264
1291
|
* backlog depth, the oldest eligible row, and how many jobs that lane actually claimed
|
|
@@ -1501,6 +1528,7 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1501
1528
|
return null
|
|
1502
1529
|
}
|
|
1503
1530
|
|
|
1531
|
+
await this._recordLaneClaim(db, queuedJob.queue, handedOffAtMs)
|
|
1504
1532
|
await this._recordStatusTransition(db, "queued", "handed_off")
|
|
1505
1533
|
/** @type {import("./types.js").BackgroundJobRow} */
|
|
1506
1534
|
const handedOffJob = {
|
|
@@ -3642,14 +3670,47 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
3642
3670
|
* @returns {Promise<void>} - Resolves when ready.
|
|
3643
3671
|
*/
|
|
3644
3672
|
async _ensureConcurrencyTable(db) {
|
|
3645
|
-
if (await db.tableExists(CONCURRENCY_TABLE))
|
|
3673
|
+
if (await db.tableExists(CONCURRENCY_TABLE)) {
|
|
3674
|
+
await this._ensureConcurrencyTableColumns(db)
|
|
3675
|
+
return
|
|
3676
|
+
}
|
|
3646
3677
|
const table = new TableData(CONCURRENCY_TABLE, {ifNotExists: true})
|
|
3647
3678
|
table.string("concurrency_key", {primaryKey: true})
|
|
3648
3679
|
table.integer("max_concurrency", {null: false})
|
|
3649
3680
|
table.integer("active_count", {null: false})
|
|
3681
|
+
table.bigint("last_claimed_at_ms", {null: true})
|
|
3650
3682
|
await db.createTable(table)
|
|
3651
3683
|
}
|
|
3652
3684
|
|
|
3685
|
+
/**
|
|
3686
|
+
* Idempotently adds the lane-service stamp column to a concurrency table created by an
|
|
3687
|
+
* earlier framework version. Existing counters carry live `active_count` values, so the
|
|
3688
|
+
* column is added in place under the same advisory lock the jobs-table column adds use.
|
|
3689
|
+
* @param {import("../database/drivers/base.js").default} db - Database connection.
|
|
3690
|
+
* @returns {Promise<void>} - Resolves when the column is present.
|
|
3691
|
+
*/
|
|
3692
|
+
async _ensureConcurrencyTableColumns(db) {
|
|
3693
|
+
const lockName = `${MIGRATION_SCOPE}:concurrency_last_claimed_column`
|
|
3694
|
+
const acquired = await db.acquireAdvisoryLock(lockName)
|
|
3695
|
+
|
|
3696
|
+
if (!acquired) throw new Error("Failed to acquire background jobs lane-claim schema lock")
|
|
3697
|
+
|
|
3698
|
+
try {
|
|
3699
|
+
db.clearSchemaCache()
|
|
3700
|
+
const table = await db.getTableByNameOrFail(CONCURRENCY_TABLE)
|
|
3701
|
+
|
|
3702
|
+
if (!(await table.getColumnByName("last_claimed_at_ms"))) {
|
|
3703
|
+
const tableData = new TableData(CONCURRENCY_TABLE)
|
|
3704
|
+
tableData.bigint("last_claimed_at_ms", {null: true})
|
|
3705
|
+
|
|
3706
|
+
for (const sql of await db.alterTableSQLs(tableData)) await db.query(sql)
|
|
3707
|
+
db.clearSchemaCache()
|
|
3708
|
+
}
|
|
3709
|
+
} finally {
|
|
3710
|
+
await db.releaseAdvisoryLock(lockName)
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
|
|
3653
3714
|
/**
|
|
3654
3715
|
* Ensures the stable schedule-key ownership table exists.
|
|
3655
3716
|
* @param {import("../database/drivers/base.js").default} db - Database connection.
|
|
@@ -3970,6 +4031,31 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
3970
4031
|
return affectedRows === 1
|
|
3971
4032
|
}
|
|
3972
4033
|
|
|
4034
|
+
/**
|
|
4035
|
+
* Stamps the instant a lane received a claim so {@link _laneFairnessOrderSql} can break the
|
|
4036
|
+
* aging-ceiling tie by lane service rather than by backlog age. The lane is the queue: a job
|
|
4037
|
+
* claimed under an explicit `concurrencyKey` still consumed its queue's turn, and leaving that
|
|
4038
|
+
* claim unstamped made the tie-break term equal the row's own age again — which the deepest
|
|
4039
|
+
* aged backlog won every cycle.
|
|
4040
|
+
* @param {import("../database/drivers/base.js").default} db - Database connection.
|
|
4041
|
+
* @param {string | null} queue - Queue of the job that just claimed.
|
|
4042
|
+
* @param {number} claimedAtMs - Claim instant.
|
|
4043
|
+
* @returns {Promise<void>} - Resolves when the lane counter is stamped.
|
|
4044
|
+
*/
|
|
4045
|
+
async _recordLaneClaim(db, queue, claimedAtMs) {
|
|
4046
|
+
const normalizedQueue = queue || DEFAULT_BACKGROUND_JOB_QUEUE
|
|
4047
|
+
const cap = this._queueMaxConcurrency(normalizedQueue)
|
|
4048
|
+
|
|
4049
|
+
if (cap === null) return
|
|
4050
|
+
|
|
4051
|
+
const concurrencyKey = laneConcurrencyKey(normalizedQueue)
|
|
4052
|
+
await this._ensureQueueConcurrencyKey(db, {concurrencyKey, maxConcurrency: cap})
|
|
4053
|
+
|
|
4054
|
+
const table = db.quoteTable(CONCURRENCY_TABLE)
|
|
4055
|
+
|
|
4056
|
+
await db.query(`UPDATE ${table} SET ${db.quoteColumn("last_claimed_at_ms")} = ${Number(claimedAtMs)} WHERE ${db.quoteColumn("concurrency_key")} = ${db.quote(concurrencyKey)}`)
|
|
4057
|
+
}
|
|
4058
|
+
|
|
3973
4059
|
/**
|
|
3974
4060
|
* Runs a portable update and returns its affected-row count.
|
|
3975
4061
|
* @param {import("../database/drivers/base.js").default} db - Database connection.
|