@dudousxd/nestjs-durable-store-typeorm 0.25.7 → 0.25.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.cjs CHANGED
@@ -1155,15 +1155,23 @@ var TypeOrmStateStore = class {
1155
1155
  await previous;
1156
1156
  }
1157
1157
  try {
1158
+ const where = {
1159
+ workflow,
1160
+ status: (0, import_typeorm2.In)(import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES)
1161
+ };
1162
+ const claimants = sqlite ? [] : await this.singletonClaimants(where, tag);
1158
1163
  return await this.dataSource.transaction(async (em) => {
1159
1164
  const repo = em.getRepository(WorkflowRunEntity);
1160
- const where = {
1161
- workflow,
1162
- status: (0, import_typeorm2.In)(import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES)
1163
- };
1164
- await repo.update(where, {
1165
+ if (sqlite) await repo.update(where, {
1165
1166
  workflow
1166
1167
  });
1168
+ for (let at = 0; at < claimants.length; at += import_nestjs_durable_core.SINGLETON_LOCK_CHUNK) {
1169
+ await repo.createQueryBuilder("r").select([
1170
+ "r.id"
1171
+ ]).where("r.id IN (:...ids)", {
1172
+ ids: claimants.slice(at, at + import_nestjs_durable_core.SINGLETON_LOCK_CHUNK)
1173
+ }).orderBy("r.id", "ASC").setLock("pessimistic_write").getMany();
1174
+ }
1167
1175
  const runs = (await repo.find({
1168
1176
  where
1169
1177
  })).map(fromRunEntity);
@@ -1191,6 +1199,24 @@ var TypeOrmStateStore = class {
1191
1199
  release?.();
1192
1200
  }
1193
1201
  }
1202
+ /** Read on the primary: a lagging replica would leave rows out of the lock set. */
1203
+ async singletonClaimants(where, tag) {
1204
+ const runner = this.dataSource.createQueryRunner("master");
1205
+ try {
1206
+ return (0, import_nestjs_durable_core.singletonClaimantIds)(await runner.manager.getRepository(WorkflowRunEntity).find({
1207
+ select: {
1208
+ id: true,
1209
+ tags: true
1210
+ },
1211
+ where,
1212
+ order: {
1213
+ id: "ASC"
1214
+ }
1215
+ }), tag);
1216
+ } finally {
1217
+ await runner.release();
1218
+ }
1219
+ }
1194
1220
  async createRun(run) {
1195
1221
  await this.runs().createQueryBuilder().insert().values(toRunEntity(run)).updateEntity(false).execute();
1196
1222
  await this.reindexAttributes(run.id, run.searchAttributes);