@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.cjs CHANGED
@@ -5031,7 +5031,10 @@ var ANNOUNCED_ROUTES_TTL_MS = 5e3;
5031
5031
  var VERSION_UNDECLARED_TAG = "version:undeclared";
5032
5032
  var versionKey = /* @__PURE__ */ __name((name, version) => `${name}@${version}`, "versionKey");
5033
5033
  var STEP_FAN_CHUNK = 200;
5034
+ var STEP_EVENTS_PER_BROADCAST = 500;
5035
+ var isStepEvent = /* @__PURE__ */ __name((event) => event.type.startsWith("step."), "isStepEvent");
5034
5036
  var FAN_REREAD_CHUNK = 1e3;
5037
+ var STEP_FAN_SEND_ROUND = 2e3;
5035
5038
  var PARENT_NOTIFY_ATTEMPTS = 4;
5036
5039
  var PARENT_NOTIFY_BACKOFF_MS = 250;
5037
5040
  var LEASE_RECHECK_MAX_MS = 2e3;
@@ -5500,6 +5503,13 @@ var WorkflowEngine = class {
5500
5503
  ...msg.event,
5501
5504
  at: new Date(msg.event.at)
5502
5505
  }));
5506
+ } else if (msg.kind === "events") {
5507
+ for (const event of msg.events) {
5508
+ this.deliver(markRemoteEvent({
5509
+ ...event,
5510
+ at: new Date(event.at)
5511
+ }));
5512
+ }
5503
5513
  } else if (msg.kind === "cancel") {
5504
5514
  this.notifyCancelled(msg.runId);
5505
5515
  } else if (msg.kind === "enqueued") {
@@ -6081,13 +6091,53 @@ var WorkflowEngine = class {
6081
6091
  at: /* @__PURE__ */ new Date()
6082
6092
  };
6083
6093
  this.deliver(full);
6084
- if (this.controlPlane) {
6085
- void this.controlPlane.publishControl({
6086
- kind: "event",
6087
- event: full,
6088
- from: this.instanceId
6089
- }).catch(() => {
6090
- });
6094
+ if (!this.controlPlane) return;
6095
+ this.eventsToBroadcast.push(full);
6096
+ if (!this.broadcastingEvents) void this.broadcastEvents();
6097
+ }
6098
+ /** Events waiting for the control-plane publish in flight, in emit order (see {@link broadcastEvents}). */
6099
+ eventsToBroadcast = [];
6100
+ broadcastingEvents = false;
6101
+ /**
6102
+ * Publish the queued events in the order they were emitted. A run's own events go out one per
6103
+ * message, as every instance expects them. Step events (a wide fan emits two per step) that queued
6104
+ * up while a publish was in flight go out together in one `events` message, so a fan of 40,000
6105
+ * steps costs the control plane a few hundred messages to every instance instead of 80,000.
6106
+ */
6107
+ async broadcastEvents() {
6108
+ this.broadcastingEvents = true;
6109
+ try {
6110
+ while (this.eventsToBroadcast.length > 0 && this.controlPlane) {
6111
+ const first = this.eventsToBroadcast[0];
6112
+ let msg;
6113
+ if (isStepEvent(first)) {
6114
+ let n = 1;
6115
+ while (n < STEP_EVENTS_PER_BROADCAST && n < this.eventsToBroadcast.length && isStepEvent(this.eventsToBroadcast[n])) {
6116
+ n += 1;
6117
+ }
6118
+ const events = this.eventsToBroadcast.splice(0, n);
6119
+ msg = events.length === 1 ? {
6120
+ kind: "event",
6121
+ event: first,
6122
+ from: this.instanceId
6123
+ } : {
6124
+ kind: "events",
6125
+ events,
6126
+ from: this.instanceId
6127
+ };
6128
+ } else {
6129
+ this.eventsToBroadcast.shift();
6130
+ msg = {
6131
+ kind: "event",
6132
+ event: first,
6133
+ from: this.instanceId
6134
+ };
6135
+ }
6136
+ await this.controlPlane.publishControl(msg).catch(() => {
6137
+ });
6138
+ }
6139
+ } finally {
6140
+ this.broadcastingEvents = false;
6091
6141
  }
6092
6142
  }
6093
6143
  /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced
@@ -10442,10 +10492,25 @@ ${run.workflowVersion}`;
10442
10492
  }
10443
10493
  return bySeq;
10444
10494
  }
10495
+ /** Write a round of a step fan's checkpoints: one call where the store batches, else chunked. */
10496
+ async saveFanCheckpoints(checkpoints) {
10497
+ if (this.store.saveCheckpoints) {
10498
+ await this.store.saveCheckpoints(checkpoints);
10499
+ return;
10500
+ }
10501
+ for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10502
+ await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10503
+ }
10504
+ }
10445
10505
  /**
10446
- * Write the `pending` checkpoints of a step fan's new steps, then dispatch them: one
10447
- * {@link StateStore.saveCheckpoints} call and one {@link TransportPool.dispatchMany} for the
10448
- * batch, where the store and transport have them.
10506
+ * Write the `pending` checkpoints of a step fan's new steps and dispatch them, in rounds of
10507
+ * {@link STEP_FAN_SEND_ROUND}: a round's steps are dispatched once its checkpoints commit, while the
10508
+ * next round's are written. One {@link StateStore.saveCheckpoints} call and one
10509
+ * {@link TransportPool.dispatchMany} per round, where the store and transport have them.
10510
+ *
10511
+ * No result can wake the run before every round is written: the run is `running` under this
10512
+ * turn's lease until the turn suspends. A send cut short leaves the unwritten steps without a
10513
+ * checkpoint, and the replay sends them once the written ones settle.
10449
10514
  */
10450
10515
  async sendStepFan(runId, positions, step, inputs, batch, group, opts, routing) {
10451
10516
  const workerGroup = stepGroup(step, routing?.namespace);
@@ -10473,27 +10538,33 @@ ${run.workflowVersion}`;
10473
10538
  } : {}
10474
10539
  };
10475
10540
  });
10476
- if (this.store.saveCheckpoints) {
10477
- await this.store.saveCheckpoints(checkpoints);
10478
- } else {
10479
- for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10480
- await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10481
- }
10482
- }
10483
10541
  const traceparent = this.traceparent?.();
10484
10542
  const context = this.context?.();
10485
- await this.pool.dispatchMany(checkpoints.map((cp, k) => ({
10486
- runId,
10487
- seq: cp.seq,
10488
- name: step.name,
10489
- stepId: cp.stepId,
10490
- group: workerGroup,
10491
- input: inputs[batch[k]],
10492
- traceparent,
10493
- context,
10494
- priority: opts.admission?.priority,
10495
- attempt: 1
10496
- })), opts.transport);
10543
+ const dispatches = [];
10544
+ try {
10545
+ for (let from = 0; from < checkpoints.length; from += STEP_FAN_SEND_ROUND) {
10546
+ const round = checkpoints.slice(from, from + STEP_FAN_SEND_ROUND);
10547
+ await this.saveFanCheckpoints(round);
10548
+ const dispatch = this.pool.dispatchMany(round.map((cp, k) => ({
10549
+ runId,
10550
+ seq: cp.seq,
10551
+ name: step.name,
10552
+ stepId: cp.stepId,
10553
+ group: workerGroup,
10554
+ input: inputs[batch[from + k]],
10555
+ traceparent,
10556
+ context,
10557
+ priority: opts.admission?.priority,
10558
+ attempt: 1
10559
+ })), opts.transport);
10560
+ dispatch.catch(() => {
10561
+ });
10562
+ dispatches.push(dispatch);
10563
+ }
10564
+ } finally {
10565
+ await Promise.allSettled(dispatches);
10566
+ }
10567
+ await Promise.all(dispatches);
10497
10568
  for (const cp of checkpoints) {
10498
10569
  this.emit({
10499
10570
  type: "step.started",