@sigil-dev/plugin-jobs 0.9.21 → 0.9.22
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/README.md +159 -154
- package/index.ts +480 -452
- package/package.json +3 -3
- package/queue.ts +396 -396
- package/store-bun.ts +137 -137
- package/store-drizzle.ts +86 -86
- package/store.ts +117 -117
- package/worker.ts +128 -128
package/store.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The interface a job store has to satisfy, and the dialect capabilities it
|
|
3
|
-
* declares.
|
|
4
|
-
*
|
|
5
|
-
* The previous version took `query(sql, params) filling ? placeholders` and
|
|
6
|
-
* hoped. That was wrong against MariaDB in two separate ways, both found by
|
|
7
|
-
* running the queue against a real MariaDB rather than by reading it:
|
|
8
|
-
*
|
|
9
|
-
* UPDATE ... RETURNING id syntax error. MariaDB has no RETURNING, on
|
|
10
|
-
* an UPDATE or anywhere else.
|
|
11
|
-
* '2026-09-27T21:17:22.073Z' Incorrect datetime value for TIMESTAMP.
|
|
12
|
-
*
|
|
13
|
-
* Neither was a bug in the tests, because there was no test that touched
|
|
14
|
-
* MySQL. So the queue compiled, passed 21 tests, and could not claim a
|
|
15
|
-
* single job on one of the three databases it documents support for.
|
|
16
|
-
*
|
|
17
|
-
* The fix is to stop inferring. A store declares what its dialect can do,
|
|
18
|
-
* and the queue picks a strategy per capability instead of per guess:
|
|
19
|
-
*
|
|
20
|
-
* timestamp How a timestamp is spelled. Postgres and sqlite accept
|
|
21
|
-
* ISO 8601 with a T and a Z; MySQL rejects both and wants
|
|
22
|
-
* "YYYY-MM-DD HH:MM:SS.mmm". Passing an ISO string at a
|
|
23
|
-
* TIMESTAMP column is the single most portable-looking
|
|
24
|
-
* thing you can do and it does not work on MySQL.
|
|
25
|
-
* skipLocked `FOR UPDATE SKIP LOCKED` exists on postgres and MySQL.
|
|
26
|
-
* sqlite has no row locking at all. This only changes
|
|
27
|
-
* throughput — a worker that cannot skip will block behind
|
|
28
|
-
* rows another worker holds.
|
|
29
|
-
*
|
|
30
|
-
* A store that declares nothing is assumed to be the weakest thing that
|
|
31
|
-
* exists: no RETURNING, ISO timestamps, no SKIP LOCKED. Wrong there is
|
|
32
|
-
* loud rather than silent, which is the point.
|
|
33
|
-
*/
|
|
34
|
-
export interface JobStore {
|
|
35
|
-
/** Run a statement and return rows. */
|
|
36
|
-
query<T = Record<string, unknown>>(
|
|
37
|
-
sql: string,
|
|
38
|
-
params?: unknown[],
|
|
39
|
-
): Promise<T[]>;
|
|
40
|
-
|
|
41
|
-
/** What this dialect can do. Every field is optional and defaults to false. */
|
|
42
|
-
readonly capabilities?: Capabilities;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface Capabilities {
|
|
46
|
-
/** `FOR UPDATE SKIP LOCKED` is valid. @default false */
|
|
47
|
-
readonly skipLocked?: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* TIMESTAMP columns need "YYYY-MM-DD HH:MM:SS" rather than ISO 8601.
|
|
50
|
-
* @default false
|
|
51
|
-
*/
|
|
52
|
-
readonly spaceSeparatedTimestamp?: boolean;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface JobRow {
|
|
56
|
-
id: string;
|
|
57
|
-
name: string;
|
|
58
|
-
payload: string;
|
|
59
|
-
run_at: string;
|
|
60
|
-
attempts: number;
|
|
61
|
-
max_attempts: number;
|
|
62
|
-
last_error: string | null;
|
|
63
|
-
claimed_at: string | null;
|
|
64
|
-
created_at: string;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** A job as it exists before it is claimed. */
|
|
68
|
-
export interface EnqueueOptions {
|
|
69
|
-
/** Delay execution. Defaults to now. */
|
|
70
|
-
runAt?: Date;
|
|
71
|
-
/** Give up after this many attempts. Defaults to 5. */
|
|
72
|
-
maxAttempts?: number;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface JobStats {
|
|
76
|
-
pending: number;
|
|
77
|
-
claimed: number;
|
|
78
|
-
failed: number;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** What a handler knows about the run it is in. */
|
|
82
|
-
export interface JobContext {
|
|
83
|
-
/** The job's id. Stable across retries, so it can key idempotency. */
|
|
84
|
-
id: string;
|
|
85
|
-
name: string;
|
|
86
|
-
/** 1 on the first run, 2 on the first retry, and so on. */
|
|
87
|
-
attempt: number;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* A job handler. The payload type is what `enqueue` checks against, so
|
|
92
|
-
* annotate it and the call sites are typed:
|
|
93
|
-
*
|
|
94
|
-
* ```ts
|
|
95
|
-
* handlers: { sendMail: async (p: { to: string }) => { ... } }
|
|
96
|
-
* jobs.enqueue("sendMail", { to: "a@b.c" }) // checked
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
// biome-ignore lint/suspicious/noExplicitAny: the default accepts any handler, and a narrower one is inferred per name
|
|
100
|
-
export type JobHandler<P = any> = (payload: P, ctx: JobContext) => Promise<void> | void;
|
|
101
|
-
|
|
102
|
-
/** The payload a handler expects. */
|
|
103
|
-
export type PayloadOf<H> = H extends (payload: infer P, ...rest: never[]) => unknown ? P : never;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* How often a schedule runs: milliseconds, or `"30s"`, `"5m"`, `"1h"`,
|
|
107
|
-
* `"1d"`.
|
|
108
|
-
*/
|
|
109
|
-
export type Every = number | `${number}${"ms" | "s" | "m" | "h" | "d"}`;
|
|
110
|
-
|
|
111
|
-
export function everyMs(every: Every): number {
|
|
112
|
-
if (typeof every === "number") return every;
|
|
113
|
-
const m = /^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/.exec(every);
|
|
114
|
-
if (!m) throw new Error(`plugin-jobs: cannot read "${every}" as an interval`);
|
|
115
|
-
const unit = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "ms"];
|
|
116
|
-
return Number(m[1]) * unit;
|
|
117
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The interface a job store has to satisfy, and the dialect capabilities it
|
|
3
|
+
* declares.
|
|
4
|
+
*
|
|
5
|
+
* The previous version took `query(sql, params) filling ? placeholders` and
|
|
6
|
+
* hoped. That was wrong against MariaDB in two separate ways, both found by
|
|
7
|
+
* running the queue against a real MariaDB rather than by reading it:
|
|
8
|
+
*
|
|
9
|
+
* UPDATE ... RETURNING id syntax error. MariaDB has no RETURNING, on
|
|
10
|
+
* an UPDATE or anywhere else.
|
|
11
|
+
* '2026-09-27T21:17:22.073Z' Incorrect datetime value for TIMESTAMP.
|
|
12
|
+
*
|
|
13
|
+
* Neither was a bug in the tests, because there was no test that touched
|
|
14
|
+
* MySQL. So the queue compiled, passed 21 tests, and could not claim a
|
|
15
|
+
* single job on one of the three databases it documents support for.
|
|
16
|
+
*
|
|
17
|
+
* The fix is to stop inferring. A store declares what its dialect can do,
|
|
18
|
+
* and the queue picks a strategy per capability instead of per guess:
|
|
19
|
+
*
|
|
20
|
+
* timestamp How a timestamp is spelled. Postgres and sqlite accept
|
|
21
|
+
* ISO 8601 with a T and a Z; MySQL rejects both and wants
|
|
22
|
+
* "YYYY-MM-DD HH:MM:SS.mmm". Passing an ISO string at a
|
|
23
|
+
* TIMESTAMP column is the single most portable-looking
|
|
24
|
+
* thing you can do and it does not work on MySQL.
|
|
25
|
+
* skipLocked `FOR UPDATE SKIP LOCKED` exists on postgres and MySQL.
|
|
26
|
+
* sqlite has no row locking at all. This only changes
|
|
27
|
+
* throughput — a worker that cannot skip will block behind
|
|
28
|
+
* rows another worker holds.
|
|
29
|
+
*
|
|
30
|
+
* A store that declares nothing is assumed to be the weakest thing that
|
|
31
|
+
* exists: no RETURNING, ISO timestamps, no SKIP LOCKED. Wrong there is
|
|
32
|
+
* loud rather than silent, which is the point.
|
|
33
|
+
*/
|
|
34
|
+
export interface JobStore {
|
|
35
|
+
/** Run a statement and return rows. */
|
|
36
|
+
query<T = Record<string, unknown>>(
|
|
37
|
+
sql: string,
|
|
38
|
+
params?: unknown[],
|
|
39
|
+
): Promise<T[]>;
|
|
40
|
+
|
|
41
|
+
/** What this dialect can do. Every field is optional and defaults to false. */
|
|
42
|
+
readonly capabilities?: Capabilities;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Capabilities {
|
|
46
|
+
/** `FOR UPDATE SKIP LOCKED` is valid. @default false */
|
|
47
|
+
readonly skipLocked?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* TIMESTAMP columns need "YYYY-MM-DD HH:MM:SS" rather than ISO 8601.
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
readonly spaceSeparatedTimestamp?: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface JobRow {
|
|
56
|
+
id: string;
|
|
57
|
+
name: string;
|
|
58
|
+
payload: string;
|
|
59
|
+
run_at: string;
|
|
60
|
+
attempts: number;
|
|
61
|
+
max_attempts: number;
|
|
62
|
+
last_error: string | null;
|
|
63
|
+
claimed_at: string | null;
|
|
64
|
+
created_at: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A job as it exists before it is claimed. */
|
|
68
|
+
export interface EnqueueOptions {
|
|
69
|
+
/** Delay execution. Defaults to now. */
|
|
70
|
+
runAt?: Date;
|
|
71
|
+
/** Give up after this many attempts. Defaults to 5. */
|
|
72
|
+
maxAttempts?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface JobStats {
|
|
76
|
+
pending: number;
|
|
77
|
+
claimed: number;
|
|
78
|
+
failed: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** What a handler knows about the run it is in. */
|
|
82
|
+
export interface JobContext {
|
|
83
|
+
/** The job's id. Stable across retries, so it can key idempotency. */
|
|
84
|
+
id: string;
|
|
85
|
+
name: string;
|
|
86
|
+
/** 1 on the first run, 2 on the first retry, and so on. */
|
|
87
|
+
attempt: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A job handler. The payload type is what `enqueue` checks against, so
|
|
92
|
+
* annotate it and the call sites are typed:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* handlers: { sendMail: async (p: { to: string }) => { ... } }
|
|
96
|
+
* jobs.enqueue("sendMail", { to: "a@b.c" }) // checked
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
// biome-ignore lint/suspicious/noExplicitAny: the default accepts any handler, and a narrower one is inferred per name
|
|
100
|
+
export type JobHandler<P = any> = (payload: P, ctx: JobContext) => Promise<void> | void;
|
|
101
|
+
|
|
102
|
+
/** The payload a handler expects. */
|
|
103
|
+
export type PayloadOf<H> = H extends (payload: infer P, ...rest: never[]) => unknown ? P : never;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* How often a schedule runs: milliseconds, or `"30s"`, `"5m"`, `"1h"`,
|
|
107
|
+
* `"1d"`.
|
|
108
|
+
*/
|
|
109
|
+
export type Every = number | `${number}${"ms" | "s" | "m" | "h" | "d"}`;
|
|
110
|
+
|
|
111
|
+
export function everyMs(every: Every): number {
|
|
112
|
+
if (typeof every === "number") return every;
|
|
113
|
+
const m = /^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/.exec(every);
|
|
114
|
+
if (!m) throw new Error(`plugin-jobs: cannot read "${every}" as an interval`);
|
|
115
|
+
const unit = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "ms"];
|
|
116
|
+
return Number(m[1]) * unit;
|
|
117
|
+
}
|
package/worker.ts
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
import type { JobQueue } from "./queue.ts";
|
|
2
|
-
import type { JobHandler } from "./store.ts";
|
|
3
|
-
|
|
4
|
-
export interface WorkerOptions {
|
|
5
|
-
/** How many jobs to claim per pass. */
|
|
6
|
-
batchSize?: number;
|
|
7
|
-
/** Milliseconds between passes when the queue is empty. */
|
|
8
|
-
intervalMs?: number;
|
|
9
|
-
/**
|
|
10
|
-
* A claim older than this is treated as abandoned — the worker that took
|
|
11
|
-
* it died without releasing. Without this, one crashed worker leaves its
|
|
12
|
-
* jobs claimed forever.
|
|
13
|
-
*/
|
|
14
|
-
staleMs?: number;
|
|
15
|
-
/** Backoff after a handler throws. */
|
|
16
|
-
backoffMs?: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Polls a JobQueue and runs handlers.
|
|
21
|
-
*
|
|
22
|
-
* Nothing here is clever: claim a batch, run each, complete or fail. The
|
|
23
|
-
* parts that matter are the ones that are easy to get wrong and hard to
|
|
24
|
-
* notice:
|
|
25
|
-
*
|
|
26
|
-
* - A throwing handler is caught per job, so one bad job does not stop the
|
|
27
|
-
* batch. If it did, a single poisonous job would block every job behind
|
|
28
|
-
* it, forever.
|
|
29
|
-
* - SIGTERM drains before exiting, so a deploy does not abandon claimed
|
|
30
|
-
* work. The stale-claim timeout is the backstop when a worker is killed
|
|
31
|
-
* without getting to drain at all.
|
|
32
|
-
*/
|
|
33
|
-
export class JobWorker {
|
|
34
|
-
private readonly batchSize: number;
|
|
35
|
-
private readonly intervalMs: number;
|
|
36
|
-
private readonly staleMs: number;
|
|
37
|
-
private readonly backoffMs: number;
|
|
38
|
-
private stopping = false;
|
|
39
|
-
private running = false;
|
|
40
|
-
|
|
41
|
-
constructor(
|
|
42
|
-
private readonly queue: JobQueue,
|
|
43
|
-
private readonly handlers: Record<string, JobHandler>,
|
|
44
|
-
opts: WorkerOptions = {},
|
|
45
|
-
) {
|
|
46
|
-
this.batchSize = opts.batchSize ?? 10;
|
|
47
|
-
this.intervalMs = opts.intervalMs ?? 1000;
|
|
48
|
-
this.staleMs = opts.staleMs ?? 60_000;
|
|
49
|
-
this.backoffMs = opts.backoffMs ?? 30_000;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Run one pass. Exposed so the CLI and tests can drive it directly. */
|
|
53
|
-
async tick(): Promise<number> {
|
|
54
|
-
const jobs = await this.queue.claim(this.batchSize, this.staleMs);
|
|
55
|
-
for (const job of jobs) {
|
|
56
|
-
const handler = this.handlers[job.name];
|
|
57
|
-
if (!handler) {
|
|
58
|
-
await this.queue.fail(
|
|
59
|
-
job.id,
|
|
60
|
-
new Error(`no handler registered for "${job.name}"`),
|
|
61
|
-
this.backoffMs,
|
|
62
|
-
);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
try {
|
|
66
|
-
let payload: unknown;
|
|
67
|
-
try {
|
|
68
|
-
payload = JSON.parse(job.payload);
|
|
69
|
-
} catch {
|
|
70
|
-
throw new Error(`payload is not valid JSON: ${job.payload}`);
|
|
71
|
-
}
|
|
72
|
-
await handler(payload, { id: job.id, name: job.name, attempt: job.attempts });
|
|
73
|
-
await this.queue.complete(job.id);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
// A recurring job that has used up its retries moves on to its
|
|
76
|
-
// next run instead of parking as failed: one bad afternoon
|
|
77
|
-
// upstream should not stop a schedule for good.
|
|
78
|
-
if (this.queue.isScheduled(job.id) && job.attempts >= job.max_attempts) {
|
|
79
|
-
await this.queue.reschedule(
|
|
80
|
-
job.id,
|
|
81
|
-
error instanceof Error ? error.message : String(error),
|
|
82
|
-
);
|
|
83
|
-
} else {
|
|
84
|
-
await this.queue.fail(job.id, error, this.backoffMs);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return jobs.length;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Poll until stop() is called. */
|
|
92
|
-
async run(signal?: AbortSignal): Promise<void> {
|
|
93
|
-
while (!this.stopping) {
|
|
94
|
-
this.running = true;
|
|
95
|
-
try {
|
|
96
|
-
const count = await this.tick();
|
|
97
|
-
// Only idle when there was nothing to do. Sleeping after a full
|
|
98
|
-
// batch would cap throughput at batchSize per interval.
|
|
99
|
-
if (count === 0) await sleep(this.intervalMs, signal);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.error("jobs worker error:", error);
|
|
102
|
-
await sleep(this.intervalMs, signal);
|
|
103
|
-
} finally {
|
|
104
|
-
this.running = false;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** Stop polling and wait for the in-flight pass to finish. */
|
|
110
|
-
async stop(): Promise<void> {
|
|
111
|
-
this.stopping = true;
|
|
112
|
-
while (this.running) await sleep(10);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
117
|
-
return new Promise((resolve) => {
|
|
118
|
-
const timer = setTimeout(resolve, ms);
|
|
119
|
-
// A timer keeps the process alive on its own, so unref and let the
|
|
120
|
-
// abort signal do the waking. Otherwise stop() could not be
|
|
121
|
-
// observed until the full interval elapsed.
|
|
122
|
-
(timer as unknown as { unref?: () => void }).unref?.();
|
|
123
|
-
signal?.addEventListener("abort", () => {
|
|
124
|
-
clearTimeout(timer);
|
|
125
|
-
resolve();
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
}
|
|
1
|
+
import type { JobQueue } from "./queue.ts";
|
|
2
|
+
import type { JobHandler } from "./store.ts";
|
|
3
|
+
|
|
4
|
+
export interface WorkerOptions {
|
|
5
|
+
/** How many jobs to claim per pass. */
|
|
6
|
+
batchSize?: number;
|
|
7
|
+
/** Milliseconds between passes when the queue is empty. */
|
|
8
|
+
intervalMs?: number;
|
|
9
|
+
/**
|
|
10
|
+
* A claim older than this is treated as abandoned — the worker that took
|
|
11
|
+
* it died without releasing. Without this, one crashed worker leaves its
|
|
12
|
+
* jobs claimed forever.
|
|
13
|
+
*/
|
|
14
|
+
staleMs?: number;
|
|
15
|
+
/** Backoff after a handler throws. */
|
|
16
|
+
backoffMs?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Polls a JobQueue and runs handlers.
|
|
21
|
+
*
|
|
22
|
+
* Nothing here is clever: claim a batch, run each, complete or fail. The
|
|
23
|
+
* parts that matter are the ones that are easy to get wrong and hard to
|
|
24
|
+
* notice:
|
|
25
|
+
*
|
|
26
|
+
* - A throwing handler is caught per job, so one bad job does not stop the
|
|
27
|
+
* batch. If it did, a single poisonous job would block every job behind
|
|
28
|
+
* it, forever.
|
|
29
|
+
* - SIGTERM drains before exiting, so a deploy does not abandon claimed
|
|
30
|
+
* work. The stale-claim timeout is the backstop when a worker is killed
|
|
31
|
+
* without getting to drain at all.
|
|
32
|
+
*/
|
|
33
|
+
export class JobWorker {
|
|
34
|
+
private readonly batchSize: number;
|
|
35
|
+
private readonly intervalMs: number;
|
|
36
|
+
private readonly staleMs: number;
|
|
37
|
+
private readonly backoffMs: number;
|
|
38
|
+
private stopping = false;
|
|
39
|
+
private running = false;
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
private readonly queue: JobQueue,
|
|
43
|
+
private readonly handlers: Record<string, JobHandler>,
|
|
44
|
+
opts: WorkerOptions = {},
|
|
45
|
+
) {
|
|
46
|
+
this.batchSize = opts.batchSize ?? 10;
|
|
47
|
+
this.intervalMs = opts.intervalMs ?? 1000;
|
|
48
|
+
this.staleMs = opts.staleMs ?? 60_000;
|
|
49
|
+
this.backoffMs = opts.backoffMs ?? 30_000;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Run one pass. Exposed so the CLI and tests can drive it directly. */
|
|
53
|
+
async tick(): Promise<number> {
|
|
54
|
+
const jobs = await this.queue.claim(this.batchSize, this.staleMs);
|
|
55
|
+
for (const job of jobs) {
|
|
56
|
+
const handler = this.handlers[job.name];
|
|
57
|
+
if (!handler) {
|
|
58
|
+
await this.queue.fail(
|
|
59
|
+
job.id,
|
|
60
|
+
new Error(`no handler registered for "${job.name}"`),
|
|
61
|
+
this.backoffMs,
|
|
62
|
+
);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
let payload: unknown;
|
|
67
|
+
try {
|
|
68
|
+
payload = JSON.parse(job.payload);
|
|
69
|
+
} catch {
|
|
70
|
+
throw new Error(`payload is not valid JSON: ${job.payload}`);
|
|
71
|
+
}
|
|
72
|
+
await handler(payload, { id: job.id, name: job.name, attempt: job.attempts });
|
|
73
|
+
await this.queue.complete(job.id);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// A recurring job that has used up its retries moves on to its
|
|
76
|
+
// next run instead of parking as failed: one bad afternoon
|
|
77
|
+
// upstream should not stop a schedule for good.
|
|
78
|
+
if (this.queue.isScheduled(job.id) && job.attempts >= job.max_attempts) {
|
|
79
|
+
await this.queue.reschedule(
|
|
80
|
+
job.id,
|
|
81
|
+
error instanceof Error ? error.message : String(error),
|
|
82
|
+
);
|
|
83
|
+
} else {
|
|
84
|
+
await this.queue.fail(job.id, error, this.backoffMs);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return jobs.length;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Poll until stop() is called. */
|
|
92
|
+
async run(signal?: AbortSignal): Promise<void> {
|
|
93
|
+
while (!this.stopping) {
|
|
94
|
+
this.running = true;
|
|
95
|
+
try {
|
|
96
|
+
const count = await this.tick();
|
|
97
|
+
// Only idle when there was nothing to do. Sleeping after a full
|
|
98
|
+
// batch would cap throughput at batchSize per interval.
|
|
99
|
+
if (count === 0) await sleep(this.intervalMs, signal);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error("jobs worker error:", error);
|
|
102
|
+
await sleep(this.intervalMs, signal);
|
|
103
|
+
} finally {
|
|
104
|
+
this.running = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Stop polling and wait for the in-flight pass to finish. */
|
|
110
|
+
async stop(): Promise<void> {
|
|
111
|
+
this.stopping = true;
|
|
112
|
+
while (this.running) await sleep(10);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
117
|
+
return new Promise((resolve) => {
|
|
118
|
+
const timer = setTimeout(resolve, ms);
|
|
119
|
+
// A timer keeps the process alive on its own, so unref and let the
|
|
120
|
+
// abort signal do the waking. Otherwise stop() could not be
|
|
121
|
+
// observed until the full interval elapsed.
|
|
122
|
+
(timer as unknown as { unref?: () => void }).unref?.();
|
|
123
|
+
signal?.addEventListener("abort", () => {
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
resolve();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|