@dudousxd/nestjs-durable-store-mikro-orm 0.30.0 → 0.31.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.d.cts CHANGED
@@ -188,6 +188,7 @@ declare class MikroOrmStateStore implements StateStore {
188
188
  tryAdmitSingleton(runId: string, tag: string, workflow: string, limit: number, retryWakeAt?: number): Promise<boolean>;
189
189
  createRun(run: WorkflowRun): Promise<void>;
190
190
  updateRun(runId: string, inputPatch: Partial<WorkflowRun>): Promise<void>;
191
+ updateRunIf(runId: string, expectedStatuses: RunStatus[], inputPatch: Partial<WorkflowRun>): Promise<boolean>;
191
192
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
192
193
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
193
194
  private reindexAttributes;
@@ -204,6 +205,7 @@ declare class MikroOrmStateStore implements StateStore {
204
205
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
205
206
  }) => Promise<T>): Promise<T>;
206
207
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
208
+ listOrphanedRuns(nowMs: number, limit: number, namespace?: string): Promise<WorkflowRun[]>;
207
209
  listPendingRuns(limit: number, namespace?: string): Promise<WorkflowRun[]>;
208
210
  listDueTimers(nowMs: number, namespace?: string, limit?: number): Promise<WorkflowRun[]>;
209
211
  tryLockRun(runId: string, owner: string, leaseUntilMs: number, nowMs: number): Promise<boolean>;
package/dist/index.d.ts CHANGED
@@ -188,6 +188,7 @@ declare class MikroOrmStateStore implements StateStore {
188
188
  tryAdmitSingleton(runId: string, tag: string, workflow: string, limit: number, retryWakeAt?: number): Promise<boolean>;
189
189
  createRun(run: WorkflowRun): Promise<void>;
190
190
  updateRun(runId: string, inputPatch: Partial<WorkflowRun>): Promise<void>;
191
+ updateRunIf(runId: string, expectedStatuses: RunStatus[], inputPatch: Partial<WorkflowRun>): Promise<boolean>;
191
192
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
192
193
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
193
194
  private reindexAttributes;
@@ -204,6 +205,7 @@ declare class MikroOrmStateStore implements StateStore {
204
205
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
205
206
  }) => Promise<T>): Promise<T>;
206
207
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
208
+ listOrphanedRuns(nowMs: number, limit: number, namespace?: string): Promise<WorkflowRun[]>;
207
209
  listPendingRuns(limit: number, namespace?: string): Promise<WorkflowRun[]>;
208
210
  listDueTimers(nowMs: number, namespace?: string, limit?: number): Promise<WorkflowRun[]>;
209
211
  tryLockRun(runId: string, owner: string, leaseUntilMs: number, nowMs: number): Promise<boolean>;
package/dist/index.js CHANGED
@@ -969,6 +969,30 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
969
969
  await em.flush();
970
970
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
971
971
  }
972
+ async updateRunIf(runId, expectedStatuses, inputPatch) {
973
+ if (expectedStatuses.length === 0) return false;
974
+ const patch = inputPatch.status && TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await clearTerminalSingletonAdmission(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
975
+ const data = runUpdateData(patch);
976
+ const em = this.fork();
977
+ let applied;
978
+ if (Object.keys(data).length) {
979
+ applied = await em.nativeUpdate(WorkflowRunEntity, {
980
+ id: runId,
981
+ status: {
982
+ $in: expectedStatuses
983
+ }
984
+ }, data) > 0;
985
+ } else {
986
+ applied = await em.count(WorkflowRunEntity, {
987
+ id: runId,
988
+ status: {
989
+ $in: expectedStatuses
990
+ }
991
+ }) > 0;
992
+ }
993
+ if (applied && "searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
994
+ return applied;
995
+ }
972
996
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
973
997
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
974
998
  async reindexAttributes(runId, attributes, forked) {
@@ -1133,6 +1157,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1133
1157
  });
1134
1158
  return rows.map(fromRunEntity);
1135
1159
  }
1160
+ async listOrphanedRuns(nowMs, limit, namespace) {
1161
+ if (limit <= 0) return [];
1162
+ const em = this.fork();
1163
+ const rows = await em.find(WorkflowRunEntity, {
1164
+ status: {
1165
+ $in: [
1166
+ "running",
1167
+ "cancelling"
1168
+ ]
1169
+ },
1170
+ $or: [
1171
+ {
1172
+ lockedUntil: null
1173
+ },
1174
+ {
1175
+ lockedUntil: {
1176
+ $lte: new Date(nowMs)
1177
+ }
1178
+ }
1179
+ ],
1180
+ ...namespace !== void 0 ? {
1181
+ namespace
1182
+ } : {}
1183
+ }, {
1184
+ orderBy: {
1185
+ createdAt: "ASC"
1186
+ },
1187
+ limit
1188
+ });
1189
+ return rows.map(fromRunEntity);
1190
+ }
1136
1191
  async listPendingRuns(limit, namespace) {
1137
1192
  const em = this.fork();
1138
1193
  const rows = await em.find(WorkflowRunEntity, {
@@ -1894,6 +1949,29 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1894
1949
  return affected > 0;
1895
1950
  }
1896
1951
  };
1952
+ function runUpdateData(patch) {
1953
+ const data = {};
1954
+ if ("workflow" in patch) data.workflow = patch.workflow;
1955
+ if ("workflowVersion" in patch) data.workflowVersion = patch.workflowVersion;
1956
+ if ("status" in patch) data.status = patch.status;
1957
+ if ("input" in patch) data.input = patch.input ?? null;
1958
+ if ("output" in patch) data.output = patch.output ?? null;
1959
+ if ("error" in patch) data.error = patch.error ?? null;
1960
+ if ("wakeAt" in patch) data.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
1961
+ if ("lockedBy" in patch) data.lockedBy = patch.lockedBy ?? null;
1962
+ if ("lockedUntil" in patch) data.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
1963
+ if ("awaitingDecisionTaskId" in patch) data.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
1964
+ if ("recoveryAttempts" in patch) data.recoveryAttempts = patch.recoveryAttempts ?? null;
1965
+ if ("tags" in patch) data.tags = patch.tags ?? null;
1966
+ if ("searchAttributes" in patch) data.searchAttributes = patch.searchAttributes ?? null;
1967
+ if ("priority" in patch) data.priority = patch.priority ?? null;
1968
+ if ("namespace" in patch) data.namespace = patch.namespace ?? "default";
1969
+ if ("origin" in patch) data.origin = patch.origin ?? null;
1970
+ if (patch.createdAt != null) data.createdAt = patch.createdAt;
1971
+ if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
1972
+ return data;
1973
+ }
1974
+ __name(runUpdateData, "runUpdateData");
1897
1975
  function toRunEntity(run) {
1898
1976
  const e = new WorkflowRunEntity();
1899
1977
  e.id = run.id;