@dudousxd/nestjs-durable-store-mikro-orm 0.30.0 → 0.32.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
@@ -1,6 +1,6 @@
1
1
  import * as _mikro_orm_core from '@mikro-orm/core';
2
2
  import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
3
- import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
3
+ import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -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, ownedBy?: OwnedLeaseReclaim): 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
@@ -1,6 +1,6 @@
1
1
  import * as _mikro_orm_core from '@mikro-orm/core';
2
2
  import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
3
- import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
3
+ import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -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, ownedBy?: OwnedLeaseReclaim): 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
@@ -938,6 +938,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
938
938
  }, {
939
939
  status: "suspended",
940
940
  wakeAt: new Date(patch.wakeAt),
941
+ tags: patch.tags,
941
942
  updatedAt: patch.updatedAt
942
943
  });
943
944
  return false;
@@ -969,6 +970,30 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
969
970
  await em.flush();
970
971
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
971
972
  }
973
+ async updateRunIf(runId, expectedStatuses, inputPatch) {
974
+ if (expectedStatuses.length === 0) return false;
975
+ const patch = inputPatch.status && TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await clearTerminalSingletonAdmission(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
976
+ const data = runUpdateData(patch);
977
+ const em = this.fork();
978
+ let applied;
979
+ if (Object.keys(data).length) {
980
+ applied = await em.nativeUpdate(WorkflowRunEntity, {
981
+ id: runId,
982
+ status: {
983
+ $in: expectedStatuses
984
+ }
985
+ }, data) > 0;
986
+ } else {
987
+ applied = await em.count(WorkflowRunEntity, {
988
+ id: runId,
989
+ status: {
990
+ $in: expectedStatuses
991
+ }
992
+ }) > 0;
993
+ }
994
+ if (applied && "searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
995
+ return applied;
996
+ }
972
997
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
973
998
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
974
999
  async reindexAttributes(runId, attributes, forked) {
@@ -1133,6 +1158,46 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1133
1158
  });
1134
1159
  return rows.map(fromRunEntity);
1135
1160
  }
1161
+ async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
1162
+ if (limit <= 0) return [];
1163
+ const em = this.fork();
1164
+ const rows = await em.find(WorkflowRunEntity, {
1165
+ status: {
1166
+ $in: [
1167
+ "running",
1168
+ "cancelling"
1169
+ ]
1170
+ },
1171
+ $or: [
1172
+ {
1173
+ lockedUntil: null
1174
+ },
1175
+ {
1176
+ lockedUntil: {
1177
+ $lte: new Date(nowMs)
1178
+ }
1179
+ },
1180
+ // A live lease left by a previous process with the caller's own instance id.
1181
+ ...ownedBy ? [
1182
+ {
1183
+ lockedBy: ownedBy.owner,
1184
+ lockedUntil: {
1185
+ $lt: new Date(ownedBy.lockedBefore)
1186
+ }
1187
+ }
1188
+ ] : []
1189
+ ],
1190
+ ...namespace !== void 0 ? {
1191
+ namespace
1192
+ } : {}
1193
+ }, {
1194
+ orderBy: {
1195
+ createdAt: "ASC"
1196
+ },
1197
+ limit
1198
+ });
1199
+ return rows.map(fromRunEntity);
1200
+ }
1136
1201
  async listPendingRuns(limit, namespace) {
1137
1202
  const em = this.fork();
1138
1203
  const rows = await em.find(WorkflowRunEntity, {
@@ -1524,7 +1589,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1524
1589
  * single statement. */
1525
1590
  async updateSchedule(id, patch, expectedNextFireAt) {
1526
1591
  const data = {};
1527
- if (patch.namespace !== void 0) data.namespace = patch.namespace;
1592
+ if (patch.namespace !== void 0) data.namespace = patch.namespace || "default";
1528
1593
  if (patch.workflow !== void 0) data.workflow = patch.workflow;
1529
1594
  if (patch.paused !== void 0) data.paused = patch.paused;
1530
1595
  if ("nextFireAt" in patch) data.nextFireAt = patch.nextFireAt ?? null;
@@ -1894,6 +1959,29 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1894
1959
  return affected > 0;
1895
1960
  }
1896
1961
  };
1962
+ function runUpdateData(patch) {
1963
+ const data = {};
1964
+ if ("workflow" in patch) data.workflow = patch.workflow;
1965
+ if ("workflowVersion" in patch) data.workflowVersion = patch.workflowVersion;
1966
+ if ("status" in patch) data.status = patch.status;
1967
+ if ("input" in patch) data.input = patch.input ?? null;
1968
+ if ("output" in patch) data.output = patch.output ?? null;
1969
+ if ("error" in patch) data.error = patch.error ?? null;
1970
+ if ("wakeAt" in patch) data.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
1971
+ if ("lockedBy" in patch) data.lockedBy = patch.lockedBy ?? null;
1972
+ if ("lockedUntil" in patch) data.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
1973
+ if ("awaitingDecisionTaskId" in patch) data.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
1974
+ if ("recoveryAttempts" in patch) data.recoveryAttempts = patch.recoveryAttempts ?? null;
1975
+ if ("tags" in patch) data.tags = patch.tags ?? null;
1976
+ if ("searchAttributes" in patch) data.searchAttributes = patch.searchAttributes ?? null;
1977
+ if ("priority" in patch) data.priority = patch.priority ?? null;
1978
+ if ("namespace" in patch) data.namespace = patch.namespace || "default";
1979
+ if ("origin" in patch) data.origin = patch.origin ?? null;
1980
+ if (patch.createdAt != null) data.createdAt = patch.createdAt;
1981
+ if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
1982
+ return data;
1983
+ }
1984
+ __name(runUpdateData, "runUpdateData");
1897
1985
  function toRunEntity(run) {
1898
1986
  const e = new WorkflowRunEntity();
1899
1987
  e.id = run.id;
@@ -1911,7 +1999,7 @@ function toRunEntity(run) {
1911
1999
  e.tags = run.tags ?? null;
1912
2000
  e.searchAttributes = run.searchAttributes ?? null;
1913
2001
  e.priority = run.priority ?? null;
1914
- e.namespace = run.namespace ?? "default";
2002
+ e.namespace = run.namespace || "default";
1915
2003
  e.origin = run.origin ?? null;
1916
2004
  e.createdAt = run.createdAt;
1917
2005
  e.updatedAt = run.updatedAt;