cairnq 0.9.0 → 0.11.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 (46) hide show
  1. package/README.md +10 -2
  2. package/dist/_protocol/migrations/postgres/0001_init.sql +2 -0
  3. package/dist/_protocol/migrations/postgres/0006_claim_name_index.sql +2 -0
  4. package/dist/_protocol/migrations/postgres/0008_claim_due_index.sql +68 -0
  5. package/dist/_protocol/migrations/sqlite/0001_init.sql +2 -0
  6. package/dist/_protocol/migrations/sqlite/0006_claim_name_index.sql +2 -0
  7. package/dist/_protocol/migrations/sqlite/0008_claim_due_index.sql +84 -0
  8. package/dist/_protocol/sql/postgres/claim.sql +6 -3
  9. package/dist/_protocol/sql/postgres/claim_one_name.sql +6 -3
  10. package/dist/_protocol/sql/postgres/claim_one_queue.sql +9 -5
  11. package/dist/_protocol/sql/postgres/claim_one_queue_one_name.sql +6 -3
  12. package/dist/_protocol/sql/postgres/installations.sql +33 -0
  13. package/dist/_protocol/sql/sqlite/claim.sql +6 -3
  14. package/dist/_protocol/sql/sqlite/claim_one_name.sql +6 -3
  15. package/dist/_protocol/sql/sqlite/claim_one_queue.sql +9 -6
  16. package/dist/_protocol/sql/sqlite/claim_one_queue_one_name.sql +9 -4
  17. package/dist/context.d.ts +4 -2
  18. package/dist/context.js +12 -8
  19. package/dist/errors.d.ts +28 -0
  20. package/dist/errors.js +34 -0
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/models.d.ts +2 -0
  24. package/dist/models.js +28 -1
  25. package/dist/retention.d.ts +13 -1
  26. package/dist/retention.js +26 -11
  27. package/dist/store/base.d.ts +12 -3
  28. package/dist/store/base.js +21 -6
  29. package/dist/store/pg-executor.d.ts +6 -5
  30. package/dist/store/pg-executor.js +6 -5
  31. package/dist/store/pg-pool.js +37 -11
  32. package/dist/store/postgres.d.ts +64 -0
  33. package/dist/store/postgres.js +121 -1
  34. package/dist/worker.d.ts +4 -2
  35. package/dist/worker.js +10 -8
  36. package/package.json +1 -1
  37. package/src/context.ts +10 -6
  38. package/src/errors.ts +36 -0
  39. package/src/index.ts +2 -0
  40. package/src/models.ts +28 -1
  41. package/src/retention.ts +27 -11
  42. package/src/store/base.ts +27 -5
  43. package/src/store/pg-executor.ts +6 -5
  44. package/src/store/pg-pool.ts +38 -11
  45. package/src/store/postgres.ts +125 -1
  46. package/src/worker.ts +10 -8
package/src/worker.ts CHANGED
@@ -222,7 +222,7 @@ function release(counts: Map<string, number>, key: string | undefined): void {
222
222
 
223
223
  export class Worker {
224
224
  private readonly handlers = new Map<string, Registration>();
225
- private readonly workerId = newId("worker");
225
+ private readonly ownId = newId("worker");
226
226
  /** Payload bytes charged to running handlers — see maxInFlightBytes. */
227
227
  private inFlightBytes = 0;
228
228
  /** Calls in flight, for the names that cap their own concurrency. */
@@ -305,8 +305,10 @@ export class Worker {
305
305
  return worker;
306
306
  }
307
307
 
308
- get id(): string {
309
- return this.workerId;
308
+ /** This worker's id — what `worker_id` on a running task points at, and what
309
+ * the Python SDK calls `worker_id` too. */
310
+ get workerId(): string {
311
+ return this.ownId;
310
312
  }
311
313
 
312
314
  task(handler: Handler): this;
@@ -474,7 +476,7 @@ export class Worker {
474
476
  }
475
477
 
476
478
  private context(task: Task, leaseMs: number): TaskContext {
477
- return new TaskContext(this.store, task, this.workerId, leaseMs, {
479
+ return new TaskContext(this.store, task, this.ownId, leaseMs, {
478
480
  retryBackoffMs: this.backoffMs,
479
481
  retryBackoffMaxMs: this.backoffMaxMs,
480
482
  });
@@ -588,7 +590,7 @@ export class Worker {
588
590
  // Only what this worker can run. Queues do not partition work by task
589
591
  // name, so another worker's tasks would otherwise be claimed here and
590
592
  // failed for want of a handler.
591
- { queues: this.queues, workerId: this.workerId, leaseMs, names },
593
+ { queues: this.queues, workerId: this.ownId, leaseMs, names },
592
594
  async (claim) => {
593
595
  const drawn: { src: ClaimSource; calls: [Registration | undefined, Task[]][] }[] = [];
594
596
  let left = free;
@@ -826,7 +828,7 @@ export class Worker {
826
828
  try {
827
829
  // complete (not succeed): finalizes as canceled if a cancel was requested
828
830
  // while the handler ran, else succeeded.
829
- await this.store.complete({ taskId: ctx.taskId, workerId: this.workerId, result });
831
+ await this.store.complete({ taskId: ctx.taskId, workerId: this.ownId, result });
830
832
  ctx.markSettled();
831
833
  } catch (err) {
832
834
  if (err instanceof LostLease) {
@@ -967,7 +969,7 @@ export class Worker {
967
969
  try {
968
970
  const renewed = await this.store.heartbeatBatch({
969
971
  taskIds: live.map((c) => c.taskId),
970
- workerId: this.workerId,
972
+ workerId: this.ownId,
971
973
  leaseMs,
972
974
  });
973
975
  for (const ctx of live) {
@@ -1003,7 +1005,7 @@ export class Worker {
1003
1005
  try {
1004
1006
  await this.store.fail({
1005
1007
  taskId: ctx.taskId,
1006
- workerId: this.workerId,
1008
+ workerId: this.ownId,
1007
1009
  error: envelope,
1008
1010
  retryable,
1009
1011
  delayMs: failDelayMs(ctx.attempt, retryable, this.backoffMs, this.backoffMaxMs),