@dudousxd/nestjs-durable-core 0.75.0 → 0.75.1

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/dist/index.cjs CHANGED
@@ -3872,6 +3872,8 @@ var WorkflowEngine = class {
3872
3872
  draining = false;
3873
3873
  /** Wait already spent on this shutdown before `drain()` (see `beginDrain`). */
3874
3874
  drainSpentMs = 0;
3875
+ /** Re-arms the deadline of every `drain()` currently waiting, when more wait is reported. */
3876
+ drainRearms = /* @__PURE__ */ new Set();
3875
3877
  constructor(deps) {
3876
3878
  this.store = deps.store;
3877
3879
  this.pool = new TransportPool(deps.transports ?? (deps.transport ? [
@@ -4557,7 +4559,10 @@ var WorkflowEngine = class {
4557
4559
  */
4558
4560
  beginDrain(spentMs = 0) {
4559
4561
  this.draining = true;
4560
- this.drainSpentMs += Math.max(0, spentMs);
4562
+ if (Number.isFinite(spentMs) && spentMs > 0) {
4563
+ this.drainSpentMs += spentMs;
4564
+ for (const rearm of this.drainRearms) rearm();
4565
+ }
4561
4566
  }
4562
4567
  /**
4563
4568
  * Graceful shutdown: stop picking up new runs (recovery/timer become no-ops) and wait for
@@ -4570,18 +4575,32 @@ var WorkflowEngine = class {
4570
4575
  */
4571
4576
  async drain(timeoutMs = 1e4) {
4572
4577
  this.beginDrain();
4573
- const budget = Math.max(0, timeoutMs - this.drainSpentMs);
4574
4578
  if (this.inflight.size === 0) return;
4579
+ const start = Date.now();
4580
+ let rearm = /* @__PURE__ */ __name(() => {
4581
+ }, "rearm");
4582
+ let timeout;
4575
4583
  const timer = new Promise((resolve) => {
4576
- const t = setTimeout(resolve, budget);
4577
- t.unref?.();
4584
+ rearm = /* @__PURE__ */ __name(() => {
4585
+ clearTimeout(timeout);
4586
+ const remaining = Math.max(0, start + timeoutMs - this.drainSpentMs - Date.now());
4587
+ timeout = setTimeout(resolve, remaining);
4588
+ timeout.unref?.();
4589
+ }, "rearm");
4590
+ rearm();
4578
4591
  });
4579
- await Promise.race([
4580
- Promise.allSettled([
4581
- ...this.inflight
4582
- ]),
4583
- timer
4584
- ]);
4592
+ this.drainRearms.add(rearm);
4593
+ try {
4594
+ await Promise.race([
4595
+ Promise.allSettled([
4596
+ ...this.inflight
4597
+ ]),
4598
+ timer
4599
+ ]);
4600
+ } finally {
4601
+ this.drainRearms.delete(rearm);
4602
+ clearTimeout(timeout);
4603
+ }
4585
4604
  }
4586
4605
  /**
4587
4606
  * Cancel in-flight runs that have outlived their workflow's `executionTimeout`. Call it from the