@hotmeshio/hotmesh 0.33.0 → 0.33.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.33.0",
3
+ "version": "0.33.1",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -59,26 +59,49 @@ async function deleteJobs(client, schema, options, hasEscalations) {
59
59
  const keyset = after ? (0, sql_1.jobsAfter)('$5', '$6') : '';
60
60
  if (after)
61
61
  params.push(after.at, after.id);
62
- // 1. Lock the oldest eligible jobs; rows another session holds are skipped
63
- const { rows } = await client.query(`SELECT j.id::text AS id, ${(0, sql_1.iso)('j.expired_at')} AS at
64
- FROM ${schema}.jobs j
65
- WHERE ${(0, sql_1.jobsEligible)(schema, '$1', '$2', hasEscalations)}
66
- AND j.updated_at < now() - ($4::int * interval '1 day')
67
- ${keyset}
68
- ORDER BY j.expired_at, j.id
69
- LIMIT $3
70
- FOR UPDATE OF j SKIP LOCKED`, params);
62
+ // 1. Take the oldest jobs in scope first, so a batch reads only its window
63
+ // 2. Check protections on that window, then lock the unprotected rows;
64
+ // rows another session holds are skipped
65
+ const { rows } = await client.query(`WITH candidates AS MATERIALIZED (
66
+ SELECT j.id, j.key, j.origin_id, j.parent_id, j.expired_at
67
+ FROM ${schema}.jobs j
68
+ WHERE ${(0, sql_1.jobsInScope)('$1', '$2')}
69
+ AND j.updated_at < now() - ($4::int * interval '1 day')
70
+ ${keyset}
71
+ ORDER BY j.expired_at, j.id
72
+ LIMIT $3
73
+ ),
74
+ checked AS MATERIALIZED (
75
+ SELECT j.id, j.expired_at, ${(0, sql_1.jobProtected)(schema, hasEscalations)} AS protected
76
+ FROM candidates j
77
+ ),
78
+ locked AS MATERIALIZED (
79
+ SELECT j.id FROM ${schema}.jobs j
80
+ WHERE j.id IN (SELECT id FROM checked WHERE NOT protected)
81
+ FOR UPDATE OF j SKIP LOCKED
82
+ )
83
+ SELECT c.id::text AS id, ${(0, sql_1.iso)('c.expired_at')} AS at,
84
+ c.id IN (SELECT id FROM locked) AS locked
85
+ FROM checked c
86
+ ORDER BY c.expired_at, c.id`, params);
71
87
  if (rows.length === 0) {
72
88
  return { deleted: 0, attributesDeleted: 0, watermark: null, visited: 0 };
73
89
  }
74
- const ids = rows.map((row) => row.id);
75
- // 2. Attributes in one statement, then the jobs
76
- const attributes = await client.query(`DELETE FROM ${schema}.jobs_attributes WHERE job_id = ANY ($1::uuid[])`, [ids]);
77
- const jobs = await client.query(`DELETE FROM ${schema}.jobs WHERE id = ANY ($1::uuid[])`, [ids]);
90
+ // 3. Attributes in one statement, then the jobs
91
+ const ids = rows.filter((row) => row.locked).map((row) => row.id);
92
+ let attributesDeleted = 0;
93
+ let deleted = 0;
94
+ if (ids.length > 0) {
95
+ const attributes = await client.query(`DELETE FROM ${schema}.jobs_attributes WHERE job_id = ANY ($1::uuid[])`, [ids]);
96
+ const jobs = await client.query(`DELETE FROM ${schema}.jobs WHERE id = ANY ($1::uuid[])`, [ids]);
97
+ attributesDeleted = (0, session_1.toNumber)(attributes.rowCount);
98
+ deleted = (0, session_1.toNumber)(jobs.rowCount);
99
+ }
100
+ // 4. The watermark passes the whole window, protected rows included
78
101
  const last = rows[rows.length - 1];
79
102
  return {
80
- deleted: (0, session_1.toNumber)(jobs.rowCount),
81
- attributesDeleted: (0, session_1.toNumber)(attributes.rowCount),
103
+ deleted,
104
+ attributesDeleted,
82
105
  watermark: { at: last.at, id: last.id },
83
106
  visited: rows.length,
84
107
  };
@@ -26,6 +26,8 @@ export declare const jobsInScope: (before: string, asOf: string) => string;
26
26
  export declare const pendingEscalation: () => string;
27
27
  /** True when the job's root or parent workflow is still running. */
28
28
  export declare const openOrigin: (schema: string) => string;
29
+ /** True when a protection keeps the job: a pending escalation or an open origin. */
30
+ export declare const jobProtected: (schema: string, hasEscalations: boolean) => string;
29
31
  /** The full eligibility predicate for jobs, protections included. */
30
32
  export declare const jobsEligible: (schema: string, before: string, asOf: string, hasEscalations: boolean) => string;
31
33
  /**
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tablesFor = exports.discoverAppsSQL = exports.backlogSQL = exports.longTransactionsSQL = exports.replicationSlotsSQL = exports.tableStatsSQL = exports.escalationsAfter = exports.escalationsEligible = exports.openReference = exports.escalationsInScope = exports.streamsAfter = exports.jobsAfter = exports.streamsEligible = exports.jobsEligible = exports.openOrigin = exports.pendingEscalation = exports.jobsInScope = exports.iso = exports.HISTOGRAM_BUCKETS = exports.ENDED_AT = exports.ESCALATIONS_TABLE = exports.SCHEMA_TARGETS = exports.RETENTION_TARGETS = void 0;
3
+ exports.tablesFor = exports.discoverAppsSQL = exports.backlogSQL = exports.longTransactionsSQL = exports.replicationSlotsSQL = exports.tableStatsSQL = exports.escalationsAfter = exports.escalationsEligible = exports.openReference = exports.escalationsInScope = exports.streamsAfter = exports.jobsAfter = exports.streamsEligible = exports.jobsEligible = exports.jobProtected = exports.openOrigin = exports.pendingEscalation = exports.jobsInScope = exports.iso = exports.HISTOGRAM_BUCKETS = exports.ENDED_AT = exports.ESCALATIONS_TABLE = exports.SCHEMA_TARGETS = exports.RETENTION_TARGETS = void 0;
4
4
  /**
5
5
  * SQL for retention. Identifiers come from `DBA.safeName` (lowercase
6
6
  * letters, digits, underscores) so interpolating them is safe; every
@@ -61,6 +61,11 @@ const openOrigin = (schema) => `
61
61
  AND p.id <> j.id
62
62
  )`;
63
63
  exports.openOrigin = openOrigin;
64
+ /** True when a protection keeps the job: a pending escalation or an open origin. */
65
+ const jobProtected = (schema, hasEscalations) => hasEscalations
66
+ ? `(${(0, exports.pendingEscalation)()} OR ${(0, exports.openOrigin)(schema)})`
67
+ : `(${(0, exports.openOrigin)(schema)})`;
68
+ exports.jobProtected = jobProtected;
64
69
  /** The full eligibility predicate for jobs, protections included. */
65
70
  const jobsEligible = (schema, before, asOf, hasEscalations) => `
66
71
  ${(0, exports.jobsInScope)(before, asOf)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.33.0",
3
+ "version": "0.33.1",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",