@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.cjs CHANGED
@@ -1011,6 +1011,30 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1011
1011
  await em.flush();
1012
1012
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1013
1013
  }
1014
+ async updateRunIf(runId, expectedStatuses, inputPatch) {
1015
+ if (expectedStatuses.length === 0) return false;
1016
+ const patch = inputPatch.status && import_nestjs_durable_core.TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await (0, import_nestjs_durable_core.clearTerminalSingletonAdmission)(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
1017
+ const data = runUpdateData(patch);
1018
+ const em = this.fork();
1019
+ let applied;
1020
+ if (Object.keys(data).length) {
1021
+ applied = await em.nativeUpdate(WorkflowRunEntity, {
1022
+ id: runId,
1023
+ status: {
1024
+ $in: expectedStatuses
1025
+ }
1026
+ }, data) > 0;
1027
+ } else {
1028
+ applied = await em.count(WorkflowRunEntity, {
1029
+ id: runId,
1030
+ status: {
1031
+ $in: expectedStatuses
1032
+ }
1033
+ }) > 0;
1034
+ }
1035
+ if (applied && "searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1036
+ return applied;
1037
+ }
1014
1038
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
1015
1039
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
1016
1040
  async reindexAttributes(runId, attributes, forked) {
@@ -1175,6 +1199,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1175
1199
  });
1176
1200
  return rows.map(fromRunEntity);
1177
1201
  }
1202
+ async listOrphanedRuns(nowMs, limit, namespace) {
1203
+ if (limit <= 0) return [];
1204
+ const em = this.fork();
1205
+ const rows = await em.find(WorkflowRunEntity, {
1206
+ status: {
1207
+ $in: [
1208
+ "running",
1209
+ "cancelling"
1210
+ ]
1211
+ },
1212
+ $or: [
1213
+ {
1214
+ lockedUntil: null
1215
+ },
1216
+ {
1217
+ lockedUntil: {
1218
+ $lte: new Date(nowMs)
1219
+ }
1220
+ }
1221
+ ],
1222
+ ...namespace !== void 0 ? {
1223
+ namespace
1224
+ } : {}
1225
+ }, {
1226
+ orderBy: {
1227
+ createdAt: "ASC"
1228
+ },
1229
+ limit
1230
+ });
1231
+ return rows.map(fromRunEntity);
1232
+ }
1178
1233
  async listPendingRuns(limit, namespace) {
1179
1234
  const em = this.fork();
1180
1235
  const rows = await em.find(WorkflowRunEntity, {
@@ -1936,6 +1991,29 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1936
1991
  return affected > 0;
1937
1992
  }
1938
1993
  };
1994
+ function runUpdateData(patch) {
1995
+ const data = {};
1996
+ if ("workflow" in patch) data.workflow = patch.workflow;
1997
+ if ("workflowVersion" in patch) data.workflowVersion = patch.workflowVersion;
1998
+ if ("status" in patch) data.status = patch.status;
1999
+ if ("input" in patch) data.input = patch.input ?? null;
2000
+ if ("output" in patch) data.output = patch.output ?? null;
2001
+ if ("error" in patch) data.error = patch.error ?? null;
2002
+ if ("wakeAt" in patch) data.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
2003
+ if ("lockedBy" in patch) data.lockedBy = patch.lockedBy ?? null;
2004
+ if ("lockedUntil" in patch) data.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
2005
+ if ("awaitingDecisionTaskId" in patch) data.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
2006
+ if ("recoveryAttempts" in patch) data.recoveryAttempts = patch.recoveryAttempts ?? null;
2007
+ if ("tags" in patch) data.tags = patch.tags ?? null;
2008
+ if ("searchAttributes" in patch) data.searchAttributes = patch.searchAttributes ?? null;
2009
+ if ("priority" in patch) data.priority = patch.priority ?? null;
2010
+ if ("namespace" in patch) data.namespace = patch.namespace ?? "default";
2011
+ if ("origin" in patch) data.origin = patch.origin ?? null;
2012
+ if (patch.createdAt != null) data.createdAt = patch.createdAt;
2013
+ if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
2014
+ return data;
2015
+ }
2016
+ __name(runUpdateData, "runUpdateData");
1939
2017
  function toRunEntity(run) {
1940
2018
  const e = new WorkflowRunEntity();
1941
2019
  e.id = run.id;