@dudousxd/nestjs-durable-store-typeorm 0.25.7 → 0.26.0

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
@@ -1090,6 +1090,9 @@ __name(existingColumns, "existingColumns");
1090
1090
  // src/typeorm-state-store.ts
1091
1091
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
1092
1092
  var import_typeorm2 = require("typeorm");
1093
+ var RETENTION_EXCLUDED = [
1094
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
1095
+ ];
1093
1096
  var CHECKPOINT_NAME_KIND_SQL = "CASE WHEN cp.name LIKE 'signal:child:%' THEN 'child' WHEN cp.name LIKE 'fan:%' THEN 'folded' WHEN cp.name LIKE 'spawn:%' THEN 'spawn' ELSE 'step' END";
1094
1097
  var sqliteSingletonClaims = /* @__PURE__ */ new WeakMap();
1095
1098
  var TypeOrmStateStore = class {
@@ -1155,15 +1158,23 @@ var TypeOrmStateStore = class {
1155
1158
  await previous;
1156
1159
  }
1157
1160
  try {
1161
+ const where = {
1162
+ workflow,
1163
+ status: (0, import_typeorm2.In)(import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES)
1164
+ };
1165
+ const claimants = sqlite ? [] : await this.singletonClaimants(where, tag);
1158
1166
  return await this.dataSource.transaction(async (em) => {
1159
1167
  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, {
1168
+ if (sqlite) await repo.update(where, {
1165
1169
  workflow
1166
1170
  });
1171
+ for (let at = 0; at < claimants.length; at += import_nestjs_durable_core.SINGLETON_LOCK_CHUNK) {
1172
+ await repo.createQueryBuilder("r").select([
1173
+ "r.id"
1174
+ ]).where("r.id IN (:...ids)", {
1175
+ ids: claimants.slice(at, at + import_nestjs_durable_core.SINGLETON_LOCK_CHUNK)
1176
+ }).orderBy("r.id", "ASC").setLock("pessimistic_write").getMany();
1177
+ }
1167
1178
  const runs = (await repo.find({
1168
1179
  where
1169
1180
  })).map(fromRunEntity);
@@ -1191,6 +1202,24 @@ var TypeOrmStateStore = class {
1191
1202
  release?.();
1192
1203
  }
1193
1204
  }
1205
+ /** Read on the primary: a lagging replica would leave rows out of the lock set. */
1206
+ async singletonClaimants(where, tag) {
1207
+ const runner = this.dataSource.createQueryRunner("master");
1208
+ try {
1209
+ return (0, import_nestjs_durable_core.singletonClaimantIds)(await runner.manager.getRepository(WorkflowRunEntity).find({
1210
+ select: {
1211
+ id: true,
1212
+ tags: true
1213
+ },
1214
+ where,
1215
+ order: {
1216
+ id: "ASC"
1217
+ }
1218
+ }), tag);
1219
+ } finally {
1220
+ await runner.release();
1221
+ }
1222
+ }
1194
1223
  async createRun(run) {
1195
1224
  await this.runs().createQueryBuilder().insert().values(toRunEntity(run)).updateEntity(false).execute();
1196
1225
  await this.reindexAttributes(run.id, run.searchAttributes);
@@ -1271,21 +1300,22 @@ var TypeOrmStateStore = class {
1271
1300
  if (policy.maxAge == null && policy.maxCount == null) return 0;
1272
1301
  const scoped = /* @__PURE__ */ __name(() => this.runQueryBuilder({
1273
1302
  ...policy.scope ?? {},
1274
- statuses
1275
- }).select("r.id", "id"), "scoped");
1303
+ statuses,
1304
+ excludeWorkflows: RETENTION_EXCLUDED
1305
+ }).select("r.id", "id").addSelect("r.tags", "tags"), "scoped");
1276
1306
  const ids = /* @__PURE__ */ new Set();
1277
1307
  if (policy.maxAge != null) {
1278
1308
  const cutoff = new Date(nowMs - (0, import_nestjs_durable_core.parseDuration)(policy.maxAge));
1279
1309
  const rows = await scoped().andWhere("r.updatedAt < :cutoff", {
1280
1310
  cutoff
1281
1311
  }).orderBy("r.updatedAt", "ASC").limit(limit).getRawMany();
1282
- for (const r of rows) ids.add(r.id);
1312
+ for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
1283
1313
  }
1284
1314
  if (policy.maxCount != null && ids.size < limit) {
1285
1315
  const rows = await scoped().orderBy("r.updatedAt", "DESC").addOrderBy("r.id", "DESC").limit(limit).offset(policy.maxCount).getRawMany();
1286
1316
  for (const r of rows) {
1287
1317
  if (ids.size >= limit) break;
1288
- ids.add(r.id);
1318
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
1289
1319
  }
1290
1320
  }
1291
1321
  const doomed = [
@@ -1603,6 +1633,14 @@ var TypeOrmStateStore = class {
1603
1633
  if (query.workflows) qb.andWhere(query.workflows.length ? "r.workflow IN (:...workflows)" : "1 = 0", query.workflows.length ? {
1604
1634
  workflows: query.workflows
1605
1635
  } : {});
1636
+ if (query.excludeWorkflows?.length) qb.andWhere("r.workflow NOT IN (:...excludeWorkflows)", {
1637
+ excludeWorkflows: query.excludeWorkflows
1638
+ });
1639
+ if (query.updatedAfter !== void 0) {
1640
+ qb.andWhere("r.updatedAt >= :updatedAfter", {
1641
+ updatedAfter: new Date(query.updatedAfter)
1642
+ });
1643
+ }
1606
1644
  if (query.namespace !== void 0) qb.andWhere("r.namespace = :namespace", {
1607
1645
  namespace: query.namespace
1608
1646
  });