@portqueue/core 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/conformance/index.cjs +70 -1
  2. package/dist/conformance/index.cjs.map +1 -1
  3. package/dist/conformance/index.d.ts +4 -2
  4. package/dist/conformance/index.mjs +70 -1
  5. package/dist/conformance/index.mjs.map +1 -1
  6. package/dist/constants/job.constants.d.ts +3 -1
  7. package/dist/constants/storage.constants.d.ts +8 -0
  8. package/dist/constants/time.constants.d.ts +13 -0
  9. package/dist/constants/worker.constants.d.ts +5 -0
  10. package/dist/drivers/driver.interface.d.ts +47 -0
  11. package/dist/drivers/mysql/index.cjs +169 -76
  12. package/dist/drivers/mysql/index.cjs.map +1 -1
  13. package/dist/drivers/mysql/index.mjs +169 -76
  14. package/dist/drivers/mysql/index.mjs.map +1 -1
  15. package/dist/drivers/mysql/mysql-driver.d.ts +42 -19
  16. package/dist/drivers/oracle/index.cjs +144 -65
  17. package/dist/drivers/oracle/index.cjs.map +1 -1
  18. package/dist/drivers/oracle/index.mjs +144 -65
  19. package/dist/drivers/oracle/index.mjs.map +1 -1
  20. package/dist/drivers/oracle/oracle-driver.d.ts +34 -15
  21. package/dist/drivers/pglite/index.cjs +3 -3
  22. package/dist/drivers/pglite/index.mjs +3 -3
  23. package/dist/drivers/postgresql/index.cjs +1 -1
  24. package/dist/drivers/postgresql/index.mjs +1 -1
  25. package/dist/drivers/postgresql/postgresql-driver.d.ts +33 -7
  26. package/dist/drivers/shared/base-driver.d.ts +2 -0
  27. package/dist/drivers/shared/migration-runner.service.d.ts +19 -4
  28. package/dist/drivers/shared/migration.d.ts +5 -3
  29. package/dist/drivers/sqlite/index.cjs +113 -42
  30. package/dist/drivers/sqlite/index.cjs.map +1 -1
  31. package/dist/drivers/sqlite/index.mjs +113 -42
  32. package/dist/drivers/sqlite/index.mjs.map +1 -1
  33. package/dist/drivers/sqlite/sqlite-driver.d.ts +26 -0
  34. package/dist/drivers/table-names.d.ts +3 -2
  35. package/dist/index.cjs +519 -316
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.mjs +521 -318
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/{migration-D73ANkYV.js → migration-D0IRP2oq.js} +6 -2
  41. package/dist/migration-D0IRP2oq.js.map +1 -0
  42. package/dist/{migration-CoTjpgBl.cjs → migration-Dx8eUthW.cjs} +6 -2
  43. package/dist/migration-Dx8eUthW.cjs.map +1 -0
  44. package/dist/monitoring/system-monitor.service.d.ts +6 -5
  45. package/dist/options/enums.d.ts +22 -0
  46. package/dist/options/job-options.d.ts +2 -1
  47. package/dist/port-queue.d.ts +76 -4
  48. package/dist/{postgresql-driver-BUHuyRYj.js → postgresql-driver-CPHv5SUT.js} +128 -55
  49. package/dist/postgresql-driver-CPHv5SUT.js.map +1 -0
  50. package/dist/{postgresql-driver-CIk94XCq.cjs → postgresql-driver-DRT68TKb.cjs} +128 -55
  51. package/dist/postgresql-driver-DRT68TKb.cjs.map +1 -0
  52. package/dist/producer/producer.service.d.ts +12 -0
  53. package/dist/producer/results.d.ts +35 -1
  54. package/dist/{storage.constants-B5oQjxp5.cjs → storage.constants-BKeXcSQe.cjs} +13 -1
  55. package/dist/storage.constants-BKeXcSQe.cjs.map +1 -0
  56. package/dist/{storage.constants-DwSzjjMZ.js → storage.constants-DYEQUmc8.js} +14 -2
  57. package/dist/storage.constants-DYEQUmc8.js.map +1 -0
  58. package/package.json +1 -1
  59. package/dist/migration-CoTjpgBl.cjs.map +0 -1
  60. package/dist/migration-D73ANkYV.js.map +0 -1
  61. package/dist/postgresql-driver-BUHuyRYj.js.map +0 -1
  62. package/dist/postgresql-driver-CIk94XCq.cjs.map +0 -1
  63. package/dist/storage.constants-B5oQjxp5.cjs.map +0 -1
  64. package/dist/storage.constants-DwSzjjMZ.js.map +0 -1
@@ -45,7 +45,8 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
45
45
  readonly migrations: MigrationStep[];
46
46
  private pool;
47
47
  /**
48
- * Connections holding the advisory locks this driver has taken, keyed by lock name. `GET_LOCK` is
48
+ * Connections holding the advisory locks this driver has taken, keyed by the runner's key and
49
+ * carrying the server-side name it resolved to (see `tryAdvisoryLock`). `GET_LOCK` is
49
50
  * scoped to the **session**, so the connection that took a lock is the only one that can release
50
51
  * it — and going through the pool would release it on whichever connection came out next.
51
52
  */
@@ -60,9 +61,12 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
60
61
  * fresh database all ran the full DDL at the same time, surviving only because `v1` is
61
62
  * `IF NOT EXISTS`-guarded throughout.
62
63
  *
63
- * MySQL lock names are capped at 64 characters, and the key is a table prefix plus a word, so it
64
- * is passed through as-is rather than hashed — a readable name in `performance_schema` is worth
65
- * more than the few bytes.
64
+ * A `GET_LOCK` name is **server-wide**, wider than the database the tables live in. The runner's
65
+ * key already carries the schema when one is configured; without one the tables live in the
66
+ * connection's default database, so the name is prefixed with `DATABASE()` here — otherwise two
67
+ * databases on one server with the default prefix would share one migration lock. A name is kept
68
+ * readable (for `performance_schema`) when it fits MySQL's 64-character cap and hashed when it does
69
+ * not, because truncating would let two different keys collapse into one lock.
66
70
  */
67
71
  tryAdvisoryLock(options: {
68
72
  key: string;
@@ -88,16 +92,9 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
88
92
  */
89
93
  getSchemaVersion(): Promise<number | null>;
90
94
  /**
91
- * Apply one migration's DDL, then record its version.
92
- *
93
- * MySQL commits DDL implicitly, so — unlike every other engine here — these cannot share one
94
- * transaction. That is safe because every statement in the migration is `IF NOT EXISTS`: a crash
95
- * between the DDL and the version row leaves a schema the next run re-applies as a no-op and then
96
- * records. The version insert is `INSERT IGNORE` for the same reason.
97
- */
98
- /**
99
- * Apply one step, then record it — **not** in one transaction, and it cannot be: MySQL commits each DDL statement implicitly, so no transaction can span them. A `BEGIN`
100
- * around them would be silently discarded at the first `CREATE TABLE`.
95
+ * Apply one step, then record it — **not** in one transaction, and it cannot be: MySQL commits
96
+ * each DDL statement implicitly, so no transaction can span them. A `BEGIN` around them would be
97
+ * silently discarded at the first `CREATE TABLE`.
101
98
  *
102
99
  * What holds instead: every statement in this engine's migrations is idempotent, so an
103
100
  * interruption between the DDL and the version insert is repaired by re-running the step, and the
@@ -121,11 +118,6 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
121
118
  jobs: NewJobRecord[];
122
119
  tx?: TransactionHandle;
123
120
  }): Promise<InsertedJobRow[]>;
124
- /**
125
- * Locate the existing jobs that dedup this batch, in one lookup per distinct queue and key kind
126
- * rather than two per job. No unique index enforces either key — dedup lives in code so every
127
- * engine behaves identically and a duplicate returns the existing job instead of raising.
128
- */
129
121
  /**
130
122
  * Serialize the dedup-lookup-then-insert pair against another producer's, per queue.
131
123
  *
@@ -138,6 +130,11 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
138
130
  * before.
139
131
  */
140
132
  private lockQueuesForDedup;
133
+ /**
134
+ * Locate the existing jobs that dedup this batch, in one lookup per distinct queue and key kind
135
+ * rather than two per job. No unique index enforces either key — dedup lives in code so every
136
+ * engine behaves identically and a duplicate returns the existing job instead of raising.
137
+ */
141
138
  private findDeduplicated;
142
139
  /** Shape a dedup hit the way `insertJob`'s caller reports it. */
143
140
  private dedupResult;
@@ -237,6 +234,10 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
237
234
  jobId: string;
238
235
  tx?: TransactionHandle;
239
236
  }): Promise<JobRecord | null>;
237
+ findJobRefs(options: {
238
+ jobIds: string[];
239
+ tx?: TransactionHandle;
240
+ }): Promise<JobRef[]>;
240
241
  /**
241
242
  * WHERE clauses for a job filter, appending each value to `params`. Shared by `getJobs` and
242
243
  * `countJobs` so a page and its total can never disagree about what they select. `search` is
@@ -312,6 +313,13 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
312
313
  now: number;
313
314
  tx?: TransactionHandle;
314
315
  }): Promise<QueueRow | null>;
316
+ setQueuesPaused(options: {
317
+ queueNames: string[];
318
+ processingPaused?: boolean;
319
+ acceptingPaused?: boolean;
320
+ now: number;
321
+ tx?: TransactionHandle;
322
+ }): Promise<QueueRow[]>;
315
323
  ensureQueue(options: {
316
324
  name: string;
317
325
  options?: QueueOptions;
@@ -338,15 +346,30 @@ export declare class MysqlDriver extends BaseDriver implements Driver {
338
346
  getInstance(options: {
339
347
  instanceKey: string;
340
348
  }): Promise<InstanceRow | null>;
349
+ getInstancesByKeys(options: {
350
+ instanceKeys: string[];
351
+ tx?: TransactionHandle;
352
+ }): Promise<InstanceRow[]>;
341
353
  setInstancePaused(options: {
342
354
  instanceKey: string;
343
355
  processingPaused: boolean;
344
356
  now: number;
345
357
  }): Promise<void>;
358
+ setInstancesPaused(options: {
359
+ instanceKeys: string[];
360
+ processingPaused: boolean;
361
+ now: number;
362
+ tx?: TransactionHandle;
363
+ }): Promise<string[]>;
346
364
  deleteInstance(options: {
347
365
  instanceKey: string;
348
366
  tx?: TransactionHandle;
349
367
  }): Promise<boolean>;
368
+ deleteInstances(options: {
369
+ instanceKeys: string[];
370
+ seenBefore?: number;
371
+ tx?: TransactionHandle;
372
+ }): Promise<string[]>;
350
373
  deleteFinishedJobs(options: {
351
374
  olderThan: number;
352
375
  limit: number;
@@ -22,8 +22,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  mod
23
23
  ));
24
24
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
- const storage_constants = require("../../storage.constants-B5oQjxp5.cjs");
26
- const migration = require("../../migration-CoTjpgBl.cjs");
25
+ const storage_constants = require("../../storage.constants-BKeXcSQe.cjs");
26
+ const migration = require("../../migration-Dx8eUthW.cjs");
27
27
  function createIfAbsent(ddl) {
28
28
  return `BEGIN
29
29
  EXECUTE IMMEDIATE '${ddl.replace(/'/g, "''")}';
@@ -336,16 +336,9 @@ class OracleDriver extends migration.BaseDriver {
336
336
  }
337
337
  }
338
338
  /**
339
- * Apply one migration's DDL, then record its version.
340
- *
341
- * Oracle commits DDL implicitly, so — like MySQL — these cannot share one transaction. That is
342
- * safe because every statement in the migration swallows its "already exists" code: a crash
343
- * between the DDL and the version row leaves a schema the next run re-applies as a no-op and then
344
- * records. The version insert is guarded on its own absence for the same reason.
345
- */
346
- /**
347
- * Apply one step, then record it — **not** in one transaction, and it cannot be: Oracle commits each DDL statement implicitly, so no transaction can span them. A `BEGIN`
348
- * around them would be silently discarded at the first `CREATE TABLE`.
339
+ * Apply one step, then record it — **not** in one transaction, and it cannot be: Oracle commits
340
+ * each DDL statement implicitly, so no transaction can span them. A `BEGIN` around them would be
341
+ * silently discarded at the first `CREATE TABLE`.
349
342
  *
350
343
  * What holds instead: every statement in this engine's migrations is idempotent, so an
351
344
  * interruption between the DDL and the version insert is repaired by re-running the step, and the
@@ -422,11 +415,6 @@ class OracleDriver extends migration.BaseDriver {
422
415
  return results;
423
416
  });
424
417
  }
425
- /**
426
- * Locate the existing jobs that dedup this batch, in one lookup per distinct queue and key kind
427
- * rather than two per job. No unique index enforces either key — dedup lives in code so every
428
- * engine behaves identically and a duplicate returns the existing job instead of raising.
429
- */
430
418
  /**
431
419
  * Serialize the dedup-lookup-then-insert pair against another producer's, per queue.
432
420
  *
@@ -447,11 +435,19 @@ class OracleDriver extends migration.BaseDriver {
447
435
  )
448
436
  ].sort();
449
437
  if (!names.length) return;
450
- await run(
451
- `SELECT ${migration.QueueColumn.NAME} FROM ${this.t(migration.TableName.QUEUE)} WHERE ${migration.QueueColumn.NAME} IN (${this.phList(names.length)}) FOR UPDATE`,
452
- names
453
- );
438
+ await this.overIdChunks(names, async (chunk) => {
439
+ const res = await run(
440
+ `SELECT ${migration.QueueColumn.NAME} FROM ${this.t(migration.TableName.QUEUE)} WHERE ${migration.QueueColumn.NAME} IN (${this.phList(chunk.length)}) FOR UPDATE`,
441
+ chunk
442
+ );
443
+ return res.rows;
444
+ });
454
445
  }
446
+ /**
447
+ * Locate the existing jobs that dedup this batch, in one lookup per distinct queue and key kind
448
+ * rather than two per job. No unique index enforces either key — dedup lives in code so every
449
+ * engine behaves identically and a duplicate returns the existing job instead of raising.
450
+ */
455
451
  async findDeduplicated(jobs, run) {
456
452
  const out = /* @__PURE__ */ new Map();
457
453
  const cols = `${migration.JobColumn.ID}, ${migration.JobColumn.STATUS}, ${migration.JobColumn.START_AFTER}`;
@@ -467,11 +463,14 @@ class OracleDriver extends migration.BaseDriver {
467
463
  ...new Set(queueJobs.flatMap((j) => j.idempotencyKey ? [j.idempotencyKey] : []))
468
464
  ];
469
465
  if (idemKeys.length) {
470
- const res = await run(
471
- `SELECT ${cols}, ${migration.JobColumn.IDEMPOTENCY_KEY} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.QUEUE_NAME} = ${this.ph(0)} AND ${migration.JobColumn.IDEMPOTENCY_KEY} IN (${this.phList(idemKeys.length, 1)})`,
472
- [queueName, ...idemKeys]
473
- );
474
- const found = new Map(res.rows.map((r) => [String(r[migration.JobColumn.IDEMPOTENCY_KEY]), r]));
466
+ const rows = await this.overIdChunks(idemKeys, async (chunk) => {
467
+ const res = await run(
468
+ `SELECT ${cols}, ${migration.JobColumn.IDEMPOTENCY_KEY} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.QUEUE_NAME} = ${this.ph(0)} AND ${migration.JobColumn.IDEMPOTENCY_KEY} IN (${this.phList(chunk.length, 1)})`,
469
+ [queueName, ...chunk]
470
+ );
471
+ return res.rows;
472
+ });
473
+ const found = new Map(rows.map((r) => [String(r[migration.JobColumn.IDEMPOTENCY_KEY]), r]));
475
474
  for (const job of queueJobs) {
476
475
  if (job.idempotencyKey == null) continue;
477
476
  const row = found.get(job.idempotencyKey);
@@ -484,11 +483,14 @@ class OracleDriver extends migration.BaseDriver {
484
483
  )
485
484
  ];
486
485
  if (singletonKeys.length) {
487
- const res = await run(
488
- `SELECT ${cols}, ${migration.JobColumn.SINGLETON_KEY} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.QUEUE_NAME} = ${this.ph(0)} AND ${migration.JobColumn.SINGLETON_KEY} IN (${this.phList(singletonKeys.length, 1)}) AND ${migration.JobColumn.STATUS} IN (${this.phList(storage_constants.ACTIVE_STATUSES.length, 1 + singletonKeys.length)})`,
489
- [queueName, ...singletonKeys, ...storage_constants.ACTIVE_STATUSES]
490
- );
491
- const found = new Map(res.rows.map((r) => [String(r[migration.JobColumn.SINGLETON_KEY]), r]));
486
+ const rows = await this.overIdChunks(singletonKeys, async (chunk) => {
487
+ const res = await run(
488
+ `SELECT ${cols}, ${migration.JobColumn.SINGLETON_KEY} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.QUEUE_NAME} = ${this.ph(0)} AND ${migration.JobColumn.SINGLETON_KEY} IN (${this.phList(chunk.length, 1)}) AND ${migration.JobColumn.STATUS} IN (${this.phList(storage_constants.ACTIVE_STATUSES.length, 1 + chunk.length)})`,
489
+ [queueName, ...chunk, ...storage_constants.ACTIVE_STATUSES]
490
+ );
491
+ return res.rows;
492
+ });
493
+ const found = new Map(rows.map((r) => [String(r[migration.JobColumn.SINGLETON_KEY]), r]));
492
494
  for (const job of queueJobs) {
493
495
  if (out.has(job) || job.singletonKey == null) continue;
494
496
  const row = found.get(job.singletonKey);
@@ -784,6 +786,17 @@ class OracleDriver extends migration.BaseDriver {
784
786
  );
785
787
  return res.rows[0] ? this.mapJob(res.rows[0]) : null;
786
788
  }
789
+ async findJobRefs(options) {
790
+ if (!options.jobIds.length) return [];
791
+ return this.overIdChunks(options.jobIds, async (jobIds) => {
792
+ const res = await this.runMaybeTx(
793
+ `SELECT ${migration.JobColumn.ID}, ${migration.JobColumn.QUEUE_NAME} FROM ${this.t(migration.TableName.JOB)} WHERE ${migration.JobColumn.ID} IN (${this.phList(jobIds.length)})`,
794
+ jobIds,
795
+ options.tx
796
+ );
797
+ return res.rows.map((r) => this.mapJobRef(r));
798
+ });
799
+ }
787
800
  /**
788
801
  * WHERE clauses for a job filter, appending each value to `params`. Shared by `getJobs` and
789
802
  * `countJobs` so a page and its total can never disagree about what they select. `search` is
@@ -892,6 +905,12 @@ class OracleDriver extends migration.BaseDriver {
892
905
  clauses.push(`${migration.QueueColumn.PROCESSING_PAUSED} = ${this.ph(params.length)}`);
893
906
  params.push(this.boolIn(options.processingPaused));
894
907
  }
908
+ if (options.names !== void 0) {
909
+ clauses.push(
910
+ options.names.length ? `${migration.QueueColumn.NAME} IN (${this.phList(options.names.length, params.length)})` : "1 = 0"
911
+ );
912
+ params.push(...options.names);
913
+ }
895
914
  return clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
896
915
  }
897
916
  async listQueues(options = {}) {
@@ -1044,30 +1063,37 @@ class OracleDriver extends migration.BaseDriver {
1044
1063
  }
1045
1064
  // --- Queue control ---
1046
1065
  async setQueuePaused(options) {
1047
- const sets = [];
1048
- const params = [];
1049
- if (options.processingPaused !== void 0) {
1050
- sets.push(`${migration.QueueColumn.PROCESSING_PAUSED} = ${this.ph(params.length)}`);
1051
- params.push(this.boolIn(options.processingPaused));
1052
- }
1053
- if (options.acceptingPaused !== void 0) {
1054
- sets.push(`${migration.QueueColumn.ACCEPTING_PAUSED} = ${this.ph(params.length)}`);
1055
- params.push(this.boolIn(options.acceptingPaused));
1056
- }
1057
- sets.push(`${migration.QueueColumn.UPDATED_AT} = ${this.ph(params.length)}`);
1058
- params.push(options.now);
1059
- const namePh = this.ph(params.length);
1060
- params.push(options.queueName);
1061
- return this.inTx(options.tx, async (run) => {
1062
- await run(
1063
- `UPDATE ${this.t(migration.TableName.QUEUE)} SET ${sets.join(", ")} WHERE ${migration.QueueColumn.NAME} = ${namePh}`,
1064
- params
1065
- );
1066
- const res = await run(
1067
- `SELECT * FROM ${this.t(migration.TableName.QUEUE)} WHERE ${migration.QueueColumn.NAME} = ${this.ph(0)}`,
1068
- [options.queueName]
1069
- );
1070
- return res.rows[0] ? this.mapQueue(res.rows[0]) : null;
1066
+ const { queueName, ...rest } = options;
1067
+ const [row] = await this.setQueuesPaused({ ...rest, queueNames: [queueName] });
1068
+ return row ?? null;
1069
+ }
1070
+ async setQueuesPaused(options) {
1071
+ if (!options.queueNames.length) return [];
1072
+ return this.overIdChunks(options.queueNames, async (queueNames) => {
1073
+ const sets = [];
1074
+ const params = [];
1075
+ if (options.processingPaused !== void 0) {
1076
+ sets.push(`${migration.QueueColumn.PROCESSING_PAUSED} = ${this.ph(params.length)}`);
1077
+ params.push(this.boolIn(options.processingPaused));
1078
+ }
1079
+ if (options.acceptingPaused !== void 0) {
1080
+ sets.push(`${migration.QueueColumn.ACCEPTING_PAUSED} = ${this.ph(params.length)}`);
1081
+ params.push(this.boolIn(options.acceptingPaused));
1082
+ }
1083
+ sets.push(`${migration.QueueColumn.UPDATED_AT} = ${this.ph(params.length)}`);
1084
+ params.push(options.now);
1085
+ const namesPh = this.phList(queueNames.length, params.length);
1086
+ return this.inTx(options.tx, async (run) => {
1087
+ await run(
1088
+ `UPDATE ${this.t(migration.TableName.QUEUE)} SET ${sets.join(", ")} WHERE ${migration.QueueColumn.NAME} IN (${namesPh})`,
1089
+ [...params, ...queueNames]
1090
+ );
1091
+ const res = await run(
1092
+ `SELECT * FROM ${this.t(migration.TableName.QUEUE)} WHERE ${migration.QueueColumn.NAME} IN (${this.phList(queueNames.length)})`,
1093
+ queueNames
1094
+ );
1095
+ return res.rows.map((r) => this.mapQueue(r));
1096
+ });
1071
1097
  });
1072
1098
  }
1073
1099
  async ensureQueue(options) {
@@ -1184,19 +1210,72 @@ class OracleDriver extends migration.BaseDriver {
1184
1210
  );
1185
1211
  return res.rows[0] ? this.mapInstance(res.rows[0]) : null;
1186
1212
  }
1213
+ async getInstancesByKeys(options) {
1214
+ if (!options.instanceKeys.length) return [];
1215
+ return this.overIdChunks(options.instanceKeys, async (instanceKeys) => {
1216
+ const res = await this.runMaybeTx(
1217
+ `SELECT * FROM ${this.t(migration.TableName.INSTANCE)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} IN (${this.phList(instanceKeys.length)})`,
1218
+ instanceKeys,
1219
+ options.tx
1220
+ );
1221
+ return res.rows.map((r) => this.mapInstance(r));
1222
+ });
1223
+ }
1187
1224
  async setInstancePaused(options) {
1188
- await this.runMaybeTx(
1189
- `UPDATE ${this.t(migration.TableName.INSTANCE)} SET ${migration.InstanceColumn.PROCESSING_PAUSED} = ${this.ph(0)}, ${migration.InstanceColumn.UPDATED_AT} = ${this.ph(1)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} = ${this.ph(2)}`,
1190
- [this.boolIn(options.processingPaused), options.now, options.instanceKey]
1191
- );
1225
+ await this.setInstancesPaused({
1226
+ instanceKeys: [options.instanceKey],
1227
+ processingPaused: options.processingPaused,
1228
+ now: options.now
1229
+ });
1230
+ }
1231
+ async setInstancesPaused(options) {
1232
+ if (!options.instanceKeys.length) return [];
1233
+ return this.overIdChunks(options.instanceKeys, async (instanceKeys) => {
1234
+ const sets = `${migration.InstanceColumn.PROCESSING_PAUSED} = ${this.ph(0)}, ${migration.InstanceColumn.UPDATED_AT} = ${this.ph(1)}`;
1235
+ const params = [this.boolIn(options.processingPaused), options.now, ...instanceKeys];
1236
+ return this.inTx(options.tx, async (run) => {
1237
+ await run(
1238
+ `UPDATE ${this.t(migration.TableName.INSTANCE)} SET ${sets} WHERE ${migration.InstanceColumn.INSTANCE_KEY} IN (${this.phList(instanceKeys.length, 2)})`,
1239
+ params
1240
+ );
1241
+ const res = await run(
1242
+ `SELECT ${migration.InstanceColumn.INSTANCE_KEY} FROM ${this.t(migration.TableName.INSTANCE)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} IN (${this.phList(instanceKeys.length)})`,
1243
+ instanceKeys
1244
+ );
1245
+ return res.rows.map((r) => this.mapInstanceKey(r));
1246
+ });
1247
+ });
1192
1248
  }
1193
1249
  async deleteInstance(options) {
1194
- const res = await this.runMaybeTx(
1195
- `DELETE FROM ${this.t(migration.TableName.INSTANCE)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} = ${this.ph(0)}`,
1196
- [options.instanceKey],
1197
- options.tx
1198
- );
1199
- return res.rowCount > 0;
1250
+ const removed = await this.deleteInstances({
1251
+ instanceKeys: [options.instanceKey],
1252
+ ...options.tx ? { tx: options.tx } : {}
1253
+ });
1254
+ return removed.length > 0;
1255
+ }
1256
+ async deleteInstances(options) {
1257
+ if (!options.instanceKeys.length) return [];
1258
+ return this.overIdChunks(options.instanceKeys, async (instanceKeys) => {
1259
+ const params = [...instanceKeys];
1260
+ let guard = "";
1261
+ if (options.seenBefore !== void 0) {
1262
+ guard = ` AND ${migration.InstanceColumn.LAST_SEEN_AT} < ${this.ph(params.length)}`;
1263
+ params.push(options.seenBefore);
1264
+ }
1265
+ return this.inTx(options.tx, async (run) => {
1266
+ const sel = await run(
1267
+ `SELECT ${migration.InstanceColumn.INSTANCE_KEY} FROM ${this.t(migration.TableName.INSTANCE)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} IN (${this.phList(instanceKeys.length)})${guard} FOR UPDATE`,
1268
+ params
1269
+ );
1270
+ const keys = sel.rows.map((r) => this.mapInstanceKey(r));
1271
+ if (!keys.length) return [];
1272
+ await run(
1273
+ `DELETE FROM ${this.t(migration.TableName.INSTANCE)} WHERE ${migration.InstanceColumn.INSTANCE_KEY} IN (${this.phList(keys.length)})`,
1274
+ keys
1275
+ );
1276
+ return keys;
1277
+ });
1278
+ });
1200
1279
  }
1201
1280
  // --- Retention ---
1202
1281
  async deleteFinishedJobs(options) {