@dudousxd/nestjs-durable-store-mikro-orm 0.33.7 → 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);