@dudousxd/nestjs-durable-core 0.85.10 → 0.85.12

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.d.cts CHANGED
@@ -2628,6 +2628,9 @@ type ControlMessage = {
2628
2628
  } & ({
2629
2629
  kind: 'event';
2630
2630
  event: EngineEvent;
2631
+ } | {
2632
+ kind: 'events';
2633
+ events: EngineEvent[];
2631
2634
  } | {
2632
2635
  kind: 'cancel';
2633
2636
  runId: string;
@@ -5089,6 +5092,16 @@ declare class WorkflowEngine {
5089
5092
  /** Emit a locally-produced lifecycle event: deliver to subscribers AND broadcast it on the
5090
5093
  * control plane so other instances (e.g. a dashboard pod) can live-tail this run. */
5091
5094
  private emit;
5095
+ /** Events waiting for the control-plane publish in flight, in emit order (see {@link broadcastEvents}). */
5096
+ private readonly eventsToBroadcast;
5097
+ private broadcastingEvents;
5098
+ /**
5099
+ * Publish the queued events in the order they were emitted. A run's own events go out one per
5100
+ * message, as every instance expects them. Step events (a wide fan emits two per step) that queued
5101
+ * up while a publish was in flight go out together in one `events` message, so a fan of 40,000
5102
+ * steps costs the control plane a few hundred messages to every instance instead of 80,000.
5103
+ */
5104
+ private broadcastEvents;
5092
5105
  /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced
5093
5106
  * events and ones received from the control plane, so an event shows up once on every instance. */
5094
5107
  private deliver;
@@ -6270,10 +6283,17 @@ declare class WorkflowEngine {
6270
6283
  * result may have landed since. The fan's last turn so reads nothing it already read.
6271
6284
  */
6272
6285
  private fanCheckpoints;
6286
+ /** Write a round of a step fan's checkpoints: one call where the store batches, else chunked. */
6287
+ private saveFanCheckpoints;
6273
6288
  /**
6274
- * Write the `pending` checkpoints of a step fan's new steps, then dispatch them: one
6275
- * {@link StateStore.saveCheckpoints} call and one {@link TransportPool.dispatchMany} for the
6276
- * batch, where the store and transport have them.
6289
+ * Write the `pending` checkpoints of a step fan's new steps and dispatch them, in rounds of
6290
+ * {@link STEP_FAN_SEND_ROUND}: a round's steps are dispatched once its checkpoints commit, while the
6291
+ * next round's are written. One {@link StateStore.saveCheckpoints} call and one
6292
+ * {@link TransportPool.dispatchMany} per round, where the store and transport have them.
6293
+ *
6294
+ * No result can wake the run before every round is written: the run is `running` under this
6295
+ * turn's lease until the turn suspends. A send cut short leaves the unwritten steps without a
6296
+ * checkpoint, and the replay sends them once the written ones settle.
6277
6297
  */
6278
6298
  private sendStepFan;
6279
6299
  /** Remember a step settled here, forgetting entries older than a lease so the map stays small. */
package/dist/index.d.ts CHANGED
@@ -2628,6 +2628,9 @@ type ControlMessage = {
2628
2628
  } & ({
2629
2629
  kind: 'event';
2630
2630
  event: EngineEvent;
2631
+ } | {
2632
+ kind: 'events';
2633
+ events: EngineEvent[];
2631
2634
  } | {
2632
2635
  kind: 'cancel';
2633
2636
  runId: string;
@@ -5089,6 +5092,16 @@ declare class WorkflowEngine {
5089
5092
  /** Emit a locally-produced lifecycle event: deliver to subscribers AND broadcast it on the
5090
5093
  * control plane so other instances (e.g. a dashboard pod) can live-tail this run. */
5091
5094
  private emit;
5095
+ /** Events waiting for the control-plane publish in flight, in emit order (see {@link broadcastEvents}). */
5096
+ private readonly eventsToBroadcast;
5097
+ private broadcastingEvents;
5098
+ /**
5099
+ * Publish the queued events in the order they were emitted. A run's own events go out one per
5100
+ * message, as every instance expects them. Step events (a wide fan emits two per step) that queued
5101
+ * up while a publish was in flight go out together in one `events` message, so a fan of 40,000
5102
+ * steps costs the control plane a few hundred messages to every instance instead of 80,000.
5103
+ */
5104
+ private broadcastEvents;
5092
5105
  /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced
5093
5106
  * events and ones received from the control plane, so an event shows up once on every instance. */
5094
5107
  private deliver;
@@ -6270,10 +6283,17 @@ declare class WorkflowEngine {
6270
6283
  * result may have landed since. The fan's last turn so reads nothing it already read.
6271
6284
  */
6272
6285
  private fanCheckpoints;
6286
+ /** Write a round of a step fan's checkpoints: one call where the store batches, else chunked. */
6287
+ private saveFanCheckpoints;
6273
6288
  /**
6274
- * Write the `pending` checkpoints of a step fan's new steps, then dispatch them: one
6275
- * {@link StateStore.saveCheckpoints} call and one {@link TransportPool.dispatchMany} for the
6276
- * batch, where the store and transport have them.
6289
+ * Write the `pending` checkpoints of a step fan's new steps and dispatch them, in rounds of
6290
+ * {@link STEP_FAN_SEND_ROUND}: a round's steps are dispatched once its checkpoints commit, while the
6291
+ * next round's are written. One {@link StateStore.saveCheckpoints} call and one
6292
+ * {@link TransportPool.dispatchMany} per round, where the store and transport have them.
6293
+ *
6294
+ * No result can wake the run before every round is written: the run is `running` under this
6295
+ * turn's lease until the turn suspends. A send cut short leaves the unwritten steps without a
6296
+ * checkpoint, and the replay sends them once the written ones settle.
6277
6297
  */
6278
6298
  private sendStepFan;
6279
6299
  /** Remember a step settled here, forgetting entries older than a lease so the map stays small. */
package/dist/index.js CHANGED
@@ -4814,7 +4814,10 @@ var ANNOUNCED_ROUTES_TTL_MS = 5e3;
4814
4814
  var VERSION_UNDECLARED_TAG = "version:undeclared";
4815
4815
  var versionKey = /* @__PURE__ */ __name((name, version) => `${name}@${version}`, "versionKey");
4816
4816
  var STEP_FAN_CHUNK = 200;
4817
+ var STEP_EVENTS_PER_BROADCAST = 500;
4818
+ var isStepEvent = /* @__PURE__ */ __name((event) => event.type.startsWith("step."), "isStepEvent");
4817
4819
  var FAN_REREAD_CHUNK = 1e3;
4820
+ var STEP_FAN_SEND_ROUND = 2e3;
4818
4821
  var PARENT_NOTIFY_ATTEMPTS = 4;
4819
4822
  var PARENT_NOTIFY_BACKOFF_MS = 250;
4820
4823
  var LEASE_RECHECK_MAX_MS = 2e3;
@@ -5283,6 +5286,13 @@ var WorkflowEngine = class {
5283
5286
  ...msg.event,
5284
5287
  at: new Date(msg.event.at)
5285
5288
  }));
5289
+ } else if (msg.kind === "events") {
5290
+ for (const event of msg.events) {
5291
+ this.deliver(markRemoteEvent({
5292
+ ...event,
5293
+ at: new Date(event.at)
5294
+ }));
5295
+ }
5286
5296
  } else if (msg.kind === "cancel") {
5287
5297
  this.notifyCancelled(msg.runId);
5288
5298
  } else if (msg.kind === "enqueued") {
@@ -5864,13 +5874,53 @@ var WorkflowEngine = class {
5864
5874
  at: /* @__PURE__ */ new Date()
5865
5875
  };
5866
5876
  this.deliver(full);
5867
- if (this.controlPlane) {
5868
- void this.controlPlane.publishControl({
5869
- kind: "event",
5870
- event: full,
5871
- from: this.instanceId
5872
- }).catch(() => {
5873
- });
5877
+ if (!this.controlPlane) return;
5878
+ this.eventsToBroadcast.push(full);
5879
+ if (!this.broadcastingEvents) void this.broadcastEvents();
5880
+ }
5881
+ /** Events waiting for the control-plane publish in flight, in emit order (see {@link broadcastEvents}). */
5882
+ eventsToBroadcast = [];
5883
+ broadcastingEvents = false;
5884
+ /**
5885
+ * Publish the queued events in the order they were emitted. A run's own events go out one per
5886
+ * message, as every instance expects them. Step events (a wide fan emits two per step) that queued
5887
+ * up while a publish was in flight go out together in one `events` message, so a fan of 40,000
5888
+ * steps costs the control plane a few hundred messages to every instance instead of 80,000.
5889
+ */
5890
+ async broadcastEvents() {
5891
+ this.broadcastingEvents = true;
5892
+ try {
5893
+ while (this.eventsToBroadcast.length > 0 && this.controlPlane) {
5894
+ const first = this.eventsToBroadcast[0];
5895
+ let msg;
5896
+ if (isStepEvent(first)) {
5897
+ let n = 1;
5898
+ while (n < STEP_EVENTS_PER_BROADCAST && n < this.eventsToBroadcast.length && isStepEvent(this.eventsToBroadcast[n])) {
5899
+ n += 1;
5900
+ }
5901
+ const events = this.eventsToBroadcast.splice(0, n);
5902
+ msg = events.length === 1 ? {
5903
+ kind: "event",
5904
+ event: first,
5905
+ from: this.instanceId
5906
+ } : {
5907
+ kind: "events",
5908
+ events,
5909
+ from: this.instanceId
5910
+ };
5911
+ } else {
5912
+ this.eventsToBroadcast.shift();
5913
+ msg = {
5914
+ kind: "event",
5915
+ event: first,
5916
+ from: this.instanceId
5917
+ };
5918
+ }
5919
+ await this.controlPlane.publishControl(msg).catch(() => {
5920
+ });
5921
+ }
5922
+ } finally {
5923
+ this.broadcastingEvents = false;
5874
5924
  }
5875
5925
  }
5876
5926
  /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced
@@ -10225,10 +10275,25 @@ ${run.workflowVersion}`;
10225
10275
  }
10226
10276
  return bySeq;
10227
10277
  }
10278
+ /** Write a round of a step fan's checkpoints: one call where the store batches, else chunked. */
10279
+ async saveFanCheckpoints(checkpoints) {
10280
+ if (this.store.saveCheckpoints) {
10281
+ await this.store.saveCheckpoints(checkpoints);
10282
+ return;
10283
+ }
10284
+ for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10285
+ await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10286
+ }
10287
+ }
10228
10288
  /**
10229
- * Write the `pending` checkpoints of a step fan's new steps, then dispatch them: one
10230
- * {@link StateStore.saveCheckpoints} call and one {@link TransportPool.dispatchMany} for the
10231
- * batch, where the store and transport have them.
10289
+ * Write the `pending` checkpoints of a step fan's new steps and dispatch them, in rounds of
10290
+ * {@link STEP_FAN_SEND_ROUND}: a round's steps are dispatched once its checkpoints commit, while the
10291
+ * next round's are written. One {@link StateStore.saveCheckpoints} call and one
10292
+ * {@link TransportPool.dispatchMany} per round, where the store and transport have them.
10293
+ *
10294
+ * No result can wake the run before every round is written: the run is `running` under this
10295
+ * turn's lease until the turn suspends. A send cut short leaves the unwritten steps without a
10296
+ * checkpoint, and the replay sends them once the written ones settle.
10232
10297
  */
10233
10298
  async sendStepFan(runId, positions, step, inputs, batch, group, opts, routing) {
10234
10299
  const workerGroup = stepGroup(step, routing?.namespace);
@@ -10256,27 +10321,33 @@ ${run.workflowVersion}`;
10256
10321
  } : {}
10257
10322
  };
10258
10323
  });
10259
- if (this.store.saveCheckpoints) {
10260
- await this.store.saveCheckpoints(checkpoints);
10261
- } else {
10262
- for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10263
- await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10264
- }
10265
- }
10266
10324
  const traceparent = this.traceparent?.();
10267
10325
  const context = this.context?.();
10268
- await this.pool.dispatchMany(checkpoints.map((cp, k) => ({
10269
- runId,
10270
- seq: cp.seq,
10271
- name: step.name,
10272
- stepId: cp.stepId,
10273
- group: workerGroup,
10274
- input: inputs[batch[k]],
10275
- traceparent,
10276
- context,
10277
- priority: opts.admission?.priority,
10278
- attempt: 1
10279
- })), opts.transport);
10326
+ const dispatches = [];
10327
+ try {
10328
+ for (let from = 0; from < checkpoints.length; from += STEP_FAN_SEND_ROUND) {
10329
+ const round = checkpoints.slice(from, from + STEP_FAN_SEND_ROUND);
10330
+ await this.saveFanCheckpoints(round);
10331
+ const dispatch = this.pool.dispatchMany(round.map((cp, k) => ({
10332
+ runId,
10333
+ seq: cp.seq,
10334
+ name: step.name,
10335
+ stepId: cp.stepId,
10336
+ group: workerGroup,
10337
+ input: inputs[batch[from + k]],
10338
+ traceparent,
10339
+ context,
10340
+ priority: opts.admission?.priority,
10341
+ attempt: 1
10342
+ })), opts.transport);
10343
+ dispatch.catch(() => {
10344
+ });
10345
+ dispatches.push(dispatch);
10346
+ }
10347
+ } finally {
10348
+ await Promise.allSettled(dispatches);
10349
+ }
10350
+ await Promise.all(dispatches);
10280
10351
  for (const cp of checkpoints) {
10281
10352
  this.emit({
10282
10353
  type: "step.started",