@oatscenter/trellis 0.100.0-rc.31 → 0.100.0-rc.33

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "dependencies": {
3
- "@oatscenter/trellis": "^0.100.0-rc.31"
3
+ "@oatscenter/trellis": "^0.100.0-rc.33"
4
4
  },
5
5
  "description": "Generated Trellis APIs and participants.",
6
6
  "exports": {
package/package.json CHANGED
@@ -51,7 +51,7 @@
51
51
  "@nats-io/obj": "^3.4.0",
52
52
  "@nats-io/transport-node": "^3.4.0",
53
53
  "@noble/hashes": "1.8.0",
54
- "@oatscenter/result": "0.100.0-rc.31",
54
+ "@oatscenter/result": "0.100.0-rc.33",
55
55
  "@opentelemetry/api": "^1.9.1",
56
56
  "@opentelemetry/context-async-hooks": "~2.11.0",
57
57
  "@opentelemetry/core": "~2.11.0",
@@ -71,7 +71,7 @@
71
71
  "uqr": "0.1.3",
72
72
  "zod": "^4.3.6"
73
73
  },
74
- "version": "0.100.0-rc.31",
74
+ "version": "0.100.0-rc.33",
75
75
  "type": "module",
76
76
  "license": "Apache-2.0",
77
77
  "publishConfig": {
@@ -545,54 +545,7 @@ export class JobManager {
545
545
  if (stopReason === "deadline-exceeded" ||
546
546
  stopReason === "retry-exhausted" ||
547
547
  stopReason === "stale-attempt") {
548
- if (lease) {
549
- try {
550
- await this.#keyedHeartbeat(job, lease)();
551
- }
552
- catch {
553
- cancellation.cancelForLeaseLoss();
554
- await this.#releaseKeyedSlot(job, lease);
555
- return { outcome: "interrupted", tries };
556
- }
557
- }
558
- if (cancellation.isHostShutdown() || cancellation.isLeaseLost()) {
559
- await this.#releaseKeyedSlot(job, lease);
560
- return { outcome: "interrupted", tries };
561
- }
562
- const expired = stopReason === "deadline-exceeded";
563
- const stale = stopReason === "stale-attempt";
564
- if (cancellation.isJobCancelled()) {
565
- await this.#releaseKeyedSlot(job, lease);
566
- return { outcome: "cancelled", tries };
567
- }
568
- await this.#publishJobEvent(job.type, job.id, {
569
- jobId: job.id,
570
- service: job.service,
571
- jobType: job.type,
572
- eventType: stale ? "stale" : expired ? "expired" : "dead",
573
- state: stale ? "stale" : expired ? "expired" : "dead",
574
- previousState: "active",
575
- context: job.context,
576
- tries,
577
- error: stale
578
- ? "key lease displaced; execution owner reconciled"
579
- : expired
580
- ? "job deadline exceeded; execution owner reconciled"
581
- : "ordinary job attempts exhausted; execution owner reconciled",
582
- timestamp: this.#meta().nowIso(),
583
- });
584
- await this.#releaseKeyedSlot(job, lease);
585
- if (stale) {
586
- await this.cleanupQueuedKeyedJob(job);
587
- return { outcome: "stale", tries };
588
- }
589
- return expired
590
- ? { outcome: "expired", tries }
591
- : {
592
- outcome: "dead",
593
- tries,
594
- error: "ordinary job attempts exhausted",
595
- };
548
+ return await this.#settleReconciled(job, lease, cancellation, tries, stopReason);
596
549
  }
597
550
  if (cancellation.isCancelled()) {
598
551
  await this.#releaseKeyedSlot(job, lease);
@@ -626,15 +579,21 @@ export class JobManager {
626
579
  await this.#releaseKeyedSlot(job, lease);
627
580
  return { outcome: "interrupted", tries };
628
581
  }
629
- if (cancellation.reason() === "deadline-exceeded" ||
630
- cancellation.reason() === "retry-exhausted" ||
631
- cancellation.reason() === "stale-attempt") {
582
+ const stopReason = cancellation.reason();
583
+ if (stopReason === "deadline-exceeded" ||
584
+ stopReason === "retry-exhausted" ||
585
+ stopReason === "stale-attempt") {
632
586
  recordTrellisError(error, {
633
587
  surface: "job",
634
588
  direction: "worker",
635
589
  operation: job.type,
636
590
  phase: "reconciliation",
637
591
  });
592
+ // Only a retryable error asks for another cleanup attempt. A failed
593
+ // one is the handler's last word, so the job settles with it.
594
+ if (error instanceof JobProcessError && error.kind === "failed") {
595
+ return await this.#settleReconciled(job, lease, cancellation, tries, stopReason, error.message);
596
+ }
638
597
  await this.#releaseKeyedSlot(job, lease);
639
598
  return { outcome: "interrupted", tries };
640
599
  }
@@ -811,6 +770,63 @@ export class JobManager {
811
770
  });
812
771
  return released.kind === "released" ? "released" : "staleCompletion";
813
772
  }
773
+ /**
774
+ * Ends a job whose attempts, deadline or key lease ran out, once its handler
775
+ * has run cleanup. `cleanupError` is the handler's final failure, if any.
776
+ */
777
+ async #settleReconciled(job, lease, cancellation, tries, stopReason, cleanupError) {
778
+ const settlement = cleanupError === undefined
779
+ ? "execution owner reconciled"
780
+ : `cleanup failed: ${cleanupError}`;
781
+ if (lease) {
782
+ try {
783
+ await this.#keyedHeartbeat(job, lease)();
784
+ }
785
+ catch {
786
+ cancellation.cancelForLeaseLoss();
787
+ await this.#releaseKeyedSlot(job, lease);
788
+ return { outcome: "interrupted", tries };
789
+ }
790
+ }
791
+ if (cancellation.isHostShutdown() || cancellation.isLeaseLost()) {
792
+ await this.#releaseKeyedSlot(job, lease);
793
+ return { outcome: "interrupted", tries };
794
+ }
795
+ const expired = stopReason === "deadline-exceeded";
796
+ const stale = stopReason === "stale-attempt";
797
+ if (cancellation.isJobCancelled()) {
798
+ await this.#releaseKeyedSlot(job, lease);
799
+ return { outcome: "cancelled", tries };
800
+ }
801
+ await this.#publishJobEvent(job.type, job.id, {
802
+ jobId: job.id,
803
+ service: job.service,
804
+ jobType: job.type,
805
+ eventType: stale ? "stale" : expired ? "expired" : "dead",
806
+ state: stale ? "stale" : expired ? "expired" : "dead",
807
+ previousState: "active",
808
+ context: job.context,
809
+ tries,
810
+ error: stale
811
+ ? `key lease displaced; ${settlement}`
812
+ : expired
813
+ ? `job deadline exceeded; ${settlement}`
814
+ : `ordinary job attempts exhausted; ${settlement}`,
815
+ timestamp: this.#meta().nowIso(),
816
+ });
817
+ await this.#releaseKeyedSlot(job, lease);
818
+ if (stale) {
819
+ await this.cleanupQueuedKeyedJob(job);
820
+ return { outcome: "stale", tries };
821
+ }
822
+ return expired
823
+ ? { outcome: "expired", tries }
824
+ : {
825
+ outcome: "dead",
826
+ tries,
827
+ error: "ordinary job attempts exhausted",
828
+ };
829
+ }
814
830
  /**
815
831
  * Publishes a terminal event while still holding the key slot, then frees it.
816
832
  * A free slot therefore proves a finished job's event is readable, so a
@@ -359,7 +359,9 @@ export function startQueueWorkerLoop(options) {
359
359
  await disposition("nak", options.deferralBackoffMs ?? 1_000);
360
360
  }
361
361
  else {
362
- await disposition("nak", retryDelayMs(outcome?.tries ?? 1, options.backoffMs));
362
+ await disposition("nak", outcome?.outcome === "interrupted"
363
+ ? interruptedDelayMs(outcome.tries, options.backoffMs)
364
+ : retryDelayMs(outcome?.tries ?? 1, options.backoffMs));
363
365
  }
364
366
  }
365
367
  catch (error) {
@@ -577,6 +579,11 @@ function retryDelayMs(delivery, configured) {
577
579
  : [5_000, 30_000, 120_000, 600_000];
578
580
  return schedule[Math.min(Math.max(0, delivery - 1), schedule.length - 1)];
579
581
  }
582
+ /** Failed cleanup can repeat indefinitely, so its delay keeps growing. */
583
+ function interruptedDelayMs(tries, configured) {
584
+ const beyondSchedule = Math.max(0, tries - (configured?.length ?? 0) - 2);
585
+ return Math.min(retryDelayMs(tries, configured) * 2 ** Math.min(beyondSchedule, 12), 300_000);
586
+ }
580
587
  export function progressAckIntervalMs(queue) {
581
588
  const wait = queue.backoffMs.length > 0 ? Math.min(...queue.backoffMs) : queue.ackWaitMs;
582
589
  if (!Number.isInteger(wait) || wait < 1) {