@nicnocquee/dataqueue 1.25.0 → 1.26.0-beta.20260223195940
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/ai/build-docs-content.ts +96 -0
- package/ai/build-llms-full.ts +42 -0
- package/ai/docs-content.json +278 -0
- package/ai/rules/advanced.md +132 -0
- package/ai/rules/basic.md +159 -0
- package/ai/rules/react-dashboard.md +83 -0
- package/ai/skills/dataqueue-advanced/SKILL.md +320 -0
- package/ai/skills/dataqueue-core/SKILL.md +234 -0
- package/ai/skills/dataqueue-react/SKILL.md +189 -0
- package/dist/cli.cjs +1149 -14
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +66 -1
- package/dist/cli.d.ts +66 -1
- package/dist/cli.js +1146 -13
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +3157 -1237
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +613 -23
- package/dist/index.d.ts +613 -23
- package/dist/index.js +3156 -1238
- package/dist/index.js.map +1 -1
- package/dist/mcp-server.cjs +186 -0
- package/dist/mcp-server.cjs.map +1 -0
- package/dist/mcp-server.d.cts +32 -0
- package/dist/mcp-server.d.ts +32 -0
- package/dist/mcp-server.js +175 -0
- package/dist/mcp-server.js.map +1 -0
- package/migrations/1781200000004_create_cron_schedules_table.sql +33 -0
- package/migrations/1781200000005_add_retry_config_to_job_queue.sql +17 -0
- package/package.json +24 -21
- package/src/backend.ts +170 -5
- package/src/backends/postgres.ts +992 -63
- package/src/backends/redis-scripts.ts +358 -26
- package/src/backends/redis.test.ts +1363 -0
- package/src/backends/redis.ts +993 -35
- package/src/cli.test.ts +82 -6
- package/src/cli.ts +73 -10
- package/src/cron.test.ts +126 -0
- package/src/cron.ts +40 -0
- package/src/db-util.ts +1 -1
- package/src/index.test.ts +682 -0
- package/src/index.ts +209 -34
- package/src/init-command.test.ts +449 -0
- package/src/init-command.ts +709 -0
- package/src/install-mcp-command.test.ts +216 -0
- package/src/install-mcp-command.ts +185 -0
- package/src/install-rules-command.test.ts +218 -0
- package/src/install-rules-command.ts +233 -0
- package/src/install-skills-command.test.ts +176 -0
- package/src/install-skills-command.ts +124 -0
- package/src/mcp-server.test.ts +162 -0
- package/src/mcp-server.ts +231 -0
- package/src/processor.ts +36 -97
- package/src/queue.test.ts +465 -0
- package/src/queue.ts +34 -252
- package/src/supervisor.test.ts +340 -0
- package/src/supervisor.ts +162 -0
- package/src/types.ts +388 -12
- package/LICENSE +0 -21
package/dist/index.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
-
import { randomUUID } from 'crypto';
|
|
3
1
|
import { Worker } from 'worker_threads';
|
|
2
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
3
|
import { Pool } from 'pg';
|
|
5
4
|
import { parse } from 'pg-connection-string';
|
|
6
5
|
import fs from 'fs';
|
|
6
|
+
import { randomUUID } from 'crypto';
|
|
7
7
|
import { createRequire } from 'module';
|
|
8
|
+
import { Cron } from 'croner';
|
|
9
|
+
|
|
10
|
+
// src/processor.ts
|
|
8
11
|
|
|
9
12
|
// src/types.ts
|
|
10
13
|
var JobEventType = /* @__PURE__ */ ((JobEventType2) => {
|
|
@@ -19,11 +22,11 @@ var JobEventType = /* @__PURE__ */ ((JobEventType2) => {
|
|
|
19
22
|
JobEventType2["Waiting"] = "waiting";
|
|
20
23
|
return JobEventType2;
|
|
21
24
|
})(JobEventType || {});
|
|
22
|
-
var FailureReason = /* @__PURE__ */ ((
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
25
|
+
var FailureReason = /* @__PURE__ */ ((FailureReason4) => {
|
|
26
|
+
FailureReason4["Timeout"] = "timeout";
|
|
27
|
+
FailureReason4["HandlerError"] = "handler_error";
|
|
28
|
+
FailureReason4["NoHandler"] = "no_handler";
|
|
29
|
+
return FailureReason4;
|
|
27
30
|
})(FailureReason || {});
|
|
28
31
|
var WaitSignal = class extends Error {
|
|
29
32
|
constructor(type, waitUntil, tokenId, stepData) {
|
|
@@ -50,265 +53,1274 @@ var log = (message) => {
|
|
|
50
53
|
}
|
|
51
54
|
};
|
|
52
55
|
|
|
53
|
-
// src/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return this.pool;
|
|
61
|
-
}
|
|
62
|
-
// ── Events ──────────────────────────────────────────────────────────
|
|
63
|
-
async recordJobEvent(jobId, eventType, metadata) {
|
|
64
|
-
const client = await this.pool.connect();
|
|
65
|
-
try {
|
|
66
|
-
await client.query(
|
|
67
|
-
`INSERT INTO job_events (job_id, event_type, metadata) VALUES ($1, $2, $3)`,
|
|
68
|
-
[jobId, eventType, metadata ? JSON.stringify(metadata) : null]
|
|
69
|
-
);
|
|
70
|
-
} catch (error) {
|
|
71
|
-
log(`Error recording job event for job ${jobId}: ${error}`);
|
|
72
|
-
} finally {
|
|
73
|
-
client.release();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async getJobEvents(jobId) {
|
|
77
|
-
const client = await this.pool.connect();
|
|
78
|
-
try {
|
|
79
|
-
const res = await client.query(
|
|
80
|
-
`SELECT id, job_id AS "jobId", event_type AS "eventType", metadata, created_at AS "createdAt" FROM job_events WHERE job_id = $1 ORDER BY created_at ASC`,
|
|
81
|
-
[jobId]
|
|
82
|
-
);
|
|
83
|
-
return res.rows;
|
|
84
|
-
} finally {
|
|
85
|
-
client.release();
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// ── Job CRUD ──────────────────────────────────────────────────────────
|
|
89
|
-
async addJob({
|
|
90
|
-
jobType,
|
|
91
|
-
payload,
|
|
92
|
-
maxAttempts = 3,
|
|
93
|
-
priority = 0,
|
|
94
|
-
runAt = null,
|
|
95
|
-
timeoutMs = void 0,
|
|
96
|
-
forceKillOnTimeout = false,
|
|
97
|
-
tags = void 0,
|
|
98
|
-
idempotencyKey = void 0
|
|
99
|
-
}) {
|
|
100
|
-
const client = await this.pool.connect();
|
|
101
|
-
try {
|
|
102
|
-
let result;
|
|
103
|
-
const onConflict = idempotencyKey ? `ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING` : "";
|
|
104
|
-
if (runAt) {
|
|
105
|
-
result = await client.query(
|
|
106
|
-
`INSERT INTO job_queue
|
|
107
|
-
(job_type, payload, max_attempts, priority, run_at, timeout_ms, force_kill_on_timeout, tags, idempotency_key)
|
|
108
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
109
|
-
${onConflict}
|
|
110
|
-
RETURNING id`,
|
|
111
|
-
[
|
|
112
|
-
jobType,
|
|
113
|
-
payload,
|
|
114
|
-
maxAttempts,
|
|
115
|
-
priority,
|
|
116
|
-
runAt,
|
|
117
|
-
timeoutMs ?? null,
|
|
118
|
-
forceKillOnTimeout ?? false,
|
|
119
|
-
tags ?? null,
|
|
120
|
-
idempotencyKey ?? null
|
|
121
|
-
]
|
|
122
|
-
);
|
|
123
|
-
} else {
|
|
124
|
-
result = await client.query(
|
|
125
|
-
`INSERT INTO job_queue
|
|
126
|
-
(job_type, payload, max_attempts, priority, timeout_ms, force_kill_on_timeout, tags, idempotency_key)
|
|
127
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
128
|
-
${onConflict}
|
|
129
|
-
RETURNING id`,
|
|
130
|
-
[
|
|
131
|
-
jobType,
|
|
132
|
-
payload,
|
|
133
|
-
maxAttempts,
|
|
134
|
-
priority,
|
|
135
|
-
timeoutMs ?? null,
|
|
136
|
-
forceKillOnTimeout ?? false,
|
|
137
|
-
tags ?? null,
|
|
138
|
-
idempotencyKey ?? null
|
|
139
|
-
]
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
if (result.rows.length === 0 && idempotencyKey) {
|
|
143
|
-
const existing = await client.query(
|
|
144
|
-
`SELECT id FROM job_queue WHERE idempotency_key = $1`,
|
|
145
|
-
[idempotencyKey]
|
|
146
|
-
);
|
|
147
|
-
if (existing.rows.length > 0) {
|
|
148
|
-
log(
|
|
149
|
-
`Job with idempotency key "${idempotencyKey}" already exists (id: ${existing.rows[0].id}), returning existing job`
|
|
150
|
-
);
|
|
151
|
-
return existing.rows[0].id;
|
|
152
|
-
}
|
|
153
|
-
throw new Error(
|
|
154
|
-
`Failed to insert job and could not find existing job with idempotency key "${idempotencyKey}"`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
const jobId = result.rows[0].id;
|
|
158
|
-
log(
|
|
159
|
-
`Added job ${jobId}: payload ${JSON.stringify(payload)}, ${runAt ? `runAt ${runAt.toISOString()}, ` : ""}priority ${priority}, maxAttempts ${maxAttempts}, jobType ${jobType}, tags ${JSON.stringify(tags)}${idempotencyKey ? `, idempotencyKey "${idempotencyKey}"` : ""}`
|
|
56
|
+
// src/processor.ts
|
|
57
|
+
function validateHandlerSerializable(handler, jobType) {
|
|
58
|
+
try {
|
|
59
|
+
const handlerString = handler.toString();
|
|
60
|
+
if (handlerString.includes("this.") && !handlerString.match(/\([^)]*this[^)]*\)/)) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Handler for job type "${jobType}" uses 'this' context which cannot be serialized. Use a regular function or avoid 'this' references when forceKillOnTimeout is enabled.`
|
|
160
63
|
);
|
|
161
|
-
await this.recordJobEvent(jobId, "added" /* Added */, {
|
|
162
|
-
jobType,
|
|
163
|
-
payload,
|
|
164
|
-
tags,
|
|
165
|
-
idempotencyKey
|
|
166
|
-
});
|
|
167
|
-
return jobId;
|
|
168
|
-
} catch (error) {
|
|
169
|
-
log(`Error adding job: ${error}`);
|
|
170
|
-
throw error;
|
|
171
|
-
} finally {
|
|
172
|
-
client.release();
|
|
173
64
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
try {
|
|
178
|
-
const result = await client.query(
|
|
179
|
-
`SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", tags, idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress FROM job_queue WHERE id = $1`,
|
|
180
|
-
[id]
|
|
65
|
+
if (handlerString.includes("[native code]")) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Handler for job type "${jobType}" contains native code which cannot be serialized. Ensure your handler is a plain function when forceKillOnTimeout is enabled.`
|
|
181
68
|
);
|
|
182
|
-
if (result.rows.length === 0) {
|
|
183
|
-
log(`Job ${id} not found`);
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
log(`Found job ${id}`);
|
|
187
|
-
const job = result.rows[0];
|
|
188
|
-
return {
|
|
189
|
-
...job,
|
|
190
|
-
payload: job.payload,
|
|
191
|
-
timeoutMs: job.timeoutMs,
|
|
192
|
-
forceKillOnTimeout: job.forceKillOnTimeout,
|
|
193
|
-
failureReason: job.failureReason
|
|
194
|
-
};
|
|
195
|
-
} catch (error) {
|
|
196
|
-
log(`Error getting job ${id}: ${error}`);
|
|
197
|
-
throw error;
|
|
198
|
-
} finally {
|
|
199
|
-
client.release();
|
|
200
69
|
}
|
|
201
|
-
}
|
|
202
|
-
async getJobsByStatus(status, limit = 100, offset = 0) {
|
|
203
|
-
const client = await this.pool.connect();
|
|
204
70
|
try {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
71
|
+
new Function("return " + handlerString);
|
|
72
|
+
} catch (parseError) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`Handler for job type "${jobType}" cannot be serialized: ${parseError instanceof Error ? parseError.message : String(parseError)}. When using forceKillOnTimeout, handlers must be serializable functions without closures over external variables.`
|
|
208
75
|
);
|
|
209
|
-
log(`Found ${result.rows.length} jobs by status ${status}`);
|
|
210
|
-
return result.rows.map((job) => ({
|
|
211
|
-
...job,
|
|
212
|
-
payload: job.payload,
|
|
213
|
-
timeoutMs: job.timeoutMs,
|
|
214
|
-
forceKillOnTimeout: job.forceKillOnTimeout,
|
|
215
|
-
failureReason: job.failureReason
|
|
216
|
-
}));
|
|
217
|
-
} catch (error) {
|
|
218
|
-
log(`Error getting jobs by status ${status}: ${error}`);
|
|
219
|
-
throw error;
|
|
220
|
-
} finally {
|
|
221
|
-
client.release();
|
|
222
76
|
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const client = await this.pool.connect();
|
|
226
|
-
try {
|
|
227
|
-
const result = await client.query(
|
|
228
|
-
`SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress FROM job_queue ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
|
|
229
|
-
[limit, offset]
|
|
230
|
-
);
|
|
231
|
-
log(`Found ${result.rows.length} jobs (all)`);
|
|
232
|
-
return result.rows.map((job) => ({
|
|
233
|
-
...job,
|
|
234
|
-
payload: job.payload,
|
|
235
|
-
timeoutMs: job.timeoutMs,
|
|
236
|
-
forceKillOnTimeout: job.forceKillOnTimeout
|
|
237
|
-
}));
|
|
238
|
-
} catch (error) {
|
|
239
|
-
log(`Error getting all jobs: ${error}`);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof Error) {
|
|
240
79
|
throw error;
|
|
241
|
-
} finally {
|
|
242
|
-
client.release();
|
|
243
80
|
}
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Failed to validate handler serialization for job type "${jobType}": ${String(error)}`
|
|
83
|
+
);
|
|
244
84
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
85
|
+
}
|
|
86
|
+
async function runHandlerInWorker(handler, payload, timeoutMs, jobType) {
|
|
87
|
+
validateHandlerSerializable(handler, jobType);
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
const workerCode = `
|
|
90
|
+
(function() {
|
|
91
|
+
const { parentPort, workerData } = require('worker_threads');
|
|
92
|
+
const { handlerCode, payload, timeoutMs } = workerData;
|
|
93
|
+
|
|
94
|
+
// Create an AbortController for the handler
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
const signal = controller.signal;
|
|
97
|
+
|
|
98
|
+
// Set up timeout
|
|
99
|
+
const timeoutId = setTimeout(() => {
|
|
100
|
+
controller.abort();
|
|
101
|
+
parentPort.postMessage({ type: 'timeout' });
|
|
102
|
+
}, timeoutMs);
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
// Execute the handler
|
|
106
|
+
// Note: This uses Function constructor which requires the handler to be serializable.
|
|
107
|
+
// The handler should be validated before reaching this point.
|
|
108
|
+
let handlerFn;
|
|
109
|
+
try {
|
|
110
|
+
// Wrap handlerCode in parentheses to ensure it's treated as an expression
|
|
111
|
+
// This handles both arrow functions and regular functions
|
|
112
|
+
const wrappedCode = handlerCode.trim().startsWith('async') || handlerCode.trim().startsWith('function')
|
|
113
|
+
? handlerCode
|
|
114
|
+
: '(' + handlerCode + ')';
|
|
115
|
+
handlerFn = new Function('return ' + wrappedCode)();
|
|
116
|
+
} catch (parseError) {
|
|
117
|
+
clearTimeout(timeoutId);
|
|
118
|
+
parentPort.postMessage({
|
|
119
|
+
type: 'error',
|
|
120
|
+
error: {
|
|
121
|
+
message: 'Handler cannot be deserialized in worker thread. ' +
|
|
122
|
+
'Ensure your handler is a standalone function without closures over external variables. ' +
|
|
123
|
+
'Original error: ' + (parseError instanceof Error ? parseError.message : String(parseError)),
|
|
124
|
+
stack: parseError instanceof Error ? parseError.stack : undefined,
|
|
125
|
+
name: 'SerializationError',
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Ensure handlerFn is actually a function
|
|
132
|
+
if (typeof handlerFn !== 'function') {
|
|
133
|
+
clearTimeout(timeoutId);
|
|
134
|
+
parentPort.postMessage({
|
|
135
|
+
type: 'error',
|
|
136
|
+
error: {
|
|
137
|
+
message: 'Handler deserialization did not produce a function. ' +
|
|
138
|
+
'Ensure your handler is a valid function when forceKillOnTimeout is enabled.',
|
|
139
|
+
name: 'SerializationError',
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
handlerFn(payload, signal)
|
|
146
|
+
.then(() => {
|
|
147
|
+
clearTimeout(timeoutId);
|
|
148
|
+
parentPort.postMessage({ type: 'success' });
|
|
149
|
+
})
|
|
150
|
+
.catch((error) => {
|
|
151
|
+
clearTimeout(timeoutId);
|
|
152
|
+
parentPort.postMessage({
|
|
153
|
+
type: 'error',
|
|
154
|
+
error: {
|
|
155
|
+
message: error.message,
|
|
156
|
+
stack: error.stack,
|
|
157
|
+
name: error.name,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
} catch (error) {
|
|
162
|
+
clearTimeout(timeoutId);
|
|
163
|
+
parentPort.postMessage({
|
|
164
|
+
type: 'error',
|
|
165
|
+
error: {
|
|
166
|
+
message: error.message,
|
|
167
|
+
stack: error.stack,
|
|
168
|
+
name: error.name,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
})();
|
|
173
|
+
`;
|
|
174
|
+
const worker = new Worker(workerCode, {
|
|
175
|
+
eval: true,
|
|
176
|
+
workerData: {
|
|
177
|
+
handlerCode: handler.toString(),
|
|
178
|
+
payload,
|
|
179
|
+
timeoutMs
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
let resolved = false;
|
|
183
|
+
worker.on("message", (message) => {
|
|
184
|
+
if (resolved) return;
|
|
185
|
+
resolved = true;
|
|
186
|
+
if (message.type === "success") {
|
|
187
|
+
resolve();
|
|
188
|
+
} else if (message.type === "timeout") {
|
|
189
|
+
const timeoutError = new Error(
|
|
190
|
+
`Job timed out after ${timeoutMs} ms and was forcefully terminated`
|
|
191
|
+
);
|
|
192
|
+
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
193
|
+
reject(timeoutError);
|
|
194
|
+
} else if (message.type === "error") {
|
|
195
|
+
const error = new Error(message.error.message);
|
|
196
|
+
error.stack = message.error.stack;
|
|
197
|
+
error.name = message.error.name;
|
|
198
|
+
reject(error);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
worker.on("error", (error) => {
|
|
202
|
+
if (resolved) return;
|
|
203
|
+
resolved = true;
|
|
204
|
+
reject(error);
|
|
205
|
+
});
|
|
206
|
+
worker.on("exit", (code) => {
|
|
207
|
+
if (resolved) return;
|
|
208
|
+
if (code !== 0) {
|
|
209
|
+
resolved = true;
|
|
210
|
+
reject(new Error(`Worker stopped with exit code ${code}`));
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
setTimeout(() => {
|
|
214
|
+
if (!resolved) {
|
|
215
|
+
resolved = true;
|
|
216
|
+
worker.terminate().then(() => {
|
|
217
|
+
const timeoutError = new Error(
|
|
218
|
+
`Job timed out after ${timeoutMs} ms and was forcefully terminated`
|
|
219
|
+
);
|
|
220
|
+
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
221
|
+
reject(timeoutError);
|
|
222
|
+
}).catch((err) => {
|
|
223
|
+
reject(err);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}, timeoutMs + 100);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function calculateWaitUntil(duration) {
|
|
230
|
+
const now = Date.now();
|
|
231
|
+
let ms = 0;
|
|
232
|
+
if (duration.seconds) ms += duration.seconds * 1e3;
|
|
233
|
+
if (duration.minutes) ms += duration.minutes * 60 * 1e3;
|
|
234
|
+
if (duration.hours) ms += duration.hours * 60 * 60 * 1e3;
|
|
235
|
+
if (duration.days) ms += duration.days * 24 * 60 * 60 * 1e3;
|
|
236
|
+
if (duration.weeks) ms += duration.weeks * 7 * 24 * 60 * 60 * 1e3;
|
|
237
|
+
if (duration.months) ms += duration.months * 30 * 24 * 60 * 60 * 1e3;
|
|
238
|
+
if (duration.years) ms += duration.years * 365 * 24 * 60 * 60 * 1e3;
|
|
239
|
+
if (ms <= 0) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
"waitFor duration must be positive. Provide at least one positive duration field."
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
return new Date(now + ms);
|
|
245
|
+
}
|
|
246
|
+
async function resolveCompletedWaits(backend, stepData) {
|
|
247
|
+
for (const key of Object.keys(stepData)) {
|
|
248
|
+
if (!key.startsWith("__wait_")) continue;
|
|
249
|
+
const entry = stepData[key];
|
|
250
|
+
if (!entry || typeof entry !== "object" || entry.completed) continue;
|
|
251
|
+
if (entry.type === "duration" || entry.type === "date") {
|
|
252
|
+
stepData[key] = { ...entry, completed: true };
|
|
253
|
+
} else if (entry.type === "token" && entry.tokenId) {
|
|
254
|
+
const wp = await backend.getWaitpoint(entry.tokenId);
|
|
255
|
+
if (wp && wp.status === "completed") {
|
|
256
|
+
stepData[key] = {
|
|
257
|
+
...entry,
|
|
258
|
+
completed: true,
|
|
259
|
+
result: { ok: true, output: wp.output }
|
|
260
|
+
};
|
|
261
|
+
} else if (wp && wp.status === "timed_out") {
|
|
262
|
+
stepData[key] = {
|
|
263
|
+
...entry,
|
|
264
|
+
completed: true,
|
|
265
|
+
result: { ok: false, error: "Token timed out" }
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function buildWaitContext(backend, jobId, stepData, baseCtx) {
|
|
272
|
+
let waitCounter = 0;
|
|
273
|
+
const ctx = {
|
|
274
|
+
prolong: baseCtx.prolong,
|
|
275
|
+
onTimeout: baseCtx.onTimeout,
|
|
276
|
+
run: async (stepName, fn) => {
|
|
277
|
+
const cached = stepData[stepName];
|
|
278
|
+
if (cached && typeof cached === "object" && cached.__completed) {
|
|
279
|
+
log(`Step "${stepName}" replayed from cache for job ${jobId}`);
|
|
280
|
+
return cached.result;
|
|
281
|
+
}
|
|
282
|
+
const result = await fn();
|
|
283
|
+
stepData[stepName] = { __completed: true, result };
|
|
284
|
+
await backend.updateStepData(jobId, stepData);
|
|
285
|
+
return result;
|
|
286
|
+
},
|
|
287
|
+
waitFor: async (duration) => {
|
|
288
|
+
const waitKey = `__wait_${waitCounter++}`;
|
|
289
|
+
const cached = stepData[waitKey];
|
|
290
|
+
if (cached && typeof cached === "object" && cached.completed) {
|
|
291
|
+
log(`Wait "${waitKey}" already completed for job ${jobId}, skipping`);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const waitUntilDate = calculateWaitUntil(duration);
|
|
295
|
+
stepData[waitKey] = { type: "duration", completed: false };
|
|
296
|
+
throw new WaitSignal("duration", waitUntilDate, void 0, stepData);
|
|
297
|
+
},
|
|
298
|
+
waitUntil: async (date) => {
|
|
299
|
+
const waitKey = `__wait_${waitCounter++}`;
|
|
300
|
+
const cached = stepData[waitKey];
|
|
301
|
+
if (cached && typeof cached === "object" && cached.completed) {
|
|
302
|
+
log(`Wait "${waitKey}" already completed for job ${jobId}, skipping`);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
stepData[waitKey] = { type: "date", completed: false };
|
|
306
|
+
throw new WaitSignal("date", date, void 0, stepData);
|
|
307
|
+
},
|
|
308
|
+
createToken: async (options) => {
|
|
309
|
+
const token = await backend.createWaitpoint(jobId, options);
|
|
310
|
+
return token;
|
|
311
|
+
},
|
|
312
|
+
waitForToken: async (tokenId) => {
|
|
313
|
+
const waitKey = `__wait_${waitCounter++}`;
|
|
314
|
+
const cached = stepData[waitKey];
|
|
315
|
+
if (cached && typeof cached === "object" && cached.completed) {
|
|
316
|
+
log(
|
|
317
|
+
`Token wait "${waitKey}" already completed for job ${jobId}, returning cached result`
|
|
318
|
+
);
|
|
319
|
+
return cached.result;
|
|
320
|
+
}
|
|
321
|
+
const wp = await backend.getWaitpoint(tokenId);
|
|
322
|
+
if (wp && wp.status === "completed") {
|
|
323
|
+
const result = {
|
|
324
|
+
ok: true,
|
|
325
|
+
output: wp.output
|
|
326
|
+
};
|
|
327
|
+
stepData[waitKey] = {
|
|
328
|
+
type: "token",
|
|
329
|
+
tokenId,
|
|
330
|
+
completed: true,
|
|
331
|
+
result
|
|
332
|
+
};
|
|
333
|
+
await backend.updateStepData(jobId, stepData);
|
|
334
|
+
return result;
|
|
335
|
+
}
|
|
336
|
+
if (wp && wp.status === "timed_out") {
|
|
337
|
+
const result = {
|
|
338
|
+
ok: false,
|
|
339
|
+
error: "Token timed out"
|
|
340
|
+
};
|
|
341
|
+
stepData[waitKey] = {
|
|
342
|
+
type: "token",
|
|
343
|
+
tokenId,
|
|
344
|
+
completed: true,
|
|
345
|
+
result
|
|
346
|
+
};
|
|
347
|
+
await backend.updateStepData(jobId, stepData);
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
stepData[waitKey] = { type: "token", tokenId, completed: false };
|
|
351
|
+
throw new WaitSignal("token", void 0, tokenId, stepData);
|
|
352
|
+
},
|
|
353
|
+
setProgress: async (percent) => {
|
|
354
|
+
if (percent < 0 || percent > 100)
|
|
355
|
+
throw new Error("Progress must be between 0 and 100");
|
|
356
|
+
await backend.updateProgress(jobId, Math.round(percent));
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
return ctx;
|
|
360
|
+
}
|
|
361
|
+
async function processJobWithHandlers(backend, job, jobHandlers) {
|
|
362
|
+
const handler = jobHandlers[job.jobType];
|
|
363
|
+
if (!handler) {
|
|
364
|
+
await backend.setPendingReasonForUnpickedJobs(
|
|
365
|
+
`No handler registered for job type: ${job.jobType}`,
|
|
366
|
+
job.jobType
|
|
367
|
+
);
|
|
368
|
+
await backend.failJob(
|
|
369
|
+
job.id,
|
|
370
|
+
new Error(`No handler registered for job type: ${job.jobType}`),
|
|
371
|
+
"no_handler" /* NoHandler */
|
|
372
|
+
);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const stepData = { ...job.stepData || {} };
|
|
376
|
+
const hasStepHistory = Object.keys(stepData).some(
|
|
377
|
+
(k) => k.startsWith("__wait_")
|
|
378
|
+
);
|
|
379
|
+
if (hasStepHistory) {
|
|
380
|
+
await resolveCompletedWaits(backend, stepData);
|
|
381
|
+
await backend.updateStepData(job.id, stepData);
|
|
382
|
+
}
|
|
383
|
+
const timeoutMs = job.timeoutMs ?? void 0;
|
|
384
|
+
const forceKillOnTimeout = job.forceKillOnTimeout ?? false;
|
|
385
|
+
let timeoutId;
|
|
386
|
+
const controller = new AbortController();
|
|
387
|
+
try {
|
|
388
|
+
if (forceKillOnTimeout && timeoutMs && timeoutMs > 0) {
|
|
389
|
+
await runHandlerInWorker(handler, job.payload, timeoutMs, job.jobType);
|
|
390
|
+
} else {
|
|
391
|
+
let onTimeoutCallback;
|
|
392
|
+
let timeoutReject;
|
|
393
|
+
const armTimeout = (ms) => {
|
|
394
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
395
|
+
timeoutId = setTimeout(() => {
|
|
396
|
+
if (onTimeoutCallback) {
|
|
397
|
+
try {
|
|
398
|
+
const extension = onTimeoutCallback();
|
|
399
|
+
if (typeof extension === "number" && extension > 0) {
|
|
400
|
+
backend.prolongJob(job.id).catch(() => {
|
|
401
|
+
});
|
|
402
|
+
armTimeout(extension);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
} catch (callbackError) {
|
|
406
|
+
log(
|
|
407
|
+
`onTimeout callback threw for job ${job.id}: ${callbackError}`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
controller.abort();
|
|
412
|
+
const timeoutError = new Error(`Job timed out after ${ms} ms`);
|
|
413
|
+
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
414
|
+
if (timeoutReject) {
|
|
415
|
+
timeoutReject(timeoutError);
|
|
416
|
+
}
|
|
417
|
+
}, ms);
|
|
418
|
+
};
|
|
419
|
+
const hasTimeout = timeoutMs != null && timeoutMs > 0;
|
|
420
|
+
const baseCtx = hasTimeout ? {
|
|
421
|
+
prolong: (ms) => {
|
|
422
|
+
const duration = ms ?? timeoutMs;
|
|
423
|
+
if (duration != null && duration > 0) {
|
|
424
|
+
armTimeout(duration);
|
|
425
|
+
backend.prolongJob(job.id).catch(() => {
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
onTimeout: (callback) => {
|
|
430
|
+
onTimeoutCallback = callback;
|
|
431
|
+
}
|
|
432
|
+
} : {
|
|
433
|
+
prolong: () => {
|
|
434
|
+
log("prolong() called but ignored: job has no timeout set");
|
|
435
|
+
},
|
|
436
|
+
onTimeout: () => {
|
|
437
|
+
log("onTimeout() called but ignored: job has no timeout set");
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
const ctx = buildWaitContext(backend, job.id, stepData, baseCtx);
|
|
441
|
+
if (forceKillOnTimeout && !hasTimeout) {
|
|
442
|
+
log(
|
|
443
|
+
`forceKillOnTimeout is set but no timeoutMs for job ${job.id}, running without force kill`
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
const jobPromise = handler(job.payload, controller.signal, ctx);
|
|
447
|
+
if (hasTimeout) {
|
|
448
|
+
await Promise.race([
|
|
449
|
+
jobPromise,
|
|
450
|
+
new Promise((_, reject) => {
|
|
451
|
+
timeoutReject = reject;
|
|
452
|
+
armTimeout(timeoutMs);
|
|
453
|
+
})
|
|
454
|
+
]);
|
|
455
|
+
} else {
|
|
456
|
+
await jobPromise;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
460
|
+
await backend.completeJob(job.id);
|
|
461
|
+
} catch (error) {
|
|
462
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
463
|
+
if (error instanceof WaitSignal) {
|
|
464
|
+
log(
|
|
465
|
+
`Job ${job.id} entering wait: type=${error.type}, waitUntil=${error.waitUntil?.toISOString() ?? "none"}, tokenId=${error.tokenId ?? "none"}`
|
|
466
|
+
);
|
|
467
|
+
await backend.waitJob(job.id, {
|
|
468
|
+
waitUntil: error.waitUntil,
|
|
469
|
+
waitTokenId: error.tokenId,
|
|
470
|
+
stepData: error.stepData
|
|
471
|
+
});
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
console.error(`Error processing job ${job.id}:`, error);
|
|
475
|
+
let failureReason = "handler_error" /* HandlerError */;
|
|
476
|
+
if (error && typeof error === "object" && "failureReason" in error && error.failureReason === "timeout" /* Timeout */) {
|
|
477
|
+
failureReason = "timeout" /* Timeout */;
|
|
478
|
+
}
|
|
479
|
+
await backend.failJob(
|
|
480
|
+
job.id,
|
|
481
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
482
|
+
failureReason
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
async function processBatchWithHandlers(backend, workerId, batchSize, jobType, jobHandlers, concurrency, onError) {
|
|
487
|
+
const jobs = await backend.getNextBatch(
|
|
488
|
+
workerId,
|
|
489
|
+
batchSize,
|
|
490
|
+
jobType
|
|
491
|
+
);
|
|
492
|
+
if (!concurrency || concurrency >= jobs.length) {
|
|
493
|
+
await Promise.all(
|
|
494
|
+
jobs.map((job) => processJobWithHandlers(backend, job, jobHandlers))
|
|
495
|
+
);
|
|
496
|
+
return jobs.length;
|
|
497
|
+
}
|
|
498
|
+
let idx = 0;
|
|
499
|
+
let running = 0;
|
|
500
|
+
let finished = 0;
|
|
501
|
+
return new Promise((resolve, reject) => {
|
|
502
|
+
const next = () => {
|
|
503
|
+
if (finished === jobs.length) return resolve(jobs.length);
|
|
504
|
+
while (running < concurrency && idx < jobs.length) {
|
|
505
|
+
const job = jobs[idx++];
|
|
506
|
+
running++;
|
|
507
|
+
processJobWithHandlers(backend, job, jobHandlers).then(() => {
|
|
508
|
+
running--;
|
|
509
|
+
finished++;
|
|
510
|
+
next();
|
|
511
|
+
}).catch((err) => {
|
|
512
|
+
running--;
|
|
513
|
+
finished++;
|
|
514
|
+
if (onError) {
|
|
515
|
+
onError(err instanceof Error ? err : new Error(String(err)));
|
|
516
|
+
}
|
|
517
|
+
next();
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
next();
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
var createProcessor = (backend, handlers, options = {}, onBeforeBatch) => {
|
|
525
|
+
const {
|
|
526
|
+
workerId = `worker-${Math.random().toString(36).substring(2, 9)}`,
|
|
527
|
+
batchSize = 10,
|
|
528
|
+
pollInterval = 5e3,
|
|
529
|
+
onError = (error) => console.error("Job processor error:", error),
|
|
530
|
+
jobType,
|
|
531
|
+
concurrency = 3
|
|
532
|
+
} = options;
|
|
533
|
+
let running = false;
|
|
534
|
+
let intervalId = null;
|
|
535
|
+
let currentBatchPromise = null;
|
|
536
|
+
setLogContext(options.verbose ?? false);
|
|
537
|
+
const processJobs = async () => {
|
|
538
|
+
if (!running) return 0;
|
|
539
|
+
if (onBeforeBatch) {
|
|
540
|
+
try {
|
|
541
|
+
await onBeforeBatch();
|
|
542
|
+
} catch (hookError) {
|
|
543
|
+
log(`onBeforeBatch hook error: ${hookError}`);
|
|
544
|
+
if (onError) {
|
|
545
|
+
onError(
|
|
546
|
+
hookError instanceof Error ? hookError : new Error(String(hookError))
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
log(
|
|
552
|
+
`Processing jobs with workerId: ${workerId}${jobType ? ` and jobType: ${Array.isArray(jobType) ? jobType.join(",") : jobType}` : ""}`
|
|
553
|
+
);
|
|
554
|
+
try {
|
|
555
|
+
const processed = await processBatchWithHandlers(
|
|
556
|
+
backend,
|
|
557
|
+
workerId,
|
|
558
|
+
batchSize,
|
|
559
|
+
jobType,
|
|
560
|
+
handlers,
|
|
561
|
+
concurrency,
|
|
562
|
+
onError
|
|
563
|
+
);
|
|
564
|
+
return processed;
|
|
565
|
+
} catch (error) {
|
|
566
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
567
|
+
}
|
|
568
|
+
return 0;
|
|
569
|
+
};
|
|
570
|
+
return {
|
|
571
|
+
/**
|
|
572
|
+
* Start the job processor in the background.
|
|
573
|
+
* - This will run periodically (every pollInterval milliseconds or 5 seconds if not provided) and process jobs as they become available.
|
|
574
|
+
* - You have to call the stop method to stop the processor.
|
|
575
|
+
*/
|
|
576
|
+
startInBackground: () => {
|
|
577
|
+
if (running) return;
|
|
578
|
+
log(`Starting job processor with workerId: ${workerId}`);
|
|
579
|
+
running = true;
|
|
580
|
+
const scheduleNext = (immediate) => {
|
|
581
|
+
if (!running) return;
|
|
582
|
+
if (immediate) {
|
|
583
|
+
intervalId = setTimeout(loop, 0);
|
|
584
|
+
} else {
|
|
585
|
+
intervalId = setTimeout(loop, pollInterval);
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
const loop = async () => {
|
|
589
|
+
if (!running) return;
|
|
590
|
+
currentBatchPromise = processJobs();
|
|
591
|
+
const processed = await currentBatchPromise;
|
|
592
|
+
currentBatchPromise = null;
|
|
593
|
+
scheduleNext(processed === batchSize);
|
|
594
|
+
};
|
|
595
|
+
loop();
|
|
596
|
+
},
|
|
597
|
+
/**
|
|
598
|
+
* Stop the job processor that runs in the background.
|
|
599
|
+
* Does not wait for in-flight jobs.
|
|
600
|
+
*/
|
|
601
|
+
stop: () => {
|
|
602
|
+
log(`Stopping job processor with workerId: ${workerId}`);
|
|
603
|
+
running = false;
|
|
604
|
+
if (intervalId) {
|
|
605
|
+
clearTimeout(intervalId);
|
|
606
|
+
intervalId = null;
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
/**
|
|
610
|
+
* Stop the job processor and wait for all in-flight jobs to complete.
|
|
611
|
+
* Useful for graceful shutdown (e.g., SIGTERM handling).
|
|
612
|
+
*/
|
|
613
|
+
stopAndDrain: async (drainTimeoutMs = 3e4) => {
|
|
614
|
+
log(`Stopping and draining job processor with workerId: ${workerId}`);
|
|
615
|
+
running = false;
|
|
616
|
+
if (intervalId) {
|
|
617
|
+
clearTimeout(intervalId);
|
|
618
|
+
intervalId = null;
|
|
619
|
+
}
|
|
620
|
+
if (currentBatchPromise) {
|
|
621
|
+
await Promise.race([
|
|
622
|
+
currentBatchPromise.catch(() => {
|
|
623
|
+
}),
|
|
624
|
+
new Promise((resolve) => setTimeout(resolve, drainTimeoutMs))
|
|
625
|
+
]);
|
|
626
|
+
currentBatchPromise = null;
|
|
627
|
+
}
|
|
628
|
+
log(`Job processor ${workerId} drained`);
|
|
629
|
+
},
|
|
630
|
+
/**
|
|
631
|
+
* Start the job processor synchronously.
|
|
632
|
+
* - This will process all jobs immediately and then stop.
|
|
633
|
+
* - The pollInterval is ignored.
|
|
634
|
+
*/
|
|
635
|
+
start: async () => {
|
|
636
|
+
log(`Starting job processor with workerId: ${workerId}`);
|
|
637
|
+
running = true;
|
|
638
|
+
const processed = await processJobs();
|
|
639
|
+
running = false;
|
|
640
|
+
return processed;
|
|
641
|
+
},
|
|
642
|
+
isRunning: () => running
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
// src/supervisor.ts
|
|
647
|
+
var createSupervisor = (backend, options = {}) => {
|
|
648
|
+
const {
|
|
649
|
+
intervalMs = 6e4,
|
|
650
|
+
stuckJobsTimeoutMinutes = 10,
|
|
651
|
+
cleanupJobsDaysToKeep = 30,
|
|
652
|
+
cleanupEventsDaysToKeep = 30,
|
|
653
|
+
cleanupBatchSize = 1e3,
|
|
654
|
+
reclaimStuckJobs = true,
|
|
655
|
+
expireTimedOutTokens = true,
|
|
656
|
+
onError = (error) => console.error("Supervisor maintenance error:", error),
|
|
657
|
+
verbose = false
|
|
658
|
+
} = options;
|
|
659
|
+
let running = false;
|
|
660
|
+
let timeoutId = null;
|
|
661
|
+
let currentRunPromise = null;
|
|
662
|
+
setLogContext(verbose);
|
|
663
|
+
const runOnce = async () => {
|
|
664
|
+
setLogContext(verbose);
|
|
665
|
+
const result = {
|
|
666
|
+
reclaimedJobs: 0,
|
|
667
|
+
cleanedUpJobs: 0,
|
|
668
|
+
cleanedUpEvents: 0,
|
|
669
|
+
expiredTokens: 0
|
|
670
|
+
};
|
|
671
|
+
if (reclaimStuckJobs) {
|
|
672
|
+
try {
|
|
673
|
+
result.reclaimedJobs = await backend.reclaimStuckJobs(
|
|
674
|
+
stuckJobsTimeoutMinutes
|
|
675
|
+
);
|
|
676
|
+
if (result.reclaimedJobs > 0) {
|
|
677
|
+
log(`Supervisor: reclaimed ${result.reclaimedJobs} stuck jobs`);
|
|
678
|
+
}
|
|
679
|
+
} catch (e) {
|
|
680
|
+
onError(e instanceof Error ? e : new Error(String(e)));
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (cleanupJobsDaysToKeep > 0) {
|
|
684
|
+
try {
|
|
685
|
+
result.cleanedUpJobs = await backend.cleanupOldJobs(
|
|
686
|
+
cleanupJobsDaysToKeep,
|
|
687
|
+
cleanupBatchSize
|
|
688
|
+
);
|
|
689
|
+
if (result.cleanedUpJobs > 0) {
|
|
690
|
+
log(`Supervisor: cleaned up ${result.cleanedUpJobs} old jobs`);
|
|
691
|
+
}
|
|
692
|
+
} catch (e) {
|
|
693
|
+
onError(e instanceof Error ? e : new Error(String(e)));
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (cleanupEventsDaysToKeep > 0) {
|
|
697
|
+
try {
|
|
698
|
+
result.cleanedUpEvents = await backend.cleanupOldJobEvents(
|
|
699
|
+
cleanupEventsDaysToKeep,
|
|
700
|
+
cleanupBatchSize
|
|
701
|
+
);
|
|
702
|
+
if (result.cleanedUpEvents > 0) {
|
|
703
|
+
log(
|
|
704
|
+
`Supervisor: cleaned up ${result.cleanedUpEvents} old job events`
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
} catch (e) {
|
|
708
|
+
onError(e instanceof Error ? e : new Error(String(e)));
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if (expireTimedOutTokens) {
|
|
712
|
+
try {
|
|
713
|
+
result.expiredTokens = await backend.expireTimedOutWaitpoints();
|
|
714
|
+
if (result.expiredTokens > 0) {
|
|
715
|
+
log(`Supervisor: expired ${result.expiredTokens} timed-out tokens`);
|
|
716
|
+
}
|
|
717
|
+
} catch (e) {
|
|
718
|
+
onError(e instanceof Error ? e : new Error(String(e)));
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return result;
|
|
722
|
+
};
|
|
723
|
+
return {
|
|
724
|
+
start: async () => {
|
|
725
|
+
return runOnce();
|
|
726
|
+
},
|
|
727
|
+
startInBackground: () => {
|
|
728
|
+
if (running) return;
|
|
729
|
+
log("Supervisor: starting background maintenance loop");
|
|
730
|
+
running = true;
|
|
731
|
+
const loop = async () => {
|
|
732
|
+
if (!running) return;
|
|
733
|
+
currentRunPromise = runOnce();
|
|
734
|
+
await currentRunPromise;
|
|
735
|
+
currentRunPromise = null;
|
|
736
|
+
if (running) {
|
|
737
|
+
timeoutId = setTimeout(loop, intervalMs);
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
loop();
|
|
741
|
+
},
|
|
742
|
+
stop: () => {
|
|
743
|
+
running = false;
|
|
744
|
+
if (timeoutId !== null) {
|
|
745
|
+
clearTimeout(timeoutId);
|
|
746
|
+
timeoutId = null;
|
|
747
|
+
}
|
|
748
|
+
log("Supervisor: stopped");
|
|
749
|
+
},
|
|
750
|
+
stopAndDrain: async (timeoutMs = 3e4) => {
|
|
751
|
+
running = false;
|
|
752
|
+
if (timeoutId !== null) {
|
|
753
|
+
clearTimeout(timeoutId);
|
|
754
|
+
timeoutId = null;
|
|
755
|
+
}
|
|
756
|
+
if (currentRunPromise) {
|
|
757
|
+
log("Supervisor: draining current maintenance run\u2026");
|
|
758
|
+
await Promise.race([
|
|
759
|
+
currentRunPromise,
|
|
760
|
+
new Promise((resolve) => setTimeout(resolve, timeoutMs))
|
|
761
|
+
]);
|
|
762
|
+
currentRunPromise = null;
|
|
763
|
+
}
|
|
764
|
+
log("Supervisor: drained and stopped");
|
|
765
|
+
},
|
|
766
|
+
isRunning: () => running
|
|
767
|
+
};
|
|
768
|
+
};
|
|
769
|
+
function loadPemOrFile(value) {
|
|
770
|
+
if (!value) return void 0;
|
|
771
|
+
if (value.startsWith("file://")) {
|
|
772
|
+
const filePath = value.slice(7);
|
|
773
|
+
return fs.readFileSync(filePath, "utf8");
|
|
774
|
+
}
|
|
775
|
+
return value;
|
|
776
|
+
}
|
|
777
|
+
var createPool = (config) => {
|
|
778
|
+
let searchPath;
|
|
779
|
+
let ssl = void 0;
|
|
780
|
+
let customCA;
|
|
781
|
+
let sslmode;
|
|
782
|
+
if (config.connectionString) {
|
|
783
|
+
try {
|
|
784
|
+
const url = new URL(config.connectionString);
|
|
785
|
+
searchPath = url.searchParams.get("search_path") || void 0;
|
|
786
|
+
sslmode = url.searchParams.get("sslmode") || void 0;
|
|
787
|
+
if (sslmode === "no-verify") {
|
|
788
|
+
ssl = { rejectUnauthorized: false };
|
|
789
|
+
}
|
|
790
|
+
} catch (e) {
|
|
791
|
+
const parsed = parse(config.connectionString);
|
|
792
|
+
if (parsed.options) {
|
|
793
|
+
const match = parsed.options.match(/search_path=([^\s]+)/);
|
|
794
|
+
if (match) {
|
|
795
|
+
searchPath = match[1];
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
sslmode = typeof parsed.sslmode === "string" ? parsed.sslmode : void 0;
|
|
799
|
+
if (sslmode === "no-verify") {
|
|
800
|
+
ssl = { rejectUnauthorized: false };
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
if (config.ssl) {
|
|
805
|
+
if (typeof config.ssl.ca === "string") {
|
|
806
|
+
customCA = config.ssl.ca;
|
|
807
|
+
} else if (typeof process.env.PGSSLROOTCERT === "string") {
|
|
808
|
+
customCA = process.env.PGSSLROOTCERT;
|
|
809
|
+
} else {
|
|
810
|
+
customCA = void 0;
|
|
811
|
+
}
|
|
812
|
+
const caValue = typeof customCA === "string" ? loadPemOrFile(customCA) : void 0;
|
|
813
|
+
ssl = {
|
|
814
|
+
...ssl,
|
|
815
|
+
...caValue ? { ca: caValue } : {},
|
|
816
|
+
cert: loadPemOrFile(
|
|
817
|
+
typeof config.ssl.cert === "string" ? config.ssl.cert : process.env.PGSSLCERT
|
|
818
|
+
),
|
|
819
|
+
key: loadPemOrFile(
|
|
820
|
+
typeof config.ssl.key === "string" ? config.ssl.key : process.env.PGSSLKEY
|
|
821
|
+
),
|
|
822
|
+
rejectUnauthorized: config.ssl.rejectUnauthorized !== void 0 ? config.ssl.rejectUnauthorized : true
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
if (sslmode && customCA) {
|
|
826
|
+
const warning = `
|
|
827
|
+
|
|
828
|
+
\x1B[33m**************************************************
|
|
829
|
+
\u26A0\uFE0F WARNING: SSL CONFIGURATION ISSUE
|
|
830
|
+
**************************************************
|
|
831
|
+
Both sslmode ('${sslmode}') is set in the connection string
|
|
832
|
+
and a custom CA is provided (via config.ssl.ca or PGSSLROOTCERT).
|
|
833
|
+
This combination may cause connection failures or unexpected behavior.
|
|
834
|
+
|
|
835
|
+
Recommended: Remove sslmode from the connection string when using a custom CA.
|
|
836
|
+
**************************************************\x1B[0m
|
|
837
|
+
`;
|
|
838
|
+
console.warn(warning);
|
|
839
|
+
}
|
|
840
|
+
const pool = new Pool({
|
|
841
|
+
...config,
|
|
842
|
+
...ssl ? { ssl } : {}
|
|
843
|
+
});
|
|
844
|
+
if (searchPath) {
|
|
845
|
+
pool.on("connect", (client) => {
|
|
846
|
+
client.query(`SET search_path TO ${searchPath}`);
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
return pool;
|
|
850
|
+
};
|
|
851
|
+
var MAX_TIMEOUT_MS = 365 * 24 * 60 * 60 * 1e3;
|
|
852
|
+
function parseTimeoutString(timeout) {
|
|
853
|
+
const match = timeout.match(/^(\d+)(s|m|h|d)$/);
|
|
854
|
+
if (!match) {
|
|
855
|
+
throw new Error(
|
|
856
|
+
`Invalid timeout format: "${timeout}". Expected format like "10m", "1h", "24h", "7d".`
|
|
857
|
+
);
|
|
858
|
+
}
|
|
859
|
+
const value = parseInt(match[1], 10);
|
|
860
|
+
const unit = match[2];
|
|
861
|
+
let ms;
|
|
862
|
+
switch (unit) {
|
|
863
|
+
case "s":
|
|
864
|
+
ms = value * 1e3;
|
|
865
|
+
break;
|
|
866
|
+
case "m":
|
|
867
|
+
ms = value * 60 * 1e3;
|
|
868
|
+
break;
|
|
869
|
+
case "h":
|
|
870
|
+
ms = value * 60 * 60 * 1e3;
|
|
871
|
+
break;
|
|
872
|
+
case "d":
|
|
873
|
+
ms = value * 24 * 60 * 60 * 1e3;
|
|
874
|
+
break;
|
|
875
|
+
default:
|
|
876
|
+
throw new Error(`Unknown timeout unit: "${unit}"`);
|
|
877
|
+
}
|
|
878
|
+
if (!Number.isFinite(ms) || ms > MAX_TIMEOUT_MS) {
|
|
879
|
+
throw new Error(
|
|
880
|
+
`Timeout value "${timeout}" is too large. Maximum allowed is 365 days.`
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
return ms;
|
|
884
|
+
}
|
|
885
|
+
var PostgresBackend = class {
|
|
886
|
+
constructor(pool) {
|
|
887
|
+
this.pool = pool;
|
|
888
|
+
}
|
|
889
|
+
/** Expose the raw pool for advanced usage. */
|
|
890
|
+
getPool() {
|
|
891
|
+
return this.pool;
|
|
892
|
+
}
|
|
893
|
+
// ── Events ──────────────────────────────────────────────────────────
|
|
894
|
+
async recordJobEvent(jobId, eventType, metadata) {
|
|
895
|
+
const client = await this.pool.connect();
|
|
896
|
+
try {
|
|
897
|
+
await client.query(
|
|
898
|
+
`INSERT INTO job_events (job_id, event_type, metadata) VALUES ($1, $2, $3)`,
|
|
899
|
+
[jobId, eventType, metadata ? JSON.stringify(metadata) : null]
|
|
900
|
+
);
|
|
901
|
+
} catch (error) {
|
|
902
|
+
log(`Error recording job event for job ${jobId}: ${error}`);
|
|
903
|
+
} finally {
|
|
904
|
+
client.release();
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
async getJobEvents(jobId) {
|
|
908
|
+
const client = await this.pool.connect();
|
|
909
|
+
try {
|
|
910
|
+
const res = await client.query(
|
|
911
|
+
`SELECT id, job_id AS "jobId", event_type AS "eventType", metadata, created_at AS "createdAt" FROM job_events WHERE job_id = $1 ORDER BY created_at ASC`,
|
|
912
|
+
[jobId]
|
|
913
|
+
);
|
|
914
|
+
return res.rows;
|
|
915
|
+
} finally {
|
|
916
|
+
client.release();
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
// ── Job CRUD ──────────────────────────────────────────────────────────
|
|
920
|
+
/**
|
|
921
|
+
* Add a job and return its numeric ID.
|
|
922
|
+
*
|
|
923
|
+
* @param job - Job configuration.
|
|
924
|
+
* @param options - Optional. Pass `{ db }` to run the INSERT on an external
|
|
925
|
+
* client (e.g., inside a transaction) so the job is part of the caller's
|
|
926
|
+
* transaction. The event INSERT also uses the same client.
|
|
927
|
+
*/
|
|
928
|
+
async addJob({
|
|
929
|
+
jobType,
|
|
930
|
+
payload,
|
|
931
|
+
maxAttempts = 3,
|
|
932
|
+
priority = 0,
|
|
933
|
+
runAt = null,
|
|
934
|
+
timeoutMs = void 0,
|
|
935
|
+
forceKillOnTimeout = false,
|
|
936
|
+
tags = void 0,
|
|
937
|
+
idempotencyKey = void 0,
|
|
938
|
+
retryDelay = void 0,
|
|
939
|
+
retryBackoff = void 0,
|
|
940
|
+
retryDelayMax = void 0
|
|
941
|
+
}, options) {
|
|
942
|
+
const externalClient = options?.db;
|
|
943
|
+
const client = externalClient ?? await this.pool.connect();
|
|
944
|
+
try {
|
|
945
|
+
let result;
|
|
946
|
+
const onConflict = idempotencyKey ? `ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING` : "";
|
|
947
|
+
if (runAt) {
|
|
948
|
+
result = await client.query(
|
|
949
|
+
`INSERT INTO job_queue
|
|
950
|
+
(job_type, payload, max_attempts, priority, run_at, timeout_ms, force_kill_on_timeout, tags, idempotency_key, retry_delay, retry_backoff, retry_delay_max)
|
|
951
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
|
952
|
+
${onConflict}
|
|
953
|
+
RETURNING id`,
|
|
954
|
+
[
|
|
955
|
+
jobType,
|
|
956
|
+
payload,
|
|
957
|
+
maxAttempts,
|
|
958
|
+
priority,
|
|
959
|
+
runAt,
|
|
960
|
+
timeoutMs ?? null,
|
|
961
|
+
forceKillOnTimeout ?? false,
|
|
962
|
+
tags ?? null,
|
|
963
|
+
idempotencyKey ?? null,
|
|
964
|
+
retryDelay ?? null,
|
|
965
|
+
retryBackoff ?? null,
|
|
966
|
+
retryDelayMax ?? null
|
|
967
|
+
]
|
|
968
|
+
);
|
|
969
|
+
} else {
|
|
970
|
+
result = await client.query(
|
|
971
|
+
`INSERT INTO job_queue
|
|
972
|
+
(job_type, payload, max_attempts, priority, timeout_ms, force_kill_on_timeout, tags, idempotency_key, retry_delay, retry_backoff, retry_delay_max)
|
|
973
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
974
|
+
${onConflict}
|
|
975
|
+
RETURNING id`,
|
|
976
|
+
[
|
|
977
|
+
jobType,
|
|
978
|
+
payload,
|
|
979
|
+
maxAttempts,
|
|
980
|
+
priority,
|
|
981
|
+
timeoutMs ?? null,
|
|
982
|
+
forceKillOnTimeout ?? false,
|
|
983
|
+
tags ?? null,
|
|
984
|
+
idempotencyKey ?? null,
|
|
985
|
+
retryDelay ?? null,
|
|
986
|
+
retryBackoff ?? null,
|
|
987
|
+
retryDelayMax ?? null
|
|
988
|
+
]
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
if (result.rows.length === 0 && idempotencyKey) {
|
|
992
|
+
const existing = await client.query(
|
|
993
|
+
`SELECT id FROM job_queue WHERE idempotency_key = $1`,
|
|
994
|
+
[idempotencyKey]
|
|
995
|
+
);
|
|
996
|
+
if (existing.rows.length > 0) {
|
|
997
|
+
log(
|
|
998
|
+
`Job with idempotency key "${idempotencyKey}" already exists (id: ${existing.rows[0].id}), returning existing job`
|
|
999
|
+
);
|
|
1000
|
+
return existing.rows[0].id;
|
|
1001
|
+
}
|
|
1002
|
+
throw new Error(
|
|
1003
|
+
`Failed to insert job and could not find existing job with idempotency key "${idempotencyKey}"`
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
const jobId = result.rows[0].id;
|
|
1007
|
+
log(
|
|
1008
|
+
`Added job ${jobId}: payload ${JSON.stringify(payload)}, ${runAt ? `runAt ${runAt.toISOString()}, ` : ""}priority ${priority}, maxAttempts ${maxAttempts}, jobType ${jobType}, tags ${JSON.stringify(tags)}${idempotencyKey ? `, idempotencyKey "${idempotencyKey}"` : ""}`
|
|
1009
|
+
);
|
|
1010
|
+
if (externalClient) {
|
|
1011
|
+
try {
|
|
1012
|
+
await client.query(
|
|
1013
|
+
`INSERT INTO job_events (job_id, event_type, metadata) VALUES ($1, $2, $3)`,
|
|
1014
|
+
[
|
|
1015
|
+
jobId,
|
|
1016
|
+
"added" /* Added */,
|
|
1017
|
+
JSON.stringify({ jobType, payload, tags, idempotencyKey })
|
|
1018
|
+
]
|
|
1019
|
+
);
|
|
1020
|
+
} catch (error) {
|
|
1021
|
+
log(`Error recording job event for job ${jobId}: ${error}`);
|
|
1022
|
+
}
|
|
1023
|
+
} else {
|
|
1024
|
+
await this.recordJobEvent(jobId, "added" /* Added */, {
|
|
1025
|
+
jobType,
|
|
1026
|
+
payload,
|
|
1027
|
+
tags,
|
|
1028
|
+
idempotencyKey
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
return jobId;
|
|
1032
|
+
} catch (error) {
|
|
1033
|
+
log(`Error adding job: ${error}`);
|
|
1034
|
+
throw error;
|
|
1035
|
+
} finally {
|
|
1036
|
+
if (!externalClient) client.release();
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Insert multiple jobs in a single database round-trip.
|
|
1041
|
+
*
|
|
1042
|
+
* Uses a multi-row INSERT with ON CONFLICT handling for idempotency keys.
|
|
1043
|
+
* Returns IDs in the same order as the input array.
|
|
1044
|
+
*/
|
|
1045
|
+
async addJobs(jobs, options) {
|
|
1046
|
+
if (jobs.length === 0) return [];
|
|
1047
|
+
const externalClient = options?.db;
|
|
1048
|
+
const client = externalClient ?? await this.pool.connect();
|
|
1049
|
+
try {
|
|
1050
|
+
const COLS_PER_JOB = 12;
|
|
1051
|
+
const valueClauses = [];
|
|
1052
|
+
const params = [];
|
|
1053
|
+
const hasAnyIdempotencyKey = jobs.some((j) => j.idempotencyKey);
|
|
1054
|
+
for (let i = 0; i < jobs.length; i++) {
|
|
1055
|
+
const {
|
|
1056
|
+
jobType,
|
|
1057
|
+
payload,
|
|
1058
|
+
maxAttempts = 3,
|
|
1059
|
+
priority = 0,
|
|
1060
|
+
runAt = null,
|
|
1061
|
+
timeoutMs = void 0,
|
|
1062
|
+
forceKillOnTimeout = false,
|
|
1063
|
+
tags = void 0,
|
|
1064
|
+
idempotencyKey = void 0,
|
|
1065
|
+
retryDelay = void 0,
|
|
1066
|
+
retryBackoff = void 0,
|
|
1067
|
+
retryDelayMax = void 0
|
|
1068
|
+
} = jobs[i];
|
|
1069
|
+
const base = i * COLS_PER_JOB;
|
|
1070
|
+
valueClauses.push(
|
|
1071
|
+
`($${base + 1}, $${base + 2}, $${base + 3}, $${base + 4}, COALESCE($${base + 5}::timestamptz, CURRENT_TIMESTAMP), $${base + 6}, $${base + 7}, $${base + 8}, $${base + 9}, $${base + 10}, $${base + 11}, $${base + 12})`
|
|
1072
|
+
);
|
|
1073
|
+
params.push(
|
|
1074
|
+
jobType,
|
|
1075
|
+
payload,
|
|
1076
|
+
maxAttempts,
|
|
1077
|
+
priority,
|
|
1078
|
+
runAt,
|
|
1079
|
+
timeoutMs ?? null,
|
|
1080
|
+
forceKillOnTimeout ?? false,
|
|
1081
|
+
tags ?? null,
|
|
1082
|
+
idempotencyKey ?? null,
|
|
1083
|
+
retryDelay ?? null,
|
|
1084
|
+
retryBackoff ?? null,
|
|
1085
|
+
retryDelayMax ?? null
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
const onConflict = hasAnyIdempotencyKey ? `ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING` : "";
|
|
1089
|
+
const result = await client.query(
|
|
1090
|
+
`INSERT INTO job_queue
|
|
1091
|
+
(job_type, payload, max_attempts, priority, run_at, timeout_ms, force_kill_on_timeout, tags, idempotency_key, retry_delay, retry_backoff, retry_delay_max)
|
|
1092
|
+
VALUES ${valueClauses.join(", ")}
|
|
1093
|
+
${onConflict}
|
|
1094
|
+
RETURNING id, idempotency_key`,
|
|
1095
|
+
params
|
|
1096
|
+
);
|
|
1097
|
+
const returnedKeyToId = /* @__PURE__ */ new Map();
|
|
1098
|
+
const returnedNullKeyIds = [];
|
|
1099
|
+
for (const row of result.rows) {
|
|
1100
|
+
if (row.idempotency_key != null) {
|
|
1101
|
+
returnedKeyToId.set(row.idempotency_key, row.id);
|
|
1102
|
+
} else {
|
|
1103
|
+
returnedNullKeyIds.push(row.id);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
const missingKeys = [];
|
|
1107
|
+
for (const job of jobs) {
|
|
1108
|
+
if (job.idempotencyKey && !returnedKeyToId.has(job.idempotencyKey)) {
|
|
1109
|
+
missingKeys.push(job.idempotencyKey);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
if (missingKeys.length > 0) {
|
|
1113
|
+
const existing = await client.query(
|
|
1114
|
+
`SELECT id, idempotency_key FROM job_queue WHERE idempotency_key = ANY($1)`,
|
|
1115
|
+
[missingKeys]
|
|
1116
|
+
);
|
|
1117
|
+
for (const row of existing.rows) {
|
|
1118
|
+
returnedKeyToId.set(row.idempotency_key, row.id);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
let nullKeyIdx = 0;
|
|
1122
|
+
const ids = [];
|
|
1123
|
+
for (const job of jobs) {
|
|
1124
|
+
if (job.idempotencyKey) {
|
|
1125
|
+
const id = returnedKeyToId.get(job.idempotencyKey);
|
|
1126
|
+
if (id === void 0) {
|
|
1127
|
+
throw new Error(
|
|
1128
|
+
`Failed to resolve job ID for idempotency key "${job.idempotencyKey}"`
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
ids.push(id);
|
|
1132
|
+
} else {
|
|
1133
|
+
ids.push(returnedNullKeyIds[nullKeyIdx++]);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
log(`Batch-inserted ${jobs.length} jobs, IDs: [${ids.join(", ")}]`);
|
|
1137
|
+
const newJobEvents = [];
|
|
1138
|
+
for (let i = 0; i < jobs.length; i++) {
|
|
1139
|
+
const job = jobs[i];
|
|
1140
|
+
const wasInserted = !job.idempotencyKey || !missingKeys.includes(job.idempotencyKey);
|
|
1141
|
+
if (wasInserted) {
|
|
1142
|
+
newJobEvents.push({
|
|
1143
|
+
jobId: ids[i],
|
|
1144
|
+
eventType: "added" /* Added */,
|
|
1145
|
+
metadata: {
|
|
1146
|
+
jobType: job.jobType,
|
|
1147
|
+
payload: job.payload,
|
|
1148
|
+
tags: job.tags,
|
|
1149
|
+
idempotencyKey: job.idempotencyKey
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
if (newJobEvents.length > 0) {
|
|
1155
|
+
if (externalClient) {
|
|
1156
|
+
const evtValues = [];
|
|
1157
|
+
const evtParams = [];
|
|
1158
|
+
let evtIdx = 1;
|
|
1159
|
+
for (const evt of newJobEvents) {
|
|
1160
|
+
evtValues.push(`($${evtIdx++}, $${evtIdx++}, $${evtIdx++})`);
|
|
1161
|
+
evtParams.push(
|
|
1162
|
+
evt.jobId,
|
|
1163
|
+
evt.eventType,
|
|
1164
|
+
evt.metadata ? JSON.stringify(evt.metadata) : null
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
try {
|
|
1168
|
+
await client.query(
|
|
1169
|
+
`INSERT INTO job_events (job_id, event_type, metadata) VALUES ${evtValues.join(", ")}`,
|
|
1170
|
+
evtParams
|
|
1171
|
+
);
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
log(`Error recording batch job events: ${error}`);
|
|
1174
|
+
}
|
|
1175
|
+
} else {
|
|
1176
|
+
await this.recordJobEventsBatch(newJobEvents);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return ids;
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
log(`Error batch-inserting jobs: ${error}`);
|
|
1182
|
+
throw error;
|
|
1183
|
+
} finally {
|
|
1184
|
+
if (!externalClient) client.release();
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
async getJob(id) {
|
|
1188
|
+
const client = await this.pool.connect();
|
|
1189
|
+
try {
|
|
1190
|
+
const result = await client.query(
|
|
1191
|
+
`SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", tags, idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax" FROM job_queue WHERE id = $1`,
|
|
1192
|
+
[id]
|
|
1193
|
+
);
|
|
1194
|
+
if (result.rows.length === 0) {
|
|
1195
|
+
log(`Job ${id} not found`);
|
|
1196
|
+
return null;
|
|
1197
|
+
}
|
|
1198
|
+
log(`Found job ${id}`);
|
|
1199
|
+
const job = result.rows[0];
|
|
1200
|
+
return {
|
|
1201
|
+
...job,
|
|
1202
|
+
payload: job.payload,
|
|
1203
|
+
timeoutMs: job.timeoutMs,
|
|
1204
|
+
forceKillOnTimeout: job.forceKillOnTimeout,
|
|
1205
|
+
failureReason: job.failureReason
|
|
1206
|
+
};
|
|
1207
|
+
} catch (error) {
|
|
1208
|
+
log(`Error getting job ${id}: ${error}`);
|
|
1209
|
+
throw error;
|
|
1210
|
+
} finally {
|
|
1211
|
+
client.release();
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
async getJobsByStatus(status, limit = 100, offset = 0) {
|
|
1215
|
+
const client = await this.pool.connect();
|
|
1216
|
+
try {
|
|
1217
|
+
const result = await client.query(
|
|
1218
|
+
`SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax" FROM job_queue WHERE status = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`,
|
|
1219
|
+
[status, limit, offset]
|
|
1220
|
+
);
|
|
1221
|
+
log(`Found ${result.rows.length} jobs by status ${status}`);
|
|
1222
|
+
return result.rows.map((job) => ({
|
|
1223
|
+
...job,
|
|
1224
|
+
payload: job.payload,
|
|
1225
|
+
timeoutMs: job.timeoutMs,
|
|
1226
|
+
forceKillOnTimeout: job.forceKillOnTimeout,
|
|
1227
|
+
failureReason: job.failureReason
|
|
1228
|
+
}));
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
log(`Error getting jobs by status ${status}: ${error}`);
|
|
1231
|
+
throw error;
|
|
1232
|
+
} finally {
|
|
1233
|
+
client.release();
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
async getAllJobs(limit = 100, offset = 0) {
|
|
1237
|
+
const client = await this.pool.connect();
|
|
1238
|
+
try {
|
|
1239
|
+
const result = await client.query(
|
|
1240
|
+
`SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax" FROM job_queue ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
|
|
1241
|
+
[limit, offset]
|
|
1242
|
+
);
|
|
1243
|
+
log(`Found ${result.rows.length} jobs (all)`);
|
|
1244
|
+
return result.rows.map((job) => ({
|
|
1245
|
+
...job,
|
|
1246
|
+
payload: job.payload,
|
|
1247
|
+
timeoutMs: job.timeoutMs,
|
|
1248
|
+
forceKillOnTimeout: job.forceKillOnTimeout
|
|
1249
|
+
}));
|
|
1250
|
+
} catch (error) {
|
|
1251
|
+
log(`Error getting all jobs: ${error}`);
|
|
1252
|
+
throw error;
|
|
1253
|
+
} finally {
|
|
1254
|
+
client.release();
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
async getJobs(filters, limit = 100, offset = 0) {
|
|
1258
|
+
const client = await this.pool.connect();
|
|
1259
|
+
try {
|
|
1260
|
+
let query = `SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", tags, idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax" FROM job_queue`;
|
|
1261
|
+
const params = [];
|
|
1262
|
+
const where = [];
|
|
1263
|
+
let paramIdx = 1;
|
|
1264
|
+
if (filters) {
|
|
1265
|
+
if (filters.jobType) {
|
|
1266
|
+
where.push(`job_type = $${paramIdx++}`);
|
|
1267
|
+
params.push(filters.jobType);
|
|
1268
|
+
}
|
|
1269
|
+
if (filters.priority !== void 0) {
|
|
1270
|
+
where.push(`priority = $${paramIdx++}`);
|
|
1271
|
+
params.push(filters.priority);
|
|
1272
|
+
}
|
|
1273
|
+
if (filters.runAt) {
|
|
1274
|
+
if (filters.runAt instanceof Date) {
|
|
1275
|
+
where.push(`run_at = $${paramIdx++}`);
|
|
1276
|
+
params.push(filters.runAt);
|
|
1277
|
+
} else if (typeof filters.runAt === "object" && (filters.runAt.gt !== void 0 || filters.runAt.gte !== void 0 || filters.runAt.lt !== void 0 || filters.runAt.lte !== void 0 || filters.runAt.eq !== void 0)) {
|
|
1278
|
+
const ops = filters.runAt;
|
|
1279
|
+
if (ops.gt) {
|
|
1280
|
+
where.push(`run_at > $${paramIdx++}`);
|
|
1281
|
+
params.push(ops.gt);
|
|
1282
|
+
}
|
|
1283
|
+
if (ops.gte) {
|
|
1284
|
+
where.push(`run_at >= $${paramIdx++}`);
|
|
1285
|
+
params.push(ops.gte);
|
|
1286
|
+
}
|
|
1287
|
+
if (ops.lt) {
|
|
1288
|
+
where.push(`run_at < $${paramIdx++}`);
|
|
1289
|
+
params.push(ops.lt);
|
|
1290
|
+
}
|
|
1291
|
+
if (ops.lte) {
|
|
1292
|
+
where.push(`run_at <= $${paramIdx++}`);
|
|
1293
|
+
params.push(ops.lte);
|
|
1294
|
+
}
|
|
1295
|
+
if (ops.eq) {
|
|
1296
|
+
where.push(`run_at = $${paramIdx++}`);
|
|
1297
|
+
params.push(ops.eq);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
if (filters.tags && filters.tags.values && filters.tags.values.length > 0) {
|
|
1302
|
+
const mode = filters.tags.mode || "all";
|
|
1303
|
+
const tagValues = filters.tags.values;
|
|
1304
|
+
switch (mode) {
|
|
1305
|
+
case "exact":
|
|
1306
|
+
where.push(`tags = $${paramIdx++}`);
|
|
1307
|
+
params.push(tagValues);
|
|
1308
|
+
break;
|
|
1309
|
+
case "all":
|
|
1310
|
+
where.push(`tags @> $${paramIdx++}`);
|
|
1311
|
+
params.push(tagValues);
|
|
1312
|
+
break;
|
|
1313
|
+
case "any":
|
|
1314
|
+
where.push(`tags && $${paramIdx++}`);
|
|
1315
|
+
params.push(tagValues);
|
|
1316
|
+
break;
|
|
1317
|
+
case "none":
|
|
1318
|
+
where.push(`NOT (tags && $${paramIdx++})`);
|
|
1319
|
+
params.push(tagValues);
|
|
1320
|
+
break;
|
|
1321
|
+
default:
|
|
1322
|
+
where.push(`tags @> $${paramIdx++}`);
|
|
1323
|
+
params.push(tagValues);
|
|
312
1324
|
}
|
|
313
1325
|
}
|
|
314
1326
|
if (filters.cursor !== void 0) {
|
|
@@ -346,7 +1358,7 @@ var PostgresBackend = class {
|
|
|
346
1358
|
async getJobsByTags(tags, mode = "all", limit = 100, offset = 0) {
|
|
347
1359
|
const client = await this.pool.connect();
|
|
348
1360
|
try {
|
|
349
|
-
let query = `SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", tags, idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress
|
|
1361
|
+
let query = `SELECT id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_failed_at AS "lastFailedAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", tags, idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax"
|
|
350
1362
|
FROM job_queue`;
|
|
351
1363
|
let params = [];
|
|
352
1364
|
switch (mode) {
|
|
@@ -440,7 +1452,7 @@ var PostgresBackend = class {
|
|
|
440
1452
|
LIMIT $2
|
|
441
1453
|
FOR UPDATE SKIP LOCKED
|
|
442
1454
|
)
|
|
443
|
-
RETURNING id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress
|
|
1455
|
+
RETURNING id, job_type AS "jobType", payload, status, max_attempts AS "maxAttempts", attempts, priority, run_at AS "runAt", timeout_ms AS "timeoutMs", force_kill_on_timeout AS "forceKillOnTimeout", created_at AS "createdAt", updated_at AS "updatedAt", started_at AS "startedAt", completed_at AS "completedAt", last_failed_at AS "lastFailedAt", locked_at AS "lockedAt", locked_by AS "lockedBy", error_history AS "errorHistory", failure_reason AS "failureReason", next_attempt_at AS "nextAttemptAt", last_retried_at AS "lastRetriedAt", last_cancelled_at AS "lastCancelledAt", pending_reason AS "pendingReason", idempotency_key AS "idempotencyKey", wait_until AS "waitUntil", wait_token_id AS "waitTokenId", step_data AS "stepData", progress, retry_delay AS "retryDelay", retry_backoff AS "retryBackoff", retry_delay_max AS "retryDelayMax"
|
|
444
1456
|
`,
|
|
445
1457
|
params
|
|
446
1458
|
);
|
|
@@ -502,9 +1514,17 @@ var PostgresBackend = class {
|
|
|
502
1514
|
UPDATE job_queue
|
|
503
1515
|
SET status = 'failed',
|
|
504
1516
|
updated_at = NOW(),
|
|
505
|
-
next_attempt_at = CASE
|
|
506
|
-
WHEN attempts
|
|
507
|
-
|
|
1517
|
+
next_attempt_at = CASE
|
|
1518
|
+
WHEN attempts >= max_attempts THEN NULL
|
|
1519
|
+
WHEN retry_delay IS NULL AND retry_backoff IS NULL AND retry_delay_max IS NULL
|
|
1520
|
+
THEN NOW() + (POWER(2, attempts) * INTERVAL '1 minute')
|
|
1521
|
+
WHEN COALESCE(retry_backoff, true) = true
|
|
1522
|
+
THEN NOW() + (LEAST(
|
|
1523
|
+
COALESCE(retry_delay_max, 2147483647),
|
|
1524
|
+
COALESCE(retry_delay, 60) * POWER(2, attempts)
|
|
1525
|
+
) * (0.5 + 0.5 * random()) * INTERVAL '1 second')
|
|
1526
|
+
ELSE
|
|
1527
|
+
NOW() + (COALESCE(retry_delay, 60) * INTERVAL '1 second')
|
|
508
1528
|
END,
|
|
509
1529
|
error_history = COALESCE(error_history, '[]'::jsonb) || $2::jsonb,
|
|
510
1530
|
failure_reason = $3,
|
|
@@ -742,6 +1762,18 @@ var PostgresBackend = class {
|
|
|
742
1762
|
updateFields.push(`tags = $${paramIdx++}`);
|
|
743
1763
|
params.push(updates.tags ?? null);
|
|
744
1764
|
}
|
|
1765
|
+
if (updates.retryDelay !== void 0) {
|
|
1766
|
+
updateFields.push(`retry_delay = $${paramIdx++}`);
|
|
1767
|
+
params.push(updates.retryDelay ?? null);
|
|
1768
|
+
}
|
|
1769
|
+
if (updates.retryBackoff !== void 0) {
|
|
1770
|
+
updateFields.push(`retry_backoff = $${paramIdx++}`);
|
|
1771
|
+
params.push(updates.retryBackoff ?? null);
|
|
1772
|
+
}
|
|
1773
|
+
if (updates.retryDelayMax !== void 0) {
|
|
1774
|
+
updateFields.push(`retry_delay_max = $${paramIdx++}`);
|
|
1775
|
+
params.push(updates.retryDelayMax ?? null);
|
|
1776
|
+
}
|
|
745
1777
|
if (updateFields.length === 0) {
|
|
746
1778
|
log(`No fields to update for job ${jobId}`);
|
|
747
1779
|
return;
|
|
@@ -763,6 +1795,12 @@ var PostgresBackend = class {
|
|
|
763
1795
|
if (updates.timeoutMs !== void 0)
|
|
764
1796
|
metadata.timeoutMs = updates.timeoutMs;
|
|
765
1797
|
if (updates.tags !== void 0) metadata.tags = updates.tags;
|
|
1798
|
+
if (updates.retryDelay !== void 0)
|
|
1799
|
+
metadata.retryDelay = updates.retryDelay;
|
|
1800
|
+
if (updates.retryBackoff !== void 0)
|
|
1801
|
+
metadata.retryBackoff = updates.retryBackoff;
|
|
1802
|
+
if (updates.retryDelayMax !== void 0)
|
|
1803
|
+
metadata.retryDelayMax = updates.retryDelayMax;
|
|
766
1804
|
await this.recordJobEvent(jobId, "edited" /* Edited */, metadata);
|
|
767
1805
|
log(`Edited job ${jobId}: ${JSON.stringify(metadata)}`);
|
|
768
1806
|
} catch (error) {
|
|
@@ -806,6 +1844,18 @@ var PostgresBackend = class {
|
|
|
806
1844
|
updateFields.push(`tags = $${paramIdx++}`);
|
|
807
1845
|
params.push(updates.tags ?? null);
|
|
808
1846
|
}
|
|
1847
|
+
if (updates.retryDelay !== void 0) {
|
|
1848
|
+
updateFields.push(`retry_delay = $${paramIdx++}`);
|
|
1849
|
+
params.push(updates.retryDelay ?? null);
|
|
1850
|
+
}
|
|
1851
|
+
if (updates.retryBackoff !== void 0) {
|
|
1852
|
+
updateFields.push(`retry_backoff = $${paramIdx++}`);
|
|
1853
|
+
params.push(updates.retryBackoff ?? null);
|
|
1854
|
+
}
|
|
1855
|
+
if (updates.retryDelayMax !== void 0) {
|
|
1856
|
+
updateFields.push(`retry_delay_max = $${paramIdx++}`);
|
|
1857
|
+
params.push(updates.retryDelayMax ?? null);
|
|
1858
|
+
}
|
|
809
1859
|
if (updateFields.length === 0) {
|
|
810
1860
|
log(`No fields to update for batch edit`);
|
|
811
1861
|
return 0;
|
|
@@ -902,45 +1952,85 @@ var PostgresBackend = class {
|
|
|
902
1952
|
client.release();
|
|
903
1953
|
}
|
|
904
1954
|
}
|
|
905
|
-
|
|
906
|
-
|
|
1955
|
+
/**
|
|
1956
|
+
* Delete completed jobs older than the given number of days.
|
|
1957
|
+
* Deletes in batches of 1000 to avoid long-running transactions
|
|
1958
|
+
* and excessive WAL bloat at scale.
|
|
1959
|
+
*
|
|
1960
|
+
* @param daysToKeep - Number of days to retain completed jobs (default 30).
|
|
1961
|
+
* @param batchSize - Number of rows to delete per batch (default 1000).
|
|
1962
|
+
* @returns Total number of deleted jobs.
|
|
1963
|
+
*/
|
|
1964
|
+
async cleanupOldJobs(daysToKeep = 30, batchSize = 1e3) {
|
|
1965
|
+
let totalDeleted = 0;
|
|
907
1966
|
try {
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
1967
|
+
let deletedInBatch;
|
|
1968
|
+
do {
|
|
1969
|
+
const client = await this.pool.connect();
|
|
1970
|
+
try {
|
|
1971
|
+
const result = await client.query(
|
|
1972
|
+
`
|
|
1973
|
+
DELETE FROM job_queue
|
|
1974
|
+
WHERE id IN (
|
|
1975
|
+
SELECT id FROM job_queue
|
|
1976
|
+
WHERE status = 'completed'
|
|
1977
|
+
AND updated_at < NOW() - INTERVAL '1 day' * $1::int
|
|
1978
|
+
LIMIT $2
|
|
1979
|
+
)
|
|
1980
|
+
`,
|
|
1981
|
+
[daysToKeep, batchSize]
|
|
1982
|
+
);
|
|
1983
|
+
deletedInBatch = result.rowCount || 0;
|
|
1984
|
+
totalDeleted += deletedInBatch;
|
|
1985
|
+
} finally {
|
|
1986
|
+
client.release();
|
|
1987
|
+
}
|
|
1988
|
+
} while (deletedInBatch === batchSize);
|
|
1989
|
+
log(`Deleted ${totalDeleted} old jobs`);
|
|
1990
|
+
return totalDeleted;
|
|
919
1991
|
} catch (error) {
|
|
920
1992
|
log(`Error cleaning up old jobs: ${error}`);
|
|
921
1993
|
throw error;
|
|
922
|
-
} finally {
|
|
923
|
-
client.release();
|
|
924
1994
|
}
|
|
925
1995
|
}
|
|
926
|
-
|
|
927
|
-
|
|
1996
|
+
/**
|
|
1997
|
+
* Delete job events older than the given number of days.
|
|
1998
|
+
* Deletes in batches of 1000 to avoid long-running transactions
|
|
1999
|
+
* and excessive WAL bloat at scale.
|
|
2000
|
+
*
|
|
2001
|
+
* @param daysToKeep - Number of days to retain events (default 30).
|
|
2002
|
+
* @param batchSize - Number of rows to delete per batch (default 1000).
|
|
2003
|
+
* @returns Total number of deleted events.
|
|
2004
|
+
*/
|
|
2005
|
+
async cleanupOldJobEvents(daysToKeep = 30, batchSize = 1e3) {
|
|
2006
|
+
let totalDeleted = 0;
|
|
928
2007
|
try {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
2008
|
+
let deletedInBatch;
|
|
2009
|
+
do {
|
|
2010
|
+
const client = await this.pool.connect();
|
|
2011
|
+
try {
|
|
2012
|
+
const result = await client.query(
|
|
2013
|
+
`
|
|
2014
|
+
DELETE FROM job_events
|
|
2015
|
+
WHERE id IN (
|
|
2016
|
+
SELECT id FROM job_events
|
|
2017
|
+
WHERE created_at < NOW() - INTERVAL '1 day' * $1::int
|
|
2018
|
+
LIMIT $2
|
|
2019
|
+
)
|
|
2020
|
+
`,
|
|
2021
|
+
[daysToKeep, batchSize]
|
|
2022
|
+
);
|
|
2023
|
+
deletedInBatch = result.rowCount || 0;
|
|
2024
|
+
totalDeleted += deletedInBatch;
|
|
2025
|
+
} finally {
|
|
2026
|
+
client.release();
|
|
2027
|
+
}
|
|
2028
|
+
} while (deletedInBatch === batchSize);
|
|
2029
|
+
log(`Deleted ${totalDeleted} old job events`);
|
|
2030
|
+
return totalDeleted;
|
|
939
2031
|
} catch (error) {
|
|
940
2032
|
log(`Error cleaning up old job events: ${error}`);
|
|
941
2033
|
throw error;
|
|
942
|
-
} finally {
|
|
943
|
-
client.release();
|
|
944
2034
|
}
|
|
945
2035
|
}
|
|
946
2036
|
async reclaimStuckJobs(maxProcessingTimeMinutes = 10) {
|
|
@@ -998,926 +2088,540 @@ var PostgresBackend = class {
|
|
|
998
2088
|
client.release();
|
|
999
2089
|
}
|
|
1000
2090
|
}
|
|
1001
|
-
|
|
2091
|
+
// ── Cron schedules ──────────────────────────────────────────────────
|
|
2092
|
+
/** Create a cron schedule and return its ID. */
|
|
2093
|
+
async addCronSchedule(input) {
|
|
1002
2094
|
const client = await this.pool.connect();
|
|
1003
2095
|
try {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
2096
|
+
const result = await client.query(
|
|
2097
|
+
`INSERT INTO cron_schedules
|
|
2098
|
+
(schedule_name, cron_expression, job_type, payload, max_attempts,
|
|
2099
|
+
priority, timeout_ms, force_kill_on_timeout, tags, timezone,
|
|
2100
|
+
allow_overlap, next_run_at, retry_delay, retry_backoff, retry_delay_max)
|
|
2101
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
|
2102
|
+
RETURNING id`,
|
|
2103
|
+
[
|
|
2104
|
+
input.scheduleName,
|
|
2105
|
+
input.cronExpression,
|
|
2106
|
+
input.jobType,
|
|
2107
|
+
input.payload,
|
|
2108
|
+
input.maxAttempts,
|
|
2109
|
+
input.priority,
|
|
2110
|
+
input.timeoutMs,
|
|
2111
|
+
input.forceKillOnTimeout,
|
|
2112
|
+
input.tags ?? null,
|
|
2113
|
+
input.timezone,
|
|
2114
|
+
input.allowOverlap,
|
|
2115
|
+
input.nextRunAt,
|
|
2116
|
+
input.retryDelay,
|
|
2117
|
+
input.retryBackoff,
|
|
2118
|
+
input.retryDelayMax
|
|
2119
|
+
]
|
|
1018
2120
|
);
|
|
2121
|
+
const id = result.rows[0].id;
|
|
2122
|
+
log(`Added cron schedule ${id}: "${input.scheduleName}"`);
|
|
2123
|
+
return id;
|
|
2124
|
+
} catch (error) {
|
|
2125
|
+
if (error?.code === "23505") {
|
|
2126
|
+
throw new Error(
|
|
2127
|
+
`Cron schedule with name "${input.scheduleName}" already exists`
|
|
2128
|
+
);
|
|
2129
|
+
}
|
|
2130
|
+
log(`Error adding cron schedule: ${error}`);
|
|
2131
|
+
throw error;
|
|
1019
2132
|
} finally {
|
|
1020
2133
|
client.release();
|
|
1021
2134
|
}
|
|
1022
2135
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
[
|
|
1041
|
-
jobId,
|
|
1042
|
-
options.waitUntil ?? null,
|
|
1043
|
-
options.waitTokenId ?? null,
|
|
1044
|
-
JSON.stringify(options.stepData)
|
|
1045
|
-
]
|
|
1046
|
-
);
|
|
1047
|
-
if (result.rowCount === 0) {
|
|
1048
|
-
log(
|
|
1049
|
-
`Job ${jobId} could not be set to waiting (may have been reclaimed or is no longer processing)`
|
|
2136
|
+
/** Get a cron schedule by ID. */
|
|
2137
|
+
async getCronSchedule(id) {
|
|
2138
|
+
const client = await this.pool.connect();
|
|
2139
|
+
try {
|
|
2140
|
+
const result = await client.query(
|
|
2141
|
+
`SELECT id, schedule_name AS "scheduleName", cron_expression AS "cronExpression",
|
|
2142
|
+
job_type AS "jobType", payload, max_attempts AS "maxAttempts",
|
|
2143
|
+
priority, timeout_ms AS "timeoutMs",
|
|
2144
|
+
force_kill_on_timeout AS "forceKillOnTimeout", tags,
|
|
2145
|
+
timezone, allow_overlap AS "allowOverlap", status,
|
|
2146
|
+
last_enqueued_at AS "lastEnqueuedAt", last_job_id AS "lastJobId",
|
|
2147
|
+
next_run_at AS "nextRunAt",
|
|
2148
|
+
created_at AS "createdAt", updated_at AS "updatedAt",
|
|
2149
|
+
retry_delay AS "retryDelay", retry_backoff AS "retryBackoff",
|
|
2150
|
+
retry_delay_max AS "retryDelayMax"
|
|
2151
|
+
FROM cron_schedules WHERE id = $1`,
|
|
2152
|
+
[id]
|
|
1050
2153
|
);
|
|
1051
|
-
return;
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
} catch (error) {
|
|
1059
|
-
log(`Error setting job ${jobId} to waiting: ${error}`);
|
|
1060
|
-
throw error;
|
|
1061
|
-
} finally {
|
|
1062
|
-
client.release();
|
|
1063
|
-
}
|
|
1064
|
-
};
|
|
1065
|
-
var updateStepData = async (pool, jobId, stepData) => {
|
|
1066
|
-
const client = await pool.connect();
|
|
1067
|
-
try {
|
|
1068
|
-
await client.query(
|
|
1069
|
-
`UPDATE job_queue SET step_data = $2, updated_at = NOW() WHERE id = $1`,
|
|
1070
|
-
[jobId, JSON.stringify(stepData)]
|
|
1071
|
-
);
|
|
1072
|
-
} catch (error) {
|
|
1073
|
-
log(`Error updating step_data for job ${jobId}: ${error}`);
|
|
1074
|
-
} finally {
|
|
1075
|
-
client.release();
|
|
1076
|
-
}
|
|
1077
|
-
};
|
|
1078
|
-
var MAX_TIMEOUT_MS = 365 * 24 * 60 * 60 * 1e3;
|
|
1079
|
-
function parseTimeoutString(timeout) {
|
|
1080
|
-
const match = timeout.match(/^(\d+)(s|m|h|d)$/);
|
|
1081
|
-
if (!match) {
|
|
1082
|
-
throw new Error(
|
|
1083
|
-
`Invalid timeout format: "${timeout}". Expected format like "10m", "1h", "24h", "7d".`
|
|
1084
|
-
);
|
|
1085
|
-
}
|
|
1086
|
-
const value = parseInt(match[1], 10);
|
|
1087
|
-
const unit = match[2];
|
|
1088
|
-
let ms;
|
|
1089
|
-
switch (unit) {
|
|
1090
|
-
case "s":
|
|
1091
|
-
ms = value * 1e3;
|
|
1092
|
-
break;
|
|
1093
|
-
case "m":
|
|
1094
|
-
ms = value * 60 * 1e3;
|
|
1095
|
-
break;
|
|
1096
|
-
case "h":
|
|
1097
|
-
ms = value * 60 * 60 * 1e3;
|
|
1098
|
-
break;
|
|
1099
|
-
case "d":
|
|
1100
|
-
ms = value * 24 * 60 * 60 * 1e3;
|
|
1101
|
-
break;
|
|
1102
|
-
default:
|
|
1103
|
-
throw new Error(`Unknown timeout unit: "${unit}"`);
|
|
1104
|
-
}
|
|
1105
|
-
if (!Number.isFinite(ms) || ms > MAX_TIMEOUT_MS) {
|
|
1106
|
-
throw new Error(
|
|
1107
|
-
`Timeout value "${timeout}" is too large. Maximum allowed is 365 days.`
|
|
1108
|
-
);
|
|
1109
|
-
}
|
|
1110
|
-
return ms;
|
|
1111
|
-
}
|
|
1112
|
-
var createWaitpoint = async (pool, jobId, options) => {
|
|
1113
|
-
const client = await pool.connect();
|
|
1114
|
-
try {
|
|
1115
|
-
const id = `wp_${randomUUID()}`;
|
|
1116
|
-
let timeoutAt = null;
|
|
1117
|
-
if (options?.timeout) {
|
|
1118
|
-
const ms = parseTimeoutString(options.timeout);
|
|
1119
|
-
timeoutAt = new Date(Date.now() + ms);
|
|
2154
|
+
if (result.rows.length === 0) return null;
|
|
2155
|
+
return result.rows[0];
|
|
2156
|
+
} catch (error) {
|
|
2157
|
+
log(`Error getting cron schedule ${id}: ${error}`);
|
|
2158
|
+
throw error;
|
|
2159
|
+
} finally {
|
|
2160
|
+
client.release();
|
|
1120
2161
|
}
|
|
1121
|
-
await client.query(
|
|
1122
|
-
`INSERT INTO waitpoints (id, job_id, status, timeout_at, tags) VALUES ($1, $2, 'waiting', $3, $4)`,
|
|
1123
|
-
[id, jobId, timeoutAt, options?.tags ?? null]
|
|
1124
|
-
);
|
|
1125
|
-
log(`Created waitpoint ${id} for job ${jobId}`);
|
|
1126
|
-
return { id };
|
|
1127
|
-
} catch (error) {
|
|
1128
|
-
log(`Error creating waitpoint: ${error}`);
|
|
1129
|
-
throw error;
|
|
1130
|
-
} finally {
|
|
1131
|
-
client.release();
|
|
1132
2162
|
}
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
if (jobId != null) {
|
|
1151
|
-
await client.query(
|
|
1152
|
-
`UPDATE job_queue
|
|
1153
|
-
SET status = 'pending', wait_token_id = NULL, wait_until = NULL, updated_at = NOW()
|
|
1154
|
-
WHERE id = $1 AND status = 'waiting'`,
|
|
1155
|
-
[jobId]
|
|
2163
|
+
/** Get a cron schedule by its unique name. */
|
|
2164
|
+
async getCronScheduleByName(name) {
|
|
2165
|
+
const client = await this.pool.connect();
|
|
2166
|
+
try {
|
|
2167
|
+
const result = await client.query(
|
|
2168
|
+
`SELECT id, schedule_name AS "scheduleName", cron_expression AS "cronExpression",
|
|
2169
|
+
job_type AS "jobType", payload, max_attempts AS "maxAttempts",
|
|
2170
|
+
priority, timeout_ms AS "timeoutMs",
|
|
2171
|
+
force_kill_on_timeout AS "forceKillOnTimeout", tags,
|
|
2172
|
+
timezone, allow_overlap AS "allowOverlap", status,
|
|
2173
|
+
last_enqueued_at AS "lastEnqueuedAt", last_job_id AS "lastJobId",
|
|
2174
|
+
next_run_at AS "nextRunAt",
|
|
2175
|
+
created_at AS "createdAt", updated_at AS "updatedAt",
|
|
2176
|
+
retry_delay AS "retryDelay", retry_backoff AS "retryBackoff",
|
|
2177
|
+
retry_delay_max AS "retryDelayMax"
|
|
2178
|
+
FROM cron_schedules WHERE schedule_name = $1`,
|
|
2179
|
+
[name]
|
|
1156
2180
|
);
|
|
2181
|
+
if (result.rows.length === 0) return null;
|
|
2182
|
+
return result.rows[0];
|
|
2183
|
+
} catch (error) {
|
|
2184
|
+
log(`Error getting cron schedule by name "${name}": ${error}`);
|
|
2185
|
+
throw error;
|
|
2186
|
+
} finally {
|
|
2187
|
+
client.release();
|
|
1157
2188
|
}
|
|
1158
|
-
await client.query("COMMIT");
|
|
1159
|
-
log(`Completed waitpoint ${tokenId} for job ${jobId}`);
|
|
1160
|
-
} catch (error) {
|
|
1161
|
-
await client.query("ROLLBACK");
|
|
1162
|
-
log(`Error completing waitpoint ${tokenId}: ${error}`);
|
|
1163
|
-
throw error;
|
|
1164
|
-
} finally {
|
|
1165
|
-
client.release();
|
|
1166
|
-
}
|
|
1167
|
-
};
|
|
1168
|
-
var getWaitpoint = async (pool, tokenId) => {
|
|
1169
|
-
const client = await pool.connect();
|
|
1170
|
-
try {
|
|
1171
|
-
const result = await client.query(
|
|
1172
|
-
`SELECT id, job_id AS "jobId", status, output, timeout_at AS "timeoutAt", created_at AS "createdAt", completed_at AS "completedAt", tags FROM waitpoints WHERE id = $1`,
|
|
1173
|
-
[tokenId]
|
|
1174
|
-
);
|
|
1175
|
-
if (result.rows.length === 0) return null;
|
|
1176
|
-
return result.rows[0];
|
|
1177
|
-
} catch (error) {
|
|
1178
|
-
log(`Error getting waitpoint ${tokenId}: ${error}`);
|
|
1179
|
-
throw error;
|
|
1180
|
-
} finally {
|
|
1181
|
-
client.release();
|
|
1182
2189
|
}
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
);
|
|
2190
|
+
/** List cron schedules, optionally filtered by status. */
|
|
2191
|
+
async listCronSchedules(status) {
|
|
2192
|
+
const client = await this.pool.connect();
|
|
2193
|
+
try {
|
|
2194
|
+
let query = `SELECT id, schedule_name AS "scheduleName", cron_expression AS "cronExpression",
|
|
2195
|
+
job_type AS "jobType", payload, max_attempts AS "maxAttempts",
|
|
2196
|
+
priority, timeout_ms AS "timeoutMs",
|
|
2197
|
+
force_kill_on_timeout AS "forceKillOnTimeout", tags,
|
|
2198
|
+
timezone, allow_overlap AS "allowOverlap", status,
|
|
2199
|
+
last_enqueued_at AS "lastEnqueuedAt", last_job_id AS "lastJobId",
|
|
2200
|
+
next_run_at AS "nextRunAt",
|
|
2201
|
+
created_at AS "createdAt", updated_at AS "updatedAt",
|
|
2202
|
+
retry_delay AS "retryDelay", retry_backoff AS "retryBackoff",
|
|
2203
|
+
retry_delay_max AS "retryDelayMax"
|
|
2204
|
+
FROM cron_schedules`;
|
|
2205
|
+
const params = [];
|
|
2206
|
+
if (status) {
|
|
2207
|
+
query += ` WHERE status = $1`;
|
|
2208
|
+
params.push(status);
|
|
1202
2209
|
}
|
|
2210
|
+
query += ` ORDER BY created_at ASC`;
|
|
2211
|
+
const result = await client.query(query, params);
|
|
2212
|
+
return result.rows;
|
|
2213
|
+
} catch (error) {
|
|
2214
|
+
log(`Error listing cron schedules: ${error}`);
|
|
2215
|
+
throw error;
|
|
2216
|
+
} finally {
|
|
2217
|
+
client.release();
|
|
1203
2218
|
}
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
}
|
|
1217
|
-
};
|
|
1218
|
-
function tryExtractPool(backend) {
|
|
1219
|
-
if (backend instanceof PostgresBackend) {
|
|
1220
|
-
return backend.getPool();
|
|
1221
|
-
}
|
|
1222
|
-
return null;
|
|
1223
|
-
}
|
|
1224
|
-
function buildBasicContext(backend, jobId, baseCtx) {
|
|
1225
|
-
const waitError = () => new Error(
|
|
1226
|
-
"Wait features (waitFor, waitUntil, createToken, waitForToken, ctx.run) are currently only supported with the PostgreSQL backend."
|
|
1227
|
-
);
|
|
1228
|
-
return {
|
|
1229
|
-
prolong: baseCtx.prolong,
|
|
1230
|
-
onTimeout: baseCtx.onTimeout,
|
|
1231
|
-
run: async (_stepName, fn) => {
|
|
1232
|
-
return fn();
|
|
1233
|
-
},
|
|
1234
|
-
waitFor: async () => {
|
|
1235
|
-
throw waitError();
|
|
1236
|
-
},
|
|
1237
|
-
waitUntil: async () => {
|
|
1238
|
-
throw waitError();
|
|
1239
|
-
},
|
|
1240
|
-
createToken: async () => {
|
|
1241
|
-
throw waitError();
|
|
1242
|
-
},
|
|
1243
|
-
waitForToken: async () => {
|
|
1244
|
-
throw waitError();
|
|
1245
|
-
},
|
|
1246
|
-
setProgress: async (percent) => {
|
|
1247
|
-
if (percent < 0 || percent > 100)
|
|
1248
|
-
throw new Error("Progress must be between 0 and 100");
|
|
1249
|
-
await backend.updateProgress(jobId, Math.round(percent));
|
|
1250
|
-
}
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
function validateHandlerSerializable(handler, jobType) {
|
|
1254
|
-
try {
|
|
1255
|
-
const handlerString = handler.toString();
|
|
1256
|
-
if (handlerString.includes("this.") && !handlerString.match(/\([^)]*this[^)]*\)/)) {
|
|
1257
|
-
throw new Error(
|
|
1258
|
-
`Handler for job type "${jobType}" uses 'this' context which cannot be serialized. Use a regular function or avoid 'this' references when forceKillOnTimeout is enabled.`
|
|
1259
|
-
);
|
|
1260
|
-
}
|
|
1261
|
-
if (handlerString.includes("[native code]")) {
|
|
1262
|
-
throw new Error(
|
|
1263
|
-
`Handler for job type "${jobType}" contains native code which cannot be serialized. Ensure your handler is a plain function when forceKillOnTimeout is enabled.`
|
|
1264
|
-
);
|
|
2219
|
+
}
|
|
2220
|
+
/** Delete a cron schedule by ID. */
|
|
2221
|
+
async removeCronSchedule(id) {
|
|
2222
|
+
const client = await this.pool.connect();
|
|
2223
|
+
try {
|
|
2224
|
+
await client.query(`DELETE FROM cron_schedules WHERE id = $1`, [id]);
|
|
2225
|
+
log(`Removed cron schedule ${id}`);
|
|
2226
|
+
} catch (error) {
|
|
2227
|
+
log(`Error removing cron schedule ${id}: ${error}`);
|
|
2228
|
+
throw error;
|
|
2229
|
+
} finally {
|
|
2230
|
+
client.release();
|
|
1265
2231
|
}
|
|
2232
|
+
}
|
|
2233
|
+
/** Pause a cron schedule. */
|
|
2234
|
+
async pauseCronSchedule(id) {
|
|
2235
|
+
const client = await this.pool.connect();
|
|
1266
2236
|
try {
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
`Handler for job type "${jobType}" cannot be serialized: ${parseError instanceof Error ? parseError.message : String(parseError)}. When using forceKillOnTimeout, handlers must be serializable functions without closures over external variables.`
|
|
2237
|
+
await client.query(
|
|
2238
|
+
`UPDATE cron_schedules SET status = 'paused', updated_at = NOW() WHERE id = $1`,
|
|
2239
|
+
[id]
|
|
1271
2240
|
);
|
|
2241
|
+
log(`Paused cron schedule ${id}`);
|
|
2242
|
+
} catch (error) {
|
|
2243
|
+
log(`Error pausing cron schedule ${id}: ${error}`);
|
|
2244
|
+
throw error;
|
|
2245
|
+
} finally {
|
|
2246
|
+
client.release();
|
|
1272
2247
|
}
|
|
1273
|
-
}
|
|
1274
|
-
|
|
2248
|
+
}
|
|
2249
|
+
/** Resume a paused cron schedule. */
|
|
2250
|
+
async resumeCronSchedule(id) {
|
|
2251
|
+
const client = await this.pool.connect();
|
|
2252
|
+
try {
|
|
2253
|
+
await client.query(
|
|
2254
|
+
`UPDATE cron_schedules SET status = 'active', updated_at = NOW() WHERE id = $1`,
|
|
2255
|
+
[id]
|
|
2256
|
+
);
|
|
2257
|
+
log(`Resumed cron schedule ${id}`);
|
|
2258
|
+
} catch (error) {
|
|
2259
|
+
log(`Error resuming cron schedule ${id}: ${error}`);
|
|
1275
2260
|
throw error;
|
|
2261
|
+
} finally {
|
|
2262
|
+
client.release();
|
|
1276
2263
|
}
|
|
1277
|
-
throw new Error(
|
|
1278
|
-
`Failed to validate handler serialization for job type "${jobType}": ${String(error)}`
|
|
1279
|
-
);
|
|
1280
2264
|
}
|
|
1281
|
-
|
|
1282
|
-
async
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
const controller = new AbortController();
|
|
1292
|
-
const signal = controller.signal;
|
|
1293
|
-
|
|
1294
|
-
// Set up timeout
|
|
1295
|
-
const timeoutId = setTimeout(() => {
|
|
1296
|
-
controller.abort();
|
|
1297
|
-
parentPort.postMessage({ type: 'timeout' });
|
|
1298
|
-
}, timeoutMs);
|
|
1299
|
-
|
|
1300
|
-
try {
|
|
1301
|
-
// Execute the handler
|
|
1302
|
-
// Note: This uses Function constructor which requires the handler to be serializable.
|
|
1303
|
-
// The handler should be validated before reaching this point.
|
|
1304
|
-
let handlerFn;
|
|
1305
|
-
try {
|
|
1306
|
-
// Wrap handlerCode in parentheses to ensure it's treated as an expression
|
|
1307
|
-
// This handles both arrow functions and regular functions
|
|
1308
|
-
const wrappedCode = handlerCode.trim().startsWith('async') || handlerCode.trim().startsWith('function')
|
|
1309
|
-
? handlerCode
|
|
1310
|
-
: '(' + handlerCode + ')';
|
|
1311
|
-
handlerFn = new Function('return ' + wrappedCode)();
|
|
1312
|
-
} catch (parseError) {
|
|
1313
|
-
clearTimeout(timeoutId);
|
|
1314
|
-
parentPort.postMessage({
|
|
1315
|
-
type: 'error',
|
|
1316
|
-
error: {
|
|
1317
|
-
message: 'Handler cannot be deserialized in worker thread. ' +
|
|
1318
|
-
'Ensure your handler is a standalone function without closures over external variables. ' +
|
|
1319
|
-
'Original error: ' + (parseError instanceof Error ? parseError.message : String(parseError)),
|
|
1320
|
-
stack: parseError instanceof Error ? parseError.stack : undefined,
|
|
1321
|
-
name: 'SerializationError',
|
|
1322
|
-
},
|
|
1323
|
-
});
|
|
1324
|
-
return;
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
// Ensure handlerFn is actually a function
|
|
1328
|
-
if (typeof handlerFn !== 'function') {
|
|
1329
|
-
clearTimeout(timeoutId);
|
|
1330
|
-
parentPort.postMessage({
|
|
1331
|
-
type: 'error',
|
|
1332
|
-
error: {
|
|
1333
|
-
message: 'Handler deserialization did not produce a function. ' +
|
|
1334
|
-
'Ensure your handler is a valid function when forceKillOnTimeout is enabled.',
|
|
1335
|
-
name: 'SerializationError',
|
|
1336
|
-
},
|
|
1337
|
-
});
|
|
1338
|
-
return;
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
handlerFn(payload, signal)
|
|
1342
|
-
.then(() => {
|
|
1343
|
-
clearTimeout(timeoutId);
|
|
1344
|
-
parentPort.postMessage({ type: 'success' });
|
|
1345
|
-
})
|
|
1346
|
-
.catch((error) => {
|
|
1347
|
-
clearTimeout(timeoutId);
|
|
1348
|
-
parentPort.postMessage({
|
|
1349
|
-
type: 'error',
|
|
1350
|
-
error: {
|
|
1351
|
-
message: error.message,
|
|
1352
|
-
stack: error.stack,
|
|
1353
|
-
name: error.name,
|
|
1354
|
-
},
|
|
1355
|
-
});
|
|
1356
|
-
});
|
|
1357
|
-
} catch (error) {
|
|
1358
|
-
clearTimeout(timeoutId);
|
|
1359
|
-
parentPort.postMessage({
|
|
1360
|
-
type: 'error',
|
|
1361
|
-
error: {
|
|
1362
|
-
message: error.message,
|
|
1363
|
-
stack: error.stack,
|
|
1364
|
-
name: error.name,
|
|
1365
|
-
},
|
|
1366
|
-
});
|
|
1367
|
-
}
|
|
1368
|
-
})();
|
|
1369
|
-
`;
|
|
1370
|
-
const worker = new Worker(workerCode, {
|
|
1371
|
-
eval: true,
|
|
1372
|
-
workerData: {
|
|
1373
|
-
handlerCode: handler.toString(),
|
|
1374
|
-
payload,
|
|
1375
|
-
timeoutMs
|
|
1376
|
-
}
|
|
1377
|
-
});
|
|
1378
|
-
let resolved = false;
|
|
1379
|
-
worker.on("message", (message) => {
|
|
1380
|
-
if (resolved) return;
|
|
1381
|
-
resolved = true;
|
|
1382
|
-
if (message.type === "success") {
|
|
1383
|
-
resolve();
|
|
1384
|
-
} else if (message.type === "timeout") {
|
|
1385
|
-
const timeoutError = new Error(
|
|
1386
|
-
`Job timed out after ${timeoutMs} ms and was forcefully terminated`
|
|
1387
|
-
);
|
|
1388
|
-
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
1389
|
-
reject(timeoutError);
|
|
1390
|
-
} else if (message.type === "error") {
|
|
1391
|
-
const error = new Error(message.error.message);
|
|
1392
|
-
error.stack = message.error.stack;
|
|
1393
|
-
error.name = message.error.name;
|
|
1394
|
-
reject(error);
|
|
1395
|
-
}
|
|
1396
|
-
});
|
|
1397
|
-
worker.on("error", (error) => {
|
|
1398
|
-
if (resolved) return;
|
|
1399
|
-
resolved = true;
|
|
1400
|
-
reject(error);
|
|
1401
|
-
});
|
|
1402
|
-
worker.on("exit", (code) => {
|
|
1403
|
-
if (resolved) return;
|
|
1404
|
-
if (code !== 0) {
|
|
1405
|
-
resolved = true;
|
|
1406
|
-
reject(new Error(`Worker stopped with exit code ${code}`));
|
|
1407
|
-
}
|
|
1408
|
-
});
|
|
1409
|
-
setTimeout(() => {
|
|
1410
|
-
if (!resolved) {
|
|
1411
|
-
resolved = true;
|
|
1412
|
-
worker.terminate().then(() => {
|
|
1413
|
-
const timeoutError = new Error(
|
|
1414
|
-
`Job timed out after ${timeoutMs} ms and was forcefully terminated`
|
|
1415
|
-
);
|
|
1416
|
-
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
1417
|
-
reject(timeoutError);
|
|
1418
|
-
}).catch((err) => {
|
|
1419
|
-
reject(err);
|
|
1420
|
-
});
|
|
2265
|
+
/** Edit a cron schedule. */
|
|
2266
|
+
async editCronSchedule(id, updates, nextRunAt) {
|
|
2267
|
+
const client = await this.pool.connect();
|
|
2268
|
+
try {
|
|
2269
|
+
const updateFields = [];
|
|
2270
|
+
const params = [];
|
|
2271
|
+
let paramIdx = 1;
|
|
2272
|
+
if (updates.cronExpression !== void 0) {
|
|
2273
|
+
updateFields.push(`cron_expression = $${paramIdx++}`);
|
|
2274
|
+
params.push(updates.cronExpression);
|
|
1421
2275
|
}
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
function calculateWaitUntil(duration) {
|
|
1426
|
-
const now = Date.now();
|
|
1427
|
-
let ms = 0;
|
|
1428
|
-
if (duration.seconds) ms += duration.seconds * 1e3;
|
|
1429
|
-
if (duration.minutes) ms += duration.minutes * 60 * 1e3;
|
|
1430
|
-
if (duration.hours) ms += duration.hours * 60 * 60 * 1e3;
|
|
1431
|
-
if (duration.days) ms += duration.days * 24 * 60 * 60 * 1e3;
|
|
1432
|
-
if (duration.weeks) ms += duration.weeks * 7 * 24 * 60 * 60 * 1e3;
|
|
1433
|
-
if (duration.months) ms += duration.months * 30 * 24 * 60 * 60 * 1e3;
|
|
1434
|
-
if (duration.years) ms += duration.years * 365 * 24 * 60 * 60 * 1e3;
|
|
1435
|
-
if (ms <= 0) {
|
|
1436
|
-
throw new Error(
|
|
1437
|
-
"waitFor duration must be positive. Provide at least one positive duration field."
|
|
1438
|
-
);
|
|
1439
|
-
}
|
|
1440
|
-
return new Date(now + ms);
|
|
1441
|
-
}
|
|
1442
|
-
async function resolveCompletedWaits(pool, stepData) {
|
|
1443
|
-
for (const key of Object.keys(stepData)) {
|
|
1444
|
-
if (!key.startsWith("__wait_")) continue;
|
|
1445
|
-
const entry = stepData[key];
|
|
1446
|
-
if (!entry || typeof entry !== "object" || entry.completed) continue;
|
|
1447
|
-
if (entry.type === "duration" || entry.type === "date") {
|
|
1448
|
-
stepData[key] = { ...entry, completed: true };
|
|
1449
|
-
} else if (entry.type === "token" && entry.tokenId) {
|
|
1450
|
-
const wp = await getWaitpoint(pool, entry.tokenId);
|
|
1451
|
-
if (wp && wp.status === "completed") {
|
|
1452
|
-
stepData[key] = {
|
|
1453
|
-
...entry,
|
|
1454
|
-
completed: true,
|
|
1455
|
-
result: { ok: true, output: wp.output }
|
|
1456
|
-
};
|
|
1457
|
-
} else if (wp && wp.status === "timed_out") {
|
|
1458
|
-
stepData[key] = {
|
|
1459
|
-
...entry,
|
|
1460
|
-
completed: true,
|
|
1461
|
-
result: { ok: false, error: "Token timed out" }
|
|
1462
|
-
};
|
|
2276
|
+
if (updates.payload !== void 0) {
|
|
2277
|
+
updateFields.push(`payload = $${paramIdx++}`);
|
|
2278
|
+
params.push(updates.payload);
|
|
1463
2279
|
}
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
function buildWaitContext(backend, pool, jobId, stepData, baseCtx) {
|
|
1468
|
-
let waitCounter = 0;
|
|
1469
|
-
const ctx = {
|
|
1470
|
-
prolong: baseCtx.prolong,
|
|
1471
|
-
onTimeout: baseCtx.onTimeout,
|
|
1472
|
-
run: async (stepName, fn) => {
|
|
1473
|
-
const cached = stepData[stepName];
|
|
1474
|
-
if (cached && typeof cached === "object" && cached.__completed) {
|
|
1475
|
-
log(`Step "${stepName}" replayed from cache for job ${jobId}`);
|
|
1476
|
-
return cached.result;
|
|
2280
|
+
if (updates.maxAttempts !== void 0) {
|
|
2281
|
+
updateFields.push(`max_attempts = $${paramIdx++}`);
|
|
2282
|
+
params.push(updates.maxAttempts);
|
|
1477
2283
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
return result;
|
|
1482
|
-
},
|
|
1483
|
-
waitFor: async (duration) => {
|
|
1484
|
-
const waitKey = `__wait_${waitCounter++}`;
|
|
1485
|
-
const cached = stepData[waitKey];
|
|
1486
|
-
if (cached && typeof cached === "object" && cached.completed) {
|
|
1487
|
-
log(`Wait "${waitKey}" already completed for job ${jobId}, skipping`);
|
|
1488
|
-
return;
|
|
2284
|
+
if (updates.priority !== void 0) {
|
|
2285
|
+
updateFields.push(`priority = $${paramIdx++}`);
|
|
2286
|
+
params.push(updates.priority);
|
|
1489
2287
|
}
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
},
|
|
1494
|
-
waitUntil: async (date) => {
|
|
1495
|
-
const waitKey = `__wait_${waitCounter++}`;
|
|
1496
|
-
const cached = stepData[waitKey];
|
|
1497
|
-
if (cached && typeof cached === "object" && cached.completed) {
|
|
1498
|
-
log(`Wait "${waitKey}" already completed for job ${jobId}, skipping`);
|
|
1499
|
-
return;
|
|
2288
|
+
if (updates.timeoutMs !== void 0) {
|
|
2289
|
+
updateFields.push(`timeout_ms = $${paramIdx++}`);
|
|
2290
|
+
params.push(updates.timeoutMs);
|
|
1500
2291
|
}
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
createToken: async (options) => {
|
|
1505
|
-
const token = await createWaitpoint(pool, jobId, options);
|
|
1506
|
-
return token;
|
|
1507
|
-
},
|
|
1508
|
-
waitForToken: async (tokenId) => {
|
|
1509
|
-
const waitKey = `__wait_${waitCounter++}`;
|
|
1510
|
-
const cached = stepData[waitKey];
|
|
1511
|
-
if (cached && typeof cached === "object" && cached.completed) {
|
|
1512
|
-
log(
|
|
1513
|
-
`Token wait "${waitKey}" already completed for job ${jobId}, returning cached result`
|
|
1514
|
-
);
|
|
1515
|
-
return cached.result;
|
|
2292
|
+
if (updates.forceKillOnTimeout !== void 0) {
|
|
2293
|
+
updateFields.push(`force_kill_on_timeout = $${paramIdx++}`);
|
|
2294
|
+
params.push(updates.forceKillOnTimeout);
|
|
1516
2295
|
}
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
ok: true,
|
|
1521
|
-
output: wp.output
|
|
1522
|
-
};
|
|
1523
|
-
stepData[waitKey] = {
|
|
1524
|
-
type: "token",
|
|
1525
|
-
tokenId,
|
|
1526
|
-
completed: true,
|
|
1527
|
-
result
|
|
1528
|
-
};
|
|
1529
|
-
await updateStepData(pool, jobId, stepData);
|
|
1530
|
-
return result;
|
|
2296
|
+
if (updates.tags !== void 0) {
|
|
2297
|
+
updateFields.push(`tags = $${paramIdx++}`);
|
|
2298
|
+
params.push(updates.tags);
|
|
1531
2299
|
}
|
|
1532
|
-
if (
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
error: "Token timed out"
|
|
1536
|
-
};
|
|
1537
|
-
stepData[waitKey] = {
|
|
1538
|
-
type: "token",
|
|
1539
|
-
tokenId,
|
|
1540
|
-
completed: true,
|
|
1541
|
-
result
|
|
1542
|
-
};
|
|
1543
|
-
await updateStepData(pool, jobId, stepData);
|
|
1544
|
-
return result;
|
|
2300
|
+
if (updates.timezone !== void 0) {
|
|
2301
|
+
updateFields.push(`timezone = $${paramIdx++}`);
|
|
2302
|
+
params.push(updates.timezone);
|
|
1545
2303
|
}
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
setProgress: async (percent) => {
|
|
1550
|
-
if (percent < 0 || percent > 100)
|
|
1551
|
-
throw new Error("Progress must be between 0 and 100");
|
|
1552
|
-
await backend.updateProgress(jobId, Math.round(percent));
|
|
1553
|
-
}
|
|
1554
|
-
};
|
|
1555
|
-
return ctx;
|
|
1556
|
-
}
|
|
1557
|
-
async function processJobWithHandlers(backend, job, jobHandlers) {
|
|
1558
|
-
const handler = jobHandlers[job.jobType];
|
|
1559
|
-
if (!handler) {
|
|
1560
|
-
await backend.setPendingReasonForUnpickedJobs(
|
|
1561
|
-
`No handler registered for job type: ${job.jobType}`,
|
|
1562
|
-
job.jobType
|
|
1563
|
-
);
|
|
1564
|
-
await backend.failJob(
|
|
1565
|
-
job.id,
|
|
1566
|
-
new Error(`No handler registered for job type: ${job.jobType}`),
|
|
1567
|
-
"no_handler" /* NoHandler */
|
|
1568
|
-
);
|
|
1569
|
-
return;
|
|
1570
|
-
}
|
|
1571
|
-
const stepData = { ...job.stepData || {} };
|
|
1572
|
-
const pool = tryExtractPool(backend);
|
|
1573
|
-
const hasStepHistory = Object.keys(stepData).some(
|
|
1574
|
-
(k) => k.startsWith("__wait_")
|
|
1575
|
-
);
|
|
1576
|
-
if (hasStepHistory && pool) {
|
|
1577
|
-
await resolveCompletedWaits(pool, stepData);
|
|
1578
|
-
await updateStepData(pool, job.id, stepData);
|
|
1579
|
-
}
|
|
1580
|
-
const timeoutMs = job.timeoutMs ?? void 0;
|
|
1581
|
-
const forceKillOnTimeout = job.forceKillOnTimeout ?? false;
|
|
1582
|
-
let timeoutId;
|
|
1583
|
-
const controller = new AbortController();
|
|
1584
|
-
try {
|
|
1585
|
-
if (forceKillOnTimeout && timeoutMs && timeoutMs > 0) {
|
|
1586
|
-
await runHandlerInWorker(handler, job.payload, timeoutMs, job.jobType);
|
|
1587
|
-
} else {
|
|
1588
|
-
let onTimeoutCallback;
|
|
1589
|
-
let timeoutReject;
|
|
1590
|
-
const armTimeout = (ms) => {
|
|
1591
|
-
if (timeoutId) clearTimeout(timeoutId);
|
|
1592
|
-
timeoutId = setTimeout(() => {
|
|
1593
|
-
if (onTimeoutCallback) {
|
|
1594
|
-
try {
|
|
1595
|
-
const extension = onTimeoutCallback();
|
|
1596
|
-
if (typeof extension === "number" && extension > 0) {
|
|
1597
|
-
backend.prolongJob(job.id).catch(() => {
|
|
1598
|
-
});
|
|
1599
|
-
armTimeout(extension);
|
|
1600
|
-
return;
|
|
1601
|
-
}
|
|
1602
|
-
} catch (callbackError) {
|
|
1603
|
-
log(
|
|
1604
|
-
`onTimeout callback threw for job ${job.id}: ${callbackError}`
|
|
1605
|
-
);
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
controller.abort();
|
|
1609
|
-
const timeoutError = new Error(`Job timed out after ${ms} ms`);
|
|
1610
|
-
timeoutError.failureReason = "timeout" /* Timeout */;
|
|
1611
|
-
if (timeoutReject) {
|
|
1612
|
-
timeoutReject(timeoutError);
|
|
1613
|
-
}
|
|
1614
|
-
}, ms);
|
|
1615
|
-
};
|
|
1616
|
-
const hasTimeout = timeoutMs != null && timeoutMs > 0;
|
|
1617
|
-
const baseCtx = hasTimeout ? {
|
|
1618
|
-
prolong: (ms) => {
|
|
1619
|
-
const duration = ms ?? timeoutMs;
|
|
1620
|
-
if (duration != null && duration > 0) {
|
|
1621
|
-
armTimeout(duration);
|
|
1622
|
-
backend.prolongJob(job.id).catch(() => {
|
|
1623
|
-
});
|
|
1624
|
-
}
|
|
1625
|
-
},
|
|
1626
|
-
onTimeout: (callback) => {
|
|
1627
|
-
onTimeoutCallback = callback;
|
|
1628
|
-
}
|
|
1629
|
-
} : {
|
|
1630
|
-
prolong: () => {
|
|
1631
|
-
log("prolong() called but ignored: job has no timeout set");
|
|
1632
|
-
},
|
|
1633
|
-
onTimeout: () => {
|
|
1634
|
-
log("onTimeout() called but ignored: job has no timeout set");
|
|
1635
|
-
}
|
|
1636
|
-
};
|
|
1637
|
-
const ctx = pool ? buildWaitContext(backend, pool, job.id, stepData, baseCtx) : buildBasicContext(backend, job.id, baseCtx);
|
|
1638
|
-
if (forceKillOnTimeout && !hasTimeout) {
|
|
1639
|
-
log(
|
|
1640
|
-
`forceKillOnTimeout is set but no timeoutMs for job ${job.id}, running without force kill`
|
|
1641
|
-
);
|
|
2304
|
+
if (updates.allowOverlap !== void 0) {
|
|
2305
|
+
updateFields.push(`allow_overlap = $${paramIdx++}`);
|
|
2306
|
+
params.push(updates.allowOverlap);
|
|
1642
2307
|
}
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
jobPromise,
|
|
1647
|
-
new Promise((_, reject) => {
|
|
1648
|
-
timeoutReject = reject;
|
|
1649
|
-
armTimeout(timeoutMs);
|
|
1650
|
-
})
|
|
1651
|
-
]);
|
|
1652
|
-
} else {
|
|
1653
|
-
await jobPromise;
|
|
2308
|
+
if (updates.retryDelay !== void 0) {
|
|
2309
|
+
updateFields.push(`retry_delay = $${paramIdx++}`);
|
|
2310
|
+
params.push(updates.retryDelay);
|
|
1654
2311
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
);
|
|
2312
|
+
if (updates.retryBackoff !== void 0) {
|
|
2313
|
+
updateFields.push(`retry_backoff = $${paramIdx++}`);
|
|
2314
|
+
params.push(updates.retryBackoff);
|
|
2315
|
+
}
|
|
2316
|
+
if (updates.retryDelayMax !== void 0) {
|
|
2317
|
+
updateFields.push(`retry_delay_max = $${paramIdx++}`);
|
|
2318
|
+
params.push(updates.retryDelayMax);
|
|
2319
|
+
}
|
|
2320
|
+
if (nextRunAt !== void 0) {
|
|
2321
|
+
updateFields.push(`next_run_at = $${paramIdx++}`);
|
|
2322
|
+
params.push(nextRunAt);
|
|
2323
|
+
}
|
|
2324
|
+
if (updateFields.length === 0) {
|
|
2325
|
+
log(`No fields to update for cron schedule ${id}`);
|
|
1669
2326
|
return;
|
|
1670
2327
|
}
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
)
|
|
1674
|
-
await
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
console.error(`Error processing job ${job.id}:`, error);
|
|
1682
|
-
let failureReason = "handler_error" /* HandlerError */;
|
|
1683
|
-
if (error && typeof error === "object" && "failureReason" in error && error.failureReason === "timeout" /* Timeout */) {
|
|
1684
|
-
failureReason = "timeout" /* Timeout */;
|
|
2328
|
+
updateFields.push(`updated_at = NOW()`);
|
|
2329
|
+
params.push(id);
|
|
2330
|
+
const query = `UPDATE cron_schedules SET ${updateFields.join(", ")} WHERE id = $${paramIdx}`;
|
|
2331
|
+
await client.query(query, params);
|
|
2332
|
+
log(`Edited cron schedule ${id}`);
|
|
2333
|
+
} catch (error) {
|
|
2334
|
+
log(`Error editing cron schedule ${id}: ${error}`);
|
|
2335
|
+
throw error;
|
|
2336
|
+
} finally {
|
|
2337
|
+
client.release();
|
|
1685
2338
|
}
|
|
1686
|
-
await backend.failJob(
|
|
1687
|
-
job.id,
|
|
1688
|
-
error instanceof Error ? error : new Error(String(error)),
|
|
1689
|
-
failureReason
|
|
1690
|
-
);
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
async function processBatchWithHandlers(backend, workerId, batchSize, jobType, jobHandlers, concurrency, onError) {
|
|
1694
|
-
const jobs = await backend.getNextBatch(
|
|
1695
|
-
workerId,
|
|
1696
|
-
batchSize,
|
|
1697
|
-
jobType
|
|
1698
|
-
);
|
|
1699
|
-
if (!concurrency || concurrency >= jobs.length) {
|
|
1700
|
-
await Promise.all(
|
|
1701
|
-
jobs.map((job) => processJobWithHandlers(backend, job, jobHandlers))
|
|
1702
|
-
);
|
|
1703
|
-
return jobs.length;
|
|
1704
2339
|
}
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2340
|
+
/**
|
|
2341
|
+
* Atomically fetch all active cron schedules whose nextRunAt <= NOW().
|
|
2342
|
+
* Uses FOR UPDATE SKIP LOCKED to prevent duplicate enqueuing across workers.
|
|
2343
|
+
*/
|
|
2344
|
+
async getDueCronSchedules() {
|
|
2345
|
+
const client = await this.pool.connect();
|
|
2346
|
+
try {
|
|
2347
|
+
const result = await client.query(
|
|
2348
|
+
`SELECT id, schedule_name AS "scheduleName", cron_expression AS "cronExpression",
|
|
2349
|
+
job_type AS "jobType", payload, max_attempts AS "maxAttempts",
|
|
2350
|
+
priority, timeout_ms AS "timeoutMs",
|
|
2351
|
+
force_kill_on_timeout AS "forceKillOnTimeout", tags,
|
|
2352
|
+
timezone, allow_overlap AS "allowOverlap", status,
|
|
2353
|
+
last_enqueued_at AS "lastEnqueuedAt", last_job_id AS "lastJobId",
|
|
2354
|
+
next_run_at AS "nextRunAt",
|
|
2355
|
+
created_at AS "createdAt", updated_at AS "updatedAt",
|
|
2356
|
+
retry_delay AS "retryDelay", retry_backoff AS "retryBackoff",
|
|
2357
|
+
retry_delay_max AS "retryDelayMax"
|
|
2358
|
+
FROM cron_schedules
|
|
2359
|
+
WHERE status = 'active'
|
|
2360
|
+
AND next_run_at IS NOT NULL
|
|
2361
|
+
AND next_run_at <= NOW()
|
|
2362
|
+
ORDER BY next_run_at ASC
|
|
2363
|
+
FOR UPDATE SKIP LOCKED`
|
|
2364
|
+
);
|
|
2365
|
+
log(`Found ${result.rows.length} due cron schedules`);
|
|
2366
|
+
return result.rows;
|
|
2367
|
+
} catch (error) {
|
|
2368
|
+
if (error?.code === "42P01") {
|
|
2369
|
+
log("cron_schedules table does not exist, skipping cron enqueue");
|
|
2370
|
+
return [];
|
|
1726
2371
|
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
} = options;
|
|
1740
|
-
let running = false;
|
|
1741
|
-
let intervalId = null;
|
|
1742
|
-
let currentBatchPromise = null;
|
|
1743
|
-
setLogContext(options.verbose ?? false);
|
|
1744
|
-
const processJobs = async () => {
|
|
1745
|
-
if (!running) return 0;
|
|
1746
|
-
log(
|
|
1747
|
-
`Processing jobs with workerId: ${workerId}${jobType ? ` and jobType: ${Array.isArray(jobType) ? jobType.join(",") : jobType}` : ""}`
|
|
1748
|
-
);
|
|
2372
|
+
log(`Error getting due cron schedules: ${error}`);
|
|
2373
|
+
throw error;
|
|
2374
|
+
} finally {
|
|
2375
|
+
client.release();
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
/**
|
|
2379
|
+
* Update a cron schedule after a job has been enqueued.
|
|
2380
|
+
* Sets lastEnqueuedAt, lastJobId, and advances nextRunAt.
|
|
2381
|
+
*/
|
|
2382
|
+
async updateCronScheduleAfterEnqueue(id, lastEnqueuedAt, lastJobId, nextRunAt) {
|
|
2383
|
+
const client = await this.pool.connect();
|
|
1749
2384
|
try {
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
2385
|
+
await client.query(
|
|
2386
|
+
`UPDATE cron_schedules
|
|
2387
|
+
SET last_enqueued_at = $2,
|
|
2388
|
+
last_job_id = $3,
|
|
2389
|
+
next_run_at = $4,
|
|
2390
|
+
updated_at = NOW()
|
|
2391
|
+
WHERE id = $1`,
|
|
2392
|
+
[id, lastEnqueuedAt, lastJobId, nextRunAt]
|
|
2393
|
+
);
|
|
2394
|
+
log(
|
|
2395
|
+
`Updated cron schedule ${id}: lastJobId=${lastJobId}, nextRunAt=${nextRunAt?.toISOString() ?? "null"}`
|
|
1758
2396
|
);
|
|
1759
|
-
return processed;
|
|
1760
2397
|
} catch (error) {
|
|
1761
|
-
|
|
2398
|
+
log(`Error updating cron schedule ${id} after enqueue: ${error}`);
|
|
2399
|
+
throw error;
|
|
2400
|
+
} finally {
|
|
2401
|
+
client.release();
|
|
1762
2402
|
}
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
const
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
if (intervalId) {
|
|
1800
|
-
clearTimeout(intervalId);
|
|
1801
|
-
intervalId = null;
|
|
1802
|
-
}
|
|
1803
|
-
},
|
|
1804
|
-
/**
|
|
1805
|
-
* Stop the job processor and wait for all in-flight jobs to complete.
|
|
1806
|
-
* Useful for graceful shutdown (e.g., SIGTERM handling).
|
|
1807
|
-
*/
|
|
1808
|
-
stopAndDrain: async (drainTimeoutMs = 3e4) => {
|
|
1809
|
-
log(`Stopping and draining job processor with workerId: ${workerId}`);
|
|
1810
|
-
running = false;
|
|
1811
|
-
if (intervalId) {
|
|
1812
|
-
clearTimeout(intervalId);
|
|
1813
|
-
intervalId = null;
|
|
1814
|
-
}
|
|
1815
|
-
if (currentBatchPromise) {
|
|
1816
|
-
await Promise.race([
|
|
1817
|
-
currentBatchPromise.catch(() => {
|
|
1818
|
-
}),
|
|
1819
|
-
new Promise((resolve) => setTimeout(resolve, drainTimeoutMs))
|
|
1820
|
-
]);
|
|
1821
|
-
currentBatchPromise = null;
|
|
2403
|
+
}
|
|
2404
|
+
// ── Wait / step-data support ────────────────────────────────────────
|
|
2405
|
+
/**
|
|
2406
|
+
* Transition a job from 'processing' to 'waiting' status.
|
|
2407
|
+
* Persists step data so the handler can resume from where it left off.
|
|
2408
|
+
*
|
|
2409
|
+
* @param jobId - The job to pause.
|
|
2410
|
+
* @param options - Wait configuration including optional waitUntil date, token ID, and step data.
|
|
2411
|
+
*/
|
|
2412
|
+
async waitJob(jobId, options) {
|
|
2413
|
+
const client = await this.pool.connect();
|
|
2414
|
+
try {
|
|
2415
|
+
const result = await client.query(
|
|
2416
|
+
`
|
|
2417
|
+
UPDATE job_queue
|
|
2418
|
+
SET status = 'waiting',
|
|
2419
|
+
wait_until = $2,
|
|
2420
|
+
wait_token_id = $3,
|
|
2421
|
+
step_data = $4,
|
|
2422
|
+
locked_at = NULL,
|
|
2423
|
+
locked_by = NULL,
|
|
2424
|
+
updated_at = NOW()
|
|
2425
|
+
WHERE id = $1 AND status = 'processing'
|
|
2426
|
+
`,
|
|
2427
|
+
[
|
|
2428
|
+
jobId,
|
|
2429
|
+
options.waitUntil ?? null,
|
|
2430
|
+
options.waitTokenId ?? null,
|
|
2431
|
+
JSON.stringify(options.stepData)
|
|
2432
|
+
]
|
|
2433
|
+
);
|
|
2434
|
+
if (result.rowCount === 0) {
|
|
2435
|
+
log(
|
|
2436
|
+
`Job ${jobId} could not be set to waiting (may have been reclaimed or is no longer processing)`
|
|
2437
|
+
);
|
|
2438
|
+
return;
|
|
1822
2439
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
running = false;
|
|
1835
|
-
return processed;
|
|
1836
|
-
},
|
|
1837
|
-
isRunning: () => running
|
|
1838
|
-
};
|
|
1839
|
-
};
|
|
1840
|
-
function loadPemOrFile(value) {
|
|
1841
|
-
if (!value) return void 0;
|
|
1842
|
-
if (value.startsWith("file://")) {
|
|
1843
|
-
const filePath = value.slice(7);
|
|
1844
|
-
return fs.readFileSync(filePath, "utf8");
|
|
2440
|
+
await this.recordJobEvent(jobId, "waiting" /* Waiting */, {
|
|
2441
|
+
waitUntil: options.waitUntil?.toISOString() ?? null,
|
|
2442
|
+
waitTokenId: options.waitTokenId ?? null
|
|
2443
|
+
});
|
|
2444
|
+
log(`Job ${jobId} set to waiting`);
|
|
2445
|
+
} catch (error) {
|
|
2446
|
+
log(`Error setting job ${jobId} to waiting: ${error}`);
|
|
2447
|
+
throw error;
|
|
2448
|
+
} finally {
|
|
2449
|
+
client.release();
|
|
2450
|
+
}
|
|
1845
2451
|
}
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
2452
|
+
/**
|
|
2453
|
+
* Persist step data for a job. Called after each ctx.run() step completes.
|
|
2454
|
+
* Best-effort: does not throw to avoid killing the running handler.
|
|
2455
|
+
*
|
|
2456
|
+
* @param jobId - The job to update.
|
|
2457
|
+
* @param stepData - The step data to persist.
|
|
2458
|
+
*/
|
|
2459
|
+
async updateStepData(jobId, stepData) {
|
|
2460
|
+
const client = await this.pool.connect();
|
|
1854
2461
|
try {
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
2462
|
+
await client.query(
|
|
2463
|
+
`UPDATE job_queue SET step_data = $2, updated_at = NOW() WHERE id = $1`,
|
|
2464
|
+
[jobId, JSON.stringify(stepData)]
|
|
2465
|
+
);
|
|
2466
|
+
} catch (error) {
|
|
2467
|
+
log(`Error updating step_data for job ${jobId}: ${error}`);
|
|
2468
|
+
} finally {
|
|
2469
|
+
client.release();
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
/**
|
|
2473
|
+
* Create a waitpoint token in the database.
|
|
2474
|
+
*
|
|
2475
|
+
* @param jobId - The job ID to associate with the token (null if created outside a handler).
|
|
2476
|
+
* @param options - Optional timeout string (e.g. '10m', '1h') and tags.
|
|
2477
|
+
* @returns The created waitpoint with its unique ID.
|
|
2478
|
+
*/
|
|
2479
|
+
async createWaitpoint(jobId, options) {
|
|
2480
|
+
const client = await this.pool.connect();
|
|
2481
|
+
try {
|
|
2482
|
+
const id = `wp_${randomUUID()}`;
|
|
2483
|
+
let timeoutAt = null;
|
|
2484
|
+
if (options?.timeout) {
|
|
2485
|
+
const ms = parseTimeoutString(options.timeout);
|
|
2486
|
+
timeoutAt = new Date(Date.now() + ms);
|
|
1860
2487
|
}
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
2488
|
+
await client.query(
|
|
2489
|
+
`INSERT INTO waitpoints (id, job_id, status, timeout_at, tags) VALUES ($1, $2, 'waiting', $3, $4)`,
|
|
2490
|
+
[id, jobId, timeoutAt, options?.tags ?? null]
|
|
2491
|
+
);
|
|
2492
|
+
log(`Created waitpoint ${id} for job ${jobId}`);
|
|
2493
|
+
return { id };
|
|
2494
|
+
} catch (error) {
|
|
2495
|
+
log(`Error creating waitpoint: ${error}`);
|
|
2496
|
+
throw error;
|
|
2497
|
+
} finally {
|
|
2498
|
+
client.release();
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
/**
|
|
2502
|
+
* Complete a waitpoint token and move the associated job back to 'pending'.
|
|
2503
|
+
*
|
|
2504
|
+
* @param tokenId - The waitpoint token ID to complete.
|
|
2505
|
+
* @param data - Optional data to pass to the waiting handler.
|
|
2506
|
+
*/
|
|
2507
|
+
async completeWaitpoint(tokenId, data) {
|
|
2508
|
+
const client = await this.pool.connect();
|
|
2509
|
+
try {
|
|
2510
|
+
await client.query("BEGIN");
|
|
2511
|
+
const wpResult = await client.query(
|
|
2512
|
+
`UPDATE waitpoints SET status = 'completed', output = $2, completed_at = NOW()
|
|
2513
|
+
WHERE id = $1 AND status = 'waiting'
|
|
2514
|
+
RETURNING job_id`,
|
|
2515
|
+
[tokenId, data != null ? JSON.stringify(data) : null]
|
|
2516
|
+
);
|
|
2517
|
+
if (wpResult.rows.length === 0) {
|
|
2518
|
+
await client.query("ROLLBACK");
|
|
2519
|
+
log(`Waitpoint ${tokenId} not found or already completed`);
|
|
2520
|
+
return;
|
|
1868
2521
|
}
|
|
1869
|
-
|
|
1870
|
-
if (
|
|
1871
|
-
|
|
2522
|
+
const jobId = wpResult.rows[0].job_id;
|
|
2523
|
+
if (jobId != null) {
|
|
2524
|
+
await client.query(
|
|
2525
|
+
`UPDATE job_queue
|
|
2526
|
+
SET status = 'pending', wait_token_id = NULL, wait_until = NULL, updated_at = NOW()
|
|
2527
|
+
WHERE id = $1 AND status = 'waiting'`,
|
|
2528
|
+
[jobId]
|
|
2529
|
+
);
|
|
1872
2530
|
}
|
|
2531
|
+
await client.query("COMMIT");
|
|
2532
|
+
log(`Completed waitpoint ${tokenId} for job ${jobId}`);
|
|
2533
|
+
} catch (error) {
|
|
2534
|
+
await client.query("ROLLBACK");
|
|
2535
|
+
log(`Error completing waitpoint ${tokenId}: ${error}`);
|
|
2536
|
+
throw error;
|
|
2537
|
+
} finally {
|
|
2538
|
+
client.release();
|
|
1873
2539
|
}
|
|
1874
2540
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
2541
|
+
/**
|
|
2542
|
+
* Retrieve a waitpoint token by its ID.
|
|
2543
|
+
*
|
|
2544
|
+
* @param tokenId - The waitpoint token ID to look up.
|
|
2545
|
+
* @returns The waitpoint record, or null if not found.
|
|
2546
|
+
*/
|
|
2547
|
+
async getWaitpoint(tokenId) {
|
|
2548
|
+
const client = await this.pool.connect();
|
|
2549
|
+
try {
|
|
2550
|
+
const result = await client.query(
|
|
2551
|
+
`SELECT id, job_id AS "jobId", status, output, timeout_at AS "timeoutAt", created_at AS "createdAt", completed_at AS "completedAt", tags FROM waitpoints WHERE id = $1`,
|
|
2552
|
+
[tokenId]
|
|
2553
|
+
);
|
|
2554
|
+
if (result.rows.length === 0) return null;
|
|
2555
|
+
return result.rows[0];
|
|
2556
|
+
} catch (error) {
|
|
2557
|
+
log(`Error getting waitpoint ${tokenId}: ${error}`);
|
|
2558
|
+
throw error;
|
|
2559
|
+
} finally {
|
|
2560
|
+
client.release();
|
|
1882
2561
|
}
|
|
1883
|
-
const caValue = typeof customCA === "string" ? loadPemOrFile(customCA) : void 0;
|
|
1884
|
-
ssl = {
|
|
1885
|
-
...ssl,
|
|
1886
|
-
...caValue ? { ca: caValue } : {},
|
|
1887
|
-
cert: loadPemOrFile(
|
|
1888
|
-
typeof config.ssl.cert === "string" ? config.ssl.cert : process.env.PGSSLCERT
|
|
1889
|
-
),
|
|
1890
|
-
key: loadPemOrFile(
|
|
1891
|
-
typeof config.ssl.key === "string" ? config.ssl.key : process.env.PGSSLKEY
|
|
1892
|
-
),
|
|
1893
|
-
rejectUnauthorized: config.ssl.rejectUnauthorized !== void 0 ? config.ssl.rejectUnauthorized : true
|
|
1894
|
-
};
|
|
1895
|
-
}
|
|
1896
|
-
if (sslmode && customCA) {
|
|
1897
|
-
const warning = `
|
|
1898
|
-
|
|
1899
|
-
\x1B[33m**************************************************
|
|
1900
|
-
\u26A0\uFE0F WARNING: SSL CONFIGURATION ISSUE
|
|
1901
|
-
**************************************************
|
|
1902
|
-
Both sslmode ('${sslmode}') is set in the connection string
|
|
1903
|
-
and a custom CA is provided (via config.ssl.ca or PGSSLROOTCERT).
|
|
1904
|
-
This combination may cause connection failures or unexpected behavior.
|
|
1905
|
-
|
|
1906
|
-
Recommended: Remove sslmode from the connection string when using a custom CA.
|
|
1907
|
-
**************************************************\x1B[0m
|
|
1908
|
-
`;
|
|
1909
|
-
console.warn(warning);
|
|
1910
2562
|
}
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
2563
|
+
/**
|
|
2564
|
+
* Expire timed-out waitpoint tokens and move their associated jobs back to 'pending'.
|
|
2565
|
+
*
|
|
2566
|
+
* @returns The number of tokens that were expired.
|
|
2567
|
+
*/
|
|
2568
|
+
async expireTimedOutWaitpoints() {
|
|
2569
|
+
const client = await this.pool.connect();
|
|
2570
|
+
try {
|
|
2571
|
+
await client.query("BEGIN");
|
|
2572
|
+
const result = await client.query(
|
|
2573
|
+
`UPDATE waitpoints
|
|
2574
|
+
SET status = 'timed_out'
|
|
2575
|
+
WHERE status = 'waiting' AND timeout_at IS NOT NULL AND timeout_at <= NOW()
|
|
2576
|
+
RETURNING id, job_id`
|
|
2577
|
+
);
|
|
2578
|
+
for (const row of result.rows) {
|
|
2579
|
+
if (row.job_id != null) {
|
|
2580
|
+
await client.query(
|
|
2581
|
+
`UPDATE job_queue
|
|
2582
|
+
SET status = 'pending', wait_token_id = NULL, wait_until = NULL, updated_at = NOW()
|
|
2583
|
+
WHERE id = $1 AND status = 'waiting'`,
|
|
2584
|
+
[row.job_id]
|
|
2585
|
+
);
|
|
2586
|
+
}
|
|
2587
|
+
}
|
|
2588
|
+
await client.query("COMMIT");
|
|
2589
|
+
const count = result.rowCount || 0;
|
|
2590
|
+
if (count > 0) {
|
|
2591
|
+
log(`Expired ${count} timed-out waitpoints`);
|
|
2592
|
+
}
|
|
2593
|
+
return count;
|
|
2594
|
+
} catch (error) {
|
|
2595
|
+
await client.query("ROLLBACK");
|
|
2596
|
+
log(`Error expiring timed-out waitpoints: ${error}`);
|
|
2597
|
+
throw error;
|
|
2598
|
+
} finally {
|
|
2599
|
+
client.release();
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
// ── Internal helpers ──────────────────────────────────────────────────
|
|
2603
|
+
async setPendingReasonForUnpickedJobs(reason, jobType) {
|
|
2604
|
+
const client = await this.pool.connect();
|
|
2605
|
+
try {
|
|
2606
|
+
let jobTypeFilter = "";
|
|
2607
|
+
const params = [reason];
|
|
2608
|
+
if (jobType) {
|
|
2609
|
+
if (Array.isArray(jobType)) {
|
|
2610
|
+
jobTypeFilter = ` AND job_type = ANY($2)`;
|
|
2611
|
+
params.push(jobType);
|
|
2612
|
+
} else {
|
|
2613
|
+
jobTypeFilter = ` AND job_type = $2`;
|
|
2614
|
+
params.push(jobType);
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
await client.query(
|
|
2618
|
+
`UPDATE job_queue SET pending_reason = $1 WHERE status = 'pending'${jobTypeFilter}`,
|
|
2619
|
+
params
|
|
2620
|
+
);
|
|
2621
|
+
} finally {
|
|
2622
|
+
client.release();
|
|
2623
|
+
}
|
|
1919
2624
|
}
|
|
1920
|
-
return pool;
|
|
1921
2625
|
};
|
|
1922
2626
|
|
|
1923
2627
|
// src/backends/redis-scripts.ts
|
|
@@ -1934,6 +2638,9 @@ local forceKillOnTimeout = ARGV[7]
|
|
|
1934
2638
|
local tagsJson = ARGV[8] -- "null" or JSON array string
|
|
1935
2639
|
local idempotencyKey = ARGV[9] -- "null" string if not set
|
|
1936
2640
|
local nowMs = tonumber(ARGV[10])
|
|
2641
|
+
local retryDelay = ARGV[11] -- "null" or seconds string
|
|
2642
|
+
local retryBackoff = ARGV[12] -- "null" or "true"/"false"
|
|
2643
|
+
local retryDelayMax = ARGV[13] -- "null" or seconds string
|
|
1937
2644
|
|
|
1938
2645
|
-- Idempotency check
|
|
1939
2646
|
if idempotencyKey ~= "null" then
|
|
@@ -1974,7 +2681,13 @@ redis.call('HMSET', jobKey,
|
|
|
1974
2681
|
'lastFailedAt', 'null',
|
|
1975
2682
|
'lastCancelledAt', 'null',
|
|
1976
2683
|
'tags', tagsJson,
|
|
1977
|
-
'idempotencyKey', idempotencyKey
|
|
2684
|
+
'idempotencyKey', idempotencyKey,
|
|
2685
|
+
'waitUntil', 'null',
|
|
2686
|
+
'waitTokenId', 'null',
|
|
2687
|
+
'stepData', 'null',
|
|
2688
|
+
'retryDelay', retryDelay,
|
|
2689
|
+
'retryBackoff', retryBackoff,
|
|
2690
|
+
'retryDelayMax', retryDelayMax
|
|
1978
2691
|
)
|
|
1979
2692
|
|
|
1980
2693
|
-- Status index
|
|
@@ -2015,6 +2728,118 @@ end
|
|
|
2015
2728
|
|
|
2016
2729
|
return id
|
|
2017
2730
|
`;
|
|
2731
|
+
var ADD_JOBS_SCRIPT = `
|
|
2732
|
+
local prefix = KEYS[1]
|
|
2733
|
+
local jobsJson = ARGV[1]
|
|
2734
|
+
local nowMs = tonumber(ARGV[2])
|
|
2735
|
+
|
|
2736
|
+
local jobs = cjson.decode(jobsJson)
|
|
2737
|
+
local results = {}
|
|
2738
|
+
|
|
2739
|
+
for i, job in ipairs(jobs) do
|
|
2740
|
+
local jobType = job.jobType
|
|
2741
|
+
local payloadJson = job.payload
|
|
2742
|
+
local maxAttempts = tonumber(job.maxAttempts)
|
|
2743
|
+
local priority = tonumber(job.priority)
|
|
2744
|
+
local runAtMs = tostring(job.runAtMs)
|
|
2745
|
+
local timeoutMs = tostring(job.timeoutMs)
|
|
2746
|
+
local forceKillOnTimeout = tostring(job.forceKillOnTimeout)
|
|
2747
|
+
local tagsJson = tostring(job.tags)
|
|
2748
|
+
local idempotencyKey = tostring(job.idempotencyKey)
|
|
2749
|
+
local retryDelay = tostring(job.retryDelay)
|
|
2750
|
+
local retryBackoff = tostring(job.retryBackoff)
|
|
2751
|
+
local retryDelayMax = tostring(job.retryDelayMax)
|
|
2752
|
+
|
|
2753
|
+
-- Idempotency check
|
|
2754
|
+
local skip = false
|
|
2755
|
+
if idempotencyKey ~= "null" then
|
|
2756
|
+
local existing = redis.call('GET', prefix .. 'idempotency:' .. idempotencyKey)
|
|
2757
|
+
if existing then
|
|
2758
|
+
results[i] = tonumber(existing)
|
|
2759
|
+
skip = true
|
|
2760
|
+
end
|
|
2761
|
+
end
|
|
2762
|
+
|
|
2763
|
+
if not skip then
|
|
2764
|
+
-- Generate ID
|
|
2765
|
+
local id = redis.call('INCR', prefix .. 'id_seq')
|
|
2766
|
+
local jobKey = prefix .. 'job:' .. id
|
|
2767
|
+
local runAt = runAtMs ~= "0" and tonumber(runAtMs) or nowMs
|
|
2768
|
+
|
|
2769
|
+
-- Store the job hash
|
|
2770
|
+
redis.call('HMSET', jobKey,
|
|
2771
|
+
'id', id,
|
|
2772
|
+
'jobType', jobType,
|
|
2773
|
+
'payload', payloadJson,
|
|
2774
|
+
'status', 'pending',
|
|
2775
|
+
'maxAttempts', maxAttempts,
|
|
2776
|
+
'attempts', 0,
|
|
2777
|
+
'priority', priority,
|
|
2778
|
+
'runAt', runAt,
|
|
2779
|
+
'timeoutMs', timeoutMs,
|
|
2780
|
+
'forceKillOnTimeout', forceKillOnTimeout,
|
|
2781
|
+
'createdAt', nowMs,
|
|
2782
|
+
'updatedAt', nowMs,
|
|
2783
|
+
'lockedAt', 'null',
|
|
2784
|
+
'lockedBy', 'null',
|
|
2785
|
+
'nextAttemptAt', 'null',
|
|
2786
|
+
'pendingReason', 'null',
|
|
2787
|
+
'errorHistory', '[]',
|
|
2788
|
+
'failureReason', 'null',
|
|
2789
|
+
'completedAt', 'null',
|
|
2790
|
+
'startedAt', 'null',
|
|
2791
|
+
'lastRetriedAt', 'null',
|
|
2792
|
+
'lastFailedAt', 'null',
|
|
2793
|
+
'lastCancelledAt', 'null',
|
|
2794
|
+
'tags', tagsJson,
|
|
2795
|
+
'idempotencyKey', idempotencyKey,
|
|
2796
|
+
'waitUntil', 'null',
|
|
2797
|
+
'waitTokenId', 'null',
|
|
2798
|
+
'stepData', 'null',
|
|
2799
|
+
'retryDelay', retryDelay,
|
|
2800
|
+
'retryBackoff', retryBackoff,
|
|
2801
|
+
'retryDelayMax', retryDelayMax
|
|
2802
|
+
)
|
|
2803
|
+
|
|
2804
|
+
-- Status index
|
|
2805
|
+
redis.call('SADD', prefix .. 'status:pending', id)
|
|
2806
|
+
|
|
2807
|
+
-- Type index
|
|
2808
|
+
redis.call('SADD', prefix .. 'type:' .. jobType, id)
|
|
2809
|
+
|
|
2810
|
+
-- Tag indexes
|
|
2811
|
+
if tagsJson ~= "null" then
|
|
2812
|
+
local tags = cjson.decode(tagsJson)
|
|
2813
|
+
for _, tag in ipairs(tags) do
|
|
2814
|
+
redis.call('SADD', prefix .. 'tag:' .. tag, id)
|
|
2815
|
+
end
|
|
2816
|
+
for _, tag in ipairs(tags) do
|
|
2817
|
+
redis.call('SADD', prefix .. 'job:' .. id .. ':tags', tag)
|
|
2818
|
+
end
|
|
2819
|
+
end
|
|
2820
|
+
|
|
2821
|
+
-- Idempotency mapping
|
|
2822
|
+
if idempotencyKey ~= "null" then
|
|
2823
|
+
redis.call('SET', prefix .. 'idempotency:' .. idempotencyKey, id)
|
|
2824
|
+
end
|
|
2825
|
+
|
|
2826
|
+
-- All-jobs sorted set
|
|
2827
|
+
redis.call('ZADD', prefix .. 'all', nowMs, id)
|
|
2828
|
+
|
|
2829
|
+
-- Queue or delayed
|
|
2830
|
+
if runAt <= nowMs then
|
|
2831
|
+
local score = priority * ${SCORE_RANGE} + (${SCORE_RANGE} - nowMs)
|
|
2832
|
+
redis.call('ZADD', prefix .. 'queue', score, id)
|
|
2833
|
+
else
|
|
2834
|
+
redis.call('ZADD', prefix .. 'delayed', runAt, id)
|
|
2835
|
+
end
|
|
2836
|
+
|
|
2837
|
+
results[i] = id
|
|
2838
|
+
end
|
|
2839
|
+
end
|
|
2840
|
+
|
|
2841
|
+
return results
|
|
2842
|
+
`;
|
|
2018
2843
|
var GET_NEXT_BATCH_SCRIPT = `
|
|
2019
2844
|
local prefix = KEYS[1]
|
|
2020
2845
|
local workerId = ARGV[1]
|
|
@@ -2057,7 +2882,25 @@ for _, jobId in ipairs(retries) do
|
|
|
2057
2882
|
redis.call('ZREM', prefix .. 'retry', jobId)
|
|
2058
2883
|
end
|
|
2059
2884
|
|
|
2060
|
-
-- 3.
|
|
2885
|
+
-- 3. Move ready waiting jobs (time-based, no token) into queue
|
|
2886
|
+
local waitingJobs = redis.call('ZRANGEBYSCORE', prefix .. 'waiting', '-inf', nowMs, 'LIMIT', 0, 200)
|
|
2887
|
+
for _, jobId in ipairs(waitingJobs) do
|
|
2888
|
+
local jk = prefix .. 'job:' .. jobId
|
|
2889
|
+
local status = redis.call('HGET', jk, 'status')
|
|
2890
|
+
local waitTokenId = redis.call('HGET', jk, 'waitTokenId')
|
|
2891
|
+
if status == 'waiting' and (waitTokenId == false or waitTokenId == 'null') then
|
|
2892
|
+
local pri = tonumber(redis.call('HGET', jk, 'priority') or '0')
|
|
2893
|
+
local ca = tonumber(redis.call('HGET', jk, 'createdAt'))
|
|
2894
|
+
local score = pri * ${SCORE_RANGE} + (${SCORE_RANGE} - ca)
|
|
2895
|
+
redis.call('ZADD', prefix .. 'queue', score, jobId)
|
|
2896
|
+
redis.call('SREM', prefix .. 'status:waiting', jobId)
|
|
2897
|
+
redis.call('SADD', prefix .. 'status:pending', jobId)
|
|
2898
|
+
redis.call('HMSET', jk, 'status', 'pending', 'waitUntil', 'null')
|
|
2899
|
+
end
|
|
2900
|
+
redis.call('ZREM', prefix .. 'waiting', jobId)
|
|
2901
|
+
end
|
|
2902
|
+
|
|
2903
|
+
-- 4. Parse job type filter
|
|
2061
2904
|
local filterTypes = nil
|
|
2062
2905
|
if jobTypeFilter ~= "null" then
|
|
2063
2906
|
-- Could be a JSON array or a plain string
|
|
@@ -2070,7 +2913,7 @@ if jobTypeFilter ~= "null" then
|
|
|
2070
2913
|
end
|
|
2071
2914
|
end
|
|
2072
2915
|
|
|
2073
|
-
--
|
|
2916
|
+
-- 5. Pop candidates from queue (highest score first)
|
|
2074
2917
|
-- We pop more than batchSize because some may be filtered out
|
|
2075
2918
|
local popCount = batchSize * 3
|
|
2076
2919
|
local candidates = redis.call('ZPOPMAX', prefix .. 'queue', popCount)
|
|
@@ -2154,7 +2997,10 @@ local jk = prefix .. 'job:' .. jobId
|
|
|
2154
2997
|
redis.call('HMSET', jk,
|
|
2155
2998
|
'status', 'completed',
|
|
2156
2999
|
'updatedAt', nowMs,
|
|
2157
|
-
'completedAt', nowMs
|
|
3000
|
+
'completedAt', nowMs,
|
|
3001
|
+
'stepData', 'null',
|
|
3002
|
+
'waitUntil', 'null',
|
|
3003
|
+
'waitTokenId', 'null'
|
|
2158
3004
|
)
|
|
2159
3005
|
redis.call('SREM', prefix .. 'status:processing', jobId)
|
|
2160
3006
|
redis.call('SADD', prefix .. 'status:completed', jobId)
|
|
@@ -2172,11 +3018,38 @@ local jk = prefix .. 'job:' .. jobId
|
|
|
2172
3018
|
local attempts = tonumber(redis.call('HGET', jk, 'attempts'))
|
|
2173
3019
|
local maxAttempts = tonumber(redis.call('HGET', jk, 'maxAttempts'))
|
|
2174
3020
|
|
|
2175
|
-
--
|
|
3021
|
+
-- Read per-job retry config (may be "null")
|
|
3022
|
+
local rdRaw = redis.call('HGET', jk, 'retryDelay')
|
|
3023
|
+
local rbRaw = redis.call('HGET', jk, 'retryBackoff')
|
|
3024
|
+
local rmRaw = redis.call('HGET', jk, 'retryDelayMax')
|
|
3025
|
+
|
|
2176
3026
|
local nextAttemptAt = 'null'
|
|
2177
3027
|
if attempts < maxAttempts then
|
|
2178
|
-
local
|
|
2179
|
-
|
|
3028
|
+
local allNull = (rdRaw == 'null' or rdRaw == false)
|
|
3029
|
+
and (rbRaw == 'null' or rbRaw == false)
|
|
3030
|
+
and (rmRaw == 'null' or rmRaw == false)
|
|
3031
|
+
if allNull then
|
|
3032
|
+
-- Legacy formula: 2^attempts minutes
|
|
3033
|
+
local delayMs = math.pow(2, attempts) * 60000
|
|
3034
|
+
nextAttemptAt = nowMs + delayMs
|
|
3035
|
+
else
|
|
3036
|
+
local retryDelaySec = 60
|
|
3037
|
+
if rdRaw and rdRaw ~= 'null' then retryDelaySec = tonumber(rdRaw) end
|
|
3038
|
+
local useBackoff = true
|
|
3039
|
+
if rbRaw and rbRaw ~= 'null' then useBackoff = (rbRaw == 'true') end
|
|
3040
|
+
local maxDelaySec = nil
|
|
3041
|
+
if rmRaw and rmRaw ~= 'null' then maxDelaySec = tonumber(rmRaw) end
|
|
3042
|
+
|
|
3043
|
+
local delaySec
|
|
3044
|
+
if useBackoff then
|
|
3045
|
+
delaySec = retryDelaySec * math.pow(2, attempts)
|
|
3046
|
+
if maxDelaySec then delaySec = math.min(delaySec, maxDelaySec) end
|
|
3047
|
+
delaySec = delaySec * (0.5 + 0.5 * math.random())
|
|
3048
|
+
else
|
|
3049
|
+
delaySec = retryDelaySec
|
|
3050
|
+
end
|
|
3051
|
+
nextAttemptAt = nowMs + math.floor(delaySec * 1000)
|
|
3052
|
+
end
|
|
2180
3053
|
end
|
|
2181
3054
|
|
|
2182
3055
|
-- Append to error_history
|
|
@@ -2213,6 +3086,7 @@ local nowMs = tonumber(ARGV[2])
|
|
|
2213
3086
|
local jk = prefix .. 'job:' .. jobId
|
|
2214
3087
|
|
|
2215
3088
|
local oldStatus = redis.call('HGET', jk, 'status')
|
|
3089
|
+
if oldStatus ~= 'failed' and oldStatus ~= 'processing' then return 0 end
|
|
2216
3090
|
|
|
2217
3091
|
redis.call('HMSET', jk,
|
|
2218
3092
|
'status', 'pending',
|
|
@@ -2224,9 +3098,7 @@ redis.call('HMSET', jk,
|
|
|
2224
3098
|
)
|
|
2225
3099
|
|
|
2226
3100
|
-- Remove from old status, add to pending
|
|
2227
|
-
|
|
2228
|
-
redis.call('SREM', prefix .. 'status:' .. oldStatus, jobId)
|
|
2229
|
-
end
|
|
3101
|
+
redis.call('SREM', prefix .. 'status:' .. oldStatus, jobId)
|
|
2230
3102
|
redis.call('SADD', prefix .. 'status:pending', jobId)
|
|
2231
3103
|
|
|
2232
3104
|
-- Remove from retry sorted set if present
|
|
@@ -2247,18 +3119,21 @@ local nowMs = ARGV[2]
|
|
|
2247
3119
|
local jk = prefix .. 'job:' .. jobId
|
|
2248
3120
|
|
|
2249
3121
|
local status = redis.call('HGET', jk, 'status')
|
|
2250
|
-
if status ~= 'pending' then return 0 end
|
|
3122
|
+
if status ~= 'pending' and status ~= 'waiting' then return 0 end
|
|
2251
3123
|
|
|
2252
3124
|
redis.call('HMSET', jk,
|
|
2253
3125
|
'status', 'cancelled',
|
|
2254
3126
|
'updatedAt', nowMs,
|
|
2255
|
-
'lastCancelledAt', nowMs
|
|
3127
|
+
'lastCancelledAt', nowMs,
|
|
3128
|
+
'waitUntil', 'null',
|
|
3129
|
+
'waitTokenId', 'null'
|
|
2256
3130
|
)
|
|
2257
|
-
redis.call('SREM', prefix .. 'status:
|
|
3131
|
+
redis.call('SREM', prefix .. 'status:' .. status, jobId)
|
|
2258
3132
|
redis.call('SADD', prefix .. 'status:cancelled', jobId)
|
|
2259
|
-
-- Remove from queue / delayed
|
|
3133
|
+
-- Remove from queue / delayed / waiting
|
|
2260
3134
|
redis.call('ZREM', prefix .. 'queue', jobId)
|
|
2261
3135
|
redis.call('ZREM', prefix .. 'delayed', jobId)
|
|
3136
|
+
redis.call('ZREM', prefix .. 'waiting', jobId)
|
|
2262
3137
|
|
|
2263
3138
|
return 1
|
|
2264
3139
|
`;
|
|
@@ -2326,18 +3201,16 @@ end
|
|
|
2326
3201
|
|
|
2327
3202
|
return count
|
|
2328
3203
|
`;
|
|
2329
|
-
var
|
|
3204
|
+
var CLEANUP_OLD_JOBS_BATCH_SCRIPT = `
|
|
2330
3205
|
local prefix = KEYS[1]
|
|
2331
3206
|
local cutoffMs = tonumber(ARGV[1])
|
|
2332
|
-
|
|
2333
|
-
local completed = redis.call('SMEMBERS', prefix .. 'status:completed')
|
|
2334
3207
|
local count = 0
|
|
2335
3208
|
|
|
2336
|
-
for
|
|
3209
|
+
for i = 2, #ARGV do
|
|
3210
|
+
local jobId = ARGV[i]
|
|
2337
3211
|
local jk = prefix .. 'job:' .. jobId
|
|
2338
3212
|
local updatedAt = tonumber(redis.call('HGET', jk, 'updatedAt'))
|
|
2339
3213
|
if updatedAt and updatedAt < cutoffMs then
|
|
2340
|
-
-- Remove all indexes
|
|
2341
3214
|
local jobType = redis.call('HGET', jk, 'jobType')
|
|
2342
3215
|
local tagsJson = redis.call('HGET', jk, 'tags')
|
|
2343
3216
|
local idempotencyKey = redis.call('HGET', jk, 'idempotencyKey')
|
|
@@ -2360,7 +3233,6 @@ for _, jobId in ipairs(completed) do
|
|
|
2360
3233
|
if idempotencyKey and idempotencyKey ~= 'null' then
|
|
2361
3234
|
redis.call('DEL', prefix .. 'idempotency:' .. idempotencyKey)
|
|
2362
3235
|
end
|
|
2363
|
-
-- Delete events
|
|
2364
3236
|
redis.call('DEL', prefix .. 'events:' .. jobId)
|
|
2365
3237
|
|
|
2366
3238
|
count = count + 1
|
|
@@ -2369,8 +3241,158 @@ end
|
|
|
2369
3241
|
|
|
2370
3242
|
return count
|
|
2371
3243
|
`;
|
|
3244
|
+
var WAIT_JOB_SCRIPT = `
|
|
3245
|
+
local prefix = KEYS[1]
|
|
3246
|
+
local jobId = ARGV[1]
|
|
3247
|
+
local waitUntilMs = ARGV[2]
|
|
3248
|
+
local waitTokenId = ARGV[3]
|
|
3249
|
+
local stepDataJson = ARGV[4]
|
|
3250
|
+
local nowMs = ARGV[5]
|
|
3251
|
+
local jk = prefix .. 'job:' .. jobId
|
|
3252
|
+
|
|
3253
|
+
local status = redis.call('HGET', jk, 'status')
|
|
3254
|
+
if status ~= 'processing' then return 0 end
|
|
3255
|
+
|
|
3256
|
+
redis.call('HMSET', jk,
|
|
3257
|
+
'status', 'waiting',
|
|
3258
|
+
'waitUntil', waitUntilMs,
|
|
3259
|
+
'waitTokenId', waitTokenId,
|
|
3260
|
+
'stepData', stepDataJson,
|
|
3261
|
+
'lockedAt', 'null',
|
|
3262
|
+
'lockedBy', 'null',
|
|
3263
|
+
'updatedAt', nowMs
|
|
3264
|
+
)
|
|
3265
|
+
redis.call('SREM', prefix .. 'status:processing', jobId)
|
|
3266
|
+
redis.call('SADD', prefix .. 'status:waiting', jobId)
|
|
3267
|
+
|
|
3268
|
+
-- Add to waiting sorted set if time-based wait
|
|
3269
|
+
if waitUntilMs ~= 'null' then
|
|
3270
|
+
redis.call('ZADD', prefix .. 'waiting', tonumber(waitUntilMs), jobId)
|
|
3271
|
+
end
|
|
3272
|
+
|
|
3273
|
+
return 1
|
|
3274
|
+
`;
|
|
3275
|
+
var COMPLETE_WAITPOINT_SCRIPT = `
|
|
3276
|
+
local prefix = KEYS[1]
|
|
3277
|
+
local tokenId = ARGV[1]
|
|
3278
|
+
local outputJson = ARGV[2]
|
|
3279
|
+
local nowMs = ARGV[3]
|
|
3280
|
+
local wpk = prefix .. 'waitpoint:' .. tokenId
|
|
3281
|
+
|
|
3282
|
+
local wpStatus = redis.call('HGET', wpk, 'status')
|
|
3283
|
+
if not wpStatus or wpStatus ~= 'waiting' then return 0 end
|
|
3284
|
+
|
|
3285
|
+
redis.call('HMSET', wpk,
|
|
3286
|
+
'status', 'completed',
|
|
3287
|
+
'output', outputJson,
|
|
3288
|
+
'completedAt', nowMs
|
|
3289
|
+
)
|
|
3290
|
+
|
|
3291
|
+
-- Move associated job back to pending
|
|
3292
|
+
local jobId = redis.call('HGET', wpk, 'jobId')
|
|
3293
|
+
if jobId and jobId ~= 'null' then
|
|
3294
|
+
local jk = prefix .. 'job:' .. jobId
|
|
3295
|
+
local jobStatus = redis.call('HGET', jk, 'status')
|
|
3296
|
+
if jobStatus == 'waiting' then
|
|
3297
|
+
redis.call('HMSET', jk,
|
|
3298
|
+
'status', 'pending',
|
|
3299
|
+
'waitTokenId', 'null',
|
|
3300
|
+
'waitUntil', 'null',
|
|
3301
|
+
'updatedAt', nowMs
|
|
3302
|
+
)
|
|
3303
|
+
redis.call('SREM', prefix .. 'status:waiting', jobId)
|
|
3304
|
+
redis.call('SADD', prefix .. 'status:pending', jobId)
|
|
3305
|
+
redis.call('ZREM', prefix .. 'waiting', jobId)
|
|
3306
|
+
|
|
3307
|
+
-- Re-add to queue
|
|
3308
|
+
local priority = tonumber(redis.call('HGET', jk, 'priority') or '0')
|
|
3309
|
+
local createdAt = tonumber(redis.call('HGET', jk, 'createdAt'))
|
|
3310
|
+
local score = priority * ${SCORE_RANGE} + (${SCORE_RANGE} - createdAt)
|
|
3311
|
+
redis.call('ZADD', prefix .. 'queue', score, jobId)
|
|
3312
|
+
end
|
|
3313
|
+
end
|
|
3314
|
+
|
|
3315
|
+
return 1
|
|
3316
|
+
`;
|
|
3317
|
+
var EXPIRE_TIMED_OUT_WAITPOINTS_SCRIPT = `
|
|
3318
|
+
local prefix = KEYS[1]
|
|
3319
|
+
local nowMs = tonumber(ARGV[1])
|
|
3320
|
+
|
|
3321
|
+
local expiredIds = redis.call('ZRANGEBYSCORE', prefix .. 'waitpoint_timeout', '-inf', nowMs)
|
|
3322
|
+
local count = 0
|
|
3323
|
+
|
|
3324
|
+
for _, tokenId in ipairs(expiredIds) do
|
|
3325
|
+
local wpk = prefix .. 'waitpoint:' .. tokenId
|
|
3326
|
+
local wpStatus = redis.call('HGET', wpk, 'status')
|
|
3327
|
+
if wpStatus == 'waiting' then
|
|
3328
|
+
redis.call('HMSET', wpk,
|
|
3329
|
+
'status', 'timed_out'
|
|
3330
|
+
)
|
|
3331
|
+
|
|
3332
|
+
-- Move associated job back to pending
|
|
3333
|
+
local jobId = redis.call('HGET', wpk, 'jobId')
|
|
3334
|
+
if jobId and jobId ~= 'null' then
|
|
3335
|
+
local jk = prefix .. 'job:' .. jobId
|
|
3336
|
+
local jobStatus = redis.call('HGET', jk, 'status')
|
|
3337
|
+
if jobStatus == 'waiting' then
|
|
3338
|
+
redis.call('HMSET', jk,
|
|
3339
|
+
'status', 'pending',
|
|
3340
|
+
'waitTokenId', 'null',
|
|
3341
|
+
'waitUntil', 'null',
|
|
3342
|
+
'updatedAt', nowMs
|
|
3343
|
+
)
|
|
3344
|
+
redis.call('SREM', prefix .. 'status:waiting', jobId)
|
|
3345
|
+
redis.call('SADD', prefix .. 'status:pending', jobId)
|
|
3346
|
+
redis.call('ZREM', prefix .. 'waiting', jobId)
|
|
3347
|
+
|
|
3348
|
+
local priority = tonumber(redis.call('HGET', jk, 'priority') or '0')
|
|
3349
|
+
local createdAt = tonumber(redis.call('HGET', jk, 'createdAt'))
|
|
3350
|
+
local score = priority * ${SCORE_RANGE} + (${SCORE_RANGE} - createdAt)
|
|
3351
|
+
redis.call('ZADD', prefix .. 'queue', score, jobId)
|
|
3352
|
+
end
|
|
3353
|
+
end
|
|
3354
|
+
|
|
3355
|
+
count = count + 1
|
|
3356
|
+
end
|
|
3357
|
+
redis.call('ZREM', prefix .. 'waitpoint_timeout', tokenId)
|
|
3358
|
+
end
|
|
2372
3359
|
|
|
2373
|
-
|
|
3360
|
+
return count
|
|
3361
|
+
`;
|
|
3362
|
+
var MAX_TIMEOUT_MS2 = 365 * 24 * 60 * 60 * 1e3;
|
|
3363
|
+
function parseTimeoutString2(timeout) {
|
|
3364
|
+
const match = timeout.match(/^(\d+)(s|m|h|d)$/);
|
|
3365
|
+
if (!match) {
|
|
3366
|
+
throw new Error(
|
|
3367
|
+
`Invalid timeout format: "${timeout}". Expected format like "10m", "1h", "24h", "7d".`
|
|
3368
|
+
);
|
|
3369
|
+
}
|
|
3370
|
+
const value = parseInt(match[1], 10);
|
|
3371
|
+
const unit = match[2];
|
|
3372
|
+
let ms;
|
|
3373
|
+
switch (unit) {
|
|
3374
|
+
case "s":
|
|
3375
|
+
ms = value * 1e3;
|
|
3376
|
+
break;
|
|
3377
|
+
case "m":
|
|
3378
|
+
ms = value * 60 * 1e3;
|
|
3379
|
+
break;
|
|
3380
|
+
case "h":
|
|
3381
|
+
ms = value * 60 * 60 * 1e3;
|
|
3382
|
+
break;
|
|
3383
|
+
case "d":
|
|
3384
|
+
ms = value * 24 * 60 * 60 * 1e3;
|
|
3385
|
+
break;
|
|
3386
|
+
default:
|
|
3387
|
+
throw new Error(`Unknown timeout unit: "${unit}"`);
|
|
3388
|
+
}
|
|
3389
|
+
if (!Number.isFinite(ms) || ms > MAX_TIMEOUT_MS2) {
|
|
3390
|
+
throw new Error(
|
|
3391
|
+
`Timeout value "${timeout}" is too large. Maximum allowed is 365 days.`
|
|
3392
|
+
);
|
|
3393
|
+
}
|
|
3394
|
+
return ms;
|
|
3395
|
+
}
|
|
2374
3396
|
function hashToObject(arr) {
|
|
2375
3397
|
const obj = {};
|
|
2376
3398
|
for (let i = 0; i < arr.length; i += 2) {
|
|
@@ -2436,11 +3458,41 @@ function deserializeJob(h) {
|
|
|
2436
3458
|
lastCancelledAt: dateOrNull(h.lastCancelledAt),
|
|
2437
3459
|
tags,
|
|
2438
3460
|
idempotencyKey: nullish(h.idempotencyKey),
|
|
2439
|
-
progress: numOrNull(h.progress)
|
|
3461
|
+
progress: numOrNull(h.progress),
|
|
3462
|
+
waitUntil: dateOrNull(h.waitUntil),
|
|
3463
|
+
waitTokenId: nullish(h.waitTokenId),
|
|
3464
|
+
stepData: parseStepData(h.stepData),
|
|
3465
|
+
retryDelay: numOrNull(h.retryDelay),
|
|
3466
|
+
retryBackoff: h.retryBackoff === "true" ? true : h.retryBackoff === "false" ? false : null,
|
|
3467
|
+
retryDelayMax: numOrNull(h.retryDelayMax)
|
|
2440
3468
|
};
|
|
2441
3469
|
}
|
|
3470
|
+
function parseStepData(raw) {
|
|
3471
|
+
if (!raw || raw === "null") return void 0;
|
|
3472
|
+
try {
|
|
3473
|
+
return JSON.parse(raw);
|
|
3474
|
+
} catch {
|
|
3475
|
+
return void 0;
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
2442
3478
|
var RedisBackend = class {
|
|
2443
|
-
|
|
3479
|
+
/**
|
|
3480
|
+
* Create a RedisBackend.
|
|
3481
|
+
*
|
|
3482
|
+
* @param configOrClient - Either `redisConfig` from the config file (the
|
|
3483
|
+
* library creates a new ioredis client) or an existing ioredis client
|
|
3484
|
+
* instance (bring your own).
|
|
3485
|
+
* @param keyPrefix - Key prefix, only used when `configOrClient` is an
|
|
3486
|
+
* external client. Ignored when `redisConfig` is passed (uses
|
|
3487
|
+
* `redisConfig.keyPrefix` instead). Default: `'dq:'`.
|
|
3488
|
+
*/
|
|
3489
|
+
constructor(configOrClient, keyPrefix) {
|
|
3490
|
+
if (configOrClient && typeof configOrClient.eval === "function") {
|
|
3491
|
+
this.client = configOrClient;
|
|
3492
|
+
this.prefix = keyPrefix ?? "dq:";
|
|
3493
|
+
return;
|
|
3494
|
+
}
|
|
3495
|
+
const redisConfig = configOrClient;
|
|
2444
3496
|
let IORedis;
|
|
2445
3497
|
try {
|
|
2446
3498
|
const _require = createRequire(import.meta.url);
|
|
@@ -2513,8 +3565,16 @@ var RedisBackend = class {
|
|
|
2513
3565
|
timeoutMs = void 0,
|
|
2514
3566
|
forceKillOnTimeout = false,
|
|
2515
3567
|
tags = void 0,
|
|
2516
|
-
idempotencyKey = void 0
|
|
2517
|
-
|
|
3568
|
+
idempotencyKey = void 0,
|
|
3569
|
+
retryDelay = void 0,
|
|
3570
|
+
retryBackoff = void 0,
|
|
3571
|
+
retryDelayMax = void 0
|
|
3572
|
+
}, options) {
|
|
3573
|
+
if (options?.db) {
|
|
3574
|
+
throw new Error(
|
|
3575
|
+
"The db option is not supported with the Redis backend. Transactional job creation is only available with PostgreSQL."
|
|
3576
|
+
);
|
|
3577
|
+
}
|
|
2518
3578
|
const now = this.nowMs();
|
|
2519
3579
|
const runAtMs = runAt ? runAt.getTime() : 0;
|
|
2520
3580
|
const result = await this.client.eval(
|
|
@@ -2530,7 +3590,10 @@ var RedisBackend = class {
|
|
|
2530
3590
|
forceKillOnTimeout ? "true" : "false",
|
|
2531
3591
|
tags ? JSON.stringify(tags) : "null",
|
|
2532
3592
|
idempotencyKey ?? "null",
|
|
2533
|
-
now
|
|
3593
|
+
now,
|
|
3594
|
+
retryDelay !== void 0 ? retryDelay.toString() : "null",
|
|
3595
|
+
retryBackoff !== void 0 ? retryBackoff.toString() : "null",
|
|
3596
|
+
retryDelayMax !== void 0 ? retryDelayMax.toString() : "null"
|
|
2534
3597
|
);
|
|
2535
3598
|
const jobId = Number(result);
|
|
2536
3599
|
log(
|
|
@@ -2544,6 +3607,58 @@ var RedisBackend = class {
|
|
|
2544
3607
|
});
|
|
2545
3608
|
return jobId;
|
|
2546
3609
|
}
|
|
3610
|
+
/**
|
|
3611
|
+
* Insert multiple jobs atomically via a single Lua script.
|
|
3612
|
+
* Returns IDs in the same order as the input array.
|
|
3613
|
+
*/
|
|
3614
|
+
async addJobs(jobs, options) {
|
|
3615
|
+
if (jobs.length === 0) return [];
|
|
3616
|
+
if (options?.db) {
|
|
3617
|
+
throw new Error(
|
|
3618
|
+
"The db option is not supported with the Redis backend. Transactional job creation is only available with PostgreSQL."
|
|
3619
|
+
);
|
|
3620
|
+
}
|
|
3621
|
+
const now = this.nowMs();
|
|
3622
|
+
const jobsPayload = jobs.map((job) => ({
|
|
3623
|
+
jobType: job.jobType,
|
|
3624
|
+
payload: JSON.stringify(job.payload),
|
|
3625
|
+
maxAttempts: job.maxAttempts ?? 3,
|
|
3626
|
+
priority: job.priority ?? 0,
|
|
3627
|
+
runAtMs: job.runAt ? job.runAt.getTime() : 0,
|
|
3628
|
+
timeoutMs: job.timeoutMs !== void 0 ? job.timeoutMs.toString() : "null",
|
|
3629
|
+
forceKillOnTimeout: job.forceKillOnTimeout ? "true" : "false",
|
|
3630
|
+
tags: job.tags ? JSON.stringify(job.tags) : "null",
|
|
3631
|
+
idempotencyKey: job.idempotencyKey ?? "null",
|
|
3632
|
+
retryDelay: job.retryDelay !== void 0 ? job.retryDelay.toString() : "null",
|
|
3633
|
+
retryBackoff: job.retryBackoff !== void 0 ? job.retryBackoff.toString() : "null",
|
|
3634
|
+
retryDelayMax: job.retryDelayMax !== void 0 ? job.retryDelayMax.toString() : "null"
|
|
3635
|
+
}));
|
|
3636
|
+
const result = await this.client.eval(
|
|
3637
|
+
ADD_JOBS_SCRIPT,
|
|
3638
|
+
1,
|
|
3639
|
+
this.prefix,
|
|
3640
|
+
JSON.stringify(jobsPayload),
|
|
3641
|
+
now
|
|
3642
|
+
);
|
|
3643
|
+
const ids = result.map(Number);
|
|
3644
|
+
log(`Batch-inserted ${jobs.length} jobs, IDs: [${ids.join(", ")}]`);
|
|
3645
|
+
const existingIdempotencyIds = /* @__PURE__ */ new Set();
|
|
3646
|
+
for (let i = 0; i < jobs.length; i++) {
|
|
3647
|
+
if (jobs[i].idempotencyKey) {
|
|
3648
|
+
if (existingIdempotencyIds.has(ids[i])) {
|
|
3649
|
+
continue;
|
|
3650
|
+
}
|
|
3651
|
+
existingIdempotencyIds.add(ids[i]);
|
|
3652
|
+
}
|
|
3653
|
+
await this.recordJobEvent(ids[i], "added" /* Added */, {
|
|
3654
|
+
jobType: jobs[i].jobType,
|
|
3655
|
+
payload: jobs[i].payload,
|
|
3656
|
+
tags: jobs[i].tags,
|
|
3657
|
+
idempotencyKey: jobs[i].idempotencyKey
|
|
3658
|
+
});
|
|
3659
|
+
}
|
|
3660
|
+
return ids;
|
|
3661
|
+
}
|
|
2547
3662
|
async getJob(id) {
|
|
2548
3663
|
const data = await this.client.hgetall(`${this.prefix}job:${id}`);
|
|
2549
3664
|
if (!data || Object.keys(data).length === 0) {
|
|
@@ -2594,8 +3709,14 @@ var RedisBackend = class {
|
|
|
2594
3709
|
if (filters.runAt) {
|
|
2595
3710
|
jobs = this.filterByRunAt(jobs, filters.runAt);
|
|
2596
3711
|
}
|
|
3712
|
+
if (filters.cursor !== void 0) {
|
|
3713
|
+
jobs = jobs.filter((j) => j.id < filters.cursor);
|
|
3714
|
+
}
|
|
3715
|
+
}
|
|
3716
|
+
jobs.sort((a, b) => b.id - a.id);
|
|
3717
|
+
if (filters?.cursor !== void 0) {
|
|
3718
|
+
return jobs.slice(0, limit);
|
|
2597
3719
|
}
|
|
2598
|
-
jobs.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
2599
3720
|
return jobs.slice(offset, offset + limit);
|
|
2600
3721
|
}
|
|
2601
3722
|
async getJobsByTags(tags, mode = "all", limit = 100, offset = 0) {
|
|
@@ -2804,58 +3925,346 @@ var RedisBackend = class {
|
|
|
2804
3925
|
}
|
|
2805
3926
|
metadata.tags = updates.tags;
|
|
2806
3927
|
}
|
|
3928
|
+
if (updates.retryDelay !== void 0) {
|
|
3929
|
+
fields.push(
|
|
3930
|
+
"retryDelay",
|
|
3931
|
+
updates.retryDelay !== null ? updates.retryDelay.toString() : "null"
|
|
3932
|
+
);
|
|
3933
|
+
metadata.retryDelay = updates.retryDelay;
|
|
3934
|
+
}
|
|
3935
|
+
if (updates.retryBackoff !== void 0) {
|
|
3936
|
+
fields.push(
|
|
3937
|
+
"retryBackoff",
|
|
3938
|
+
updates.retryBackoff !== null ? updates.retryBackoff.toString() : "null"
|
|
3939
|
+
);
|
|
3940
|
+
metadata.retryBackoff = updates.retryBackoff;
|
|
3941
|
+
}
|
|
3942
|
+
if (updates.retryDelayMax !== void 0) {
|
|
3943
|
+
fields.push(
|
|
3944
|
+
"retryDelayMax",
|
|
3945
|
+
updates.retryDelayMax !== null ? updates.retryDelayMax.toString() : "null"
|
|
3946
|
+
);
|
|
3947
|
+
metadata.retryDelayMax = updates.retryDelayMax;
|
|
3948
|
+
}
|
|
2807
3949
|
if (fields.length === 0) {
|
|
2808
3950
|
log(`No fields to update for job ${jobId}`);
|
|
2809
3951
|
return;
|
|
2810
3952
|
}
|
|
2811
|
-
fields.push("updatedAt", now.toString());
|
|
2812
|
-
await this.client.hmset(jk, ...fields);
|
|
2813
|
-
await this.recordJobEvent(jobId, "edited" /* Edited */, metadata);
|
|
2814
|
-
log(`Edited job ${jobId}: ${JSON.stringify(metadata)}`);
|
|
3953
|
+
fields.push("updatedAt", now.toString());
|
|
3954
|
+
await this.client.hmset(jk, ...fields);
|
|
3955
|
+
await this.recordJobEvent(jobId, "edited" /* Edited */, metadata);
|
|
3956
|
+
log(`Edited job ${jobId}: ${JSON.stringify(metadata)}`);
|
|
3957
|
+
}
|
|
3958
|
+
async editAllPendingJobs(filters, updates) {
|
|
3959
|
+
let ids = await this.client.smembers(`${this.prefix}status:pending`);
|
|
3960
|
+
if (ids.length === 0) return 0;
|
|
3961
|
+
if (filters) {
|
|
3962
|
+
ids = await this.applyFilters(ids, filters);
|
|
3963
|
+
}
|
|
3964
|
+
let count = 0;
|
|
3965
|
+
for (const id of ids) {
|
|
3966
|
+
await this.editJob(Number(id), updates);
|
|
3967
|
+
count++;
|
|
3968
|
+
}
|
|
3969
|
+
log(`Edited ${count} pending jobs`);
|
|
3970
|
+
return count;
|
|
3971
|
+
}
|
|
3972
|
+
/**
|
|
3973
|
+
* Delete completed jobs older than the given number of days.
|
|
3974
|
+
* Uses SSCAN to iterate the completed set in batches, avoiding
|
|
3975
|
+
* loading all IDs into memory and preventing long Redis blocks.
|
|
3976
|
+
*
|
|
3977
|
+
* @param daysToKeep - Number of days to retain completed jobs (default 30).
|
|
3978
|
+
* @param batchSize - Number of IDs to scan per SSCAN iteration (default 200).
|
|
3979
|
+
* @returns Total number of deleted jobs.
|
|
3980
|
+
*/
|
|
3981
|
+
async cleanupOldJobs(daysToKeep = 30, batchSize = 200) {
|
|
3982
|
+
const cutoffMs = this.nowMs() - daysToKeep * 24 * 60 * 60 * 1e3;
|
|
3983
|
+
const setKey = `${this.prefix}status:completed`;
|
|
3984
|
+
let totalDeleted = 0;
|
|
3985
|
+
let cursor = "0";
|
|
3986
|
+
do {
|
|
3987
|
+
const [nextCursor, ids] = await this.client.sscan(
|
|
3988
|
+
setKey,
|
|
3989
|
+
cursor,
|
|
3990
|
+
"COUNT",
|
|
3991
|
+
batchSize
|
|
3992
|
+
);
|
|
3993
|
+
cursor = nextCursor;
|
|
3994
|
+
if (ids.length > 0) {
|
|
3995
|
+
const result = await this.client.eval(
|
|
3996
|
+
CLEANUP_OLD_JOBS_BATCH_SCRIPT,
|
|
3997
|
+
1,
|
|
3998
|
+
this.prefix,
|
|
3999
|
+
cutoffMs,
|
|
4000
|
+
...ids
|
|
4001
|
+
);
|
|
4002
|
+
totalDeleted += Number(result);
|
|
4003
|
+
}
|
|
4004
|
+
} while (cursor !== "0");
|
|
4005
|
+
log(`Deleted ${totalDeleted} old jobs`);
|
|
4006
|
+
return totalDeleted;
|
|
4007
|
+
}
|
|
4008
|
+
/**
|
|
4009
|
+
* Delete job events older than the given number of days.
|
|
4010
|
+
* Iterates all event lists and removes events whose createdAt is before the cutoff.
|
|
4011
|
+
* Also removes orphaned event lists (where the job no longer exists).
|
|
4012
|
+
*
|
|
4013
|
+
* @param daysToKeep - Number of days to retain events (default 30).
|
|
4014
|
+
* @param batchSize - Number of event keys to scan per SCAN iteration (default 200).
|
|
4015
|
+
* @returns Total number of deleted events.
|
|
4016
|
+
*/
|
|
4017
|
+
async cleanupOldJobEvents(daysToKeep = 30, batchSize = 200) {
|
|
4018
|
+
const cutoffMs = this.nowMs() - daysToKeep * 24 * 60 * 60 * 1e3;
|
|
4019
|
+
const pattern = `${this.prefix}events:*`;
|
|
4020
|
+
let totalDeleted = 0;
|
|
4021
|
+
let cursor = "0";
|
|
4022
|
+
do {
|
|
4023
|
+
const [nextCursor, keys] = await this.client.scan(
|
|
4024
|
+
cursor,
|
|
4025
|
+
"MATCH",
|
|
4026
|
+
pattern,
|
|
4027
|
+
"COUNT",
|
|
4028
|
+
batchSize
|
|
4029
|
+
);
|
|
4030
|
+
cursor = nextCursor;
|
|
4031
|
+
for (const key of keys) {
|
|
4032
|
+
const jobIdStr = key.slice(`${this.prefix}events:`.length);
|
|
4033
|
+
const jobExists = await this.client.exists(
|
|
4034
|
+
`${this.prefix}job:${jobIdStr}`
|
|
4035
|
+
);
|
|
4036
|
+
if (!jobExists) {
|
|
4037
|
+
const len = await this.client.llen(key);
|
|
4038
|
+
await this.client.del(key);
|
|
4039
|
+
totalDeleted += len;
|
|
4040
|
+
continue;
|
|
4041
|
+
}
|
|
4042
|
+
const events = await this.client.lrange(key, 0, -1);
|
|
4043
|
+
const kept = [];
|
|
4044
|
+
for (const raw of events) {
|
|
4045
|
+
try {
|
|
4046
|
+
const e = JSON.parse(raw);
|
|
4047
|
+
if (e.createdAt >= cutoffMs) {
|
|
4048
|
+
kept.push(raw);
|
|
4049
|
+
} else {
|
|
4050
|
+
totalDeleted++;
|
|
4051
|
+
}
|
|
4052
|
+
} catch {
|
|
4053
|
+
totalDeleted++;
|
|
4054
|
+
}
|
|
4055
|
+
}
|
|
4056
|
+
if (kept.length === 0) {
|
|
4057
|
+
await this.client.del(key);
|
|
4058
|
+
} else if (kept.length < events.length) {
|
|
4059
|
+
const pipeline = this.client.pipeline();
|
|
4060
|
+
pipeline.del(key);
|
|
4061
|
+
for (const raw of kept) {
|
|
4062
|
+
pipeline.rpush(key, raw);
|
|
4063
|
+
}
|
|
4064
|
+
await pipeline.exec();
|
|
4065
|
+
}
|
|
4066
|
+
}
|
|
4067
|
+
} while (cursor !== "0");
|
|
4068
|
+
log(`Deleted ${totalDeleted} old job events`);
|
|
4069
|
+
return totalDeleted;
|
|
4070
|
+
}
|
|
4071
|
+
async reclaimStuckJobs(maxProcessingTimeMinutes = 10) {
|
|
4072
|
+
const maxAgeMs = maxProcessingTimeMinutes * 60 * 1e3;
|
|
4073
|
+
const now = this.nowMs();
|
|
4074
|
+
const result = await this.client.eval(
|
|
4075
|
+
RECLAIM_STUCK_JOBS_SCRIPT,
|
|
4076
|
+
1,
|
|
4077
|
+
this.prefix,
|
|
4078
|
+
maxAgeMs,
|
|
4079
|
+
now
|
|
4080
|
+
);
|
|
4081
|
+
log(`Reclaimed ${result} stuck jobs`);
|
|
4082
|
+
return Number(result);
|
|
4083
|
+
}
|
|
4084
|
+
// ── Wait / step-data support ────────────────────────────────────────
|
|
4085
|
+
/**
|
|
4086
|
+
* Transition a job from 'processing' to 'waiting' status.
|
|
4087
|
+
* Persists step data so the handler can resume from where it left off.
|
|
4088
|
+
*
|
|
4089
|
+
* @param jobId - The job to pause.
|
|
4090
|
+
* @param options - Wait configuration including optional waitUntil date, token ID, and step data.
|
|
4091
|
+
*/
|
|
4092
|
+
async waitJob(jobId, options) {
|
|
4093
|
+
const now = this.nowMs();
|
|
4094
|
+
const waitUntilMs = options.waitUntil ? options.waitUntil.getTime().toString() : "null";
|
|
4095
|
+
const waitTokenId = options.waitTokenId ?? "null";
|
|
4096
|
+
const stepDataJson = JSON.stringify(options.stepData);
|
|
4097
|
+
const result = await this.client.eval(
|
|
4098
|
+
WAIT_JOB_SCRIPT,
|
|
4099
|
+
1,
|
|
4100
|
+
this.prefix,
|
|
4101
|
+
jobId,
|
|
4102
|
+
waitUntilMs,
|
|
4103
|
+
waitTokenId,
|
|
4104
|
+
stepDataJson,
|
|
4105
|
+
now
|
|
4106
|
+
);
|
|
4107
|
+
if (Number(result) === 0) {
|
|
4108
|
+
log(
|
|
4109
|
+
`Job ${jobId} could not be set to waiting (may have been reclaimed or is no longer processing)`
|
|
4110
|
+
);
|
|
4111
|
+
return;
|
|
4112
|
+
}
|
|
4113
|
+
await this.recordJobEvent(jobId, "waiting" /* Waiting */, {
|
|
4114
|
+
waitUntil: options.waitUntil?.toISOString() ?? null,
|
|
4115
|
+
waitTokenId: options.waitTokenId ?? null
|
|
4116
|
+
});
|
|
4117
|
+
log(`Job ${jobId} set to waiting`);
|
|
2815
4118
|
}
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
4119
|
+
/**
|
|
4120
|
+
* Persist step data for a job. Called after each ctx.run() step completes.
|
|
4121
|
+
* Best-effort: does not throw to avoid killing the running handler.
|
|
4122
|
+
*
|
|
4123
|
+
* @param jobId - The job to update.
|
|
4124
|
+
* @param stepData - The step data to persist.
|
|
4125
|
+
*/
|
|
4126
|
+
async updateStepData(jobId, stepData) {
|
|
4127
|
+
try {
|
|
4128
|
+
const now = this.nowMs();
|
|
4129
|
+
await this.client.hset(
|
|
4130
|
+
`${this.prefix}job:${jobId}`,
|
|
4131
|
+
"stepData",
|
|
4132
|
+
JSON.stringify(stepData),
|
|
4133
|
+
"updatedAt",
|
|
4134
|
+
now.toString()
|
|
4135
|
+
);
|
|
4136
|
+
} catch (error) {
|
|
4137
|
+
log(`Error updating stepData for job ${jobId}: ${error}`);
|
|
2821
4138
|
}
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
4139
|
+
}
|
|
4140
|
+
/**
|
|
4141
|
+
* Create a waitpoint token.
|
|
4142
|
+
*
|
|
4143
|
+
* @param jobId - The job ID to associate with the token (null if created outside a handler).
|
|
4144
|
+
* @param options - Optional timeout string (e.g. '10m', '1h') and tags.
|
|
4145
|
+
* @returns The created waitpoint with its unique ID.
|
|
4146
|
+
*/
|
|
4147
|
+
async createWaitpoint(jobId, options) {
|
|
4148
|
+
const id = `wp_${randomUUID()}`;
|
|
4149
|
+
const now = this.nowMs();
|
|
4150
|
+
let timeoutAt = null;
|
|
4151
|
+
if (options?.timeout) {
|
|
4152
|
+
const ms = parseTimeoutString2(options.timeout);
|
|
4153
|
+
timeoutAt = now + ms;
|
|
2826
4154
|
}
|
|
2827
|
-
|
|
2828
|
-
|
|
4155
|
+
const key = `${this.prefix}waitpoint:${id}`;
|
|
4156
|
+
const fields = [
|
|
4157
|
+
"id",
|
|
4158
|
+
id,
|
|
4159
|
+
"jobId",
|
|
4160
|
+
jobId !== null ? jobId.toString() : "null",
|
|
4161
|
+
"status",
|
|
4162
|
+
"waiting",
|
|
4163
|
+
"output",
|
|
4164
|
+
"null",
|
|
4165
|
+
"timeoutAt",
|
|
4166
|
+
timeoutAt !== null ? timeoutAt.toString() : "null",
|
|
4167
|
+
"createdAt",
|
|
4168
|
+
now.toString(),
|
|
4169
|
+
"completedAt",
|
|
4170
|
+
"null",
|
|
4171
|
+
"tags",
|
|
4172
|
+
options?.tags ? JSON.stringify(options.tags) : "null"
|
|
4173
|
+
];
|
|
4174
|
+
await this.client.hmset(key, ...fields);
|
|
4175
|
+
if (timeoutAt !== null) {
|
|
4176
|
+
await this.client.zadd(`${this.prefix}waitpoint_timeout`, timeoutAt, id);
|
|
4177
|
+
}
|
|
4178
|
+
log(`Created waitpoint ${id} for job ${jobId}`);
|
|
4179
|
+
return { id };
|
|
2829
4180
|
}
|
|
2830
|
-
|
|
2831
|
-
|
|
4181
|
+
/**
|
|
4182
|
+
* Complete a waitpoint token and move the associated job back to 'pending'.
|
|
4183
|
+
*
|
|
4184
|
+
* @param tokenId - The waitpoint token ID to complete.
|
|
4185
|
+
* @param data - Optional data to pass to the waiting handler.
|
|
4186
|
+
*/
|
|
4187
|
+
async completeWaitpoint(tokenId, data) {
|
|
4188
|
+
const now = this.nowMs();
|
|
4189
|
+
const outputJson = data != null ? JSON.stringify(data) : "null";
|
|
2832
4190
|
const result = await this.client.eval(
|
|
2833
|
-
|
|
4191
|
+
COMPLETE_WAITPOINT_SCRIPT,
|
|
2834
4192
|
1,
|
|
2835
4193
|
this.prefix,
|
|
2836
|
-
|
|
4194
|
+
tokenId,
|
|
4195
|
+
outputJson,
|
|
4196
|
+
now
|
|
2837
4197
|
);
|
|
2838
|
-
|
|
2839
|
-
|
|
4198
|
+
if (Number(result) === 0) {
|
|
4199
|
+
log(`Waitpoint ${tokenId} not found or already completed`);
|
|
4200
|
+
return;
|
|
4201
|
+
}
|
|
4202
|
+
log(`Completed waitpoint ${tokenId}`);
|
|
2840
4203
|
}
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
4204
|
+
/**
|
|
4205
|
+
* Retrieve a waitpoint token by its ID.
|
|
4206
|
+
*
|
|
4207
|
+
* @param tokenId - The waitpoint token ID to look up.
|
|
4208
|
+
* @returns The waitpoint record, or null if not found.
|
|
4209
|
+
*/
|
|
4210
|
+
async getWaitpoint(tokenId) {
|
|
4211
|
+
const data = await this.client.hgetall(
|
|
4212
|
+
`${this.prefix}waitpoint:${tokenId}`
|
|
2844
4213
|
);
|
|
2845
|
-
return
|
|
4214
|
+
if (!data || Object.keys(data).length === 0) return null;
|
|
4215
|
+
const nullish = (v) => v === void 0 || v === "null" || v === "" ? null : v;
|
|
4216
|
+
const numOrNull = (v) => {
|
|
4217
|
+
const n = nullish(v);
|
|
4218
|
+
return n === null ? null : Number(n);
|
|
4219
|
+
};
|
|
4220
|
+
const dateOrNull = (v) => {
|
|
4221
|
+
const n = numOrNull(v);
|
|
4222
|
+
return n === null ? null : new Date(n);
|
|
4223
|
+
};
|
|
4224
|
+
let output = null;
|
|
4225
|
+
if (data.output && data.output !== "null") {
|
|
4226
|
+
try {
|
|
4227
|
+
output = JSON.parse(data.output);
|
|
4228
|
+
} catch {
|
|
4229
|
+
output = data.output;
|
|
4230
|
+
}
|
|
4231
|
+
}
|
|
4232
|
+
let tags = null;
|
|
4233
|
+
if (data.tags && data.tags !== "null") {
|
|
4234
|
+
try {
|
|
4235
|
+
tags = JSON.parse(data.tags);
|
|
4236
|
+
} catch {
|
|
4237
|
+
}
|
|
4238
|
+
}
|
|
4239
|
+
return {
|
|
4240
|
+
id: data.id,
|
|
4241
|
+
jobId: numOrNull(data.jobId),
|
|
4242
|
+
status: data.status,
|
|
4243
|
+
output,
|
|
4244
|
+
timeoutAt: dateOrNull(data.timeoutAt),
|
|
4245
|
+
createdAt: new Date(Number(data.createdAt)),
|
|
4246
|
+
completedAt: dateOrNull(data.completedAt),
|
|
4247
|
+
tags
|
|
4248
|
+
};
|
|
2846
4249
|
}
|
|
2847
|
-
|
|
2848
|
-
|
|
4250
|
+
/**
|
|
4251
|
+
* Expire timed-out waitpoint tokens and move their associated jobs back to 'pending'.
|
|
4252
|
+
*
|
|
4253
|
+
* @returns The number of tokens that were expired.
|
|
4254
|
+
*/
|
|
4255
|
+
async expireTimedOutWaitpoints() {
|
|
2849
4256
|
const now = this.nowMs();
|
|
2850
4257
|
const result = await this.client.eval(
|
|
2851
|
-
|
|
4258
|
+
EXPIRE_TIMED_OUT_WAITPOINTS_SCRIPT,
|
|
2852
4259
|
1,
|
|
2853
4260
|
this.prefix,
|
|
2854
|
-
maxAgeMs,
|
|
2855
4261
|
now
|
|
2856
4262
|
);
|
|
2857
|
-
|
|
2858
|
-
|
|
4263
|
+
const count = Number(result);
|
|
4264
|
+
if (count > 0) {
|
|
4265
|
+
log(`Expired ${count} timed-out waitpoints`);
|
|
4266
|
+
}
|
|
4267
|
+
return count;
|
|
2859
4268
|
}
|
|
2860
4269
|
// ── Internal helpers ──────────────────────────────────────────────────
|
|
2861
4270
|
async setPendingReasonForUnpickedJobs(reason, jobType) {
|
|
@@ -2961,6 +4370,359 @@ var RedisBackend = class {
|
|
|
2961
4370
|
return true;
|
|
2962
4371
|
});
|
|
2963
4372
|
}
|
|
4373
|
+
// ── Cron schedules ──────────────────────────────────────────────────
|
|
4374
|
+
/** Create a cron schedule and return its ID. */
|
|
4375
|
+
async addCronSchedule(input) {
|
|
4376
|
+
const existingId = await this.client.get(
|
|
4377
|
+
`${this.prefix}cron_name:${input.scheduleName}`
|
|
4378
|
+
);
|
|
4379
|
+
if (existingId !== null) {
|
|
4380
|
+
throw new Error(
|
|
4381
|
+
`Cron schedule with name "${input.scheduleName}" already exists`
|
|
4382
|
+
);
|
|
4383
|
+
}
|
|
4384
|
+
const id = await this.client.incr(`${this.prefix}cron_id_seq`);
|
|
4385
|
+
const now = this.nowMs();
|
|
4386
|
+
const key = `${this.prefix}cron:${id}`;
|
|
4387
|
+
const fields = [
|
|
4388
|
+
"id",
|
|
4389
|
+
id.toString(),
|
|
4390
|
+
"scheduleName",
|
|
4391
|
+
input.scheduleName,
|
|
4392
|
+
"cronExpression",
|
|
4393
|
+
input.cronExpression,
|
|
4394
|
+
"jobType",
|
|
4395
|
+
input.jobType,
|
|
4396
|
+
"payload",
|
|
4397
|
+
JSON.stringify(input.payload),
|
|
4398
|
+
"maxAttempts",
|
|
4399
|
+
input.maxAttempts.toString(),
|
|
4400
|
+
"priority",
|
|
4401
|
+
input.priority.toString(),
|
|
4402
|
+
"timeoutMs",
|
|
4403
|
+
input.timeoutMs !== null ? input.timeoutMs.toString() : "null",
|
|
4404
|
+
"forceKillOnTimeout",
|
|
4405
|
+
input.forceKillOnTimeout ? "true" : "false",
|
|
4406
|
+
"tags",
|
|
4407
|
+
input.tags ? JSON.stringify(input.tags) : "null",
|
|
4408
|
+
"timezone",
|
|
4409
|
+
input.timezone,
|
|
4410
|
+
"allowOverlap",
|
|
4411
|
+
input.allowOverlap ? "true" : "false",
|
|
4412
|
+
"status",
|
|
4413
|
+
"active",
|
|
4414
|
+
"lastEnqueuedAt",
|
|
4415
|
+
"null",
|
|
4416
|
+
"lastJobId",
|
|
4417
|
+
"null",
|
|
4418
|
+
"nextRunAt",
|
|
4419
|
+
input.nextRunAt ? input.nextRunAt.getTime().toString() : "null",
|
|
4420
|
+
"createdAt",
|
|
4421
|
+
now.toString(),
|
|
4422
|
+
"updatedAt",
|
|
4423
|
+
now.toString(),
|
|
4424
|
+
"retryDelay",
|
|
4425
|
+
input.retryDelay !== null && input.retryDelay !== void 0 ? input.retryDelay.toString() : "null",
|
|
4426
|
+
"retryBackoff",
|
|
4427
|
+
input.retryBackoff !== null && input.retryBackoff !== void 0 ? input.retryBackoff.toString() : "null",
|
|
4428
|
+
"retryDelayMax",
|
|
4429
|
+
input.retryDelayMax !== null && input.retryDelayMax !== void 0 ? input.retryDelayMax.toString() : "null"
|
|
4430
|
+
];
|
|
4431
|
+
await this.client.hmset(key, ...fields);
|
|
4432
|
+
await this.client.set(
|
|
4433
|
+
`${this.prefix}cron_name:${input.scheduleName}`,
|
|
4434
|
+
id.toString()
|
|
4435
|
+
);
|
|
4436
|
+
await this.client.sadd(`${this.prefix}crons`, id.toString());
|
|
4437
|
+
await this.client.sadd(`${this.prefix}cron_status:active`, id.toString());
|
|
4438
|
+
if (input.nextRunAt) {
|
|
4439
|
+
await this.client.zadd(
|
|
4440
|
+
`${this.prefix}cron_due`,
|
|
4441
|
+
input.nextRunAt.getTime(),
|
|
4442
|
+
id.toString()
|
|
4443
|
+
);
|
|
4444
|
+
}
|
|
4445
|
+
log(`Added cron schedule ${id}: "${input.scheduleName}"`);
|
|
4446
|
+
return id;
|
|
4447
|
+
}
|
|
4448
|
+
/** Get a cron schedule by ID. */
|
|
4449
|
+
async getCronSchedule(id) {
|
|
4450
|
+
const data = await this.client.hgetall(`${this.prefix}cron:${id}`);
|
|
4451
|
+
if (!data || Object.keys(data).length === 0) return null;
|
|
4452
|
+
return this.deserializeCronSchedule(data);
|
|
4453
|
+
}
|
|
4454
|
+
/** Get a cron schedule by its unique name. */
|
|
4455
|
+
async getCronScheduleByName(name) {
|
|
4456
|
+
const id = await this.client.get(`${this.prefix}cron_name:${name}`);
|
|
4457
|
+
if (id === null) return null;
|
|
4458
|
+
return this.getCronSchedule(Number(id));
|
|
4459
|
+
}
|
|
4460
|
+
/** List cron schedules, optionally filtered by status. */
|
|
4461
|
+
async listCronSchedules(status) {
|
|
4462
|
+
let ids;
|
|
4463
|
+
if (status) {
|
|
4464
|
+
ids = await this.client.smembers(`${this.prefix}cron_status:${status}`);
|
|
4465
|
+
} else {
|
|
4466
|
+
ids = await this.client.smembers(`${this.prefix}crons`);
|
|
4467
|
+
}
|
|
4468
|
+
if (ids.length === 0) return [];
|
|
4469
|
+
const pipeline = this.client.pipeline();
|
|
4470
|
+
for (const id of ids) {
|
|
4471
|
+
pipeline.hgetall(`${this.prefix}cron:${id}`);
|
|
4472
|
+
}
|
|
4473
|
+
const results = await pipeline.exec();
|
|
4474
|
+
const schedules = [];
|
|
4475
|
+
if (results) {
|
|
4476
|
+
for (const [err, data] of results) {
|
|
4477
|
+
if (!err && data && typeof data === "object" && Object.keys(data).length > 0) {
|
|
4478
|
+
schedules.push(
|
|
4479
|
+
this.deserializeCronSchedule(data)
|
|
4480
|
+
);
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
}
|
|
4484
|
+
schedules.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
|
|
4485
|
+
return schedules;
|
|
4486
|
+
}
|
|
4487
|
+
/** Delete a cron schedule by ID. */
|
|
4488
|
+
async removeCronSchedule(id) {
|
|
4489
|
+
const data = await this.client.hgetall(`${this.prefix}cron:${id}`);
|
|
4490
|
+
if (!data || Object.keys(data).length === 0) return;
|
|
4491
|
+
const name = data.scheduleName;
|
|
4492
|
+
const status = data.status;
|
|
4493
|
+
await this.client.del(`${this.prefix}cron:${id}`);
|
|
4494
|
+
await this.client.del(`${this.prefix}cron_name:${name}`);
|
|
4495
|
+
await this.client.srem(`${this.prefix}crons`, id.toString());
|
|
4496
|
+
await this.client.srem(
|
|
4497
|
+
`${this.prefix}cron_status:${status}`,
|
|
4498
|
+
id.toString()
|
|
4499
|
+
);
|
|
4500
|
+
await this.client.zrem(`${this.prefix}cron_due`, id.toString());
|
|
4501
|
+
log(`Removed cron schedule ${id}`);
|
|
4502
|
+
}
|
|
4503
|
+
/** Pause a cron schedule. */
|
|
4504
|
+
async pauseCronSchedule(id) {
|
|
4505
|
+
const now = this.nowMs();
|
|
4506
|
+
await this.client.hset(
|
|
4507
|
+
`${this.prefix}cron:${id}`,
|
|
4508
|
+
"status",
|
|
4509
|
+
"paused",
|
|
4510
|
+
"updatedAt",
|
|
4511
|
+
now.toString()
|
|
4512
|
+
);
|
|
4513
|
+
await this.client.srem(`${this.prefix}cron_status:active`, id.toString());
|
|
4514
|
+
await this.client.sadd(`${this.prefix}cron_status:paused`, id.toString());
|
|
4515
|
+
await this.client.zrem(`${this.prefix}cron_due`, id.toString());
|
|
4516
|
+
log(`Paused cron schedule ${id}`);
|
|
4517
|
+
}
|
|
4518
|
+
/** Resume a paused cron schedule. */
|
|
4519
|
+
async resumeCronSchedule(id) {
|
|
4520
|
+
const now = this.nowMs();
|
|
4521
|
+
await this.client.hset(
|
|
4522
|
+
`${this.prefix}cron:${id}`,
|
|
4523
|
+
"status",
|
|
4524
|
+
"active",
|
|
4525
|
+
"updatedAt",
|
|
4526
|
+
now.toString()
|
|
4527
|
+
);
|
|
4528
|
+
await this.client.srem(`${this.prefix}cron_status:paused`, id.toString());
|
|
4529
|
+
await this.client.sadd(`${this.prefix}cron_status:active`, id.toString());
|
|
4530
|
+
const nextRunAt = await this.client.hget(
|
|
4531
|
+
`${this.prefix}cron:${id}`,
|
|
4532
|
+
"nextRunAt"
|
|
4533
|
+
);
|
|
4534
|
+
if (nextRunAt && nextRunAt !== "null") {
|
|
4535
|
+
await this.client.zadd(
|
|
4536
|
+
`${this.prefix}cron_due`,
|
|
4537
|
+
Number(nextRunAt),
|
|
4538
|
+
id.toString()
|
|
4539
|
+
);
|
|
4540
|
+
}
|
|
4541
|
+
log(`Resumed cron schedule ${id}`);
|
|
4542
|
+
}
|
|
4543
|
+
/** Edit a cron schedule. */
|
|
4544
|
+
async editCronSchedule(id, updates, nextRunAt) {
|
|
4545
|
+
const now = this.nowMs();
|
|
4546
|
+
const fields = [];
|
|
4547
|
+
if (updates.cronExpression !== void 0) {
|
|
4548
|
+
fields.push("cronExpression", updates.cronExpression);
|
|
4549
|
+
}
|
|
4550
|
+
if (updates.payload !== void 0) {
|
|
4551
|
+
fields.push("payload", JSON.stringify(updates.payload));
|
|
4552
|
+
}
|
|
4553
|
+
if (updates.maxAttempts !== void 0) {
|
|
4554
|
+
fields.push("maxAttempts", updates.maxAttempts.toString());
|
|
4555
|
+
}
|
|
4556
|
+
if (updates.priority !== void 0) {
|
|
4557
|
+
fields.push("priority", updates.priority.toString());
|
|
4558
|
+
}
|
|
4559
|
+
if (updates.timeoutMs !== void 0) {
|
|
4560
|
+
fields.push(
|
|
4561
|
+
"timeoutMs",
|
|
4562
|
+
updates.timeoutMs !== null ? updates.timeoutMs.toString() : "null"
|
|
4563
|
+
);
|
|
4564
|
+
}
|
|
4565
|
+
if (updates.forceKillOnTimeout !== void 0) {
|
|
4566
|
+
fields.push(
|
|
4567
|
+
"forceKillOnTimeout",
|
|
4568
|
+
updates.forceKillOnTimeout ? "true" : "false"
|
|
4569
|
+
);
|
|
4570
|
+
}
|
|
4571
|
+
if (updates.tags !== void 0) {
|
|
4572
|
+
fields.push(
|
|
4573
|
+
"tags",
|
|
4574
|
+
updates.tags !== null ? JSON.stringify(updates.tags) : "null"
|
|
4575
|
+
);
|
|
4576
|
+
}
|
|
4577
|
+
if (updates.timezone !== void 0) {
|
|
4578
|
+
fields.push("timezone", updates.timezone);
|
|
4579
|
+
}
|
|
4580
|
+
if (updates.allowOverlap !== void 0) {
|
|
4581
|
+
fields.push("allowOverlap", updates.allowOverlap ? "true" : "false");
|
|
4582
|
+
}
|
|
4583
|
+
if (updates.retryDelay !== void 0) {
|
|
4584
|
+
fields.push(
|
|
4585
|
+
"retryDelay",
|
|
4586
|
+
updates.retryDelay !== null ? updates.retryDelay.toString() : "null"
|
|
4587
|
+
);
|
|
4588
|
+
}
|
|
4589
|
+
if (updates.retryBackoff !== void 0) {
|
|
4590
|
+
fields.push(
|
|
4591
|
+
"retryBackoff",
|
|
4592
|
+
updates.retryBackoff !== null ? updates.retryBackoff.toString() : "null"
|
|
4593
|
+
);
|
|
4594
|
+
}
|
|
4595
|
+
if (updates.retryDelayMax !== void 0) {
|
|
4596
|
+
fields.push(
|
|
4597
|
+
"retryDelayMax",
|
|
4598
|
+
updates.retryDelayMax !== null ? updates.retryDelayMax.toString() : "null"
|
|
4599
|
+
);
|
|
4600
|
+
}
|
|
4601
|
+
if (nextRunAt !== void 0) {
|
|
4602
|
+
const val = nextRunAt !== null ? nextRunAt.getTime().toString() : "null";
|
|
4603
|
+
fields.push("nextRunAt", val);
|
|
4604
|
+
if (nextRunAt !== null) {
|
|
4605
|
+
await this.client.zadd(
|
|
4606
|
+
`${this.prefix}cron_due`,
|
|
4607
|
+
nextRunAt.getTime(),
|
|
4608
|
+
id.toString()
|
|
4609
|
+
);
|
|
4610
|
+
} else {
|
|
4611
|
+
await this.client.zrem(`${this.prefix}cron_due`, id.toString());
|
|
4612
|
+
}
|
|
4613
|
+
}
|
|
4614
|
+
if (fields.length === 0) {
|
|
4615
|
+
log(`No fields to update for cron schedule ${id}`);
|
|
4616
|
+
return;
|
|
4617
|
+
}
|
|
4618
|
+
fields.push("updatedAt", now.toString());
|
|
4619
|
+
await this.client.hmset(`${this.prefix}cron:${id}`, ...fields);
|
|
4620
|
+
log(`Edited cron schedule ${id}`);
|
|
4621
|
+
}
|
|
4622
|
+
/**
|
|
4623
|
+
* Fetch all active cron schedules whose nextRunAt <= now.
|
|
4624
|
+
* Uses a sorted set (cron_due) for efficient range query.
|
|
4625
|
+
*/
|
|
4626
|
+
async getDueCronSchedules() {
|
|
4627
|
+
const now = this.nowMs();
|
|
4628
|
+
const ids = await this.client.zrangebyscore(
|
|
4629
|
+
`${this.prefix}cron_due`,
|
|
4630
|
+
0,
|
|
4631
|
+
now
|
|
4632
|
+
);
|
|
4633
|
+
if (ids.length === 0) {
|
|
4634
|
+
log("Found 0 due cron schedules");
|
|
4635
|
+
return [];
|
|
4636
|
+
}
|
|
4637
|
+
const schedules = [];
|
|
4638
|
+
for (const id of ids) {
|
|
4639
|
+
const data = await this.client.hgetall(`${this.prefix}cron:${id}`);
|
|
4640
|
+
if (data && Object.keys(data).length > 0 && data.status === "active") {
|
|
4641
|
+
schedules.push(this.deserializeCronSchedule(data));
|
|
4642
|
+
}
|
|
4643
|
+
}
|
|
4644
|
+
log(`Found ${schedules.length} due cron schedules`);
|
|
4645
|
+
return schedules;
|
|
4646
|
+
}
|
|
4647
|
+
/**
|
|
4648
|
+
* Update a cron schedule after a job has been enqueued.
|
|
4649
|
+
* Sets lastEnqueuedAt, lastJobId, and advances nextRunAt.
|
|
4650
|
+
*/
|
|
4651
|
+
async updateCronScheduleAfterEnqueue(id, lastEnqueuedAt, lastJobId, nextRunAt) {
|
|
4652
|
+
const fields = [
|
|
4653
|
+
"lastEnqueuedAt",
|
|
4654
|
+
lastEnqueuedAt.getTime().toString(),
|
|
4655
|
+
"lastJobId",
|
|
4656
|
+
lastJobId.toString(),
|
|
4657
|
+
"nextRunAt",
|
|
4658
|
+
nextRunAt ? nextRunAt.getTime().toString() : "null",
|
|
4659
|
+
"updatedAt",
|
|
4660
|
+
this.nowMs().toString()
|
|
4661
|
+
];
|
|
4662
|
+
await this.client.hmset(`${this.prefix}cron:${id}`, ...fields);
|
|
4663
|
+
if (nextRunAt) {
|
|
4664
|
+
await this.client.zadd(
|
|
4665
|
+
`${this.prefix}cron_due`,
|
|
4666
|
+
nextRunAt.getTime(),
|
|
4667
|
+
id.toString()
|
|
4668
|
+
);
|
|
4669
|
+
} else {
|
|
4670
|
+
await this.client.zrem(`${this.prefix}cron_due`, id.toString());
|
|
4671
|
+
}
|
|
4672
|
+
log(
|
|
4673
|
+
`Updated cron schedule ${id}: lastJobId=${lastJobId}, nextRunAt=${nextRunAt?.toISOString() ?? "null"}`
|
|
4674
|
+
);
|
|
4675
|
+
}
|
|
4676
|
+
/** Deserialize a Redis hash into a CronScheduleRecord. */
|
|
4677
|
+
deserializeCronSchedule(h) {
|
|
4678
|
+
const nullish = (v) => v === void 0 || v === "null" || v === "" ? null : v;
|
|
4679
|
+
const numOrNull = (v) => {
|
|
4680
|
+
const n = nullish(v);
|
|
4681
|
+
return n === null ? null : Number(n);
|
|
4682
|
+
};
|
|
4683
|
+
const dateOrNull = (v) => {
|
|
4684
|
+
const n = numOrNull(v);
|
|
4685
|
+
return n === null ? null : new Date(n);
|
|
4686
|
+
};
|
|
4687
|
+
let payload;
|
|
4688
|
+
try {
|
|
4689
|
+
payload = JSON.parse(h.payload);
|
|
4690
|
+
} catch {
|
|
4691
|
+
payload = h.payload;
|
|
4692
|
+
}
|
|
4693
|
+
let tags;
|
|
4694
|
+
try {
|
|
4695
|
+
const raw = h.tags;
|
|
4696
|
+
if (raw && raw !== "null") {
|
|
4697
|
+
tags = JSON.parse(raw);
|
|
4698
|
+
}
|
|
4699
|
+
} catch {
|
|
4700
|
+
}
|
|
4701
|
+
return {
|
|
4702
|
+
id: Number(h.id),
|
|
4703
|
+
scheduleName: h.scheduleName,
|
|
4704
|
+
cronExpression: h.cronExpression,
|
|
4705
|
+
jobType: h.jobType,
|
|
4706
|
+
payload,
|
|
4707
|
+
maxAttempts: Number(h.maxAttempts),
|
|
4708
|
+
priority: Number(h.priority),
|
|
4709
|
+
timeoutMs: numOrNull(h.timeoutMs),
|
|
4710
|
+
forceKillOnTimeout: h.forceKillOnTimeout === "true",
|
|
4711
|
+
tags,
|
|
4712
|
+
timezone: h.timezone,
|
|
4713
|
+
allowOverlap: h.allowOverlap === "true",
|
|
4714
|
+
status: h.status,
|
|
4715
|
+
lastEnqueuedAt: dateOrNull(h.lastEnqueuedAt),
|
|
4716
|
+
lastJobId: numOrNull(h.lastJobId),
|
|
4717
|
+
nextRunAt: dateOrNull(h.nextRunAt),
|
|
4718
|
+
createdAt: new Date(Number(h.createdAt)),
|
|
4719
|
+
updatedAt: new Date(Number(h.updatedAt)),
|
|
4720
|
+
retryDelay: numOrNull(h.retryDelay),
|
|
4721
|
+
retryBackoff: h.retryBackoff === "true" ? true : h.retryBackoff === "false" ? false : null,
|
|
4722
|
+
retryDelayMax: numOrNull(h.retryDelayMax)
|
|
4723
|
+
};
|
|
4724
|
+
}
|
|
4725
|
+
// ── Private helpers (filters) ─────────────────────────────────────────
|
|
2964
4726
|
async applyFilters(ids, filters) {
|
|
2965
4727
|
let result = ids;
|
|
2966
4728
|
if (filters.jobType) {
|
|
@@ -2990,6 +4752,19 @@ var RedisBackend = class {
|
|
|
2990
4752
|
return result;
|
|
2991
4753
|
}
|
|
2992
4754
|
};
|
|
4755
|
+
function getNextCronOccurrence(cronExpression, timezone = "UTC", after, CronImpl = Cron) {
|
|
4756
|
+
const cron = new CronImpl(cronExpression, { timezone });
|
|
4757
|
+
const next = cron.nextRun(after ?? /* @__PURE__ */ new Date());
|
|
4758
|
+
return next ?? null;
|
|
4759
|
+
}
|
|
4760
|
+
function validateCronExpression(cronExpression, CronImpl = Cron) {
|
|
4761
|
+
try {
|
|
4762
|
+
new CronImpl(cronExpression);
|
|
4763
|
+
return true;
|
|
4764
|
+
} catch {
|
|
4765
|
+
return false;
|
|
4766
|
+
}
|
|
4767
|
+
}
|
|
2993
4768
|
|
|
2994
4769
|
// src/handler-validation.ts
|
|
2995
4770
|
function validateHandlerSerializable2(handler, jobType) {
|
|
@@ -3065,29 +4840,89 @@ var initJobQueue = (config) => {
|
|
|
3065
4840
|
const backendType = config.backend ?? "postgres";
|
|
3066
4841
|
setLogContext(config.verbose ?? false);
|
|
3067
4842
|
let backend;
|
|
3068
|
-
let pool;
|
|
3069
4843
|
if (backendType === "postgres") {
|
|
3070
4844
|
const pgConfig = config;
|
|
3071
|
-
|
|
3072
|
-
|
|
4845
|
+
if (pgConfig.pool) {
|
|
4846
|
+
backend = new PostgresBackend(pgConfig.pool);
|
|
4847
|
+
} else if (pgConfig.databaseConfig) {
|
|
4848
|
+
const pool = createPool(pgConfig.databaseConfig);
|
|
4849
|
+
backend = new PostgresBackend(pool);
|
|
4850
|
+
} else {
|
|
4851
|
+
throw new Error(
|
|
4852
|
+
'PostgreSQL backend requires either "databaseConfig" or "pool" to be provided.'
|
|
4853
|
+
);
|
|
4854
|
+
}
|
|
3073
4855
|
} else if (backendType === "redis") {
|
|
3074
|
-
const redisConfig = config
|
|
3075
|
-
|
|
4856
|
+
const redisConfig = config;
|
|
4857
|
+
if (redisConfig.client) {
|
|
4858
|
+
backend = new RedisBackend(
|
|
4859
|
+
redisConfig.client,
|
|
4860
|
+
redisConfig.keyPrefix
|
|
4861
|
+
);
|
|
4862
|
+
} else if (redisConfig.redisConfig) {
|
|
4863
|
+
backend = new RedisBackend(redisConfig.redisConfig);
|
|
4864
|
+
} else {
|
|
4865
|
+
throw new Error(
|
|
4866
|
+
'Redis backend requires either "redisConfig" or "client" to be provided.'
|
|
4867
|
+
);
|
|
4868
|
+
}
|
|
3076
4869
|
} else {
|
|
3077
4870
|
throw new Error(`Unknown backend: ${backendType}`);
|
|
3078
4871
|
}
|
|
3079
|
-
const
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
4872
|
+
const enqueueDueCronJobsImpl = async () => {
|
|
4873
|
+
const dueSchedules = await backend.getDueCronSchedules();
|
|
4874
|
+
let count = 0;
|
|
4875
|
+
for (const schedule of dueSchedules) {
|
|
4876
|
+
if (!schedule.allowOverlap && schedule.lastJobId !== null) {
|
|
4877
|
+
const lastJob = await backend.getJob(schedule.lastJobId);
|
|
4878
|
+
if (lastJob && (lastJob.status === "pending" || lastJob.status === "processing" || lastJob.status === "waiting")) {
|
|
4879
|
+
const nextRunAt2 = getNextCronOccurrence(
|
|
4880
|
+
schedule.cronExpression,
|
|
4881
|
+
schedule.timezone
|
|
4882
|
+
);
|
|
4883
|
+
await backend.updateCronScheduleAfterEnqueue(
|
|
4884
|
+
schedule.id,
|
|
4885
|
+
/* @__PURE__ */ new Date(),
|
|
4886
|
+
schedule.lastJobId,
|
|
4887
|
+
nextRunAt2
|
|
4888
|
+
);
|
|
4889
|
+
continue;
|
|
4890
|
+
}
|
|
4891
|
+
}
|
|
4892
|
+
const jobId = await backend.addJob({
|
|
4893
|
+
jobType: schedule.jobType,
|
|
4894
|
+
payload: schedule.payload,
|
|
4895
|
+
maxAttempts: schedule.maxAttempts,
|
|
4896
|
+
priority: schedule.priority,
|
|
4897
|
+
timeoutMs: schedule.timeoutMs ?? void 0,
|
|
4898
|
+
forceKillOnTimeout: schedule.forceKillOnTimeout,
|
|
4899
|
+
tags: schedule.tags,
|
|
4900
|
+
retryDelay: schedule.retryDelay ?? void 0,
|
|
4901
|
+
retryBackoff: schedule.retryBackoff ?? void 0,
|
|
4902
|
+
retryDelayMax: schedule.retryDelayMax ?? void 0
|
|
4903
|
+
});
|
|
4904
|
+
const nextRunAt = getNextCronOccurrence(
|
|
4905
|
+
schedule.cronExpression,
|
|
4906
|
+
schedule.timezone
|
|
4907
|
+
);
|
|
4908
|
+
await backend.updateCronScheduleAfterEnqueue(
|
|
4909
|
+
schedule.id,
|
|
4910
|
+
/* @__PURE__ */ new Date(),
|
|
4911
|
+
jobId,
|
|
4912
|
+
nextRunAt
|
|
3083
4913
|
);
|
|
4914
|
+
count++;
|
|
3084
4915
|
}
|
|
3085
|
-
return
|
|
4916
|
+
return count;
|
|
3086
4917
|
};
|
|
3087
4918
|
return {
|
|
3088
4919
|
// Job queue operations
|
|
3089
4920
|
addJob: withLogContext(
|
|
3090
|
-
(job) => backend.addJob(job),
|
|
4921
|
+
(job, options) => backend.addJob(job, options),
|
|
4922
|
+
config.verbose ?? false
|
|
4923
|
+
),
|
|
4924
|
+
addJobs: withLogContext(
|
|
4925
|
+
(jobs, options) => backend.addJobs(jobs, options),
|
|
3091
4926
|
config.verbose ?? false
|
|
3092
4927
|
),
|
|
3093
4928
|
getJob: withLogContext(
|
|
@@ -3107,8 +4942,8 @@ var initJobQueue = (config) => {
|
|
|
3107
4942
|
config.verbose ?? false
|
|
3108
4943
|
),
|
|
3109
4944
|
retryJob: (jobId) => backend.retryJob(jobId),
|
|
3110
|
-
cleanupOldJobs: (daysToKeep) => backend.cleanupOldJobs(daysToKeep),
|
|
3111
|
-
cleanupOldJobEvents: (daysToKeep) => backend.cleanupOldJobEvents(daysToKeep),
|
|
4945
|
+
cleanupOldJobs: (daysToKeep, batchSize) => backend.cleanupOldJobs(daysToKeep, batchSize),
|
|
4946
|
+
cleanupOldJobEvents: (daysToKeep, batchSize) => backend.cleanupOldJobEvents(daysToKeep, batchSize),
|
|
3112
4947
|
cancelJob: withLogContext(
|
|
3113
4948
|
(jobId) => backend.cancelJob(jobId),
|
|
3114
4949
|
config.verbose ?? false
|
|
@@ -3136,33 +4971,116 @@ var initJobQueue = (config) => {
|
|
|
3136
4971
|
(tags, mode = "all", limit, offset) => backend.getJobsByTags(tags, mode, limit, offset),
|
|
3137
4972
|
config.verbose ?? false
|
|
3138
4973
|
),
|
|
3139
|
-
// Job processing
|
|
3140
|
-
createProcessor: (handlers, options) => createProcessor(backend, handlers, options)
|
|
4974
|
+
// Job processing — automatically enqueues due cron jobs before each batch
|
|
4975
|
+
createProcessor: (handlers, options) => createProcessor(backend, handlers, options, async () => {
|
|
4976
|
+
await enqueueDueCronJobsImpl();
|
|
4977
|
+
}),
|
|
4978
|
+
// Background supervisor — automated maintenance
|
|
4979
|
+
createSupervisor: (options) => createSupervisor(backend, options),
|
|
3141
4980
|
// Job events
|
|
3142
4981
|
getJobEvents: withLogContext(
|
|
3143
4982
|
(jobId) => backend.getJobEvents(jobId),
|
|
3144
4983
|
config.verbose ?? false
|
|
3145
4984
|
),
|
|
3146
|
-
// Wait / Token support (
|
|
4985
|
+
// Wait / Token support (works with all backends)
|
|
3147
4986
|
createToken: withLogContext(
|
|
3148
|
-
(options) => createWaitpoint(
|
|
4987
|
+
(options) => backend.createWaitpoint(null, options),
|
|
3149
4988
|
config.verbose ?? false
|
|
3150
4989
|
),
|
|
3151
4990
|
completeToken: withLogContext(
|
|
3152
|
-
(tokenId, data) => completeWaitpoint(
|
|
4991
|
+
(tokenId, data) => backend.completeWaitpoint(tokenId, data),
|
|
3153
4992
|
config.verbose ?? false
|
|
3154
4993
|
),
|
|
3155
4994
|
getToken: withLogContext(
|
|
3156
|
-
(tokenId) => getWaitpoint(
|
|
4995
|
+
(tokenId) => backend.getWaitpoint(tokenId),
|
|
3157
4996
|
config.verbose ?? false
|
|
3158
4997
|
),
|
|
3159
4998
|
expireTimedOutTokens: withLogContext(
|
|
3160
|
-
() => expireTimedOutWaitpoints(
|
|
4999
|
+
() => backend.expireTimedOutWaitpoints(),
|
|
5000
|
+
config.verbose ?? false
|
|
5001
|
+
),
|
|
5002
|
+
// Cron schedule operations
|
|
5003
|
+
addCronJob: withLogContext(
|
|
5004
|
+
(options) => {
|
|
5005
|
+
if (!validateCronExpression(options.cronExpression)) {
|
|
5006
|
+
return Promise.reject(
|
|
5007
|
+
new Error(`Invalid cron expression: "${options.cronExpression}"`)
|
|
5008
|
+
);
|
|
5009
|
+
}
|
|
5010
|
+
const nextRunAt = getNextCronOccurrence(
|
|
5011
|
+
options.cronExpression,
|
|
5012
|
+
options.timezone ?? "UTC"
|
|
5013
|
+
);
|
|
5014
|
+
const input = {
|
|
5015
|
+
scheduleName: options.scheduleName,
|
|
5016
|
+
cronExpression: options.cronExpression,
|
|
5017
|
+
jobType: options.jobType,
|
|
5018
|
+
payload: options.payload,
|
|
5019
|
+
maxAttempts: options.maxAttempts ?? 3,
|
|
5020
|
+
priority: options.priority ?? 0,
|
|
5021
|
+
timeoutMs: options.timeoutMs ?? null,
|
|
5022
|
+
forceKillOnTimeout: options.forceKillOnTimeout ?? false,
|
|
5023
|
+
tags: options.tags,
|
|
5024
|
+
timezone: options.timezone ?? "UTC",
|
|
5025
|
+
allowOverlap: options.allowOverlap ?? false,
|
|
5026
|
+
nextRunAt,
|
|
5027
|
+
retryDelay: options.retryDelay ?? null,
|
|
5028
|
+
retryBackoff: options.retryBackoff ?? null,
|
|
5029
|
+
retryDelayMax: options.retryDelayMax ?? null
|
|
5030
|
+
};
|
|
5031
|
+
return backend.addCronSchedule(input);
|
|
5032
|
+
},
|
|
5033
|
+
config.verbose ?? false
|
|
5034
|
+
),
|
|
5035
|
+
getCronJob: withLogContext(
|
|
5036
|
+
(id) => backend.getCronSchedule(id),
|
|
5037
|
+
config.verbose ?? false
|
|
5038
|
+
),
|
|
5039
|
+
getCronJobByName: withLogContext(
|
|
5040
|
+
(name) => backend.getCronScheduleByName(name),
|
|
5041
|
+
config.verbose ?? false
|
|
5042
|
+
),
|
|
5043
|
+
listCronJobs: withLogContext(
|
|
5044
|
+
(status) => backend.listCronSchedules(status),
|
|
5045
|
+
config.verbose ?? false
|
|
5046
|
+
),
|
|
5047
|
+
removeCronJob: withLogContext(
|
|
5048
|
+
(id) => backend.removeCronSchedule(id),
|
|
5049
|
+
config.verbose ?? false
|
|
5050
|
+
),
|
|
5051
|
+
pauseCronJob: withLogContext(
|
|
5052
|
+
(id) => backend.pauseCronSchedule(id),
|
|
5053
|
+
config.verbose ?? false
|
|
5054
|
+
),
|
|
5055
|
+
resumeCronJob: withLogContext(
|
|
5056
|
+
(id) => backend.resumeCronSchedule(id),
|
|
5057
|
+
config.verbose ?? false
|
|
5058
|
+
),
|
|
5059
|
+
editCronJob: withLogContext(
|
|
5060
|
+
async (id, updates) => {
|
|
5061
|
+
if (updates.cronExpression !== void 0 && !validateCronExpression(updates.cronExpression)) {
|
|
5062
|
+
throw new Error(
|
|
5063
|
+
`Invalid cron expression: "${updates.cronExpression}"`
|
|
5064
|
+
);
|
|
5065
|
+
}
|
|
5066
|
+
let nextRunAt;
|
|
5067
|
+
if (updates.cronExpression !== void 0 || updates.timezone !== void 0) {
|
|
5068
|
+
const existing = await backend.getCronSchedule(id);
|
|
5069
|
+
const expr = updates.cronExpression ?? existing?.cronExpression ?? "";
|
|
5070
|
+
const tz = updates.timezone ?? existing?.timezone ?? "UTC";
|
|
5071
|
+
nextRunAt = getNextCronOccurrence(expr, tz);
|
|
5072
|
+
}
|
|
5073
|
+
await backend.editCronSchedule(id, updates, nextRunAt);
|
|
5074
|
+
},
|
|
5075
|
+
config.verbose ?? false
|
|
5076
|
+
),
|
|
5077
|
+
enqueueDueCronJobs: withLogContext(
|
|
5078
|
+
() => enqueueDueCronJobsImpl(),
|
|
3161
5079
|
config.verbose ?? false
|
|
3162
5080
|
),
|
|
3163
5081
|
// Advanced access
|
|
3164
5082
|
getPool: () => {
|
|
3165
|
-
if (
|
|
5083
|
+
if (!(backend instanceof PostgresBackend)) {
|
|
3166
5084
|
throw new Error(
|
|
3167
5085
|
"getPool() is only available with the PostgreSQL backend."
|
|
3168
5086
|
);
|
|
@@ -3184,6 +5102,6 @@ var withLogContext = (fn, verbose) => (...args) => {
|
|
|
3184
5102
|
return fn(...args);
|
|
3185
5103
|
};
|
|
3186
5104
|
|
|
3187
|
-
export { FailureReason, JobEventType, PostgresBackend, WaitSignal, initJobQueue, testHandlerSerialization, validateHandlerSerializable2 as validateHandlerSerializable };
|
|
5105
|
+
export { FailureReason, JobEventType, PostgresBackend, WaitSignal, getNextCronOccurrence, initJobQueue, testHandlerSerialization, validateCronExpression, validateHandlerSerializable2 as validateHandlerSerializable };
|
|
3188
5106
|
//# sourceMappingURL=index.js.map
|
|
3189
5107
|
//# sourceMappingURL=index.js.map
|