@dudousxd/nestjs-durable-core 0.95.0 → 0.97.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.
package/dist/index.cjs CHANGED
@@ -208,6 +208,7 @@ __export(index_exports, {
208
208
  negotiate: () => negotiate,
209
209
  nextCronFireMs: () => nextCronFireMs,
210
210
  nextRRuleFireMs: () => nextRRuleFireMs,
211
+ nextScheduleFireAt: () => nextScheduleFireAt,
211
212
  normalizeAttributeRows: () => normalizeAttributeRows,
212
213
  normalizeDescriptor: () => normalizeDescriptor,
213
214
  observedAnnouncements: () => observedAnnouncements,
@@ -3860,6 +3861,25 @@ function jitterDue(engine, s, candidates, nowMs, random) {
3860
3861
  return due;
3861
3862
  }
3862
3863
  __name(jitterDue, "jitterDue");
3864
+ function nextScheduleFireAt(engine, schedules, nowMs) {
3865
+ let next;
3866
+ const consider = /* @__PURE__ */ __name((at) => {
3867
+ if (next === void 0 || at < next) next = at;
3868
+ }, "consider");
3869
+ for (const s of schedules) {
3870
+ for (const at of jitterFireAt.get(engine)?.get(s.key)?.values() ?? []) consider(at);
3871
+ if (s.paused) continue;
3872
+ try {
3873
+ if (s.cron != null) consider(nextCronFireMs(s.cron, nowMs, s.timezone));
3874
+ else if (s.everyMs != null && s.everyMs > 0) consider((Math.floor(nowMs / s.everyMs) + 1) * s.everyMs);
3875
+ else return nowMs;
3876
+ } catch {
3877
+ return nowMs;
3878
+ }
3879
+ }
3880
+ return next;
3881
+ }
3882
+ __name(nextScheduleFireAt, "nextScheduleFireAt");
3863
3883
  function scheduledRunId(key, everyMs, nowMs) {
3864
3884
  return `sched:${key}:${Math.floor(nowMs / everyMs)}`;
3865
3885
  }
@@ -4307,6 +4327,21 @@ var ScheduleClient = class {
4307
4327
  }
4308
4328
  return started;
4309
4329
  }
4330
+ /**
4331
+ * When {@link tick} next has a schedule to fire, if one is due by `byMs`: the earliest `nextFireAt`
4332
+ * of the unpaused schedules (of this engine's namespace) due by then, else `undefined`. One indexed
4333
+ * read (the due-schedule scan `tick` makes, with a later bound and a limit of one). A store that
4334
+ * does not persist schedules has none.
4335
+ */
4336
+ async nextFireAt(byMs) {
4337
+ if (!this.supported) return void 0;
4338
+ const [next] = await this.requireStore().listSchedules({
4339
+ dueBy: byMs,
4340
+ namespace: this.options.namespace,
4341
+ limit: 1
4342
+ });
4343
+ return next?.nextFireAt ?? void 0;
4344
+ }
4310
4345
  /** Fire one due schedule and advance it. Returns the run id started, if any. */
4311
4346
  async fire(record, nowMs) {
4312
4347
  const store = this.requireStore();
@@ -5339,11 +5374,11 @@ var TransportPool = class {
5339
5374
  * but none is published — both are the "guard disengaged / legacy assume-compatible" case, so the
5340
5375
  * engine's routing guard treats an empty result identically (design §7.7).
5341
5376
  */
5342
- async readWorkerDescriptors(group) {
5377
+ async readWorkerDescriptors(group, opts) {
5343
5378
  const descriptors = [];
5344
5379
  for (const { transport } of this.transports) {
5345
5380
  if (transport.readWorkerDescriptors) {
5346
- descriptors.push(...await transport.readWorkerDescriptors(group));
5381
+ descriptors.push(...await transport.readWorkerDescriptors(group, opts));
5347
5382
  }
5348
5383
  }
5349
5384
  return descriptors;
@@ -7511,6 +7546,38 @@ var WorkflowEngine = class _WorkflowEngine {
7511
7546
  return () => this.enqueuedListeners.delete(listener);
7512
7547
  }
7513
7548
  /**
7549
+ * Whether a poller may wait longer than its tick while there is nothing to do: a run another
7550
+ * instance starts must reach this one without the pending-run poll, so this engine needs a control
7551
+ * plane (the `enqueued` nudge, and the run events of other instances that tell a poller to look
7552
+ * again) or a run queue it consumes (see {@link consumeRuns}). See {@link nextDueAt}.
7553
+ */
7554
+ get idleBackoffSupported() {
7555
+ return this.controlPlane !== void 0 || this.queuesRunsOverTransport;
7556
+ }
7557
+ /**
7558
+ * The earliest known time (epoch ms) the poll has work, if it is at or before `byMs`: this
7559
+ * namespace's next due timer (`listDueTimers` up to `byMs`, limit one: it returns the most overdue
7560
+ * first), the next `blocked` run's recovery poll, and with `persistedSchedules` the next persisted
7561
+ * schedule to fire (see {@link ScheduleClient.nextFireAt}). `undefined` when nothing is due by
7562
+ * then. A poller reads it before waiting longer than its tick, so the wait never outlasts a timer.
7563
+ */
7564
+ async nextDueAt(byMs, opts = {}) {
7565
+ const [timers, blocked, schedule] = await Promise.all([
7566
+ this.store.listDueTimers(byMs, this.namespace, 1),
7567
+ this.dueBlockedRuns(byMs),
7568
+ opts.persistedSchedules ? this.schedules.nextFireAt(byMs) : void 0
7569
+ ]);
7570
+ let next;
7571
+ for (const at of [
7572
+ timers[0]?.wakeAt,
7573
+ ...blocked.map((run) => run.wakeAt),
7574
+ schedule ?? void 0
7575
+ ]) {
7576
+ if (at !== void 0 && (next === void 0 || at < next)) next = at;
7577
+ }
7578
+ return next;
7579
+ }
7580
+ /**
7514
7581
  * Pick up a run another instance announced as enqueued: run it here if it is still `pending`.
7515
7582
  * Bounded by {@link LOCAL_RUN_CONCURRENCY}, and a run another instance already took is skipped
7516
7583
  * after one read, instead of being leased and replayed here once more.
@@ -9038,14 +9105,21 @@ var WorkflowEngine = class _WorkflowEngine {
9038
9105
  * {@link LIFECYCLE_SWEEP_SCAN_MAX}. Pages advance by offset over a set the sweep itself shrinks,
9039
9106
  * so a run that moves into an earlier page is reached on the next tick, which starts over. A run
9040
9107
  * whose lifecycle lease another publisher holds is left for the next tick.
9108
+ *
9109
+ * `options.owed: false` does only the cheap part: the registry heartbeat (itself throttled) and the
9110
+ * run waits. The listing of marked runs filters on a tag (an unindexed `LIKE` on most stores, a
9111
+ * partial index on Postgres) and the heal reads recent runs per source workflow, so a poller runs
9112
+ * that part less often than its tick (see the NestJS timer poller) and passes `owed: false` in
9113
+ * between. It returns 0 then.
9041
9114
  */
9042
- async sweepLifecycleEvents(nowMs = this.clock(), limit = 100) {
9115
+ async sweepLifecycleEvents(nowMs = this.clock(), limit = 100, options = {}) {
9043
9116
  await this.heartbeatLifecycleRegistry().catch((error2) => {
9044
9117
  console.error("[nestjs-durable] lifecycle registry heartbeat failed; retrying next tick:", error2);
9045
9118
  });
9046
9119
  await this.sweepRunWaits(limit).catch((error2) => {
9047
9120
  console.error("[nestjs-durable] run-wait sweep failed; retrying next tick:", error2);
9048
9121
  });
9122
+ if (options.owed === false) return 0;
9049
9123
  await this.announcedSubscriptions();
9050
9124
  const pageSize = Math.max(1, limit);
9051
9125
  const scanMax = Math.max(pageSize, _WorkflowEngine.LIFECYCLE_SWEEP_SCAN_MAX);
@@ -14029,11 +14103,14 @@ ${run.workflowVersion}`;
14029
14103
  }
14030
14104
  /** The live descriptors for `token`, reused for {@link workerDescriptorCacheMs}; one read in flight. */
14031
14105
  liveWorkerDescriptors(token, fresh = false) {
14032
- if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token);
14106
+ const opts = fresh ? {
14107
+ fresh: true
14108
+ } : void 0;
14109
+ if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token, opts);
14033
14110
  const now = Date.now();
14034
14111
  const cached = this.descriptorCache.get(token);
14035
14112
  if (!fresh && cached && now - cached.at < this.workerDescriptorCacheMs) return cached.read;
14036
- const read = this.pool.readWorkerDescriptors(token);
14113
+ const read = this.pool.readWorkerDescriptors(token, opts);
14037
14114
  const entry = {
14038
14115
  at: now,
14039
14116
  read
@@ -17867,6 +17944,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17867
17944
  negotiate,
17868
17945
  nextCronFireMs,
17869
17946
  nextRRuleFireMs,
17947
+ nextScheduleFireAt,
17870
17948
  normalizeAttributeRows,
17871
17949
  normalizeDescriptor,
17872
17950
  observedAnnouncements,