@azlib/scheduler 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Dockerfile +14 -0
- package/README.md +40 -0
- package/compose.yaml +28 -0
- package/dist/cli-config-7F2U8qPM.mjs +462 -0
- package/dist/cli-config-7F2U8qPM.mjs.map +1 -0
- package/dist/cli-config-Cz8QP_4I.cjs +512 -0
- package/dist/dashboard/cli.cjs +30 -0
- package/dist/dashboard/cli.d.cts +6 -0
- package/dist/dashboard/cli.d.cts.map +1 -0
- package/dist/dashboard/cli.d.mts +6 -0
- package/dist/dashboard/cli.d.mts.map +1 -0
- package/dist/dashboard/cli.mjs +31 -0
- package/dist/dashboard/cli.mjs.map +1 -0
- package/dist/dashboard/public/assets/index-CbmpG6_w.css +2 -0
- package/dist/dashboard/public/assets/index-eWhp41cY.js +12 -0
- package/dist/dashboard/public/index.html +13 -0
- package/dist/dashboard-types-BHjRLcxJ.d.cts +127 -0
- package/dist/dashboard-types-BHjRLcxJ.d.cts.map +1 -0
- package/dist/dashboard-types-BHjRLcxJ.d.mts +127 -0
- package/dist/dashboard-types-BHjRLcxJ.d.mts.map +1 -0
- package/dist/dashboard.cjs +9 -0
- package/dist/dashboard.d.cts +20 -0
- package/dist/dashboard.d.cts.map +1 -0
- package/dist/dashboard.d.mts +20 -0
- package/dist/dashboard.d.mts.map +1 -0
- package/dist/dashboard.mjs +4 -0
- package/dist/http-server-DN6Lg464.d.cts +19 -0
- package/dist/http-server-DN6Lg464.d.cts.map +1 -0
- package/dist/http-server-DPQ9HIK-.d.mts +19 -0
- package/dist/http-server-DPQ9HIK-.d.mts.map +1 -0
- package/dist/index.cjs +49 -442
- package/dist/index.d.cts +3 -158
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -158
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +44 -439
- package/dist/index.mjs.map +1 -1
- package/dist/queue-dashboard-service-BFXcnfUr.cjs +55 -0
- package/dist/queue-dashboard-service-C0QapgOg.mjs +52 -0
- package/dist/queue-dashboard-service-C0QapgOg.mjs.map +1 -0
- package/dist/store-dashboard-C44d7sdh.d.cts +59 -0
- package/dist/store-dashboard-C44d7sdh.d.cts.map +1 -0
- package/dist/store-dashboard-Cyf_JILa.mjs +515 -0
- package/dist/store-dashboard-Cyf_JILa.mjs.map +1 -0
- package/dist/store-dashboard-D-yIxXxL.d.mts +59 -0
- package/dist/store-dashboard-D-yIxXxL.d.mts.map +1 -0
- package/dist/store-dashboard-Sg4BBWiJ.cjs +554 -0
- package/package.json +55 -7
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
let node_crypto = require("node:crypto");
|
|
2
|
+
//#region src/core/schedule-parser.ts
|
|
3
|
+
const CRON_FIELD_PATTERN = /^\*|\*\/\d+|\d+|\d+-\d+|\d+(?:,\d+)*$/;
|
|
4
|
+
function assertValidTimezone(timezone) {
|
|
5
|
+
try {
|
|
6
|
+
new Intl.DateTimeFormat("en-US", { timeZone: timezone }).format(/* @__PURE__ */ new Date());
|
|
7
|
+
} catch {
|
|
8
|
+
throw new Error(`Invalid timezone: ${timezone}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function assertValidCronExpression(expression) {
|
|
12
|
+
const fields = expression.trim().split(/\s+/);
|
|
13
|
+
if (fields.length !== 5) throw new Error("Cron expression must contain 5 fields");
|
|
14
|
+
for (const field of fields) if (!CRON_FIELD_PATTERN.test(field)) throw new Error(`Invalid cron field: ${field}`);
|
|
15
|
+
}
|
|
16
|
+
function assertValidOnceExpression(expression) {
|
|
17
|
+
const date = new Date(expression);
|
|
18
|
+
if (Number.isNaN(date.getTime())) throw new Error("Once schedule expression must be a valid ISO datetime");
|
|
19
|
+
}
|
|
20
|
+
function parseSchedule(scheduleType, expression, timezone) {
|
|
21
|
+
assertValidTimezone(timezone);
|
|
22
|
+
if (scheduleType === "cron") assertValidCronExpression(expression);
|
|
23
|
+
else assertValidOnceExpression(expression);
|
|
24
|
+
return {
|
|
25
|
+
scheduleType,
|
|
26
|
+
expression: expression.trim(),
|
|
27
|
+
timezone
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function computeNextRunAt(parsed, now = /* @__PURE__ */ new Date()) {
|
|
31
|
+
if (parsed.scheduleType === "once") return new Date(parsed.expression).toISOString();
|
|
32
|
+
const next = new Date(now);
|
|
33
|
+
next.setUTCSeconds(0, 0);
|
|
34
|
+
next.setUTCMinutes(next.getUTCMinutes() + 1);
|
|
35
|
+
return next.toISOString();
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/core/scheduler-job-trigger.ts
|
|
39
|
+
async function dispatchJobExecution(input) {
|
|
40
|
+
const executionId = `exec_${(0, node_crypto.randomUUID)()}`;
|
|
41
|
+
const created = {
|
|
42
|
+
executionId,
|
|
43
|
+
jobId: input.job.jobId,
|
|
44
|
+
scheduledFor: input.scheduledFor,
|
|
45
|
+
triggeredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
46
|
+
status: "queued",
|
|
47
|
+
attemptCount: input.attemptCount ?? 0
|
|
48
|
+
};
|
|
49
|
+
await input.executionStore.create(created);
|
|
50
|
+
if (!input.queueService) return created;
|
|
51
|
+
const accepted = await input.queueService.enqueue({
|
|
52
|
+
idempotencyKey: input.idempotencyKey,
|
|
53
|
+
payloadRef: {
|
|
54
|
+
executionId,
|
|
55
|
+
handlerKey: input.job.handlerKey,
|
|
56
|
+
config: input.job.config
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return await input.executionStore.update(executionId, { queueTaskId: accepted.taskId }) ?? {
|
|
60
|
+
...created,
|
|
61
|
+
queueTaskId: accepted.taskId
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/core/scheduler-service-retry.ts
|
|
66
|
+
function findRetryableExecution(allExecutions, executionId) {
|
|
67
|
+
const target = allExecutions.find((item) => item.executionId === executionId);
|
|
68
|
+
if (!target) throw new Error(`Execution not found: ${executionId}`);
|
|
69
|
+
if (target.status !== "failed" && target.status !== "dead-letter") throw new Error("Only failed or dead-letter executions can be retried");
|
|
70
|
+
return target;
|
|
71
|
+
}
|
|
72
|
+
async function retryFailedExecutionById(allExecutions, jobs, executionStore, queueService, executionId) {
|
|
73
|
+
const target = findRetryableExecution(allExecutions, executionId);
|
|
74
|
+
const job = jobs.find((item) => item.jobId === target.jobId);
|
|
75
|
+
if (!job) throw new Error(`Job not found: ${target.jobId}`);
|
|
76
|
+
return dispatchJobExecution({
|
|
77
|
+
job,
|
|
78
|
+
executionStore,
|
|
79
|
+
queueService,
|
|
80
|
+
scheduledFor: target.scheduledFor,
|
|
81
|
+
attemptCount: target.attemptCount + 1,
|
|
82
|
+
idempotencyKey: `scheduler:${job.jobId}:retry:${executionId}`
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/core/persistence.ts
|
|
87
|
+
function namespaceFor(config) {
|
|
88
|
+
return config.namespace?.trim() || "azlib";
|
|
89
|
+
}
|
|
90
|
+
function tableNames(namespace) {
|
|
91
|
+
return {
|
|
92
|
+
jobs: `${namespace}__scheduler_jobs`,
|
|
93
|
+
cursors: `${namespace}__scheduler_cursors`,
|
|
94
|
+
executions: `${namespace}__scheduler_executions`
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function quoted(dialect, tableName) {
|
|
98
|
+
return dialect.quoteIdentifier(tableName);
|
|
99
|
+
}
|
|
100
|
+
async function bootstrapSchedulerPersistence(client, dialect, names) {
|
|
101
|
+
await client.transaction(async (tx) => {
|
|
102
|
+
await tx.execute(`CREATE TABLE IF NOT EXISTS ${quoted(dialect, names.jobs)} (
|
|
103
|
+
job_id TEXT PRIMARY KEY,
|
|
104
|
+
name TEXT NOT NULL,
|
|
105
|
+
handler_key TEXT NOT NULL,
|
|
106
|
+
schedule_type TEXT NOT NULL,
|
|
107
|
+
expression TEXT NOT NULL,
|
|
108
|
+
timezone TEXT NOT NULL,
|
|
109
|
+
overlap_policy TEXT,
|
|
110
|
+
missed_run_policy TEXT,
|
|
111
|
+
config_json TEXT NOT NULL,
|
|
112
|
+
enabled INTEGER NOT NULL,
|
|
113
|
+
created_at TEXT NOT NULL,
|
|
114
|
+
updated_at TEXT NOT NULL
|
|
115
|
+
)`);
|
|
116
|
+
await tx.execute(`CREATE TABLE IF NOT EXISTS ${quoted(dialect, names.cursors)} (
|
|
117
|
+
job_id TEXT PRIMARY KEY,
|
|
118
|
+
last_evaluated_at TEXT NOT NULL,
|
|
119
|
+
last_triggered_at TEXT,
|
|
120
|
+
next_run_at TEXT NOT NULL,
|
|
121
|
+
version INTEGER NOT NULL
|
|
122
|
+
)`);
|
|
123
|
+
await tx.execute(`CREATE TABLE IF NOT EXISTS ${quoted(dialect, names.executions)} (
|
|
124
|
+
execution_id TEXT PRIMARY KEY,
|
|
125
|
+
job_id TEXT NOT NULL,
|
|
126
|
+
scheduled_for TEXT NOT NULL,
|
|
127
|
+
triggered_at TEXT NOT NULL,
|
|
128
|
+
status TEXT NOT NULL,
|
|
129
|
+
queue_task_id TEXT,
|
|
130
|
+
attempt_count INTEGER NOT NULL,
|
|
131
|
+
last_error TEXT
|
|
132
|
+
)`);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function toJobRow(jobId, definition) {
|
|
136
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
137
|
+
return {
|
|
138
|
+
jobId,
|
|
139
|
+
name: definition.name,
|
|
140
|
+
handlerKey: definition.handlerKey,
|
|
141
|
+
scheduleType: definition.schedule.scheduleType,
|
|
142
|
+
expression: definition.schedule.expression,
|
|
143
|
+
timezone: definition.schedule.timezone,
|
|
144
|
+
overlapPolicy: definition.schedule.overlapPolicy ?? null,
|
|
145
|
+
missedRunPolicy: definition.schedule.missedRunPolicy ?? null,
|
|
146
|
+
configJson: JSON.stringify(definition.config),
|
|
147
|
+
enabled: definition.enabled ?? true ? 1 : 0,
|
|
148
|
+
createdAt: now,
|
|
149
|
+
updatedAt: now
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function rowToJobRecord(row) {
|
|
153
|
+
return {
|
|
154
|
+
jobId: row.jobId,
|
|
155
|
+
name: row.name,
|
|
156
|
+
handlerKey: row.handlerKey,
|
|
157
|
+
schedule: {
|
|
158
|
+
scheduleType: row.scheduleType,
|
|
159
|
+
expression: row.expression,
|
|
160
|
+
timezone: row.timezone,
|
|
161
|
+
overlapPolicy: row.overlapPolicy === null ? void 0 : row.overlapPolicy,
|
|
162
|
+
missedRunPolicy: row.missedRunPolicy === null ? void 0 : row.missedRunPolicy
|
|
163
|
+
},
|
|
164
|
+
config: JSON.parse(row.configJson),
|
|
165
|
+
enabled: Boolean(row.enabled),
|
|
166
|
+
createdAt: row.createdAt,
|
|
167
|
+
updatedAt: row.updatedAt
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async function readJobRow(client, dialect, names, jobId) {
|
|
171
|
+
const rows = await client.query(`SELECT job_id AS jobId, name, handler_key AS handlerKey, schedule_type AS scheduleType, expression, timezone, overlap_policy AS overlapPolicy, missed_run_policy AS missedRunPolicy, config_json AS configJson, enabled, created_at AS createdAt, updated_at AS updatedAt
|
|
172
|
+
FROM ${quoted(dialect, names.jobs)} WHERE job_id = ? LIMIT 1`, [jobId]);
|
|
173
|
+
return rows[0] ? rowToJobRecord(rows[0]) : null;
|
|
174
|
+
}
|
|
175
|
+
async function readCursorRow(client, dialect, names, jobId) {
|
|
176
|
+
const row = (await client.query(`SELECT job_id AS jobId, last_evaluated_at AS lastEvaluatedAt, last_triggered_at AS lastTriggeredAt, next_run_at AS nextRunAt, version
|
|
177
|
+
FROM ${quoted(dialect, names.cursors)} WHERE job_id = ? LIMIT 1`, [jobId]))[0];
|
|
178
|
+
return row ? {
|
|
179
|
+
jobId: row.jobId,
|
|
180
|
+
lastEvaluatedAt: row.lastEvaluatedAt,
|
|
181
|
+
lastTriggeredAt: row.lastTriggeredAt ?? void 0,
|
|
182
|
+
nextRunAt: row.nextRunAt,
|
|
183
|
+
version: row.version
|
|
184
|
+
} : null;
|
|
185
|
+
}
|
|
186
|
+
async function readExecutionRow(client, dialect, names, executionId) {
|
|
187
|
+
const row = (await client.query(`SELECT execution_id AS executionId, job_id AS jobId, scheduled_for AS scheduledFor, triggered_at AS triggeredAt, status, queue_task_id AS queueTaskId, attempt_count AS attemptCount, last_error AS lastError
|
|
188
|
+
FROM ${quoted(dialect, names.executions)} WHERE execution_id = ? LIMIT 1`, [executionId]))[0];
|
|
189
|
+
return row ? {
|
|
190
|
+
executionId: row.executionId,
|
|
191
|
+
jobId: row.jobId,
|
|
192
|
+
scheduledFor: row.scheduledFor,
|
|
193
|
+
triggeredAt: row.triggeredAt,
|
|
194
|
+
status: row.status,
|
|
195
|
+
queueTaskId: row.queueTaskId ?? void 0,
|
|
196
|
+
attemptCount: row.attemptCount,
|
|
197
|
+
lastError: row.lastError ?? void 0
|
|
198
|
+
} : null;
|
|
199
|
+
}
|
|
200
|
+
function createSchedulerPersistence(config) {
|
|
201
|
+
const names = tableNames(namespaceFor(config));
|
|
202
|
+
let bootstrapPromise = null;
|
|
203
|
+
async function ensureBootstrapped() {
|
|
204
|
+
if (!bootstrapPromise) bootstrapPromise = bootstrapSchedulerPersistence(config.client, config.dialect, names);
|
|
205
|
+
await bootstrapPromise;
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
jobStore: {
|
|
209
|
+
async create(jobId, definition) {
|
|
210
|
+
await ensureBootstrapped();
|
|
211
|
+
const row = toJobRow(jobId, definition);
|
|
212
|
+
await config.client.transaction(async (tx) => {
|
|
213
|
+
await tx.execute(`DELETE FROM ${quoted(config.dialect, names.jobs)} WHERE job_id = ?`, [jobId]);
|
|
214
|
+
await tx.execute(`INSERT INTO ${quoted(config.dialect, names.jobs)} (job_id, name, handler_key, schedule_type, expression, timezone, overlap_policy, missed_run_policy, config_json, enabled, created_at, updated_at)
|
|
215
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
216
|
+
row.jobId,
|
|
217
|
+
row.name,
|
|
218
|
+
row.handlerKey,
|
|
219
|
+
row.scheduleType,
|
|
220
|
+
row.expression,
|
|
221
|
+
row.timezone,
|
|
222
|
+
row.overlapPolicy,
|
|
223
|
+
row.missedRunPolicy,
|
|
224
|
+
row.configJson,
|
|
225
|
+
row.enabled,
|
|
226
|
+
row.createdAt,
|
|
227
|
+
row.updatedAt
|
|
228
|
+
]);
|
|
229
|
+
});
|
|
230
|
+
return rowToJobRecord(row);
|
|
231
|
+
},
|
|
232
|
+
async update(jobId, patch) {
|
|
233
|
+
const current = await readJobRow(config.client, config.dialect, names, jobId);
|
|
234
|
+
if (!current) return null;
|
|
235
|
+
const next = {
|
|
236
|
+
...current,
|
|
237
|
+
...patch,
|
|
238
|
+
schedule: {
|
|
239
|
+
...current.schedule,
|
|
240
|
+
...patch.schedule
|
|
241
|
+
},
|
|
242
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
243
|
+
};
|
|
244
|
+
await config.client.transaction(async (tx) => {
|
|
245
|
+
await tx.execute(`DELETE FROM ${quoted(config.dialect, names.jobs)} WHERE job_id = ?`, [jobId]);
|
|
246
|
+
await tx.execute(`INSERT INTO ${quoted(config.dialect, names.jobs)} (job_id, name, handler_key, schedule_type, expression, timezone, overlap_policy, missed_run_policy, config_json, enabled, created_at, updated_at)
|
|
247
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
248
|
+
next.jobId,
|
|
249
|
+
next.name,
|
|
250
|
+
next.handlerKey,
|
|
251
|
+
next.schedule.scheduleType,
|
|
252
|
+
next.schedule.expression,
|
|
253
|
+
next.schedule.timezone,
|
|
254
|
+
next.schedule.overlapPolicy ?? null,
|
|
255
|
+
next.schedule.missedRunPolicy ?? null,
|
|
256
|
+
JSON.stringify(next.config),
|
|
257
|
+
next.enabled ? 1 : 0,
|
|
258
|
+
next.createdAt,
|
|
259
|
+
next.updatedAt
|
|
260
|
+
]);
|
|
261
|
+
});
|
|
262
|
+
return next;
|
|
263
|
+
},
|
|
264
|
+
async setEnabled(jobId, enabled) {
|
|
265
|
+
const current = await readJobRow(config.client, config.dialect, names, jobId);
|
|
266
|
+
if (!current) return null;
|
|
267
|
+
const next = {
|
|
268
|
+
...current,
|
|
269
|
+
enabled,
|
|
270
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
271
|
+
};
|
|
272
|
+
await config.client.transaction(async (tx) => {
|
|
273
|
+
await tx.execute(`DELETE FROM ${quoted(config.dialect, names.jobs)} WHERE job_id = ?`, [jobId]);
|
|
274
|
+
await tx.execute(`INSERT INTO ${quoted(config.dialect, names.jobs)} (job_id, name, handler_key, schedule_type, expression, timezone, overlap_policy, missed_run_policy, config_json, enabled, created_at, updated_at)
|
|
275
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
276
|
+
next.jobId,
|
|
277
|
+
next.name,
|
|
278
|
+
next.handlerKey,
|
|
279
|
+
next.schedule.scheduleType,
|
|
280
|
+
next.schedule.expression,
|
|
281
|
+
next.schedule.timezone,
|
|
282
|
+
next.schedule.overlapPolicy ?? null,
|
|
283
|
+
next.schedule.missedRunPolicy ?? null,
|
|
284
|
+
JSON.stringify(next.config),
|
|
285
|
+
next.enabled ? 1 : 0,
|
|
286
|
+
next.createdAt,
|
|
287
|
+
next.updatedAt
|
|
288
|
+
]);
|
|
289
|
+
});
|
|
290
|
+
return next;
|
|
291
|
+
},
|
|
292
|
+
async get(jobId) {
|
|
293
|
+
await ensureBootstrapped();
|
|
294
|
+
return readJobRow(config.client, config.dialect, names, jobId);
|
|
295
|
+
},
|
|
296
|
+
async list() {
|
|
297
|
+
await ensureBootstrapped();
|
|
298
|
+
return (await config.client.query(`SELECT job_id AS jobId, name, handler_key AS handlerKey, schedule_type AS scheduleType, expression, timezone, overlap_policy AS overlapPolicy, missed_run_policy AS missedRunPolicy, config_json AS configJson, enabled, created_at AS createdAt, updated_at AS updatedAt
|
|
299
|
+
FROM ${quoted(config.dialect, names.jobs)} ORDER BY created_at ASC`, [])).map(rowToJobRecord);
|
|
300
|
+
},
|
|
301
|
+
async remove(jobId) {
|
|
302
|
+
await ensureBootstrapped();
|
|
303
|
+
await config.client.execute(`DELETE FROM ${quoted(config.dialect, names.jobs)} WHERE job_id = ?`, [jobId]);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
cursorStore: {
|
|
308
|
+
async set(jobId, nextRunAt, now) {
|
|
309
|
+
await ensureBootstrapped();
|
|
310
|
+
const current = await readCursorRow(config.client, config.dialect, names, jobId);
|
|
311
|
+
const next = {
|
|
312
|
+
jobId,
|
|
313
|
+
lastEvaluatedAt: now,
|
|
314
|
+
lastTriggeredAt: current?.lastTriggeredAt ?? null,
|
|
315
|
+
nextRunAt,
|
|
316
|
+
version: (current?.version ?? 0) + 1
|
|
317
|
+
};
|
|
318
|
+
await config.client.transaction(async (tx) => {
|
|
319
|
+
await tx.execute(`DELETE FROM ${quoted(config.dialect, names.cursors)} WHERE job_id = ?`, [jobId]);
|
|
320
|
+
await tx.execute(`INSERT INTO ${quoted(config.dialect, names.cursors)} (job_id, last_evaluated_at, last_triggered_at, next_run_at, version) VALUES (?, ?, ?, ?, ?)`, [
|
|
321
|
+
next.jobId,
|
|
322
|
+
next.lastEvaluatedAt,
|
|
323
|
+
next.lastTriggeredAt,
|
|
324
|
+
next.nextRunAt,
|
|
325
|
+
next.version
|
|
326
|
+
]);
|
|
327
|
+
});
|
|
328
|
+
return {
|
|
329
|
+
jobId: next.jobId,
|
|
330
|
+
lastEvaluatedAt: next.lastEvaluatedAt,
|
|
331
|
+
lastTriggeredAt: next.lastTriggeredAt ?? void 0,
|
|
332
|
+
nextRunAt: next.nextRunAt,
|
|
333
|
+
version: next.version
|
|
334
|
+
};
|
|
335
|
+
},
|
|
336
|
+
async markTriggered(jobId, triggeredAt) {
|
|
337
|
+
await ensureBootstrapped();
|
|
338
|
+
const current = await readCursorRow(config.client, config.dialect, names, jobId);
|
|
339
|
+
if (!current) return null;
|
|
340
|
+
const next = {
|
|
341
|
+
...current,
|
|
342
|
+
lastTriggeredAt: triggeredAt,
|
|
343
|
+
version: current.version + 1
|
|
344
|
+
};
|
|
345
|
+
await config.client.execute(`UPDATE ${quoted(config.dialect, names.cursors)} SET last_triggered_at = ?, version = ? WHERE job_id = ?`, [
|
|
346
|
+
next.lastTriggeredAt,
|
|
347
|
+
next.version,
|
|
348
|
+
jobId
|
|
349
|
+
]);
|
|
350
|
+
return {
|
|
351
|
+
jobId: next.jobId,
|
|
352
|
+
lastEvaluatedAt: next.lastEvaluatedAt,
|
|
353
|
+
lastTriggeredAt: next.lastTriggeredAt ?? void 0,
|
|
354
|
+
nextRunAt: next.nextRunAt,
|
|
355
|
+
version: next.version
|
|
356
|
+
};
|
|
357
|
+
},
|
|
358
|
+
async get(jobId) {
|
|
359
|
+
await ensureBootstrapped();
|
|
360
|
+
return readCursorRow(config.client, config.dialect, names, jobId);
|
|
361
|
+
},
|
|
362
|
+
async remove(jobId) {
|
|
363
|
+
await ensureBootstrapped();
|
|
364
|
+
await config.client.execute(`DELETE FROM ${quoted(config.dialect, names.cursors)} WHERE job_id = ?`, [jobId]);
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
executionStore: {
|
|
369
|
+
async create(record) {
|
|
370
|
+
await ensureBootstrapped();
|
|
371
|
+
await config.client.execute(`INSERT INTO ${quoted(config.dialect, names.executions)} (execution_id, job_id, scheduled_for, triggered_at, status, queue_task_id, attempt_count, last_error)
|
|
372
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
373
|
+
record.executionId,
|
|
374
|
+
record.jobId,
|
|
375
|
+
record.scheduledFor,
|
|
376
|
+
record.triggeredAt,
|
|
377
|
+
record.status,
|
|
378
|
+
record.queueTaskId ?? null,
|
|
379
|
+
record.attemptCount,
|
|
380
|
+
record.lastError ?? null
|
|
381
|
+
]);
|
|
382
|
+
return record;
|
|
383
|
+
},
|
|
384
|
+
async update(executionId, patch) {
|
|
385
|
+
const current = await readExecutionRow(config.client, config.dialect, names, executionId);
|
|
386
|
+
if (!current) return null;
|
|
387
|
+
const next = {
|
|
388
|
+
...current,
|
|
389
|
+
...patch
|
|
390
|
+
};
|
|
391
|
+
await config.client.execute(`UPDATE ${quoted(config.dialect, names.executions)} SET job_id = ?, scheduled_for = ?, triggered_at = ?, status = ?, queue_task_id = ?, attempt_count = ?, last_error = ? WHERE execution_id = ?`, [
|
|
392
|
+
next.jobId,
|
|
393
|
+
next.scheduledFor,
|
|
394
|
+
next.triggeredAt,
|
|
395
|
+
next.status,
|
|
396
|
+
next.queueTaskId ?? null,
|
|
397
|
+
next.attemptCount,
|
|
398
|
+
next.lastError ?? null,
|
|
399
|
+
executionId
|
|
400
|
+
]);
|
|
401
|
+
return next;
|
|
402
|
+
},
|
|
403
|
+
async listByJob(jobId) {
|
|
404
|
+
await ensureBootstrapped();
|
|
405
|
+
return (await config.client.query(`SELECT execution_id AS executionId, job_id AS jobId, scheduled_for AS scheduledFor, triggered_at AS triggeredAt, status, queue_task_id AS queueTaskId, attempt_count AS attemptCount, last_error AS lastError
|
|
406
|
+
FROM ${quoted(config.dialect, names.executions)} WHERE job_id = ? ORDER BY triggered_at ASC`, [jobId])).map((row) => ({
|
|
407
|
+
executionId: row.executionId,
|
|
408
|
+
jobId: row.jobId,
|
|
409
|
+
scheduledFor: row.scheduledFor,
|
|
410
|
+
triggeredAt: row.triggeredAt,
|
|
411
|
+
status: row.status,
|
|
412
|
+
queueTaskId: row.queueTaskId ?? void 0,
|
|
413
|
+
attemptCount: row.attemptCount,
|
|
414
|
+
lastError: row.lastError ?? void 0
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/dashboard/store-dashboard.ts
|
|
422
|
+
function nextRunForJob(job, now = /* @__PURE__ */ new Date()) {
|
|
423
|
+
return computeNextRunAt(parseSchedule(job.schedule.scheduleType, job.schedule.expression, job.schedule.timezone), now);
|
|
424
|
+
}
|
|
425
|
+
async function toDashboardItem(jobStore, cursorStore, executionStore, jobId) {
|
|
426
|
+
const job = await jobStore.get(jobId);
|
|
427
|
+
if (!job) return null;
|
|
428
|
+
const cursor = await cursorStore.get(jobId);
|
|
429
|
+
const executions = await executionStore.listByJob(jobId);
|
|
430
|
+
return {
|
|
431
|
+
...job,
|
|
432
|
+
nextRunAt: cursor?.nextRunAt,
|
|
433
|
+
executions
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function createSchedulerDashboardFromStores(stores, options) {
|
|
437
|
+
const { jobStore, cursorStore, executionStore } = stores;
|
|
438
|
+
const handlerKeys = [...options?.handlerKeys ?? []];
|
|
439
|
+
return {
|
|
440
|
+
async getHealth() {
|
|
441
|
+
const jobs = await jobStore.list();
|
|
442
|
+
const failed = (await Promise.all(jobs.map((job) => executionStore.listByJob(job.jobId)))).flat().filter((item) => item.status === "failed" || item.status === "dead-letter");
|
|
443
|
+
return {
|
|
444
|
+
totalJobs: jobs.length,
|
|
445
|
+
enabledJobs: jobs.filter((job) => job.enabled).length,
|
|
446
|
+
pausedJobs: jobs.filter((job) => !job.enabled).length,
|
|
447
|
+
failedExecutions: failed.length
|
|
448
|
+
};
|
|
449
|
+
},
|
|
450
|
+
async listItems() {
|
|
451
|
+
const jobs = await jobStore.list();
|
|
452
|
+
return Promise.all(jobs.map(async (job) => {
|
|
453
|
+
return await toDashboardItem(jobStore, cursorStore, executionStore, job.jobId);
|
|
454
|
+
}));
|
|
455
|
+
},
|
|
456
|
+
async getJob(jobId) {
|
|
457
|
+
return toDashboardItem(jobStore, cursorStore, executionStore, jobId);
|
|
458
|
+
},
|
|
459
|
+
async createJob(job) {
|
|
460
|
+
parseSchedule(job.schedule.scheduleType, job.schedule.expression, job.schedule.timezone);
|
|
461
|
+
const jobId = `job_${(0, node_crypto.randomUUID)()}`;
|
|
462
|
+
const nextRunAt = nextRunForJob(await jobStore.create(jobId, job));
|
|
463
|
+
await cursorStore.set(jobId, nextRunAt, (/* @__PURE__ */ new Date()).toISOString());
|
|
464
|
+
return { jobId };
|
|
465
|
+
},
|
|
466
|
+
async updateJob(jobId, patch) {
|
|
467
|
+
const updated = await jobStore.update(jobId, patch);
|
|
468
|
+
if (!updated) throw new Error(`Job not found: ${jobId}`);
|
|
469
|
+
const nextRunAt = nextRunForJob(updated);
|
|
470
|
+
await cursorStore.set(jobId, nextRunAt, (/* @__PURE__ */ new Date()).toISOString());
|
|
471
|
+
},
|
|
472
|
+
async pauseJob(jobId) {
|
|
473
|
+
if (!await jobStore.setEnabled(jobId, false)) throw new Error(`Job not found: ${jobId}`);
|
|
474
|
+
},
|
|
475
|
+
async resumeJob(jobId) {
|
|
476
|
+
if (!await jobStore.setEnabled(jobId, true)) throw new Error(`Job not found: ${jobId}`);
|
|
477
|
+
},
|
|
478
|
+
async deleteJob(jobId) {
|
|
479
|
+
const removed = await jobStore.remove(jobId);
|
|
480
|
+
await cursorStore.remove(jobId);
|
|
481
|
+
if (!removed) throw new Error(`Job not found: ${jobId}`);
|
|
482
|
+
},
|
|
483
|
+
async runJob(jobId) {
|
|
484
|
+
const job = await jobStore.get(jobId);
|
|
485
|
+
if (!job) throw new Error(`Job not found: ${jobId}`);
|
|
486
|
+
const scheduledFor = (/* @__PURE__ */ new Date()).toISOString();
|
|
487
|
+
await dispatchJobExecution({
|
|
488
|
+
job,
|
|
489
|
+
executionStore,
|
|
490
|
+
queueService: options?.queueService,
|
|
491
|
+
scheduledFor,
|
|
492
|
+
attemptCount: 0,
|
|
493
|
+
idempotencyKey: `scheduler:${job.jobId}:manual:${scheduledFor}`
|
|
494
|
+
});
|
|
495
|
+
},
|
|
496
|
+
async listExecutions(jobId) {
|
|
497
|
+
if (!await jobStore.get(jobId)) throw new Error(`Job not found: ${jobId}`);
|
|
498
|
+
return executionStore.listByJob(jobId);
|
|
499
|
+
},
|
|
500
|
+
async retryExecution(executionId) {
|
|
501
|
+
const jobs = await jobStore.list();
|
|
502
|
+
await retryFailedExecutionById((await Promise.all(jobs.map((job) => executionStore.listByJob(job.jobId)))).flat(), jobs, executionStore, options?.queueService, executionId);
|
|
503
|
+
},
|
|
504
|
+
listHandlerKeys() {
|
|
505
|
+
return handlerKeys;
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
function createSchedulerDashboardFromPersistence(persistence, options) {
|
|
510
|
+
return createSchedulerDashboardFromStores(createSchedulerPersistence(persistence), options);
|
|
511
|
+
}
|
|
512
|
+
//#endregion
|
|
513
|
+
Object.defineProperty(exports, "computeNextRunAt", {
|
|
514
|
+
enumerable: true,
|
|
515
|
+
get: function() {
|
|
516
|
+
return computeNextRunAt;
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
Object.defineProperty(exports, "createSchedulerDashboardFromPersistence", {
|
|
520
|
+
enumerable: true,
|
|
521
|
+
get: function() {
|
|
522
|
+
return createSchedulerDashboardFromPersistence;
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
Object.defineProperty(exports, "createSchedulerDashboardFromStores", {
|
|
526
|
+
enumerable: true,
|
|
527
|
+
get: function() {
|
|
528
|
+
return createSchedulerDashboardFromStores;
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
Object.defineProperty(exports, "createSchedulerPersistence", {
|
|
532
|
+
enumerable: true,
|
|
533
|
+
get: function() {
|
|
534
|
+
return createSchedulerPersistence;
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
Object.defineProperty(exports, "dispatchJobExecution", {
|
|
538
|
+
enumerable: true,
|
|
539
|
+
get: function() {
|
|
540
|
+
return dispatchJobExecution;
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
Object.defineProperty(exports, "parseSchedule", {
|
|
544
|
+
enumerable: true,
|
|
545
|
+
get: function() {
|
|
546
|
+
return parseSchedule;
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
Object.defineProperty(exports, "retryFailedExecutionById", {
|
|
550
|
+
enumerable: true,
|
|
551
|
+
get: function() {
|
|
552
|
+
return retryFailedExecutionById;
|
|
553
|
+
}
|
|
554
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azlib/scheduler",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -9,28 +9,74 @@
|
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"dist",
|
|
12
|
-
"README.md"
|
|
12
|
+
"README.md",
|
|
13
|
+
"Dockerfile",
|
|
14
|
+
"compose.yaml"
|
|
13
15
|
],
|
|
14
16
|
"sideEffects": false,
|
|
17
|
+
"bin": {
|
|
18
|
+
"azlib-scheduler-dashboard": "./dist/dashboard/cli.mjs"
|
|
19
|
+
},
|
|
15
20
|
"exports": {
|
|
16
21
|
".": {
|
|
17
22
|
"types": "./dist/index.d.mts",
|
|
18
23
|
"import": "./dist/index.mjs",
|
|
19
24
|
"require": "./dist/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./dashboard": {
|
|
27
|
+
"types": "./dist/dashboard.d.mts",
|
|
28
|
+
"import": "./dist/dashboard.mjs",
|
|
29
|
+
"require": "./dist/dashboard.cjs"
|
|
20
30
|
}
|
|
21
31
|
},
|
|
22
32
|
"dependencies": {
|
|
23
33
|
"@azlib/cache": "0.2.2",
|
|
24
34
|
"@azlib/logger": "0.2.2",
|
|
25
|
-
"@azlib/persistence": "0.5.0",
|
|
26
35
|
"@azlib/queue": "1.0.5",
|
|
36
|
+
"@azlib/persistence": "0.5.0",
|
|
27
37
|
"@azlib/std": "0.5.0"
|
|
28
38
|
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"better-sqlite3": ">=9.0.0",
|
|
41
|
+
"mssql": ">=10.0.0",
|
|
42
|
+
"mysql2": ">=3.0.0",
|
|
43
|
+
"pg": ">=8.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"better-sqlite3": {
|
|
47
|
+
"optional": true
|
|
48
|
+
},
|
|
49
|
+
"mssql": {
|
|
50
|
+
"optional": true
|
|
51
|
+
},
|
|
52
|
+
"mysql2": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"pg": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
},
|
|
29
59
|
"devDependencies": {
|
|
60
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
61
|
+
"@testing-library/react": "^16.3.2",
|
|
62
|
+
"@testing-library/user-event": "^14.6.1",
|
|
30
63
|
"@types/node": "^22.20.1",
|
|
64
|
+
"@types/pg": "^8.15.5",
|
|
65
|
+
"@types/react": "19.2.14",
|
|
66
|
+
"@types/react-dom": "19.2.3",
|
|
67
|
+
"@vitejs/plugin-react": "^6.0.4",
|
|
68
|
+
"@vitest/browser-playwright": "^4.1.11",
|
|
69
|
+
"jsdom": "^29.1.1",
|
|
70
|
+
"pg": "^8.16.3",
|
|
71
|
+
"playwright": "^1.62.1",
|
|
72
|
+
"react": "^19.2.8",
|
|
73
|
+
"react-dom": "^19.2.8",
|
|
31
74
|
"tsdown": "^0.22.14",
|
|
32
75
|
"typescript": "7.0.2",
|
|
33
|
-
"
|
|
76
|
+
"vite": "^8.1.5",
|
|
77
|
+
"vitest": "^4.1.11",
|
|
78
|
+
"vitest-browser-react": "^2.2.0",
|
|
79
|
+
"@azlib/react": "0.10.5",
|
|
34
80
|
"@azlib/ts-config": "0.0.0"
|
|
35
81
|
},
|
|
36
82
|
"publishConfig": {
|
|
@@ -38,10 +84,12 @@
|
|
|
38
84
|
"registry": "https://registry.npmjs.org"
|
|
39
85
|
},
|
|
40
86
|
"scripts": {
|
|
41
|
-
"build": "rm -rf dist && tsdown",
|
|
87
|
+
"build": "rm -rf dist && tsdown && vite build --config dashboard-app/vite.config.ts",
|
|
42
88
|
"dev": "tsdown --watch",
|
|
43
|
-
"lint": "tsc -p tsconfig.json --noEmit",
|
|
44
|
-
"test": "vitest run",
|
|
89
|
+
"lint": "tsc -p tsconfig.json --noEmit && tsc -p dashboard-app/tsconfig.json --noEmit",
|
|
90
|
+
"test": "vitest run --project unit --project ui",
|
|
91
|
+
"test:browser": "vitest run --project browser",
|
|
92
|
+
"test:browser:headed": "vitest run --project browser --browser.headless=false",
|
|
45
93
|
"clean": "rm -rf .turbo node_modules dist"
|
|
46
94
|
}
|
|
47
95
|
}
|