@dudousxd/nestjs-durable-store-typeorm 0.22.0 → 0.23.1

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
@@ -1019,31 +1019,26 @@ var TypeOrmStateStore = class {
1019
1019
  }
1020
1020
  async updateRun(runId, inputPatch) {
1021
1021
  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.getRun(id)) : inputPatch;
1022
- const update = {};
1023
- if ("workflow" in patch) update.workflow = patch.workflow;
1024
- if ("workflowVersion" in patch) update.workflowVersion = patch.workflowVersion;
1025
- if ("status" in patch) update.status = patch.status;
1026
- if ("input" in patch) update.input = patch.input ?? null;
1027
- if ("output" in patch) update.output = patch.output ?? null;
1028
- if ("error" in patch) update.error = patch.error ?? null;
1029
- if ("wakeAt" in patch) update.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
1030
- if ("lockedBy" in patch) update.lockedBy = patch.lockedBy ?? null;
1031
- if ("lockedUntil" in patch) update.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
1032
- if ("awaitingDecisionTaskId" in patch) update.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
1033
- if ("recoveryAttempts" in patch) update.recoveryAttempts = patch.recoveryAttempts;
1034
- if ("tags" in patch) update.tags = patch.tags ?? null;
1035
- if ("searchAttributes" in patch) update.searchAttributes = patch.searchAttributes ?? null;
1036
- if ("priority" in patch) update.priority = patch.priority ?? null;
1037
- if ("namespace" in patch) update.namespace = patch.namespace ?? "default";
1038
- if ("origin" in patch) update.origin = patch.origin ?? null;
1039
- if ("createdAt" in patch) update.createdAt = patch.createdAt;
1040
- if ("updatedAt" in patch) update.updatedAt = patch.updatedAt;
1022
+ const update = runUpdateSet(patch);
1041
1023
  const result = await this.runs().createQueryBuilder().update().set(update).where({
1042
1024
  id: runId
1043
1025
  }).execute();
1044
1026
  if (!result.affected) throw new Error(`run ${runId} not found`);
1045
1027
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1046
1028
  }
1029
+ async updateRunIf(runId, expectedStatuses, inputPatch) {
1030
+ if (expectedStatuses.length === 0) return false;
1031
+ 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.getRun(id)) : inputPatch;
1032
+ const set = runUpdateSet(patch);
1033
+ const where = {
1034
+ id: runId,
1035
+ status: (0, import_typeorm2.In)(expectedStatuses)
1036
+ };
1037
+ const matched = Object.keys(set).length ? (await this.runs().createQueryBuilder().update().set(set).where(where).execute()).affected ?? 0 : await this.runs().countBy(where);
1038
+ if (!matched) return false;
1039
+ if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1040
+ return true;
1041
+ }
1047
1042
  async getRun(runId) {
1048
1043
  const e = await this.runs().findOneBy({
1049
1044
  id: runId
@@ -1163,6 +1158,39 @@ var TypeOrmStateStore = class {
1163
1158
  });
1164
1159
  return rows.map(fromRunEntity);
1165
1160
  }
1161
+ async listOrphanedRuns(nowMs, limit, namespace) {
1162
+ if (limit <= 0) return [];
1163
+ const now = new Date(nowMs);
1164
+ const rows = await this.runs().find({
1165
+ where: [
1166
+ {
1167
+ status: (0, import_typeorm2.In)([
1168
+ "running",
1169
+ "cancelling"
1170
+ ]),
1171
+ lockedUntil: (0, import_typeorm2.IsNull)(),
1172
+ ...namespace !== void 0 ? {
1173
+ namespace
1174
+ } : {}
1175
+ },
1176
+ {
1177
+ status: (0, import_typeorm2.In)([
1178
+ "running",
1179
+ "cancelling"
1180
+ ]),
1181
+ lockedUntil: (0, import_typeorm2.LessThanOrEqual)(now),
1182
+ ...namespace !== void 0 ? {
1183
+ namespace
1184
+ } : {}
1185
+ }
1186
+ ],
1187
+ order: {
1188
+ createdAt: "ASC"
1189
+ },
1190
+ take: limit
1191
+ });
1192
+ return rows.map(fromRunEntity);
1193
+ }
1166
1194
  async listPendingRuns(limit, namespace) {
1167
1195
  const rows = await this.runs().find({
1168
1196
  where: {
@@ -1827,6 +1855,29 @@ function fromScheduleEntity(e) {
1827
1855
  };
1828
1856
  }
1829
1857
  __name(fromScheduleEntity, "fromScheduleEntity");
1858
+ function runUpdateSet(patch) {
1859
+ const update = {};
1860
+ if ("workflow" in patch) update.workflow = patch.workflow;
1861
+ if ("workflowVersion" in patch) update.workflowVersion = patch.workflowVersion;
1862
+ if ("status" in patch) update.status = patch.status;
1863
+ if ("input" in patch) update.input = patch.input ?? null;
1864
+ if ("output" in patch) update.output = patch.output ?? null;
1865
+ if ("error" in patch) update.error = patch.error ?? null;
1866
+ if ("wakeAt" in patch) update.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
1867
+ if ("lockedBy" in patch) update.lockedBy = patch.lockedBy ?? null;
1868
+ if ("lockedUntil" in patch) update.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
1869
+ if ("awaitingDecisionTaskId" in patch) update.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
1870
+ if ("recoveryAttempts" in patch) update.recoveryAttempts = patch.recoveryAttempts;
1871
+ if ("tags" in patch) update.tags = patch.tags ?? null;
1872
+ if ("searchAttributes" in patch) update.searchAttributes = patch.searchAttributes ?? null;
1873
+ if ("priority" in patch) update.priority = patch.priority ?? null;
1874
+ if ("namespace" in patch) update.namespace = patch.namespace ?? "default";
1875
+ if ("origin" in patch) update.origin = patch.origin ?? null;
1876
+ if ("createdAt" in patch) update.createdAt = patch.createdAt;
1877
+ if ("updatedAt" in patch) update.updatedAt = patch.updatedAt;
1878
+ return update;
1879
+ }
1880
+ __name(runUpdateSet, "runUpdateSet");
1830
1881
  async function updateIfPending(repo, checkpoint) {
1831
1882
  const { runId, seq, ...data } = toCheckpointEntity(checkpoint);
1832
1883
  const set = {