@hotmeshio/hotmesh 0.28.1 → 0.29.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/build/package.json +2 -1
- package/build/services/activities/hook.d.ts +7 -4
- package/build/services/activities/hook.js +59 -17
- package/build/services/durable/schemas/factory.d.ts +1 -1
- package/build/services/durable/schemas/factory.js +7 -7
- package/build/services/durable/workflow/condition.js +19 -10
- package/build/services/escalations/accumulate.d.ts +30 -0
- package/build/services/escalations/accumulate.js +86 -0
- package/build/services/escalations/batch.d.ts +9 -4
- package/build/services/escalations/batch.js +21 -5
- package/build/services/escalations/client.d.ts +63 -1
- package/build/services/escalations/client.js +227 -22
- package/build/services/escalations/fold.d.ts +16 -0
- package/build/services/escalations/fold.js +28 -0
- package/build/services/escalations/index.d.ts +3 -1
- package/build/services/escalations/index.js +14 -1
- package/build/services/store/providers/postgres/escalation-accumulate.d.ts +96 -0
- package/build/services/store/providers/postgres/escalation-accumulate.js +309 -0
- package/build/services/store/providers/postgres/postgres.d.ts +64 -7
- package/build/services/store/providers/postgres/postgres.js +231 -24
- package/build/types/hmsh_escalations.d.ts +225 -0
- package/build/types/hmsh_escalations.js +27 -1
- package/build/types/index.d.ts +1 -1
- package/build/types/index.js +9 -1
- package/build/types/system_events.d.ts +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildRemoveStatement = exports.buildAccumulateStatement = exports.resolveWakeMessageSql = exports.timeoutPayloadSql = exports.resolvePayloadSql = exports.accumulatedCollectionSql = exports.isAccumulatorSql = exports.SqlParams = void 0;
|
|
4
|
+
const hmsh_escalations_1 = require("../../../../types/hmsh_escalations");
|
|
5
|
+
/**
|
|
6
|
+
* SQL builders for the accumulator store operations. Every builder is a
|
|
7
|
+
* pure function of parameter positions; the provider owns the connection
|
|
8
|
+
* and the result mapping. Two rows (container and reciprocal) are locked
|
|
9
|
+
* in one ordered CTE and written by one UPDATE gated on both being
|
|
10
|
+
* eligible, so a reciprocal add is both-or-neither by construction.
|
|
11
|
+
*/
|
|
12
|
+
/** Collects statement parameters and hands back their `$n` placeholders. */
|
|
13
|
+
class SqlParams {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.values = [];
|
|
16
|
+
}
|
|
17
|
+
add(value) {
|
|
18
|
+
this.values.push(value);
|
|
19
|
+
return `$${this.values.length}`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SqlParams = SqlParams;
|
|
23
|
+
const ITEMS = hmsh_escalations_1.ESCALATION_ACCUMULATE_ITEMS_KEY;
|
|
24
|
+
const CONFIG = hmsh_escalations_1.ESCALATION_ACCUMULATE_CONFIG_KEY;
|
|
25
|
+
const KEYS = hmsh_escalations_1.ESCALATION_ACCUMULATE_KEYS_KEY;
|
|
26
|
+
const COUNT = hmsh_escalations_1.ESCALATION_ACCUMULATE_COUNT_KEY;
|
|
27
|
+
const MAX = hmsh_escalations_1.ESCALATION_ACCUMULATE_MAX_KEY;
|
|
28
|
+
/** True when the row's envelope carries an accumulator item store. */
|
|
29
|
+
function isAccumulatorSql(envelopeExpr) {
|
|
30
|
+
return `COALESCE(jsonb_typeof(${envelopeExpr}->'${ITEMS}') = 'object', false)`;
|
|
31
|
+
}
|
|
32
|
+
exports.isAccumulatorSql = isAccumulatorSql;
|
|
33
|
+
/**
|
|
34
|
+
* The delivered collection for an item store expression: `$accumulated`
|
|
35
|
+
* ordered by each entry's database-clock `at`, plus the `$trigger` that
|
|
36
|
+
* ended the wait.
|
|
37
|
+
*/
|
|
38
|
+
function accumulatedCollectionSql(itemsExpr, trigger) {
|
|
39
|
+
return `jsonb_build_object(
|
|
40
|
+
'${hmsh_escalations_1.ESCALATION_ACCUMULATED_KEY}',
|
|
41
|
+
(SELECT COALESCE(jsonb_agg(jsonb_build_object('itemKey', kv.key) || kv.value
|
|
42
|
+
ORDER BY (kv.value->>'at')::timestamptz, kv.key), '[]'::jsonb)
|
|
43
|
+
FROM jsonb_each(${itemsExpr}) AS kv),
|
|
44
|
+
'${hmsh_escalations_1.ESCALATION_TRIGGER_KEY}', '${trigger}')`;
|
|
45
|
+
}
|
|
46
|
+
exports.accumulatedCollectionSql = accumulatedCollectionSql;
|
|
47
|
+
/**
|
|
48
|
+
* The stored `resolver_payload` for a manual resolve: the collection merged
|
|
49
|
+
* with the resolver's payload on an accumulator row, the payload alone on
|
|
50
|
+
* any other row.
|
|
51
|
+
*/
|
|
52
|
+
function resolvePayloadSql(rowAlias, payloadParam) {
|
|
53
|
+
return `CASE WHEN ${isAccumulatorSql(`${rowAlias}.envelope`)}
|
|
54
|
+
THEN ${accumulatedCollectionSql(`${rowAlias}.envelope->'${ITEMS}'`, 'resolve')}
|
|
55
|
+
|| COALESCE(${payloadParam}::jsonb, '{}'::jsonb)
|
|
56
|
+
ELSE ${payloadParam}::jsonb END`;
|
|
57
|
+
}
|
|
58
|
+
exports.resolvePayloadSql = resolvePayloadSql;
|
|
59
|
+
/**
|
|
60
|
+
* The `resolver_payload` written by the timeout path: an accumulator
|
|
61
|
+
* delivers its collection, a batch row with `partialOnTimeout` delivers the
|
|
62
|
+
* items filled so far, every other row keeps its column untouched.
|
|
63
|
+
*/
|
|
64
|
+
function timeoutPayloadSql(rowAlias) {
|
|
65
|
+
const env = `${rowAlias}.envelope`;
|
|
66
|
+
return `CASE
|
|
67
|
+
WHEN ${isAccumulatorSql(env)}
|
|
68
|
+
THEN ${accumulatedCollectionSql(`${env}->'${ITEMS}'`, 'timeout')}
|
|
69
|
+
WHEN jsonb_typeof(${env}->'${hmsh_escalations_1.ESCALATION_BATCH_ITEMS_KEY}') = 'object'
|
|
70
|
+
AND COALESCE((${env}->>'${hmsh_escalations_1.ESCALATION_BATCH_PARTIAL_ON_TIMEOUT_KEY}')::boolean, false)
|
|
71
|
+
THEN (${env}->'${hmsh_escalations_1.ESCALATION_BATCH_ITEMS_KEY}')
|
|
72
|
+
|| jsonb_build_object('${hmsh_escalations_1.ESCALATION_TRIGGER_KEY}', 'timeout')
|
|
73
|
+
ELSE ${rowAlias}.resolver_payload END`;
|
|
74
|
+
}
|
|
75
|
+
exports.timeoutPayloadSql = timeoutPayloadSql;
|
|
76
|
+
/**
|
|
77
|
+
* The wake message for a resolve statement: on an accumulator row the
|
|
78
|
+
* `{data,data}` slot is rewritten from the committed `resolver_payload`
|
|
79
|
+
* (plus `$resolution`), so the waiter receives exactly what the row stores.
|
|
80
|
+
*/
|
|
81
|
+
function resolveWakeMessageSql(fromCTE, messageParam, resolutionParam) {
|
|
82
|
+
return `CASE WHEN ${isAccumulatorSql(`${fromCTE}.envelope`)}
|
|
83
|
+
THEN jsonb_set(${messageParam}::jsonb, '{data,data}',
|
|
84
|
+
${fromCTE}.resolver_payload::jsonb || COALESCE(${resolutionParam}::jsonb, '{}'::jsonb))::text
|
|
85
|
+
ELSE ${messageParam}::text END`;
|
|
86
|
+
}
|
|
87
|
+
exports.resolveWakeMessageSql = resolveWakeMessageSql;
|
|
88
|
+
function claimPredicateSql(alias, claimParam) {
|
|
89
|
+
return `(${claimParam}::text IS NULL
|
|
90
|
+
OR ${alias}.assigned_to IS NULL
|
|
91
|
+
OR ${alias}.assigned_until IS NULL
|
|
92
|
+
OR (${alias}.assigned_to = ${claimParam}::text AND ${alias}.assigned_until > NOW())
|
|
93
|
+
OR (${alias}.assigned_to <> ${claimParam}::text AND ${alias}.assigned_until <= NOW()))`;
|
|
94
|
+
}
|
|
95
|
+
function pickCte(name, selector, namespace, p) {
|
|
96
|
+
if (!selector)
|
|
97
|
+
return `${name} AS MATERIALIZED (SELECT NULL::uuid AS id WHERE FALSE)`;
|
|
98
|
+
const ns = namespace ? ` AND e.namespace = ${p.add(namespace)}` : '';
|
|
99
|
+
const from = `FROM public.hmsh_escalations e`;
|
|
100
|
+
switch (selector.kind) {
|
|
101
|
+
case 'id':
|
|
102
|
+
return `${name} AS MATERIALIZED (SELECT e.id ${from} WHERE e.id = ${p.add(selector.id)}${ns} LIMIT 1)`;
|
|
103
|
+
case 'signalKey':
|
|
104
|
+
return `${name} AS MATERIALIZED (SELECT e.id ${from} WHERE e.signal_key = ${p.add(selector.signalKey)}${ns} LIMIT 1)`;
|
|
105
|
+
case 'metadata': {
|
|
106
|
+
const filter = p.add(JSON.stringify({ [selector.key]: selector.value }));
|
|
107
|
+
const roles = p.add(selector.roles ?? null);
|
|
108
|
+
return `${name} AS MATERIALIZED (
|
|
109
|
+
SELECT e.id ${from}
|
|
110
|
+
WHERE e.metadata @> ${filter}::jsonb
|
|
111
|
+
AND (${roles}::text[] IS NULL OR e.role = ANY(${roles}::text[]))
|
|
112
|
+
AND e.status IN ('pending', 'cancelled')${ns}
|
|
113
|
+
ORDER BY e.priority ASC, e.created_at ASC
|
|
114
|
+
LIMIT 1)`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The shared head of both item statements: pick the container (and the
|
|
120
|
+
* reciprocal), lock both in id order in ONE CTE, and derive each side's
|
|
121
|
+
* item key, payload, and partner id. The reciprocal's item key is the
|
|
122
|
+
* container's id; each side's `other_id` becomes its entry's
|
|
123
|
+
* `reciprocalId`.
|
|
124
|
+
*/
|
|
125
|
+
function targetCtes(primary, reciprocal, namespace, p, itemKeyParam, payloadParam, reciprocalPayloadParam) {
|
|
126
|
+
const payload = payloadParam ? `${payloadParam}::jsonb` : 'NULL::jsonb';
|
|
127
|
+
const rPayload = reciprocalPayloadParam
|
|
128
|
+
? `${reciprocalPayloadParam}::jsonb`
|
|
129
|
+
: 'NULL::jsonb';
|
|
130
|
+
return `
|
|
131
|
+
${pickCte('primary_pick', primary, namespace, p)},
|
|
132
|
+
${pickCte('reciprocal_pick', reciprocal, namespace, p)},
|
|
133
|
+
target AS MATERIALIZED (
|
|
134
|
+
SELECT e.id, e.signal_key, e.topic, e.status, e.assigned_to, e.assigned_until,
|
|
135
|
+
e.metadata, e.envelope,
|
|
136
|
+
(e.id = (SELECT id FROM primary_pick)) AS is_primary
|
|
137
|
+
FROM public.hmsh_escalations e
|
|
138
|
+
WHERE e.id IN (SELECT id FROM primary_pick UNION SELECT id FROM reciprocal_pick)
|
|
139
|
+
ORDER BY e.id
|
|
140
|
+
FOR UPDATE
|
|
141
|
+
),
|
|
142
|
+
sides AS (
|
|
143
|
+
SELECT t.*,
|
|
144
|
+
CASE WHEN t.is_primary THEN ${itemKeyParam}::text
|
|
145
|
+
ELSE (SELECT p.id::text FROM target p WHERE p.is_primary) END AS item_key,
|
|
146
|
+
CASE WHEN t.is_primary THEN ${payload} ELSE ${rPayload} END AS item_payload,
|
|
147
|
+
(SELECT o.id::text FROM target o WHERE o.is_primary <> t.is_primary) AS other_id,
|
|
148
|
+
COALESCE((t.envelope->'${CONFIG}'->>'unique')::boolean, true) AS is_unique,
|
|
149
|
+
COALESCE((t.envelope->'${CONFIG}'->>'resolveAtMax')::boolean, true) AS resolve_at_max,
|
|
150
|
+
(t.metadata->>'${MAX}')::int AS max_items,
|
|
151
|
+
COALESCE(t.envelope->'${ITEMS}', '{}'::jsonb) AS items,
|
|
152
|
+
COALESCE(t.metadata->'${KEYS}', '[]'::jsonb) AS keys
|
|
153
|
+
FROM target t
|
|
154
|
+
)`;
|
|
155
|
+
}
|
|
156
|
+
function wakeCtes(wakes, appId, schemaName, p) {
|
|
157
|
+
if (!wakes.length)
|
|
158
|
+
return { ctes: '', counts: `ARRAY[]::int[] AS wake_counts` };
|
|
159
|
+
const app = p.add(appId);
|
|
160
|
+
const names = [];
|
|
161
|
+
const ctes = wakes
|
|
162
|
+
.map((wake, i) => {
|
|
163
|
+
const name = `wake_${i}`;
|
|
164
|
+
names.push(name);
|
|
165
|
+
const msg = p.add(wake.command.message);
|
|
166
|
+
const sk = p.add(wake.command.forSignalKey);
|
|
167
|
+
const res = p.add(wake.resolutionJson);
|
|
168
|
+
return `,
|
|
169
|
+
${name} AS (
|
|
170
|
+
INSERT INTO ${schemaName}.engine_streams (stream_name, message, priority)
|
|
171
|
+
SELECT ${app},
|
|
172
|
+
jsonb_set(${msg}::jsonb, '{data,data}',
|
|
173
|
+
w.resolver_payload::jsonb || COALESCE(${res}::jsonb, '{}'::jsonb))::text,
|
|
174
|
+
5
|
|
175
|
+
FROM written w
|
|
176
|
+
WHERE w.status = 'resolved' AND w.signal_key = ${sk}
|
|
177
|
+
RETURNING id
|
|
178
|
+
)`;
|
|
179
|
+
})
|
|
180
|
+
.join('');
|
|
181
|
+
const counts = `ARRAY[${names.map((n) => `(SELECT COUNT(*) FROM ${n})::int`).join(', ')}] AS wake_counts`;
|
|
182
|
+
return { ctes, counts };
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* One statement: guarded append of one item on the container (and the
|
|
186
|
+
* container's id on the reciprocal), facet recompute, resolve-at-max with
|
|
187
|
+
* the ordered collection as `resolver_payload`, per-side wake enqueue, and
|
|
188
|
+
* per-side outcome classification. Both writes or neither.
|
|
189
|
+
*/
|
|
190
|
+
function buildAccumulateStatement(input) {
|
|
191
|
+
const p = new SqlParams();
|
|
192
|
+
const itemKey = p.add(input.itemKey);
|
|
193
|
+
const payload = p.add(input.payload === null ? null : JSON.stringify(input.payload));
|
|
194
|
+
const rPayload = p.add(input.reciprocalPayload === null
|
|
195
|
+
? null
|
|
196
|
+
: JSON.stringify(input.reciprocalPayload));
|
|
197
|
+
const meta = p.add(input.metadata ? JSON.stringify(input.metadata) : null);
|
|
198
|
+
const actor = p.add(input.actor);
|
|
199
|
+
const claim = p.add(input.assertClaim);
|
|
200
|
+
const expected = p.add(input.reciprocal ? 2 : 1);
|
|
201
|
+
const head = targetCtes(input.primary, input.reciprocal, input.namespace, p, itemKey, payload, rPayload);
|
|
202
|
+
const { ctes: wakeSql, counts: wakeCounts } = wakeCtes(input.wakes, input.appId, input.schemaName, p);
|
|
203
|
+
const newKeys = `CASE WHEN s.keys ? s.item_key THEN s.keys ELSE s.keys || to_jsonb(s.item_key) END`;
|
|
204
|
+
const newCount = `jsonb_array_length(${newKeys})`;
|
|
205
|
+
const entry = `jsonb_build_object('at', to_jsonb(NOW()))
|
|
206
|
+
|| CASE WHEN s.item_payload IS NULL THEN '{}'::jsonb ELSE jsonb_build_object('payload', s.item_payload) END
|
|
207
|
+
|| CASE WHEN ${actor}::text IS NULL THEN '{}'::jsonb ELSE jsonb_build_object('actor', ${actor}::text) END
|
|
208
|
+
|| CASE WHEN s.other_id IS NULL THEN '{}'::jsonb ELSE jsonb_build_object('reciprocalId', s.other_id) END`;
|
|
209
|
+
const newItems = `s.items || jsonb_build_object(s.item_key, ${entry})`;
|
|
210
|
+
const complete = `(s.max_items IS NOT NULL AND s.resolve_at_max AND ${newCount} >= s.max_items)`;
|
|
211
|
+
const isFull = `(s.max_items IS NOT NULL AND NOT (s.items ? s.item_key) AND jsonb_array_length(s.keys) >= s.max_items)`;
|
|
212
|
+
const isDuplicate = `(s.is_unique AND s.items ? s.item_key)`;
|
|
213
|
+
const sql = `
|
|
214
|
+
WITH ${head},
|
|
215
|
+
eligible AS (
|
|
216
|
+
SELECT s.id FROM sides s
|
|
217
|
+
WHERE s.status = 'pending'
|
|
218
|
+
AND ${isAccumulatorSql('s.envelope')}
|
|
219
|
+
AND s.item_key IS NOT NULL
|
|
220
|
+
AND (NOT s.is_primary OR ${claimPredicateSql('s', claim)})
|
|
221
|
+
AND NOT ${isDuplicate}
|
|
222
|
+
AND NOT ${isFull}
|
|
223
|
+
),
|
|
224
|
+
written AS (
|
|
225
|
+
UPDATE public.hmsh_escalations e
|
|
226
|
+
SET envelope = COALESCE(e.envelope, '{}'::jsonb)
|
|
227
|
+
|| jsonb_build_object('${ITEMS}', ${newItems}),
|
|
228
|
+
metadata = COALESCE(e.metadata, '{}'::jsonb)
|
|
229
|
+
|| CASE WHEN s.is_primary THEN COALESCE(${meta}::jsonb, '{}'::jsonb) ELSE '{}'::jsonb END
|
|
230
|
+
|| jsonb_build_object('${KEYS}', ${newKeys}, '${COUNT}', ${newCount}),
|
|
231
|
+
status = CASE WHEN ${complete} THEN 'resolved' ELSE e.status END,
|
|
232
|
+
resolved_at = CASE WHEN ${complete} THEN NOW() ELSE e.resolved_at END,
|
|
233
|
+
resolver_payload = CASE WHEN ${complete}
|
|
234
|
+
THEN ${accumulatedCollectionSql(newItems, 'count')}
|
|
235
|
+
ELSE e.resolver_payload END,
|
|
236
|
+
updated_at = NOW()
|
|
237
|
+
FROM sides s
|
|
238
|
+
WHERE e.id = s.id
|
|
239
|
+
AND (SELECT COUNT(*) FROM eligible) = ${expected}::int
|
|
240
|
+
AND (SELECT COUNT(*) FROM target) = ${expected}::int
|
|
241
|
+
RETURNING e.*
|
|
242
|
+
)${wakeSql}
|
|
243
|
+
SELECT s.id, s.is_primary, s.status AS prior_status, s.signal_key, s.topic, s.assigned_to,
|
|
244
|
+
CASE
|
|
245
|
+
WHEN w.id IS NOT NULL AND w.status = 'resolved' THEN 'completed'
|
|
246
|
+
WHEN w.id IS NOT NULL THEN 'accepted'
|
|
247
|
+
WHEN s.status <> 'pending' THEN 'blocked'
|
|
248
|
+
WHEN NOT ${isAccumulatorSql('s.envelope')} THEN 'not-accumulator'
|
|
249
|
+
WHEN s.is_primary AND NOT ${claimPredicateSql('s', claim)} THEN 'claim-blocked'
|
|
250
|
+
WHEN ${isDuplicate} THEN 'duplicate-item'
|
|
251
|
+
WHEN ${isFull} THEN 'full'
|
|
252
|
+
ELSE 'gated'
|
|
253
|
+
END AS outcome,
|
|
254
|
+
row_to_json(w.*) AS entry_json,
|
|
255
|
+
${wakeCounts}
|
|
256
|
+
FROM sides s
|
|
257
|
+
LEFT JOIN written w ON w.id = s.id
|
|
258
|
+
ORDER BY s.is_primary DESC`;
|
|
259
|
+
return { sql, values: p.values };
|
|
260
|
+
}
|
|
261
|
+
exports.buildAccumulateStatement = buildAccumulateStatement;
|
|
262
|
+
/**
|
|
263
|
+
* One statement: guarded removal of one held item from the container (and
|
|
264
|
+
* of the container's id from the reciprocal), facet recompute, no wake and
|
|
265
|
+
* no status change. Both writes or neither.
|
|
266
|
+
*/
|
|
267
|
+
function buildRemoveStatement(input) {
|
|
268
|
+
const p = new SqlParams();
|
|
269
|
+
const itemKey = p.add(input.itemKey);
|
|
270
|
+
const expected = p.add(input.reciprocal ? 2 : 1);
|
|
271
|
+
const head = targetCtes(input.primary, input.reciprocal, input.namespace, p, itemKey, null, null);
|
|
272
|
+
const newKeys = `s.keys - s.item_key`;
|
|
273
|
+
const sql = `
|
|
274
|
+
WITH ${head},
|
|
275
|
+
eligible AS (
|
|
276
|
+
SELECT s.id FROM sides s
|
|
277
|
+
WHERE s.status = 'pending'
|
|
278
|
+
AND ${isAccumulatorSql('s.envelope')}
|
|
279
|
+
AND s.item_key IS NOT NULL
|
|
280
|
+
AND s.items ? s.item_key
|
|
281
|
+
),
|
|
282
|
+
written AS (
|
|
283
|
+
UPDATE public.hmsh_escalations e
|
|
284
|
+
SET envelope = COALESCE(e.envelope, '{}'::jsonb)
|
|
285
|
+
|| jsonb_build_object('${ITEMS}', s.items - s.item_key),
|
|
286
|
+
metadata = COALESCE(e.metadata, '{}'::jsonb)
|
|
287
|
+
|| jsonb_build_object('${KEYS}', ${newKeys}, '${COUNT}', jsonb_array_length(${newKeys})),
|
|
288
|
+
updated_at = NOW()
|
|
289
|
+
FROM sides s
|
|
290
|
+
WHERE e.id = s.id
|
|
291
|
+
AND (SELECT COUNT(*) FROM eligible) = ${expected}::int
|
|
292
|
+
AND (SELECT COUNT(*) FROM target) = ${expected}::int
|
|
293
|
+
RETURNING e.*
|
|
294
|
+
)
|
|
295
|
+
SELECT s.id, s.is_primary, s.status AS prior_status,
|
|
296
|
+
CASE
|
|
297
|
+
WHEN w.id IS NOT NULL THEN 'removed'
|
|
298
|
+
WHEN s.status <> 'pending' THEN 'blocked'
|
|
299
|
+
WHEN NOT ${isAccumulatorSql('s.envelope')} THEN 'not-accumulator'
|
|
300
|
+
WHEN NOT (s.items ? s.item_key) THEN 'item-absent'
|
|
301
|
+
ELSE 'gated'
|
|
302
|
+
END AS outcome,
|
|
303
|
+
row_to_json(w.*) AS entry_json
|
|
304
|
+
FROM sides s
|
|
305
|
+
LEFT JOIN written w ON w.id = s.id
|
|
306
|
+
ORDER BY s.is_primary DESC`;
|
|
307
|
+
return { sql, values: p.values };
|
|
308
|
+
}
|
|
309
|
+
exports.buildRemoveStatement = buildRemoveStatement;
|
|
@@ -12,6 +12,7 @@ import { JobInterruptOptions } from '../../../../types/job';
|
|
|
12
12
|
import { WorkListTaskType } from '../../../../types/task';
|
|
13
13
|
import { ThrottleOptions } from '../../../../types/quorum';
|
|
14
14
|
import { StoreService } from '../..';
|
|
15
|
+
import { AccumulateWake } from './escalation-accumulate';
|
|
15
16
|
import { PostgresClientType } from '../../../../types';
|
|
16
17
|
import { KVSQL } from './kvsql';
|
|
17
18
|
import { KVTables } from './kvtables';
|
|
@@ -282,10 +283,14 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
282
283
|
* Transition a wait's escalation row to `expired` when its resume timer
|
|
283
284
|
* fires first (`condition(signalId, { ..., timeout })`). Guarded by
|
|
284
285
|
* `status = 'pending'`: a signal that won the race already resolved the
|
|
285
|
-
* row and is never touched.
|
|
286
|
-
*
|
|
287
|
-
|
|
288
|
-
|
|
286
|
+
* row and is never touched. An accumulator row (or a batch row with
|
|
287
|
+
* `partialOnTimeout`) has its `resolver_payload` set to the delivered
|
|
288
|
+
* collection in the same UPDATE, so row truth and the waiter's value
|
|
289
|
+
* agree. Returns the expired row plus the row's prior status, so the
|
|
290
|
+
* caller can tell "a resolve won" (row exists, not pending) from "no
|
|
291
|
+
* row" (the wait carried no escalation).
|
|
292
|
+
*/
|
|
293
|
+
expireEscalationBySignalKey(signalKey: string, namespace?: string, appId?: string): Promise<import('../../../../types/hmsh_escalations').ExpireEscalationResult>;
|
|
289
294
|
private _escalationFilterConditions;
|
|
290
295
|
listEscalations(params?: import('../../../../types/hmsh_escalations').ListEscalationsParams): Promise<import('../../../../types/hmsh_escalations').EscalationEntry[]>;
|
|
291
296
|
countEscalations(params?: import('../../../../types/hmsh_escalations').ListEscalationsParams): Promise<number>;
|
|
@@ -301,7 +306,7 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
301
306
|
* into. Appends the wake params to `params` in place.
|
|
302
307
|
*/
|
|
303
308
|
private composeEscalationWakeCTE;
|
|
304
|
-
resolveEscalation(params: import('../../../../types/hmsh_escalations').ResolveEscalationParams, wakeCommand?: import('../../../../types/hmsh_escalations').EscalationWakeCommand): Promise<import('../../../../types/hmsh_escalations').ResolveEscalationResult & {
|
|
309
|
+
resolveEscalation(params: import('../../../../types/hmsh_escalations').ResolveEscalationParams, wakeCommand?: import('../../../../types/hmsh_escalations').EscalationWakeCommand, resolutionJson?: string | null): Promise<import('../../../../types/hmsh_escalations').ResolveEscalationResult & {
|
|
305
310
|
signalKey?: string | null;
|
|
306
311
|
topic?: string | null;
|
|
307
312
|
wakeEnqueued?: boolean;
|
|
@@ -317,7 +322,7 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
317
322
|
signalKey: string | null;
|
|
318
323
|
topic: string | null;
|
|
319
324
|
} | null>;
|
|
320
|
-
resolveEscalationByMetadata(params: import('../../../../types/hmsh_escalations').ResolveByMetadataParams, wakeCommand?: import('../../../../types/hmsh_escalations').EscalationWakeCommand): Promise<import('../../../../types/hmsh_escalations').ResolveEscalationResult & {
|
|
325
|
+
resolveEscalationByMetadata(params: import('../../../../types/hmsh_escalations').ResolveByMetadataParams, wakeCommand?: import('../../../../types/hmsh_escalations').EscalationWakeCommand, resolutionJson?: string | null): Promise<import('../../../../types/hmsh_escalations').ResolveEscalationResult & {
|
|
321
326
|
signalKey?: string | null;
|
|
322
327
|
topic?: string | null;
|
|
323
328
|
wakeEnqueued?: boolean;
|
|
@@ -368,6 +373,58 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
368
373
|
topic?: string | null;
|
|
369
374
|
wakeEnqueued?: boolean;
|
|
370
375
|
}>;
|
|
376
|
+
private _accumulateSelector;
|
|
377
|
+
private _mapAccumulateRows;
|
|
378
|
+
/**
|
|
379
|
+
* Appends one item to an accumulator escalation, and the container's id
|
|
380
|
+
* to the reciprocal row when one is named, in ONE statement: both rows
|
|
381
|
+
* locked in id order, both guards evaluated before either write, both
|
|
382
|
+
* writes or neither. A side reaching `max` (with `resolveAtMax`) resolves
|
|
383
|
+
* with the ordered collection as `resolver_payload` and its wake commits
|
|
384
|
+
* in the same statement. See `buildAccumulateStatement`.
|
|
385
|
+
*/
|
|
386
|
+
accumulateEscalationItem(params: import('../../../../types/hmsh_escalations').AccumulateItemParams, wakes?: AccumulateWake[]): Promise<import("../../../../types").AccumulateItemResult & {
|
|
387
|
+
wake?: {
|
|
388
|
+
primary: {
|
|
389
|
+
signalKey: string;
|
|
390
|
+
topic: string;
|
|
391
|
+
wakeEnqueued: boolean;
|
|
392
|
+
};
|
|
393
|
+
reciprocal?: {
|
|
394
|
+
signalKey: string;
|
|
395
|
+
topic: string;
|
|
396
|
+
wakeEnqueued: boolean;
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
}>;
|
|
400
|
+
/** By-metadata form of `accumulateEscalationItem`, mirroring the
|
|
401
|
+
* `resolveEscalationByMetadata` selector. No claim assertion. */
|
|
402
|
+
accumulateEscalationItemByMetadata(params: import('../../../../types/hmsh_escalations').AccumulateItemByMetadataParams, wakes?: AccumulateWake[]): Promise<import("../../../../types").AccumulateItemResult & {
|
|
403
|
+
wake?: {
|
|
404
|
+
primary: {
|
|
405
|
+
signalKey: string;
|
|
406
|
+
topic: string;
|
|
407
|
+
wakeEnqueued: boolean;
|
|
408
|
+
};
|
|
409
|
+
reciprocal?: {
|
|
410
|
+
signalKey: string;
|
|
411
|
+
topic: string;
|
|
412
|
+
wakeEnqueued: boolean;
|
|
413
|
+
};
|
|
414
|
+
};
|
|
415
|
+
}>;
|
|
416
|
+
private _runAccumulate;
|
|
417
|
+
private _mapRemoveRows;
|
|
418
|
+
/**
|
|
419
|
+
* Removes one held item from an accumulator escalation (and the
|
|
420
|
+
* container's id from the reciprocal row when named) in ONE guarded
|
|
421
|
+
* statement. Never wakes the waiter and never changes status. See
|
|
422
|
+
* `buildRemoveStatement`.
|
|
423
|
+
*/
|
|
424
|
+
removeAccumulatedEscalationItem(params: import('../../../../types/hmsh_escalations').RemoveAccumulatedItemParams): Promise<import('../../../../types/hmsh_escalations').RemoveAccumulatedItemResult>;
|
|
425
|
+
/** By-metadata form of `removeAccumulatedEscalationItem`. */
|
|
426
|
+
removeAccumulatedEscalationItemByMetadata(params: import('../../../../types/hmsh_escalations').RemoveAccumulatedItemByMetadataParams): Promise<import('../../../../types/hmsh_escalations').RemoveAccumulatedItemResult>;
|
|
427
|
+
private _runRemove;
|
|
371
428
|
cancelEscalation(id: string, namespace?: string, wakeCommand?: import('../../../../types/hmsh_escalations').EscalationWakeCommand): Promise<import('../../../../types/hmsh_escalations').CancelEscalationResult & {
|
|
372
429
|
wakeEnqueued?: boolean;
|
|
373
430
|
}>;
|
|
@@ -399,7 +456,7 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
399
456
|
* names each blocking row and why. Same durability contract as
|
|
400
457
|
* `resolveEscalation`: each wake commits WITH its row's resolve.
|
|
401
458
|
*/
|
|
402
|
-
resolveAllOrNoneEscalations(params: import('../../../../types/hmsh_escalations').ResolveAllOrNoneParams, wakeCommands?: import('../../../../types/hmsh_escalations').EscalationWakeCommand[]): Promise<{
|
|
459
|
+
resolveAllOrNoneEscalations(params: import('../../../../types/hmsh_escalations').ResolveAllOrNoneParams, wakeCommands?: import('../../../../types/hmsh_escalations').EscalationWakeCommand[], resolutionJsons?: (string | null)[]): Promise<{
|
|
403
460
|
ok: true;
|
|
404
461
|
entries: import('../../../../types/hmsh_escalations').EscalationEntry[];
|
|
405
462
|
wakeCount: number;
|