@farm.js/jobs 0.1.65 → 0.1.67
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/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +14 -0
- package/dist/index.d.ts +75 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +59 -71
- package/dist/local.d.ts +67 -0
- package/dist/local.d.ts.map +1 -0
- package/dist/local.js +568 -0
- package/dist/runtime.d.ts +169 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +345 -0
- package/dist/testing.d.ts +46 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +251 -0
- package/dist/time.d.ts +21 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +66 -0
- package/package.json +7 -3
package/dist/local.js
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import { JobsRuntimeError } from "./errors.js";
|
|
2
|
+
import { defineJobsRuntime, } from "./runtime.js";
|
|
3
|
+
import { parseDurationMs, resolveScheduledTimestamp } from "./time.js";
|
|
4
|
+
const LOCAL_CAPABILITIES = {
|
|
5
|
+
delay: true,
|
|
6
|
+
schedule: true,
|
|
7
|
+
debounce: false,
|
|
8
|
+
tags: true,
|
|
9
|
+
cancel: true,
|
|
10
|
+
batchTrigger: true,
|
|
11
|
+
queue: false,
|
|
12
|
+
retry: true,
|
|
13
|
+
ttl: true,
|
|
14
|
+
concurrencyKey: false,
|
|
15
|
+
idempotencyKey: true,
|
|
16
|
+
};
|
|
17
|
+
const LOCAL_SCHEDULE_RUNTIME = { kind: "local", label: "Local jobs" };
|
|
18
|
+
const DEFAULT_ATTEMPTS = 3;
|
|
19
|
+
const TERMINAL_STATES = new Set(["completed", "failed", "canceled", "expired"]);
|
|
20
|
+
const RUN_ID_PATTERN = /^run_[a-f0-9]{32}$/;
|
|
21
|
+
const TIME_WIDTH = 15;
|
|
22
|
+
function readNodeEnv() {
|
|
23
|
+
return globalThis.process?.env
|
|
24
|
+
?.NODE_ENV;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether this is a deployed server. Farm's Node server sets NODE_ENV to
|
|
28
|
+
* production; a Cloudflare worker leaves it unset and carries its bindings on
|
|
29
|
+
* `globalThis.__env__`, as core's own deployed-runtime check reads them.
|
|
30
|
+
*/
|
|
31
|
+
function isDeployedRuntime() {
|
|
32
|
+
if (readNodeEnv() === "production")
|
|
33
|
+
return true;
|
|
34
|
+
const bindings = globalThis.__env__;
|
|
35
|
+
return Boolean(bindings) && typeof bindings === "object";
|
|
36
|
+
}
|
|
37
|
+
function randomHex() {
|
|
38
|
+
return globalThis.crypto.randomUUID().replaceAll("-", "");
|
|
39
|
+
}
|
|
40
|
+
async function sha256Hex(value) {
|
|
41
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(value));
|
|
42
|
+
let hex = "";
|
|
43
|
+
for (const byte of new Uint8Array(digest))
|
|
44
|
+
hex += byte.toString(16).padStart(2, "0");
|
|
45
|
+
return hex;
|
|
46
|
+
}
|
|
47
|
+
function padTime(ms) {
|
|
48
|
+
return String(Math.max(0, Math.floor(ms))).padStart(TIME_WIDTH, "0");
|
|
49
|
+
}
|
|
50
|
+
function iso(ms) {
|
|
51
|
+
return new Date(ms).toISOString();
|
|
52
|
+
}
|
|
53
|
+
/** `due:<ms>:<id>` and `gc:<ms>:<id>` index keys sort by time as strings. */
|
|
54
|
+
function indexKey(prefix, ms, id) {
|
|
55
|
+
return `${prefix}:${padTime(ms)}:${id}`;
|
|
56
|
+
}
|
|
57
|
+
function parseIndexKey(key) {
|
|
58
|
+
const match = /^(?:due|gc):(\d+):(run_[a-f0-9]{32})$/.exec(key);
|
|
59
|
+
return match ? { at: Number(match[1]), id: match[2] } : undefined;
|
|
60
|
+
}
|
|
61
|
+
function defaultRetryDelay(attempt) {
|
|
62
|
+
return Math.min(60_000, 1_000 * 2 ** Math.max(0, attempt - 1));
|
|
63
|
+
}
|
|
64
|
+
/** Trigger.dev reads a numeric `ttl` as seconds, so `local()` does too. */
|
|
65
|
+
function resolveTtlMs(ttl) {
|
|
66
|
+
const ms = typeof ttl === "number" ? ttl * 1_000 : parseDurationMs(ttl.trim());
|
|
67
|
+
if (ms == null || !Number.isFinite(ms) || ms <= 0) {
|
|
68
|
+
throw new JobsRuntimeError("Local jobs ttl must be a positive number of seconds or a duration ending in ms, s, m, h, or d.", 400, { code: "JOBS_INVALID_TTL", runtime: "local", value: ttl });
|
|
69
|
+
}
|
|
70
|
+
return ms;
|
|
71
|
+
}
|
|
72
|
+
function toStatus(state) {
|
|
73
|
+
return state;
|
|
74
|
+
}
|
|
75
|
+
function isStorage(value) {
|
|
76
|
+
if (!value || typeof value !== "object")
|
|
77
|
+
return false;
|
|
78
|
+
const storage = value;
|
|
79
|
+
return ["getItem", "setItem", "removeItem", "getKeys"].every((method) => typeof storage[method] === "function");
|
|
80
|
+
}
|
|
81
|
+
function validateOptions(options) {
|
|
82
|
+
const issues = [];
|
|
83
|
+
const positive = (name, value, integer = false) => {
|
|
84
|
+
if (value === undefined)
|
|
85
|
+
return;
|
|
86
|
+
if (typeof value !== "number" ||
|
|
87
|
+
!Number.isFinite(value) ||
|
|
88
|
+
value <= 0 ||
|
|
89
|
+
(integer && !Number.isInteger(value))) {
|
|
90
|
+
issues.push(`\`${name}\` must be a positive ${integer ? "integer" : "number"}; received ${JSON.stringify(value)}.`);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
if (options.pollIntervalMs !== false)
|
|
94
|
+
positive("pollIntervalMs", options.pollIntervalMs);
|
|
95
|
+
positive("leaseMs", options.leaseMs);
|
|
96
|
+
positive("retentionMs", options.retentionMs);
|
|
97
|
+
positive("tickLimit", options.tickLimit, true);
|
|
98
|
+
const retry = options.retryDelayMs;
|
|
99
|
+
if (retry !== undefined &&
|
|
100
|
+
typeof retry !== "function" &&
|
|
101
|
+
(typeof retry !== "number" || !Number.isFinite(retry) || retry < 0)) {
|
|
102
|
+
issues.push(`\`retryDelayMs\` must be zero or more milliseconds, or a function of the attempt number; received ${JSON.stringify(retry)}.`);
|
|
103
|
+
}
|
|
104
|
+
const storage = options.storage;
|
|
105
|
+
if (storage !== undefined &&
|
|
106
|
+
!(typeof storage === "string" && storage.trim().length > 0) &&
|
|
107
|
+
!isStorage(storage)) {
|
|
108
|
+
issues.push("`storage` must be a storage namespace name or an object with getItem, setItem, removeItem, and getKeys.");
|
|
109
|
+
}
|
|
110
|
+
if (issues.length > 0) {
|
|
111
|
+
throw new TypeError(`local() options are invalid:\n${issues.map((i) => `- ${i}`).join("\n")}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Runs tasks in the app itself: a run is stored, its `run` callback is called
|
|
116
|
+
* after the response that started it, and failed attempts are retried. Use it
|
|
117
|
+
* in development, in tests, and for apps that run as one long-lived process.
|
|
118
|
+
*/
|
|
119
|
+
export function local(options = {}) {
|
|
120
|
+
validateOptions(options);
|
|
121
|
+
return defineJobsRuntime({
|
|
122
|
+
kind: "local",
|
|
123
|
+
create: (context) => createLocalRuntime(context, options),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function createLocalRuntime(context, options) {
|
|
127
|
+
const pollIntervalMs = options.pollIntervalMs === false ? false : (options.pollIntervalMs ?? 1_000);
|
|
128
|
+
const leaseMs = options.leaseMs ?? 300_000;
|
|
129
|
+
const retentionMs = options.retentionMs ?? 604_800_000;
|
|
130
|
+
const tickLimit = options.tickLimit ?? 25;
|
|
131
|
+
const retryDelay = (attempt) => {
|
|
132
|
+
const value = typeof options.retryDelayMs === "function"
|
|
133
|
+
? options.retryDelayMs(attempt)
|
|
134
|
+
: (options.retryDelayMs ?? defaultRetryDelay(attempt));
|
|
135
|
+
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
136
|
+
};
|
|
137
|
+
const tickPath = context.routePath("tick");
|
|
138
|
+
const locks = new Map();
|
|
139
|
+
const inFlight = new Map();
|
|
140
|
+
let poller;
|
|
141
|
+
let disposed = false;
|
|
142
|
+
let tickChain = Promise.resolve();
|
|
143
|
+
let warnedNoStart = false;
|
|
144
|
+
let lastPollError;
|
|
145
|
+
/** Serializes work on one key in this process. Storage has no compare-and-swap. */
|
|
146
|
+
function withLock(key, work) {
|
|
147
|
+
const previous = locks.get(key) ?? Promise.resolve();
|
|
148
|
+
const next = previous.then(work, work);
|
|
149
|
+
const settled = next.then(() => undefined, () => undefined);
|
|
150
|
+
locks.set(key, settled);
|
|
151
|
+
void settled.then(() => {
|
|
152
|
+
if (locks.get(key) === settled)
|
|
153
|
+
locks.delete(key);
|
|
154
|
+
});
|
|
155
|
+
return next;
|
|
156
|
+
}
|
|
157
|
+
let storageModule;
|
|
158
|
+
async function store() {
|
|
159
|
+
if (options.storage && typeof options.storage === "object")
|
|
160
|
+
return options.storage;
|
|
161
|
+
storageModule ??= import("@farm.js/core/storage");
|
|
162
|
+
const { getStorage } = await storageModule;
|
|
163
|
+
const namespace = options.storage ?? "jobs";
|
|
164
|
+
const storage = getStorage(namespace);
|
|
165
|
+
if (isDeployedRuntime() && storage.getMount?.("")?.driver?.name === "memory") {
|
|
166
|
+
throw new JobsRuntimeError(`local() keeps runs in the "${namespace}" storage namespace, which uses the in-memory driver in this production build, so a restart or a second instance loses them. Configure a persistent driver with \`storage\` in farm.config.ts, or pass local({ storage }) a persistent storage instance.`, 500, { code: "JOBS_LOCAL_STORAGE_NOT_PERSISTENT", runtime: "local" });
|
|
167
|
+
}
|
|
168
|
+
return storage;
|
|
169
|
+
}
|
|
170
|
+
async function readRun(storage, id) {
|
|
171
|
+
const value = (await storage.getItem(`runs:${id}`));
|
|
172
|
+
return value && typeof value === "object" && value.v === 1 && value.id === id
|
|
173
|
+
? value
|
|
174
|
+
: undefined;
|
|
175
|
+
}
|
|
176
|
+
async function writeRun(storage, record) {
|
|
177
|
+
await storage.setItem(`runs:${record.id}`, record);
|
|
178
|
+
}
|
|
179
|
+
async function moveIndex(storage, record, nextDueKey) {
|
|
180
|
+
if (nextDueKey)
|
|
181
|
+
await storage.setItem(nextDueKey, record.id);
|
|
182
|
+
if (record.dueKey && record.dueKey !== nextDueKey)
|
|
183
|
+
await storage.removeItem(record.dueKey);
|
|
184
|
+
}
|
|
185
|
+
async function finish(storage, record, state, now, patch = {}) {
|
|
186
|
+
const finished = {
|
|
187
|
+
...record,
|
|
188
|
+
...patch,
|
|
189
|
+
state,
|
|
190
|
+
finishedAt: iso(now),
|
|
191
|
+
leaseId: null,
|
|
192
|
+
dueKey: null,
|
|
193
|
+
};
|
|
194
|
+
await storage.setItem(indexKey("gc", now + retentionMs, record.id), record.id);
|
|
195
|
+
await writeRun(storage, finished);
|
|
196
|
+
await moveIndex(storage, record, null);
|
|
197
|
+
return finished;
|
|
198
|
+
}
|
|
199
|
+
function ensurePolling() {
|
|
200
|
+
if (poller || disposed || pollIntervalMs === false || typeof setInterval !== "function")
|
|
201
|
+
return;
|
|
202
|
+
poller = setInterval(() => {
|
|
203
|
+
void enqueueTick().then(() => {
|
|
204
|
+
lastPollError = undefined;
|
|
205
|
+
}, (error) => {
|
|
206
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
207
|
+
// Report a failing poll once, not on every interval.
|
|
208
|
+
if (message !== lastPollError)
|
|
209
|
+
context.logger.error("Polling for due runs failed:", message);
|
|
210
|
+
lastPollError = message;
|
|
211
|
+
});
|
|
212
|
+
}, pollIntervalMs);
|
|
213
|
+
poller.unref?.();
|
|
214
|
+
}
|
|
215
|
+
async function startAfterResponse(id) {
|
|
216
|
+
let scheduled = false;
|
|
217
|
+
try {
|
|
218
|
+
const { after } = await import("@farm.js/core/after");
|
|
219
|
+
after(async () => {
|
|
220
|
+
await claimAndRun(id).catch(reportBackgroundError);
|
|
221
|
+
});
|
|
222
|
+
scheduled = true;
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
// Not inside a request, for example a script. Polling or the tick route starts the run.
|
|
226
|
+
}
|
|
227
|
+
if (!scheduled && pollIntervalMs === false && !warnedNoStart) {
|
|
228
|
+
warnedNoStart = true;
|
|
229
|
+
context.logger.warn(`Run ${id} was started outside a request and polling is off, so it starts on the next call to ${tickPath}.`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function reportBackgroundError(error) {
|
|
233
|
+
context.logger.error("A run failed to start:", error instanceof Error ? error.message : String(error));
|
|
234
|
+
}
|
|
235
|
+
/** Takes the lease on run `id` when it is due. Runs inside the run's lock. */
|
|
236
|
+
async function claim(id, indexEntry) {
|
|
237
|
+
const skipped = { kind: "done", attempt: { started: false, outcome: "skipped" } };
|
|
238
|
+
if (disposed)
|
|
239
|
+
return skipped;
|
|
240
|
+
const storage = await store();
|
|
241
|
+
const record = await readRun(storage, id);
|
|
242
|
+
const now = Date.now();
|
|
243
|
+
if (!record) {
|
|
244
|
+
if (indexEntry)
|
|
245
|
+
await storage.removeItem(indexEntry);
|
|
246
|
+
return skipped;
|
|
247
|
+
}
|
|
248
|
+
if (indexEntry && record.dueKey !== indexEntry) {
|
|
249
|
+
// A stale index entry left by a write that was interrupted.
|
|
250
|
+
await storage.removeItem(indexEntry);
|
|
251
|
+
return skipped;
|
|
252
|
+
}
|
|
253
|
+
if (TERMINAL_STATES.has(record.state) || record.runAt > now || inFlight.has(id)) {
|
|
254
|
+
return skipped;
|
|
255
|
+
}
|
|
256
|
+
if (record.expiresAt != null && record.attempt === 0 && now > record.expiresAt) {
|
|
257
|
+
await finish(storage, record, "expired", now);
|
|
258
|
+
return { kind: "done", attempt: { started: false, outcome: "expired" } };
|
|
259
|
+
}
|
|
260
|
+
if (record.state === "running" && record.attempt >= record.maxAttempts) {
|
|
261
|
+
// The last attempt's lease ran out without a result.
|
|
262
|
+
await finish(storage, record, "failed", now, {
|
|
263
|
+
error: {
|
|
264
|
+
name: "JobsLeaseExpired",
|
|
265
|
+
message: `Attempt ${record.attempt} did not finish within leaseMs (${leaseMs} ms). The process may have stopped, or the task needs a longer leaseMs.`,
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
return { kind: "done", attempt: { started: false, outcome: "failed" } };
|
|
269
|
+
}
|
|
270
|
+
const leaseId = randomHex();
|
|
271
|
+
const leaseUntil = now + leaseMs;
|
|
272
|
+
const leaseKey = indexKey("due", leaseUntil, id);
|
|
273
|
+
const claimed = {
|
|
274
|
+
...record,
|
|
275
|
+
state: "running",
|
|
276
|
+
attempt: record.attempt + 1,
|
|
277
|
+
startedAt: record.startedAt ?? iso(now),
|
|
278
|
+
runAt: leaseUntil,
|
|
279
|
+
leaseId,
|
|
280
|
+
dueKey: leaseKey,
|
|
281
|
+
};
|
|
282
|
+
await writeRun(storage, claimed);
|
|
283
|
+
await moveIndex(storage, record, leaseKey);
|
|
284
|
+
// Another instance may have claimed it between our read and write. The last
|
|
285
|
+
// writer keeps the lease; this check makes the loser back off.
|
|
286
|
+
if ((await readRun(storage, id))?.leaseId !== leaseId)
|
|
287
|
+
return skipped;
|
|
288
|
+
return { kind: "claimed", storage, record: claimed };
|
|
289
|
+
}
|
|
290
|
+
/** Claims run `id`, calls its task outside the lock so cancel stays prompt, and records the result. */
|
|
291
|
+
async function claimAndRun(id, indexEntry) {
|
|
292
|
+
const claimed = await withLock(`run:${id}`, () => claim(id, indexEntry));
|
|
293
|
+
if (claimed.kind === "done")
|
|
294
|
+
return claimed.attempt;
|
|
295
|
+
const { storage, record } = claimed;
|
|
296
|
+
const controller = new AbortController();
|
|
297
|
+
inFlight.set(id, controller);
|
|
298
|
+
const timer = setTimeout(() => controller.abort(new Error(`The attempt outlived leaseMs (${leaseMs} ms).`)), leaseMs);
|
|
299
|
+
timer.unref?.();
|
|
300
|
+
let result;
|
|
301
|
+
let retryable = true;
|
|
302
|
+
try {
|
|
303
|
+
result = await context.execute({
|
|
304
|
+
task: record.task,
|
|
305
|
+
input: record.input,
|
|
306
|
+
run: { id, attempt: record.attempt },
|
|
307
|
+
signal: controller.signal,
|
|
308
|
+
});
|
|
309
|
+
if (result.ok) {
|
|
310
|
+
try {
|
|
311
|
+
JSON.stringify(result.output);
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
retryable = false;
|
|
315
|
+
result = {
|
|
316
|
+
ok: false,
|
|
317
|
+
error: {
|
|
318
|
+
name: "JobsOutputNotSerializable",
|
|
319
|
+
message: `The task returned output that cannot be stored as JSON: ${error instanceof Error ? error.message : String(error)}`,
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
// The task is no longer registered, for example after a deploy removed it.
|
|
327
|
+
retryable = false;
|
|
328
|
+
result = {
|
|
329
|
+
ok: false,
|
|
330
|
+
error: {
|
|
331
|
+
name: error instanceof Error ? error.name : "Error",
|
|
332
|
+
message: error instanceof Error ? error.message : String(error),
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
finally {
|
|
337
|
+
clearTimeout(timer);
|
|
338
|
+
inFlight.delete(id);
|
|
339
|
+
}
|
|
340
|
+
const outcome = await withLock(`run:${id}`, async () => {
|
|
341
|
+
const finishedAt = Date.now();
|
|
342
|
+
const current = await readRun(storage, id);
|
|
343
|
+
// Canceled, or reclaimed after the lease ended: the newer state wins.
|
|
344
|
+
if (!current || current.leaseId !== record.leaseId)
|
|
345
|
+
return "skipped";
|
|
346
|
+
if (result.ok) {
|
|
347
|
+
await finish(storage, current, "completed", finishedAt, {
|
|
348
|
+
output: result.output ?? null,
|
|
349
|
+
error: null,
|
|
350
|
+
});
|
|
351
|
+
return "completed";
|
|
352
|
+
}
|
|
353
|
+
if (retryable && current.attempt < current.maxAttempts) {
|
|
354
|
+
const runAt = finishedAt + retryDelay(current.attempt);
|
|
355
|
+
const dueKey = indexKey("due", runAt, id);
|
|
356
|
+
const next = {
|
|
357
|
+
...current,
|
|
358
|
+
state: runAt > finishedAt ? "delayed" : "queued",
|
|
359
|
+
runAt,
|
|
360
|
+
leaseId: null,
|
|
361
|
+
dueKey,
|
|
362
|
+
error: result.error,
|
|
363
|
+
};
|
|
364
|
+
await writeRun(storage, next);
|
|
365
|
+
await moveIndex(storage, current, dueKey);
|
|
366
|
+
return "retried";
|
|
367
|
+
}
|
|
368
|
+
await finish(storage, current, "failed", finishedAt, { error: result.error });
|
|
369
|
+
return "failed";
|
|
370
|
+
});
|
|
371
|
+
return { started: true, outcome };
|
|
372
|
+
}
|
|
373
|
+
async function removeExpiredRecord(storage, id, gcKey) {
|
|
374
|
+
const record = await readRun(storage, id);
|
|
375
|
+
await storage.removeItem(gcKey);
|
|
376
|
+
if (!record || !TERMINAL_STATES.has(record.state))
|
|
377
|
+
return false;
|
|
378
|
+
// finishedAt + retentionMs is when the record is due for removal.
|
|
379
|
+
const finishedAt = record.finishedAt ? Date.parse(record.finishedAt) : 0;
|
|
380
|
+
if (finishedAt + retentionMs > Date.now())
|
|
381
|
+
return false;
|
|
382
|
+
if (record.idempotencyKey) {
|
|
383
|
+
const pointer = `idem:${record.idempotencyKey}`;
|
|
384
|
+
if ((await storage.getItem(pointer)) === id)
|
|
385
|
+
await storage.removeItem(pointer);
|
|
386
|
+
}
|
|
387
|
+
await storage.removeItem(`runs:${id}`);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
async function tick() {
|
|
391
|
+
const result = {
|
|
392
|
+
started: 0,
|
|
393
|
+
completed: 0,
|
|
394
|
+
failed: 0,
|
|
395
|
+
retried: 0,
|
|
396
|
+
expired: 0,
|
|
397
|
+
removed: 0,
|
|
398
|
+
};
|
|
399
|
+
if (disposed)
|
|
400
|
+
return result;
|
|
401
|
+
const storage = await store();
|
|
402
|
+
const now = Date.now();
|
|
403
|
+
const due = (await storage.getKeys("due"))
|
|
404
|
+
.map((key) => ({ key, parsed: parseIndexKey(key) }))
|
|
405
|
+
.filter((entry) => entry.parsed && entry.parsed.at <= now)
|
|
406
|
+
.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
|
|
407
|
+
.slice(0, tickLimit);
|
|
408
|
+
for (const entry of due) {
|
|
409
|
+
const id = entry.parsed.id;
|
|
410
|
+
const attempt = await claimAndRun(id, entry.key);
|
|
411
|
+
if (attempt.started)
|
|
412
|
+
result.started += 1;
|
|
413
|
+
if (attempt.outcome !== "skipped")
|
|
414
|
+
result[attempt.outcome] += 1;
|
|
415
|
+
}
|
|
416
|
+
const collectable = (await storage.getKeys("gc"))
|
|
417
|
+
.map((key) => ({ key, parsed: parseIndexKey(key) }))
|
|
418
|
+
.filter((entry) => entry.parsed && entry.parsed.at <= now);
|
|
419
|
+
for (const entry of collectable) {
|
|
420
|
+
const id = entry.parsed.id;
|
|
421
|
+
if (await withLock(`run:${id}`, () => removeExpiredRecord(storage, id, entry.key))) {
|
|
422
|
+
result.removed += 1;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
/** Ticks never overlap in one process, whether the poller or the route started them. */
|
|
428
|
+
function enqueueTick() {
|
|
429
|
+
const next = tickChain.then(tick, tick);
|
|
430
|
+
tickChain = next.catch(() => undefined);
|
|
431
|
+
return next;
|
|
432
|
+
}
|
|
433
|
+
function notFound(task, handleId) {
|
|
434
|
+
return new JobsRuntimeError(`Run ${handleId} was not found for task "${task.key}".`, 404);
|
|
435
|
+
}
|
|
436
|
+
async function readTaskRun(task, handleId) {
|
|
437
|
+
if (!RUN_ID_PATTERN.test(handleId))
|
|
438
|
+
throw notFound(task, handleId);
|
|
439
|
+
const record = await readRun(await store(), handleId);
|
|
440
|
+
if (!record || record.task !== task.key)
|
|
441
|
+
throw notFound(task, handleId);
|
|
442
|
+
return record;
|
|
443
|
+
}
|
|
444
|
+
async function launch(task, input, launchOptions) {
|
|
445
|
+
const storage = await store();
|
|
446
|
+
const now = Date.now();
|
|
447
|
+
const runAt = launchOptions.delay == null
|
|
448
|
+
? now
|
|
449
|
+
: (resolveScheduledTimestamp(launchOptions, now, LOCAL_SCHEDULE_RUNTIME) ?? now);
|
|
450
|
+
const id = `run_${randomHex()}`;
|
|
451
|
+
const dueKey = indexKey("due", runAt, id);
|
|
452
|
+
const record = {
|
|
453
|
+
v: 1,
|
|
454
|
+
id,
|
|
455
|
+
task: task.key,
|
|
456
|
+
taskId: task.id,
|
|
457
|
+
input,
|
|
458
|
+
state: runAt > now ? "delayed" : "queued",
|
|
459
|
+
attempt: 0,
|
|
460
|
+
maxAttempts: launchOptions.retryAttempts ?? DEFAULT_ATTEMPTS,
|
|
461
|
+
queuedAt: iso(now),
|
|
462
|
+
runAt,
|
|
463
|
+
startedAt: null,
|
|
464
|
+
finishedAt: null,
|
|
465
|
+
expiresAt: launchOptions.ttl == null ? null : runAt + resolveTtlMs(launchOptions.ttl),
|
|
466
|
+
leaseId: null,
|
|
467
|
+
dueKey,
|
|
468
|
+
idempotencyKey: null,
|
|
469
|
+
output: null,
|
|
470
|
+
error: null,
|
|
471
|
+
tags: launchOptions.tags ? [...launchOptions.tags] : [],
|
|
472
|
+
};
|
|
473
|
+
return { storage, record, dueKey, immediate: runAt <= now };
|
|
474
|
+
}
|
|
475
|
+
async function trigger(task, input, launchOptions) {
|
|
476
|
+
ensurePolling();
|
|
477
|
+
const create = async () => {
|
|
478
|
+
const prepared = await launch(task, input, launchOptions);
|
|
479
|
+
const { storage, record } = prepared;
|
|
480
|
+
if (launchOptions.idempotencyKey) {
|
|
481
|
+
const hashed = await sha256Hex(`${task.id}\n${launchOptions.idempotencyKey}`);
|
|
482
|
+
const pointer = `idem:${hashed}`;
|
|
483
|
+
const existingId = await storage.getItem(pointer);
|
|
484
|
+
if (typeof existingId === "string") {
|
|
485
|
+
const existing = await readRun(storage, existingId);
|
|
486
|
+
if (existing && existing.task === task.key) {
|
|
487
|
+
return { handleId: existing.id, queuedAt: existing.queuedAt, immediate: false };
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
record.idempotencyKey = hashed;
|
|
491
|
+
await writeRun(storage, record);
|
|
492
|
+
await storage.setItem(prepared.dueKey, record.id);
|
|
493
|
+
await storage.setItem(pointer, record.id);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
await writeRun(storage, record);
|
|
497
|
+
await storage.setItem(prepared.dueKey, record.id);
|
|
498
|
+
}
|
|
499
|
+
return { handleId: record.id, queuedAt: record.queuedAt, immediate: prepared.immediate };
|
|
500
|
+
};
|
|
501
|
+
const launched = launchOptions.idempotencyKey
|
|
502
|
+
? await withLock(`idem:${task.id}\n${launchOptions.idempotencyKey}`, create)
|
|
503
|
+
: await create();
|
|
504
|
+
if (launched.immediate)
|
|
505
|
+
await startAfterResponse(launched.handleId);
|
|
506
|
+
return { handleId: launched.handleId, queuedAt: launched.queuedAt };
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
capabilities: LOCAL_CAPABILITIES,
|
|
510
|
+
trigger,
|
|
511
|
+
async status(task, handleId) {
|
|
512
|
+
ensurePolling();
|
|
513
|
+
const record = await readTaskRun(task, handleId);
|
|
514
|
+
return {
|
|
515
|
+
status: toStatus(record.state),
|
|
516
|
+
providerRunId: record.id,
|
|
517
|
+
providerStatus: record.state,
|
|
518
|
+
queuedAt: record.queuedAt,
|
|
519
|
+
startedAt: record.startedAt,
|
|
520
|
+
finishedAt: record.finishedAt,
|
|
521
|
+
output: record.output,
|
|
522
|
+
error: record.error,
|
|
523
|
+
tags: record.tags,
|
|
524
|
+
raw: {
|
|
525
|
+
attempt: record.attempt,
|
|
526
|
+
maxAttempts: record.maxAttempts,
|
|
527
|
+
nextAttemptAt: TERMINAL_STATES.has(record.state) ? null : iso(record.runAt),
|
|
528
|
+
},
|
|
529
|
+
};
|
|
530
|
+
},
|
|
531
|
+
async cancel(task, handleId) {
|
|
532
|
+
await readTaskRun(task, handleId);
|
|
533
|
+
await withLock(`run:${handleId}`, async () => {
|
|
534
|
+
const storage = await store();
|
|
535
|
+
const record = await readRun(storage, handleId);
|
|
536
|
+
if (!record)
|
|
537
|
+
throw notFound(task, handleId);
|
|
538
|
+
if (TERMINAL_STATES.has(record.state)) {
|
|
539
|
+
throw new JobsRuntimeError(`Run ${handleId} already finished as "${record.state}".`, 409, { code: "JOBS_RUN_FINISHED", runtime: "local", status: record.state });
|
|
540
|
+
}
|
|
541
|
+
await finish(storage, record, "canceled", Date.now());
|
|
542
|
+
});
|
|
543
|
+
inFlight.get(handleId)?.abort(new Error("The run was canceled."));
|
|
544
|
+
},
|
|
545
|
+
routes: [
|
|
546
|
+
{
|
|
547
|
+
method: "GET",
|
|
548
|
+
path: "tick",
|
|
549
|
+
async verify(request) {
|
|
550
|
+
const { authorizeCronRequest } = await import("@farm.js/core/cron");
|
|
551
|
+
return authorizeCronRequest(request);
|
|
552
|
+
},
|
|
553
|
+
async handler() {
|
|
554
|
+
return Response.json(await enqueueTick());
|
|
555
|
+
},
|
|
556
|
+
},
|
|
557
|
+
],
|
|
558
|
+
start() {
|
|
559
|
+
ensurePolling();
|
|
560
|
+
},
|
|
561
|
+
dispose() {
|
|
562
|
+
disposed = true;
|
|
563
|
+
if (poller)
|
|
564
|
+
clearInterval(poller);
|
|
565
|
+
poller = undefined;
|
|
566
|
+
},
|
|
567
|
+
};
|
|
568
|
+
}
|