@dudousxd/nestjs-durable-store-mikro-orm 0.33.6 → 0.33.8

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.js CHANGED
@@ -667,7 +667,7 @@ __name(durableEntities, "durableEntities");
667
667
  var ENTITIES = durableEntities();
668
668
 
669
669
  // src/mikro-orm-state-store.ts
670
- import { ENGINE_MINTED_TAG_PREFIXES, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
670
+ import { ENGINE_MINTED_TAG_PREFIXES, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
671
671
  import { LockMode, raw } from "@mikro-orm/core";
672
672
 
673
673
  // src/schema.ts
@@ -1093,19 +1093,48 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1093
1093
  });
1094
1094
  }
1095
1095
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
1096
+ const where = {
1097
+ workflow,
1098
+ status: {
1099
+ $in: SINGLETON_ACTIVE_STATUSES
1100
+ },
1101
+ ...this.scopeNamespace !== void 0 ? {
1102
+ namespace: this.scopeNamespace
1103
+ } : {}
1104
+ };
1105
+ const reader = this.fork();
1106
+ const sqlite = /sqlite|libsql/i.test(reader.getPlatform().constructor.name);
1107
+ const claimants = sqlite ? [] : singletonClaimantIds(await reader.find(WorkflowRunEntity, where, {
1108
+ fields: [
1109
+ "id",
1110
+ "tags"
1111
+ ],
1112
+ orderBy: {
1113
+ id: "ASC"
1114
+ },
1115
+ // The primary: a lagging replica would leave rows out of the lock set.
1116
+ connectionType: "write"
1117
+ }), tag);
1096
1118
  return this.fork().transactional(async (em) => {
1097
- const where = {
1098
- workflow,
1099
- status: {
1100
- $in: SINGLETON_ACTIVE_STATUSES
1101
- },
1102
- ...this.scopeNamespace !== void 0 ? {
1103
- namespace: this.scopeNamespace
1104
- } : {}
1105
- };
1106
- await em.nativeUpdate(WorkflowRunEntity, where, {
1119
+ if (sqlite) await em.nativeUpdate(WorkflowRunEntity, where, {
1107
1120
  workflow
1108
1121
  });
1122
+ for (let at = 0; at < claimants.length; at += SINGLETON_LOCK_CHUNK) {
1123
+ await em.find(WorkflowRunEntity, {
1124
+ id: {
1125
+ $in: claimants.slice(at, at + SINGLETON_LOCK_CHUNK)
1126
+ }
1127
+ }, {
1128
+ fields: [
1129
+ "id"
1130
+ ],
1131
+ orderBy: {
1132
+ id: "ASC"
1133
+ },
1134
+ lockMode: LockMode.PESSIMISTIC_WRITE
1135
+ });
1136
+ }
1137
+ em.clear();
1109
1138
  const rows = await em.find(WorkflowRunEntity, where);
1110
1139
  const runs = rows.map(fromRunEntity);
1111
1140
  const tags = singletonAdmissionTags(runs, runId, tag, workflow, limit);
@@ -1130,22 +1159,25 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1130
1159
  });
1131
1160
  }
1132
1161
  async createRun(run) {
1133
- const em = this.fork();
1134
- em.create(WorkflowRunEntity, toRunEntity(run));
1135
- await em.flush();
1162
+ await this.fork().insert(WorkflowRunEntity, toRunEntity(run));
1136
1163
  await this.reindexAttributes(run.id, run.searchAttributes);
1137
1164
  }
1138
1165
  async updateRun(runId, inputPatch) {
1139
1166
  const patch = inputPatch.status && TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await clearTerminalSingletonAdmission(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
1167
+ const data = runUpdateData(patch);
1140
1168
  const em = this.fork();
1141
- const entity = await em.findOneOrFail(WorkflowRunEntity, {
1142
- id: runId
1143
- });
1144
- Object.assign(entity, toRunEntity({
1145
- ...fromRunEntity(entity),
1146
- ...patch
1147
- }));
1148
- await em.flush();
1169
+ if (Object.keys(data).length) {
1170
+ const affected = await em.nativeUpdate(WorkflowRunEntity, {
1171
+ id: runId
1172
+ }, data);
1173
+ if (affected === 0) await em.findOneOrFail(WorkflowRunEntity, {
1174
+ id: runId
1175
+ });
1176
+ } else {
1177
+ await em.findOneOrFail(WorkflowRunEntity, {
1178
+ id: runId
1179
+ });
1180
+ }
1149
1181
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1150
1182
  }
1151
1183
  async updateRunIf(runId, expectedStatuses, inputPatch) {