@dudousxd/nestjs-durable-core 0.88.0 → 0.89.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
@@ -6726,6 +6726,10 @@ var WorkflowEngine = class _WorkflowEngine {
6726
6726
  workflows = /* @__PURE__ */ new Map();
6727
6727
  /** The newest registered version per workflow name — used to `start` new runs. */
6728
6728
  latest = /* @__PURE__ */ new Map();
6729
+ /** Workflows this instance starts but never runs, keyed `name@version` (like {@link workflows}), so every version stays startable. See {@link registerStartOnly}. */
6730
+ startOnly = /* @__PURE__ */ new Map();
6731
+ /** The latest start-only registration per name, for unpinned starts (like {@link latest}). */
6732
+ startOnlyLatest = /* @__PURE__ */ new Map();
6729
6733
  /** Event name → subscribing workflow name → that workflow's subscriptions to it. See `onEvent`. */
6730
6734
  eventTriggers = /* @__PURE__ */ new Map();
6731
6735
  /** Durable-entity subsystem (registers the `__entity` runner; see `registerEntity`). */
@@ -7246,7 +7250,7 @@ var WorkflowEngine = class _WorkflowEngine {
7246
7250
  if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
7247
7251
  const result = await this.settleUnregistered(run, nowMs);
7248
7252
  if (result) settled?.set(run.id, result);
7249
- else if (result === void 0) this.logUnregisteredSkip(run);
7253
+ else if (result === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
7250
7254
  continue;
7251
7255
  }
7252
7256
  try {
@@ -7392,6 +7396,34 @@ var WorkflowEngine = class _WorkflowEngine {
7392
7396
  }
7393
7397
  }
7394
7398
  /**
7399
+ * Let {@link start} create runs of `name@version` on this instance without registering a body here:
7400
+ * the run is stored and left for an instance that {@link register}ed the workflow, and this one
7401
+ * never executes it. For a process that only emits work (an API pod) while workers run it.
7402
+ *
7403
+ * Runs this instance picks up anyway (a driving poll, the run queue) are released for another
7404
+ * instance without a log line, and dead-lettered after `unregisteredWorkflowGraceMs` like any run
7405
+ * whose workflow nobody registered. A {@link register} of the same name on this instance wins.
7406
+ */
7407
+ registerStartOnly(name, version, opts) {
7408
+ const entry = {
7409
+ name,
7410
+ version,
7411
+ fn: /* @__PURE__ */ __name(() => {
7412
+ throw new Error(`workflow ${name} is start-only on this instance \u2014 it has no body here`);
7413
+ }, "fn"),
7414
+ tags: opts?.tags,
7415
+ validateInput: opts?.validateInput,
7416
+ origin: opts?.origin
7417
+ };
7418
+ this.startOnly.set(versionKey(name, version), entry);
7419
+ const current = this.startOnlyLatest.get(name);
7420
+ if (!current || isNewerVersion(version, current.version)) this.startOnlyLatest.set(name, entry);
7421
+ }
7422
+ /** Whether `run` is of a workflow this instance only starts (see {@link registerStartOnly}). */
7423
+ isStartOnlyHere(run) {
7424
+ return !this.isRegisteredHere(run) && this.startOnly.has(versionKey(run.workflow, run.workflowVersion));
7425
+ }
7426
+ /**
7395
7427
  * The retained in-process body for `name@version`, for an IN-APP worker that consumes this engine's
7396
7428
  * own group to SERVE a group-served `register(..., { group, executor })` workflow (uniform dispatch):
7397
7429
  * the worker fetches the body by name when it picks up the dispatched {@link WorkflowTask}, replays
@@ -8931,7 +8963,8 @@ var WorkflowEngine = class _WorkflowEngine {
8931
8963
  async start(workflow, input, runId, opts) {
8932
8964
  const name = workflowName(workflow);
8933
8965
  const pinned = opts?.version;
8934
- let registered = pinned === void 0 ? this.latest.get(name) : this.workflows.get(versionKey(name, pinned));
8966
+ const startOnly = pinned === void 0 ? this.startOnlyLatest.get(name) : this.startOnly.get(versionKey(name, pinned));
8967
+ let registered = pinned === void 0 ? this.latest.get(name) ?? startOnly : this.workflows.get(versionKey(name, pinned)) ?? startOnly;
8935
8968
  if (!registered && pinned === void 0) {
8936
8969
  const ancestor = await this.findRemoteAncestor(runId);
8937
8970
  if (ancestor) registered = this.synthesizeRemoteChild(name, ancestor.version, ancestor);
@@ -9034,7 +9067,10 @@ var WorkflowEngine = class _WorkflowEngine {
9034
9067
  status: "suspended"
9035
9068
  };
9036
9069
  }
9037
- await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
9070
+ const runsLocally = this.customDispatcher === void 0 && !this.dispatchesOverTransport;
9071
+ if (!(runsLocally && registered === startOnly)) {
9072
+ await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
9073
+ }
9038
9074
  if (this.controlPlane && !this.queuesRunsOverTransport) {
9039
9075
  void this.controlPlane.publishControl({
9040
9076
  kind: "enqueued",
@@ -9691,7 +9727,7 @@ var WorkflowEngine = class _WorkflowEngine {
9691
9727
  result: settled2,
9692
9728
  dispatch: false
9693
9729
  };
9694
- if (settled2 === void 0) this.logUnregisteredSkip(run);
9730
+ if (settled2 === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
9695
9731
  return void 0;
9696
9732
  }
9697
9733
  const settled = await this.countRecovery(run);
@@ -9972,6 +10008,7 @@ var WorkflowEngine = class _WorkflowEngine {
9972
10008
  const run = await this.store.getRun(runId);
9973
10009
  const settled = run ? await this.settleUnregistered(run, nowMs) : null;
9974
10010
  if (settled !== void 0) return settled;
10011
+ if (run && this.isStartOnlyHere(run)) return null;
9975
10012
  }
9976
10013
  throw err;
9977
10014
  });
@@ -10261,6 +10298,7 @@ var WorkflowEngine = class _WorkflowEngine {
10261
10298
  if (error2 instanceof WorkflowNotRegistered) {
10262
10299
  const settled = await this.settleUnregistered(run, this.clock()).catch(() => void 0);
10263
10300
  if (settled) return settled;
10301
+ if (this.isStartOnlyHere(run)) return void 0;
10264
10302
  }
10265
10303
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
10266
10304
  return void 0;
@@ -10290,7 +10328,7 @@ var WorkflowEngine = class _WorkflowEngine {
10290
10328
  if (error2 instanceof WorkflowNotRegistered) {
10291
10329
  const settled = await this.settleUnregistered(run, nowMs).catch(() => void 0);
10292
10330
  if (settled) results.push(settled);
10293
- if (settled !== void 0) continue;
10331
+ if (settled !== void 0 || this.isStartOnlyHere(run)) continue;
10294
10332
  }
10295
10333
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
10296
10334
  } finally {