withruntime 0.35.0 → 0.37.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/BLAXEL.md +207 -204
- package/COMPATIBILITY.md +367 -337
- package/DAYTONA.md +180 -173
- package/E2B.md +246 -230
- package/README.md +182 -142
- package/README.src.md +182 -142
- package/VERCEL.md +173 -168
- package/agent-platforms-inventory.json +387 -0
- package/dist/agents-cli.d.ts +7 -1
- package/dist/agents-cli.js +219 -2
- package/dist/ci-cli.d.ts +7 -1
- package/dist/ci-cli.js +25 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +16 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +4 -1
- package/dist/index.d.ts +3 -3
- package/dist/inngest/index.d.ts +96 -0
- package/dist/inngest/index.js +183 -0
- package/dist/jobs-code/cron.d.ts +12 -0
- package/dist/jobs-code/cron.js +93 -0
- package/dist/jobs-code/registry.d.ts +31 -0
- package/dist/jobs-code/registry.js +30 -0
- package/dist/jobs-code/runner.d.ts +1 -0
- package/dist/jobs-code/runner.js +74 -0
- package/dist/jobs-deploy.d.ts +22 -0
- package/dist/jobs-deploy.js +247 -0
- package/dist/jobs-import.d.ts +15 -0
- package/dist/jobs-import.js +231 -0
- package/dist/products/agents.d.ts +161 -1
- package/dist/products/agents.js +40 -0
- package/dist/products/ci.d.ts +19 -1
- package/dist/products/jobs.d.ts +77 -0
- package/dist/products/jobs.js +5 -0
- package/dist/transport.d.ts +5 -1
- package/dist/transport.js +7 -1
- package/dist/trigger/index.d.ts +202 -0
- package/dist/trigger/index.js +244 -0
- package/package.json +14 -3
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/* Inngest's SDK, run on Runtime Jobs: change `inngest` (and `inngest/next`,
|
|
2
|
+
`inngest/express` and the other serve adapters) to `withruntime/inngest`,
|
|
3
|
+
then `npx withruntime jobs deploy`. Each function with a cron trigger
|
|
4
|
+
becomes a Runtime job on that schedule; each function with an event
|
|
5
|
+
trigger becomes a job that `inngest.send` runs once, in a fresh sandbox,
|
|
6
|
+
per matching event. `step.run` runs its code in the run and returns its
|
|
7
|
+
result; `step.sleep` and `step.sleepUntil` wait in the run, which counts
|
|
8
|
+
toward its timeout. A function's `retries` become the job's attempts.
|
|
9
|
+
|
|
10
|
+
What has no equivalent is refused by name when called, never ignored:
|
|
11
|
+
step.waitForEvent, step.invoke, concurrency keys, throttling, debounce and
|
|
12
|
+
batching. Its serve() answers an Inngest server's calls with 410, since
|
|
13
|
+
nothing calls the app any more. */
|
|
14
|
+
import { numericCron } from "../trigger/index.js";
|
|
15
|
+
import { jobNameOf, registerTask, tasksRegistry } from "../jobs-code/registry.js";
|
|
16
|
+
export class NonRetriableError extends Error {
|
|
17
|
+
}
|
|
18
|
+
export class RetryAfterError extends Error {
|
|
19
|
+
}
|
|
20
|
+
export class CompatibilityError extends Error {
|
|
21
|
+
code = "not_supported";
|
|
22
|
+
}
|
|
23
|
+
const refuse = (what, instead) => {
|
|
24
|
+
throw new CompatibilityError(`${what} has no equivalent on Runtime Jobs: ${instead}`);
|
|
25
|
+
};
|
|
26
|
+
/** "30s", "5m", "2h", "1d", "1w", or milliseconds. */
|
|
27
|
+
export function durationMs(value) {
|
|
28
|
+
if (typeof value === "number")
|
|
29
|
+
return value;
|
|
30
|
+
let total = 0;
|
|
31
|
+
const re = /(\d+(?:\.\d+)?)\s*(ms|s|m|h|d|w)/g;
|
|
32
|
+
let matched = "";
|
|
33
|
+
for (const m of value.matchAll(re)) {
|
|
34
|
+
matched += m[0];
|
|
35
|
+
total +=
|
|
36
|
+
Number(m[1]) *
|
|
37
|
+
{ ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }[m[2]];
|
|
38
|
+
}
|
|
39
|
+
if (!matched || matched.replace(/\s/g, "") !== value.replace(/\s/g, ""))
|
|
40
|
+
throw new Error(`"${value}" is not a duration such as 30s, 5m, 2h or 1d`);
|
|
41
|
+
return total;
|
|
42
|
+
}
|
|
43
|
+
/** Inngest's `TZ=Europe/Paris 0 12 * * 5`, as an expression and a zone. */
|
|
44
|
+
export function cronOf(text) {
|
|
45
|
+
const found = /^\s*(?:(?:TZ|CRON_TZ)=(\S+)\s+)?(.+)$/.exec(text);
|
|
46
|
+
return { cron: numericCron(found[2]), timezone: found[1] ?? "UTC" };
|
|
47
|
+
}
|
|
48
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, Math.max(0, ms)));
|
|
49
|
+
function stepTools(client) {
|
|
50
|
+
return {
|
|
51
|
+
run: async (_id, fn) => fn(),
|
|
52
|
+
sleep: (_id, duration) => sleep(durationMs(duration)),
|
|
53
|
+
sleepUntil: (_id, until) => sleep(new Date(until).getTime() - Date.now()),
|
|
54
|
+
sendEvent: (_id, events) => client.send(events),
|
|
55
|
+
waitForEvent: () => refuse("step.waitForEvent", "a run cannot pause for an event; send the event to start a function instead"),
|
|
56
|
+
invoke: () => refuse("step.invoke", "call the function's code directly, or send an event it is triggered by"),
|
|
57
|
+
ai: {
|
|
58
|
+
infer: () => refuse("step.ai.infer", "call the model's API in step.run"),
|
|
59
|
+
wrap: async (_id, fn, ...args) => fn(...args),
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export class Inngest {
|
|
64
|
+
id;
|
|
65
|
+
constructor(options) {
|
|
66
|
+
if (!options?.id)
|
|
67
|
+
throw new Error("new Inngest({ id }) needs the app's id.");
|
|
68
|
+
this.id = options.id;
|
|
69
|
+
}
|
|
70
|
+
createFunction(options, triggerOrHandler, maybeHandler) {
|
|
71
|
+
const handler = (typeof triggerOrHandler === "function" ? triggerOrHandler : maybeHandler);
|
|
72
|
+
const trigger = typeof triggerOrHandler === "function" ? (options.triggers ?? []) : triggerOrHandler;
|
|
73
|
+
for (const key of [
|
|
74
|
+
"concurrency",
|
|
75
|
+
"throttle",
|
|
76
|
+
"debounce",
|
|
77
|
+
"batchEvents",
|
|
78
|
+
"rateLimit",
|
|
79
|
+
"priority",
|
|
80
|
+
"cancelOn",
|
|
81
|
+
])
|
|
82
|
+
if (options[key] !== undefined)
|
|
83
|
+
refuse(`${options.id}'s ${key}`, "a function's runs here each get their own sandbox; remove it to deploy");
|
|
84
|
+
const triggers = Array.isArray(trigger) ? trigger : [trigger];
|
|
85
|
+
if (!triggers.length)
|
|
86
|
+
refuse(`${options.id} with no trigger`, "give it a cron or an event");
|
|
87
|
+
const crons = triggers.flatMap((t) => ("cron" in t ? [cronOf(t.cron)] : []));
|
|
88
|
+
const events = triggers.flatMap((t) => ("event" in t ? [t.event] : []));
|
|
89
|
+
if (crons.length > 1)
|
|
90
|
+
refuse(`${options.id}'s ${crons.length} cron triggers`, "a job has one schedule; split it into one function per cron");
|
|
91
|
+
const finish = options.timeouts?.finish
|
|
92
|
+
? Math.ceil(durationMs(options.timeouts.finish) / 1000)
|
|
93
|
+
: undefined;
|
|
94
|
+
registerTask({
|
|
95
|
+
kind: "inngest",
|
|
96
|
+
id: options.id,
|
|
97
|
+
schedule: crons[0] ?? null,
|
|
98
|
+
...(events.length ? { events } : {}),
|
|
99
|
+
...(finish ? { maxDuration: finish } : {}),
|
|
100
|
+
// Inngest retries 4 times unless told otherwise; a job makes at most 5 attempts.
|
|
101
|
+
retry: { maxAttempts: Math.min(5, (options.retries ?? 4) + 1) },
|
|
102
|
+
invoke: async ({ payload, attempt, runId }) => {
|
|
103
|
+
const event = payload ?? {
|
|
104
|
+
name: "inngest/scheduled.timer",
|
|
105
|
+
data: { cron: crons[0]?.cron },
|
|
106
|
+
ts: Date.now(),
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
return await handler({
|
|
110
|
+
event,
|
|
111
|
+
events: [event],
|
|
112
|
+
step: stepTools(this),
|
|
113
|
+
logger: console,
|
|
114
|
+
runId,
|
|
115
|
+
attempt: attempt - 1,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
// A NonRetriableError ends the run as succeeded-with-error, as
|
|
120
|
+
// Inngest marks it failed but tries no more: exit 0 stops retries.
|
|
121
|
+
if (error instanceof NonRetriableError) {
|
|
122
|
+
console.error(`NonRetriableError: ${error.message}`);
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
id: (prefix) => (prefix ? `${prefix}-${options.id}` : options.id),
|
|
131
|
+
name: options.name ?? options.id,
|
|
132
|
+
triggers,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Runs every function of this app triggered by each event's name, once,
|
|
136
|
+
* now, in a fresh sandbox, with the event as its payload. The functions
|
|
137
|
+
* must be imported where this is called (as serve() imports them) and
|
|
138
|
+
* deployed with `runtime jobs deploy`. */
|
|
139
|
+
async send(payload) {
|
|
140
|
+
const events = Array.isArray(payload) ? payload : [payload];
|
|
141
|
+
const listening = [...tasksRegistry().values()].filter((t) => t.kind === "inngest");
|
|
142
|
+
const { Runtime } = await import("../client.js");
|
|
143
|
+
const runtime = new Runtime();
|
|
144
|
+
const jobs = new Map();
|
|
145
|
+
for await (const job of await runtime.jobs.list({ limit: 100 }))
|
|
146
|
+
if (job.state === "active" || job.state === "paused")
|
|
147
|
+
jobs.set(job.name, job);
|
|
148
|
+
const ids = [];
|
|
149
|
+
for (const event of events) {
|
|
150
|
+
const full = { id: crypto.randomUUID(), ts: Date.now(), data: {}, ...event };
|
|
151
|
+
for (const fn of listening.filter((t) => t.events?.includes(event.name))) {
|
|
152
|
+
const name = jobNameOf(this.id, fn.id);
|
|
153
|
+
const template = jobs.get(name);
|
|
154
|
+
if (!template)
|
|
155
|
+
throw new Error(`No job ${name} for function "${fn.id}": deploy it with \`npx withruntime jobs deploy\`.`);
|
|
156
|
+
const body = Buffer.from(JSON.stringify(full)).toString("base64");
|
|
157
|
+
if (body.length > 65_536)
|
|
158
|
+
throw new Error(`Event ${event.name} is over 64 KiB as JSON.`);
|
|
159
|
+
const d = template.definition;
|
|
160
|
+
const run = await runtime.jobs.create({
|
|
161
|
+
name: `${name.slice(0, 60)}-${Date.now().toString(36)}`,
|
|
162
|
+
schedule: { at: Date.now() },
|
|
163
|
+
command: [...d.command.argv.slice(0, 6), body],
|
|
164
|
+
...(d.image ? { image: d.image } : {}),
|
|
165
|
+
compute: d.compute,
|
|
166
|
+
timeoutSeconds: d.timeoutSeconds,
|
|
167
|
+
retry: d.retry,
|
|
168
|
+
...(d.secrets?.length ? { secrets: d.secrets } : {}),
|
|
169
|
+
});
|
|
170
|
+
ids.push(run.id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { ids };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** serve() of every adapter (inngest/next, /express, /hono, ...): nothing
|
|
177
|
+
* calls the app's endpoint once its functions run as jobs, so it answers 410
|
|
178
|
+
* with where they run now. */
|
|
179
|
+
export function serve(_options) {
|
|
180
|
+
const gone = () => new Response(JSON.stringify({ message: "These functions run on Runtime Jobs now: `runtime jobs ls`." }), { status: 410, headers: { "content-type": "application/json" } });
|
|
181
|
+
const handler = Object.assign(gone, { GET: gone, POST: gone, PUT: gone });
|
|
182
|
+
return handler;
|
|
183
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Fields = {
|
|
2
|
+
minutes: Set<number>;
|
|
3
|
+
hours: Set<number>;
|
|
4
|
+
days: Set<number> | null;
|
|
5
|
+
months: Set<number>;
|
|
6
|
+
weekdays: Set<number> | null;
|
|
7
|
+
};
|
|
8
|
+
export declare function parseCron(expression: string): Fields;
|
|
9
|
+
/** Up to `count` times from `from` (inclusive), forward or back, within 400
|
|
10
|
+
* days; an hour whose wall clock cannot match is skipped whole. */
|
|
11
|
+
export declare function cronTimes(expression: string, timeZone: string, from: number, direction: 1 | -1, count: number): Date[];
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* A cron's times in a zone, as Runtime's scheduler finds them (five numeric
|
|
2
|
+
fields, packages/db/src/schedule.ts): the run's own time, the one before it
|
|
3
|
+
and the next ones, for the payload a Trigger.dev scheduled task receives. A
|
|
4
|
+
run starts within seconds of its time, so its time is the latest match at
|
|
5
|
+
or before now. */
|
|
6
|
+
function setOf(field, low, high) {
|
|
7
|
+
const out = new Set();
|
|
8
|
+
for (const part of field.split(",")) {
|
|
9
|
+
const [range, stepText] = part.split("/");
|
|
10
|
+
const step = stepText === undefined ? 1 : Number(stepText);
|
|
11
|
+
let from;
|
|
12
|
+
let to;
|
|
13
|
+
if (range === "*")
|
|
14
|
+
[from, to] = [low, high];
|
|
15
|
+
else if (range.includes("-"))
|
|
16
|
+
[from, to] = range.split("-").map(Number);
|
|
17
|
+
else
|
|
18
|
+
[from, to] = [Number(range), stepText === undefined ? Number(range) : high];
|
|
19
|
+
if (![from, to, step].every(Number.isInteger) ||
|
|
20
|
+
step < 1 ||
|
|
21
|
+
from < low ||
|
|
22
|
+
to > high ||
|
|
23
|
+
from > to)
|
|
24
|
+
throw new Error(`"${field}" is not a cron field from ${low} to ${high}`);
|
|
25
|
+
for (let n = from; n <= to; n += step)
|
|
26
|
+
out.add(n);
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
export function parseCron(expression) {
|
|
31
|
+
const f = expression.trim().split(/\s+/);
|
|
32
|
+
if (f.length !== 5)
|
|
33
|
+
throw new Error(`"${expression}" is not five cron fields`);
|
|
34
|
+
const weekdays = f[4] === "*" ? null : setOf(f[4], 0, 7);
|
|
35
|
+
if (weekdays?.has(7))
|
|
36
|
+
weekdays.add(0);
|
|
37
|
+
return {
|
|
38
|
+
minutes: setOf(f[0], 0, 59),
|
|
39
|
+
hours: setOf(f[1], 0, 23),
|
|
40
|
+
days: f[2] === "*" ? null : setOf(f[2], 1, 31),
|
|
41
|
+
months: setOf(f[3], 1, 12),
|
|
42
|
+
weekdays,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** The wall clock of an instant in a zone. */
|
|
46
|
+
function wall(at, timeZone) {
|
|
47
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
48
|
+
timeZone,
|
|
49
|
+
hourCycle: "h23",
|
|
50
|
+
month: "numeric",
|
|
51
|
+
day: "numeric",
|
|
52
|
+
hour: "numeric",
|
|
53
|
+
minute: "numeric",
|
|
54
|
+
weekday: "short",
|
|
55
|
+
}).formatToParts(new Date(at));
|
|
56
|
+
const n = (type) => Number(parts.find((p) => p.type === type)?.value);
|
|
57
|
+
const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].indexOf(parts.find((p) => p.type === "weekday")?.value ?? "");
|
|
58
|
+
return { month: n("month"), day: n("day"), hour: n("hour"), minute: n("minute"), weekday };
|
|
59
|
+
}
|
|
60
|
+
function matches(f, w) {
|
|
61
|
+
if (!f.months.has(w.month) || !f.hours.has(w.hour) || !f.minutes.has(w.minute))
|
|
62
|
+
return false;
|
|
63
|
+
// As in cron: with both day fields set, either matches.
|
|
64
|
+
if (f.days && f.weekdays)
|
|
65
|
+
return f.days.has(w.day) || f.weekdays.has(w.weekday);
|
|
66
|
+
if (f.days)
|
|
67
|
+
return f.days.has(w.day);
|
|
68
|
+
if (f.weekdays)
|
|
69
|
+
return f.weekdays.has(w.weekday);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
const MINUTE = 60_000;
|
|
73
|
+
const HOUR = 3_600_000;
|
|
74
|
+
/** Up to `count` times from `from` (inclusive), forward or back, within 400
|
|
75
|
+
* days; an hour whose wall clock cannot match is skipped whole. */
|
|
76
|
+
export function cronTimes(expression, timeZone, from, direction, count) {
|
|
77
|
+
const f = parseCron(expression);
|
|
78
|
+
const out = [];
|
|
79
|
+
let at = Math.floor(from / MINUTE) * MINUTE;
|
|
80
|
+
const end = from + direction * 400 * 24 * HOUR;
|
|
81
|
+
while (out.length < count && (direction === 1 ? at <= end : at >= end)) {
|
|
82
|
+
const w = wall(at, timeZone);
|
|
83
|
+
if (!f.hours.has(w.hour) || !f.months.has(w.month)) {
|
|
84
|
+
// To the next (or previous) hour boundary of this zone's clock.
|
|
85
|
+
at = direction === 1 ? at + (60 - w.minute) * MINUTE : at - (w.minute + 1) * MINUTE;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (matches(f, w))
|
|
89
|
+
out.push(new Date(at));
|
|
90
|
+
at += direction * MINUTE;
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type CodeTask = {
|
|
2
|
+
/** "trigger" or "inngest": which drop-in declared it. */
|
|
3
|
+
kind: "trigger" | "inngest";
|
|
4
|
+
id: string;
|
|
5
|
+
/** Five cron fields and a zone; null for a task run on demand. */
|
|
6
|
+
schedule: {
|
|
7
|
+
cron: string;
|
|
8
|
+
timezone: string;
|
|
9
|
+
} | null;
|
|
10
|
+
/** Inngest: the event names that run it. */
|
|
11
|
+
events?: string[];
|
|
12
|
+
compute?: {
|
|
13
|
+
vcpu: number;
|
|
14
|
+
memoryMiB: number;
|
|
15
|
+
};
|
|
16
|
+
/** Seconds. */
|
|
17
|
+
maxDuration?: number;
|
|
18
|
+
retry?: {
|
|
19
|
+
maxAttempts: number;
|
|
20
|
+
};
|
|
21
|
+
/** Calls the task with what the runner hands it. */
|
|
22
|
+
invoke(input: {
|
|
23
|
+
payload: unknown;
|
|
24
|
+
attempt: number;
|
|
25
|
+
runId: string;
|
|
26
|
+
}): Promise<unknown>;
|
|
27
|
+
};
|
|
28
|
+
export declare function tasksRegistry(): Map<string, CodeTask>;
|
|
29
|
+
export declare function registerTask(task: CodeTask): void;
|
|
30
|
+
/** The name of the Runtime job that runs a task. */
|
|
31
|
+
export declare function jobNameOf(project: string, id: string): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/* The tasks a project's code declares through a drop-in (withruntime/trigger,
|
|
2
|
+
withruntime/inngest), kept on globalThis under one symbol so the runner
|
|
3
|
+
(runner.ts, copied into the job's image) finds them whatever copy of the
|
|
4
|
+
SDK the project installed. `runtime jobs deploy` asks the runner to list
|
|
5
|
+
them, then makes one Runtime job per task; each run imports the task's file
|
|
6
|
+
and calls it. */
|
|
7
|
+
const KEY = Symbol.for("withruntime.jobs.tasks");
|
|
8
|
+
export function tasksRegistry() {
|
|
9
|
+
const g = globalThis;
|
|
10
|
+
let found = g[KEY];
|
|
11
|
+
if (!found) {
|
|
12
|
+
found = new Map();
|
|
13
|
+
g[KEY] = found;
|
|
14
|
+
}
|
|
15
|
+
return found;
|
|
16
|
+
}
|
|
17
|
+
export function registerTask(task) {
|
|
18
|
+
const registry = tasksRegistry();
|
|
19
|
+
const key = `${task.kind}:${task.id}`;
|
|
20
|
+
if (registry.has(key))
|
|
21
|
+
throw new Error(`Two ${task.kind} tasks have the id "${task.id}"; each id is one Runtime job.`);
|
|
22
|
+
registry.set(key, task);
|
|
23
|
+
}
|
|
24
|
+
/** The name of the Runtime job that runs a task. */
|
|
25
|
+
export function jobNameOf(project, id) {
|
|
26
|
+
return `${project}-${id}`
|
|
27
|
+
.replace(/[^A-Za-z0-9_.-]/g, "-")
|
|
28
|
+
.replace(/^[^A-Za-z0-9]+/, "")
|
|
29
|
+
.slice(0, 80);
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function main(argv: string[]): Promise<number>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/* The runner in a code job's image (`runtime jobs deploy` copies this file,
|
|
2
|
+
built, to .runtime/jobs-runner.ts). It imports nothing of the SDK: the
|
|
3
|
+
project's own files import withruntime/trigger or withruntime/inngest,
|
|
4
|
+
which register their tasks on globalThis (registry.ts), and this reads
|
|
5
|
+
them there.
|
|
6
|
+
|
|
7
|
+
bun .runtime/jobs-runner.ts discover <file>...
|
|
8
|
+
prints one line, JOBS-DISCOVERED <json>: every task the files declare,
|
|
9
|
+
and trigger.config's defaults when a file defines them
|
|
10
|
+
bun .runtime/jobs-runner.ts run <file> <kind> <id> [<payload as base64 JSON>]
|
|
11
|
+
runs one task; exits 0 when it returns, 1 when it throws */
|
|
12
|
+
import { resolve } from "node:path";
|
|
13
|
+
import { pathToFileURL } from "node:url";
|
|
14
|
+
const registry = () => globalThis[Symbol.for("withruntime.jobs.tasks")] ?? new Map();
|
|
15
|
+
async function load(file) {
|
|
16
|
+
await import(pathToFileURL(resolve(file)).href);
|
|
17
|
+
}
|
|
18
|
+
export async function main(argv) {
|
|
19
|
+
const [verb, ...rest] = argv;
|
|
20
|
+
if (verb === "discover") {
|
|
21
|
+
const found = [];
|
|
22
|
+
for (const file of rest) {
|
|
23
|
+
const before = new Set(registry().keys());
|
|
24
|
+
try {
|
|
25
|
+
await load(file);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error(`JOBS-FILE-FAILED ${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
for (const [key, task] of registry()) {
|
|
32
|
+
if (before.has(key))
|
|
33
|
+
continue;
|
|
34
|
+
const described = Object.fromEntries(Object.entries(task).filter(([field]) => field !== "invoke"));
|
|
35
|
+
found.push({ ...described, file });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const config = globalThis[Symbol.for("withruntime.trigger.config")];
|
|
39
|
+
console.log(`JOBS-DISCOVERED ${JSON.stringify({ tasks: found, triggerConfig: config ?? null })}`);
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
if (verb === "run") {
|
|
43
|
+
const [file, kind, id, payload] = rest;
|
|
44
|
+
if (!file || !kind || !id) {
|
|
45
|
+
console.error("usage: run <file> <kind> <id> [<payload>]");
|
|
46
|
+
return 2;
|
|
47
|
+
}
|
|
48
|
+
await load(file);
|
|
49
|
+
const task = registry().get(`${kind}:${id}`);
|
|
50
|
+
if (!task) {
|
|
51
|
+
console.error(`${file} declares no ${kind} task "${id}": was it renamed? Run \`runtime jobs deploy\` again.`);
|
|
52
|
+
return 2;
|
|
53
|
+
}
|
|
54
|
+
const input = payload
|
|
55
|
+
? JSON.parse(Buffer.from(payload, "base64").toString("utf8"))
|
|
56
|
+
: undefined;
|
|
57
|
+
try {
|
|
58
|
+
await task.invoke({
|
|
59
|
+
payload: input,
|
|
60
|
+
attempt: 1,
|
|
61
|
+
runId: process.env.RUNTIME_SANDBOX_ID ?? "",
|
|
62
|
+
});
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error(error instanceof Error ? (error.stack ?? error.message) : String(error));
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
console.error("usage: discover <file>... | run <file> <kind> <id> [<payload>]");
|
|
71
|
+
return 2;
|
|
72
|
+
}
|
|
73
|
+
if (/(^|[/\\])(jobs-)?runner\.[jt]s$/.test(process.argv[1] ?? ""))
|
|
74
|
+
process.exit(await main(process.argv.slice(2)));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CliKit } from "./cli.js";
|
|
2
|
+
import type { Runtime } from "./client.js";
|
|
3
|
+
type Out = {
|
|
4
|
+
json: boolean;
|
|
5
|
+
write: (text: string) => void;
|
|
6
|
+
error: (text: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const JOB_DEPLOY_HELP = "runtime jobs deploy [<dir>] [--name <project>] [--dry-run]\n\n Run Trigger.dev tasks and Inngest functions as Runtime jobs. First change the\n import: `@trigger.dev/sdk` (or /v3) to `withruntime/trigger`, or `inngest`\n (and its serve adapters, such as inngest/next) to `withruntime/inngest`.\n\n Each schedules.task, or Inngest function with a cron trigger, becomes a job on\n that schedule. Each task, or function with an event trigger, becomes a paused\n job that tasks.trigger(id, payload) or inngest.send(event) runs once per call.\n A task's machine, maxDuration and retry.maxAttempts (an Inngest function's\n retries and timeouts.finish) size its job.\n\n --name the jobs' prefix: <name>-<task id>. Default: trigger.config's project,\n the Inngest app's id, or package.json's name\n --dry-run list the tasks the files declare; create nothing (it still builds the image)\n";
|
|
9
|
+
/** The project's files that import a drop-in, and those that still import
|
|
10
|
+
* the platform's own SDK. */
|
|
11
|
+
export declare function scanProject(dir: string): Promise<{
|
|
12
|
+
ours: string[];
|
|
13
|
+
theirs: string[];
|
|
14
|
+
}>;
|
|
15
|
+
/** The Dockerfile a project's code jobs run from: Node 22 and Bun, the
|
|
16
|
+
* project's dependencies by its own lockfile, and the runner. */
|
|
17
|
+
/** When an on-demand task's template job is due: 1 January 9999, UTC, never
|
|
18
|
+
* reached (the API takes a time up to the year 275760). */
|
|
19
|
+
export declare const TEMPLATE_AT: number;
|
|
20
|
+
export declare function codeDockerfile(runner: string): string;
|
|
21
|
+
export declare function jobsDeployCommand(argv: string[], out: Out, runtime: () => Promise<Runtime>, kit: CliKit): Promise<number>;
|
|
22
|
+
export {};
|