@dudousxd/nestjs-durable-store-typeorm 0.19.0 → 0.19.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
@@ -886,6 +886,7 @@ __name(existingColumns, "existingColumns");
886
886
  // src/typeorm-state-store.ts
887
887
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
888
888
  var import_typeorm2 = require("typeorm");
889
+ var sqliteSingletonClaims = /* @__PURE__ */ new WeakMap();
889
890
  var TypeOrmStateStore = class {
890
891
  static {
891
892
  __name(this, "TypeOrmStateStore");
@@ -928,11 +929,63 @@ var TypeOrmStateStore = class {
928
929
  const rows = (0, import_nestjs_durable_core.normalizeAttributeRows)(runId, attributes);
929
930
  if (rows.length) await repo.insert(rows);
930
931
  }
932
+ async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
933
+ const previous = sqliteSingletonClaims.get(this.dataSource) ?? Promise.resolve();
934
+ const sqlite = [
935
+ "sqlite",
936
+ "better-sqlite3",
937
+ "sqljs"
938
+ ].includes(this.dataSource.options.type);
939
+ let release;
940
+ if (sqlite) {
941
+ const next = new Promise((resolve) => {
942
+ release = resolve;
943
+ });
944
+ sqliteSingletonClaims.set(this.dataSource, previous.then(() => next));
945
+ await previous;
946
+ }
947
+ try {
948
+ return await this.dataSource.transaction(async (em) => {
949
+ const repo = em.getRepository(WorkflowRunEntity);
950
+ const where = {
951
+ workflow,
952
+ status: (0, import_typeorm2.In)(import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES)
953
+ };
954
+ await repo.update(where, {
955
+ workflow
956
+ });
957
+ const runs = (await repo.find({
958
+ where
959
+ })).map(fromRunEntity);
960
+ const tags = (0, import_nestjs_durable_core.singletonAdmissionTags)(runs, runId, tag, workflow, limit);
961
+ if (!tags) {
962
+ const patch = (0, import_nestjs_durable_core.singletonAdmissionDenialPatch)(runs, runId, tag, workflow, retryWakeAt);
963
+ if (patch) await repo.update({
964
+ id: runId
965
+ }, {
966
+ status: "suspended",
967
+ wakeAt: new Date(patch.wakeAt),
968
+ updatedAt: patch.updatedAt
969
+ });
970
+ return false;
971
+ }
972
+ await repo.update({
973
+ id: runId
974
+ }, {
975
+ tags
976
+ });
977
+ return true;
978
+ });
979
+ } finally {
980
+ release?.();
981
+ }
982
+ }
931
983
  async createRun(run) {
932
984
  await this.runs().save(toRunEntity(run));
933
985
  await this.reindexAttributes(run.id, run.searchAttributes);
934
986
  }
935
- async updateRun(runId, patch) {
987
+ async updateRun(runId, inputPatch) {
988
+ 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;
936
989
  const update = {};
937
990
  if ("workflow" in patch) update.workflow = patch.workflow;
938
991
  if ("workflowVersion" in patch) update.workflowVersion = patch.workflowVersion;