logic-sdk 0.7.2 → 0.7.3
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 +2 -3
- package/dist/adapters/cloudflare-queue.d.ts +3 -2
- package/dist/adapters/cloudflare-queue.js +14 -4
- package/dist/adapters/drizzle-pg.d.ts +31 -1
- package/dist/adapters/drizzle-pg.js +14 -5
- package/dist/adapters/drizzle-sqlite.d.ts +17 -1
- package/dist/adapters/drizzle-sqlite.js +11 -4
- package/dist/adapters/postgres.d.ts +9 -8
- package/dist/adapters/postgres.js +43 -19
- package/dist/adapters/sqlite.d.ts +1 -1
- package/dist/adapters/sqlite.js +32 -12
- package/dist/adapters/stored-jobs.d.ts +27 -19
- package/dist/adapters/stored-jobs.js +157 -61
- package/dist/core/in-process.js +23 -3
- package/dist/core/jobs.d.ts +12 -6
- package/dist/core/jobs.js +158 -17
- package/dist/core/registry.d.ts +39 -3
- package/dist/core/registry.js +23 -11
- package/dist/core/testing.js +3 -1
- package/dist/core/types.d.ts +96 -34
- package/dist/hosts/cloudflare.d.ts +5 -4
- package/dist/hosts/cloudflare.js +5 -4
- package/dist/hosts/node.d.ts +25 -15
- package/dist/hosts/node.js +61 -34
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +1 -1
- package/package.json +1 -11
- package/skills/logic-sdk/SKILL.md +4 -4
- package/src/adapters/cloudflare-queue.ts +13 -4
- package/src/adapters/drizzle-pg.ts +14 -5
- package/src/adapters/drizzle-sqlite.ts +11 -4
- package/src/adapters/postgres.ts +43 -19
- package/src/adapters/sqlite.ts +32 -12
- package/src/adapters/stored-jobs.ts +174 -74
- package/src/core/in-process.ts +22 -2
- package/src/core/jobs.ts +146 -19
- package/src/core/registry.ts +62 -14
- package/src/core/testing.ts +3 -1
- package/src/core/types.ts +93 -30
- package/src/hosts/cloudflare.ts +5 -4
- package/src/hosts/node.ts +73 -46
- package/src/index.ts +3 -1
- package/src/node.ts +1 -1
- package/dist/adapters/pg-boss.d.ts +0 -45
- package/dist/adapters/pg-boss.js +0 -180
- package/dist/hosts/pg-boss.d.ts +0 -22
- package/dist/hosts/pg-boss.js +0 -55
- package/dist/pg-boss.d.ts +0 -2
- package/dist/pg-boss.js +0 -2
- package/src/adapters/pg-boss.ts +0 -195
- package/src/hosts/pg-boss.ts +0 -71
- package/src/pg-boss.ts +0 -2
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
// Jobs in the registry's database, written in the mutation's transaction: run from there (databaseQueue), or kept
|
|
2
2
|
// until a queue has them (outbox). The table's statements are SQL, one set per database (postgres.ts, sqlite.ts), and run through
|
|
3
3
|
// the database adapter's `query`, so this file knows no ORM and no driver.
|
|
4
|
-
import { defaultBackoff,
|
|
5
|
-
/**
|
|
4
|
+
import { defaultBackoff, storedUniqueKey } from "../core/jobs.js";
|
|
5
|
+
/**
|
|
6
|
+
* Seconds a job due now waits for the request that queued it before a sweep may take it (how soon a crash after
|
|
7
|
+
* commit is noticed). Behind an outbox, also how long past due a sweep leaves a row to its signal.
|
|
8
|
+
*/
|
|
6
9
|
const PICKUP = 30;
|
|
10
|
+
/** A signal for a later time goes out this much later, so a clock ahead of the database's doesn't land it early. */
|
|
11
|
+
const SLACK = 1000;
|
|
7
12
|
/**
|
|
8
|
-
* Seconds a runner keeps extending a job's lease: the limit of a Workers cron or queue consumer,
|
|
9
|
-
*
|
|
13
|
+
* Seconds a runner keeps extending a job's lease: the limit of a Workers cron or queue consumer. Past it, a job that
|
|
14
|
+
* hangs is taken over like a crashed one. A longer job needs a `lease` that long.
|
|
10
15
|
*/
|
|
11
16
|
const MAX_EXTEND = 15 * 60;
|
|
17
|
+
/**
|
|
18
|
+
* Seconds a queue consumer keeps running one key's jobs before it hands the rest to the next message: one busy key
|
|
19
|
+
* can't hold a consumer, and the messages behind it, until the platform stops it.
|
|
20
|
+
*/
|
|
21
|
+
const DRAIN = 60;
|
|
22
|
+
/** A delivery's `key`: its jobs run one at a time, in insert order. */
|
|
23
|
+
const keyOf = (job) => (job.kind === "delivery" ? job.key : undefined);
|
|
12
24
|
/**
|
|
13
25
|
* The registry's database, taken at `createRegistry` (`sql: true`, so the core refuses one that doesn't run SQL): jobs
|
|
14
26
|
* are rows written in the mutation's transaction, so there's no other database they could go to. Laravel's `database`
|
|
@@ -16,12 +28,17 @@ const MAX_EXTEND = 15 * 60;
|
|
|
16
28
|
*/
|
|
17
29
|
function registryDatabase(name) {
|
|
18
30
|
let database;
|
|
31
|
+
let workers = { takes: () => false, wake: () => { } };
|
|
19
32
|
return {
|
|
20
|
-
connect(
|
|
33
|
+
connect(setup) {
|
|
34
|
+
const { sql } = setup;
|
|
21
35
|
if (database && sql && database.db !== sql.db)
|
|
22
36
|
throw new Error(`[logic-sdk] ${name} is already connected to another registry's database: call it once per registry.`);
|
|
23
37
|
database = sql;
|
|
38
|
+
workers = setup.workers;
|
|
24
39
|
},
|
|
40
|
+
/** The loops running jobs in this process (`work()`). */
|
|
41
|
+
workers: () => workers,
|
|
25
42
|
get() {
|
|
26
43
|
if (!database)
|
|
27
44
|
throw new Error(`[logic-sdk] ${name} isn't connected: pass it to createRegistry.`);
|
|
@@ -29,23 +46,30 @@ function registryDatabase(name) {
|
|
|
29
46
|
},
|
|
30
47
|
};
|
|
31
48
|
}
|
|
32
|
-
/** Both: rows in `logic_jobs`, claimed by receipt. Without `via` they run here; with it
|
|
49
|
+
/** Both: rows in `logic_jobs`, claimed by receipt. Without `via` they run here; with it, the queue carries signals. */
|
|
33
50
|
export function storedJobs(sql, options, via) {
|
|
34
51
|
const name = via ? "outbox(queue)" : "databaseQueue()";
|
|
52
|
+
// its jobs would be signalled to a `deliver` that has nothing to do
|
|
53
|
+
if (via?.enqueue)
|
|
54
|
+
throw new Error(`[logic-sdk] outbox(queue) got a queue that writes its jobs in your transaction itself (it has \`enqueue\`): use it on its own, without an outbox.`);
|
|
35
55
|
const database = registryDatabase(name);
|
|
36
56
|
const lease = options.lease ?? 300;
|
|
37
|
-
const maxAttempts = options.maxAttempts ?? 10;
|
|
38
57
|
const backoff = options.backoff ?? defaultBackoff;
|
|
58
|
+
const inWorker = options.runIn === "worker";
|
|
59
|
+
/**
|
|
60
|
+
* Whether this request leaves its jobs due now to workers: always with `runIn: "worker"`, and when a loop in this
|
|
61
|
+
* process takes every one of them. Asked the same way at insert and after commit, so the row is free for it at once.
|
|
62
|
+
*/
|
|
63
|
+
const leaves = (jobs) => inWorker || jobs.filter((j) => !j.runAt).every((j) => database.workers().takes(j));
|
|
39
64
|
/** Outside a mutation: on the database itself. */
|
|
40
65
|
const query = (text, params) => database.get().query(database.get().db, text, params);
|
|
41
66
|
const list = (ids) => JSON.stringify(ids);
|
|
42
67
|
// These three only land on rows still pending under this claim (each statement says so): a slow runner can't undo a
|
|
43
|
-
// newer runner's work
|
|
44
|
-
// receipt, not attempt number: `retry(id)` starts those again at 1.
|
|
68
|
+
// newer runner's work. By receipt, not attempt number: `retry(id)` starts those again at 1.
|
|
45
69
|
const failedRows = rowsOfFailed(sql, query);
|
|
46
|
-
const markFailed = (
|
|
47
|
-
const backOff = (
|
|
48
|
-
const remove = (
|
|
70
|
+
const markFailed = (job, claim, error) => query(sql.markFailed, [describe(error), job.id, claim.receipt]);
|
|
71
|
+
const backOff = (job, claim, seconds, error) => query(sql.backOff, [seconds, describe(error), job.id, claim.receipt]);
|
|
72
|
+
const remove = (job, claim) => query(sql.remove, [job.id, claim.receipt]);
|
|
49
73
|
/**
|
|
50
74
|
* While a job runs, its lease is extended, as Laravel Cloud does on SQS: a slow job stays its runner's, and a dead
|
|
51
75
|
* runner's job is free a lease later. Returns the stop, which waits for the extension on its way: landing after a
|
|
@@ -58,7 +82,7 @@ export function storedJobs(sql, options, via) {
|
|
|
58
82
|
if (Date.now() >= until)
|
|
59
83
|
return clearInterval(timer);
|
|
60
84
|
extending = extending
|
|
61
|
-
.then(() => query(sql.extend, [lease,
|
|
85
|
+
.then(() => query(sql.extend, [lease, job.id, claim.receipt]))
|
|
62
86
|
.catch((e) => runner.report(e, job, claim.attempt));
|
|
63
87
|
}, (lease * 1000) / 3);
|
|
64
88
|
return () => {
|
|
@@ -68,7 +92,7 @@ export function storedJobs(sql, options, via) {
|
|
|
68
92
|
}
|
|
69
93
|
/**
|
|
70
94
|
* Run one claimed job here and record how it went. The core decides: `failed` (its `fail` wrote the row), a throw
|
|
71
|
-
* (tries left: back off), or done and discarded (delete the row).
|
|
95
|
+
* (tries left: back off, and behind an outbox signal the retry), or done and discarded (delete the row).
|
|
72
96
|
*/
|
|
73
97
|
async function run(job, claim, runner) {
|
|
74
98
|
const stopExtending = keepExtending(job, claim, runner);
|
|
@@ -78,37 +102,85 @@ export function storedJobs(sql, options, via) {
|
|
|
78
102
|
.run(job, {
|
|
79
103
|
attempt: claim.attempt,
|
|
80
104
|
fail: async (error) => {
|
|
81
|
-
await markFailed(
|
|
105
|
+
await markFailed(job, claim, error);
|
|
82
106
|
},
|
|
83
107
|
})
|
|
84
108
|
.finally(stopExtending);
|
|
85
109
|
}
|
|
86
110
|
catch (e) {
|
|
87
|
-
|
|
111
|
+
const seconds = backoff(claim.attempt);
|
|
112
|
+
await backOff(job, claim, seconds, e);
|
|
113
|
+
if (via)
|
|
114
|
+
await signal([{ ...job, runAt: new Date(Date.now() + seconds * 1000).toISOString() }], runner);
|
|
88
115
|
return "retrying";
|
|
89
116
|
}
|
|
90
117
|
if (outcome !== "failed")
|
|
91
|
-
await remove(
|
|
118
|
+
await remove(job, claim);
|
|
92
119
|
return outcome;
|
|
93
120
|
}
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
|
|
121
|
+
/**
|
|
122
|
+
* Run a key's next jobs here, one by one, as they come free: after a job in the request, rather than leave them to a
|
|
123
|
+
* sweep (which runs them as they're due, one per claim), and for a consumer's signal behind an outbox. There, after
|
|
124
|
+
* `DRAIN` seconds the rest get one more signal.
|
|
125
|
+
*/
|
|
126
|
+
async function runKey(key, runner) {
|
|
127
|
+
const until = Date.now() + DRAIN * 1000;
|
|
128
|
+
for (;;) {
|
|
129
|
+
const receipt = crypto.randomUUID();
|
|
130
|
+
const [row] = await query(sql.claimNext, [receipt, lease, key]);
|
|
131
|
+
if (!row)
|
|
132
|
+
return;
|
|
133
|
+
const job = JSON.parse(String(row.job));
|
|
134
|
+
await run(job, { receipt, attempt: Number(row.attempts) }, runner);
|
|
135
|
+
if (via && Date.now() >= until)
|
|
136
|
+
return signal([job], runner);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Behind an outbox, a consumer got a signal: run the row it names (with a `key`, the key's next jobs). The row is the
|
|
141
|
+
* truth, so a duplicate, a late one, or one whose row is gone or taken has nothing to do.
|
|
142
|
+
*/
|
|
143
|
+
async function consume(job, runner) {
|
|
144
|
+
const key = keyOf(job);
|
|
145
|
+
if (key)
|
|
146
|
+
return runKey(key, runner);
|
|
147
|
+
const receipt = crypto.randomUUID();
|
|
148
|
+
const [row] = await query(sql.claimOne, [receipt, lease, job.id]);
|
|
149
|
+
if (row)
|
|
150
|
+
await run(JSON.parse(String(row.job)), { receipt, attempt: Number(row.attempts) }, runner);
|
|
151
|
+
}
|
|
152
|
+
/** Can `via` hold a signal until `at`? */
|
|
153
|
+
const canHold = (at) => via?.holds === true || (!!via?.holds && (at - Date.now()) / 1000 <= via.holds.max);
|
|
154
|
+
/**
|
|
155
|
+
* Behind an outbox, every job stays a row and the queue gets a signal: whoever gets one runs the row from the table
|
|
156
|
+
* (`consume`). One for a later time goes out only if the queue can hold it; a sweep runs the rest. A signal that
|
|
157
|
+
* doesn't go out is reported and the rows wait for a sweep. The runner the queue gets runs signals too, for a queue
|
|
158
|
+
* that runs what it's sent itself.
|
|
159
|
+
*/
|
|
160
|
+
async function signal(jobs, runner) {
|
|
161
|
+
const signals = [];
|
|
162
|
+
for (const job of jobs) {
|
|
163
|
+
if (!job.runAt)
|
|
164
|
+
signals.push(job);
|
|
165
|
+
else if (canHold(Date.parse(job.runAt)))
|
|
166
|
+
signals.push({ ...job, runAt: new Date(Date.parse(job.runAt) + SLACK).toISOString() });
|
|
167
|
+
}
|
|
168
|
+
if (!signals.length)
|
|
169
|
+
return;
|
|
170
|
+
const consumer = {
|
|
171
|
+
...runner,
|
|
172
|
+
run: async (job) => {
|
|
173
|
+
await consume(job, runner);
|
|
174
|
+
return "done";
|
|
175
|
+
},
|
|
176
|
+
};
|
|
97
177
|
try {
|
|
98
|
-
await
|
|
178
|
+
await via.deliver(signals, consumer);
|
|
99
179
|
}
|
|
100
180
|
catch (e) {
|
|
101
|
-
for (const job of
|
|
102
|
-
runner.report(e, job
|
|
103
|
-
if (claim.attempt >= maxAttempts) {
|
|
104
|
-
await markFailed(ids, claim, e);
|
|
105
|
-
return "failed";
|
|
106
|
-
}
|
|
107
|
-
await backOff(ids, claim, e);
|
|
108
|
-
return "retrying";
|
|
181
|
+
for (const job of signals)
|
|
182
|
+
runner.report(e, job);
|
|
109
183
|
}
|
|
110
|
-
await remove(ids, claim);
|
|
111
|
-
return "done";
|
|
112
184
|
}
|
|
113
185
|
/** A cron's minute runs once: records the ticks no one recorded yet and returns their ids. Prunes old ones. */
|
|
114
186
|
async function recordTicks(tx, ticks) {
|
|
@@ -118,73 +190,98 @@ export function storedJobs(sql, options, via) {
|
|
|
118
190
|
await database.get().query(tx, sql.pruneTicks, []);
|
|
119
191
|
return new Set(first.map((row) => row.id));
|
|
120
192
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
async function claimNew(jobs) {
|
|
193
|
+
/** The first claim of a job just committed, by the request that queued it, unless a sweep took it meanwhile. */
|
|
194
|
+
async function claimNew(job) {
|
|
124
195
|
const claim = { receipt: crypto.randomUUID(), attempt: 1 };
|
|
125
|
-
const rows = await query(sql.claimNew, [claim.receipt, lease,
|
|
126
|
-
|
|
127
|
-
return { claim, won: jobs.filter((j) => ids.has(j.id)) }; // in emit order: RETURNING has none
|
|
196
|
+
const rows = await query(sql.claimNew, [claim.receipt, lease, job.id]);
|
|
197
|
+
return rows.length ? claim : undefined;
|
|
128
198
|
}
|
|
129
199
|
return {
|
|
130
200
|
name,
|
|
131
201
|
retries: true, // a failed try backs off as a row
|
|
132
202
|
holds: true, // rows wait until they're due, however long, so `via`'s limits on delays don't apply
|
|
133
|
-
replaces: true, // a waiting row is replaced in place;
|
|
203
|
+
replaces: true, // a waiting row is replaced in place; behind an outbox too, since every job stays a row
|
|
204
|
+
orders: true, // a key's jobs run from the table, in order; behind an outbox the queue only signals
|
|
205
|
+
queues: true, // a row's queue is in its job: a sweep takes the ones it's asked for, in that order
|
|
134
206
|
/** Takes the registry's database: it writes in the mutation's transaction. */
|
|
135
207
|
sql: true,
|
|
136
208
|
connect: (setup) => database.connect(setup),
|
|
137
209
|
/**
|
|
138
210
|
* Inside the mutation's transaction: one unclaimed row per job. Due now: a sweep leaves it `PICKUP` seconds to this
|
|
139
|
-
* request
|
|
211
|
+
* request, or none when the request runs nothing (workers take it, or a signal, which the sweep's grace covers). Delayed:
|
|
212
|
+
* available from `runAt`. A cron's minute runs once: only ticks nobody recorded yet.
|
|
140
213
|
*/
|
|
141
214
|
async enqueue(tx, jobs) {
|
|
142
215
|
const firstTicks = await recordTicks(tx, jobs.filter((j) => j.kind === "cron"));
|
|
143
216
|
const kept = jobs.filter((j) => j.kind !== "cron" || firstTicks.has(j.id));
|
|
144
|
-
const rows = (await Promise.all(kept.map(async (j) => ({ id: j.id, job: j, at: j.runAt ? Date.parse(j.runAt) : null,
|
|
145
|
-
// in key order, so two transactions
|
|
146
|
-
.sort((a, b) => byText(a.
|
|
217
|
+
const rows = (await Promise.all(kept.map(async (j) => ({ id: j.id, job: j, at: j.runAt ? Date.parse(j.runAt) : null, unique: (await storedUniqueKey(j)) ?? null, order: (j.kind === "delivery" && j.key) || null }))))
|
|
218
|
+
// in key order, so two transactions with the same unique keys lock them in the same order and can't deadlock
|
|
219
|
+
.sort((a, b) => byText(a.unique ?? "", b.unique ?? ""));
|
|
147
220
|
if (rows.length)
|
|
148
|
-
await database.get().query(tx, sql.insert, [PICKUP, JSON.stringify(rows)]);
|
|
221
|
+
await database.get().query(tx, sql.insert, [via || leaves(kept) ? 0 : PICKUP, JSON.stringify(rows)]);
|
|
149
222
|
},
|
|
150
223
|
/**
|
|
151
|
-
* After commit, in this request
|
|
152
|
-
*
|
|
153
|
-
* is a sweep's, as a first try.
|
|
224
|
+
* After commit, in this request. Behind an outbox: one signal per job, and one per key. Left to workers: wake this
|
|
225
|
+
* process's, if any (others poll). Otherwise each job due now is claimed just before it runs, so a slow or dead one
|
|
226
|
+
* doesn't spend the others' attempt; what isn't claimed yet when the process dies is a sweep's, as a first try.
|
|
154
227
|
*/
|
|
155
228
|
async deliver(jobs, runner) {
|
|
156
|
-
const due = jobs.filter((j) => !j.runAt);
|
|
157
|
-
if (!due.length)
|
|
158
|
-
return;
|
|
159
229
|
if (via) {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
230
|
+
const keys = new Set();
|
|
231
|
+
const firsts = jobs.filter((job) => {
|
|
232
|
+
const key = keyOf(job);
|
|
233
|
+
if (!key)
|
|
234
|
+
return true;
|
|
235
|
+
if (keys.has(key))
|
|
236
|
+
return false;
|
|
237
|
+
keys.add(key);
|
|
238
|
+
return true;
|
|
239
|
+
});
|
|
240
|
+
return signal(firsts, runner);
|
|
241
|
+
}
|
|
242
|
+
const due = jobs.filter((j) => !j.runAt);
|
|
243
|
+
if (leaves(jobs)) {
|
|
244
|
+
if (due.length)
|
|
245
|
+
database.workers().wake(due.length);
|
|
163
246
|
return;
|
|
164
247
|
}
|
|
165
248
|
for (const job of due) {
|
|
166
249
|
try {
|
|
167
|
-
const
|
|
168
|
-
if (
|
|
169
|
-
|
|
250
|
+
const claim = await claimNew(job);
|
|
251
|
+
if (!claim)
|
|
252
|
+
continue;
|
|
253
|
+
await run(job, claim, runner);
|
|
254
|
+
const key = keyOf(job);
|
|
255
|
+
if (key)
|
|
256
|
+
await runKey(key, runner); // its key's next ones, rather than leave them to a sweep
|
|
170
257
|
}
|
|
171
258
|
catch (e) {
|
|
172
259
|
runner.report(e, job, 1); // its bookkeeping failed (a lost connection): a sweep sorts it out, and the others still run
|
|
173
260
|
}
|
|
174
261
|
}
|
|
175
262
|
},
|
|
176
|
-
/**
|
|
177
|
-
|
|
263
|
+
/**
|
|
264
|
+
* What's due (retries, delayed jobs, what a crash left behind), claimed one at a time, so every claim gets a full
|
|
265
|
+
* lease. Behind an outbox, only rows `PICKUP` seconds past due: until then they're their signal's.
|
|
266
|
+
*/
|
|
267
|
+
async sweep(runner, { limit = 100, queues } = {}) {
|
|
178
268
|
const result = { done: 0, failed: 0, discarded: 0, retrying: 0 };
|
|
269
|
+
// in the order given: each claim takes the first queue with a job due, as a worker listing queues does
|
|
270
|
+
const order = queues?.length ? queues : [""];
|
|
179
271
|
for (let i = 0; i < limit; i++) {
|
|
180
272
|
const receipt = crypto.randomUUID();
|
|
181
|
-
|
|
273
|
+
let row;
|
|
274
|
+
for (const queue of order) {
|
|
275
|
+
[row] = await query(sql.claimDue, [receipt, lease, via ? PICKUP : 0, queue]);
|
|
276
|
+
if (row)
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
182
279
|
if (!row)
|
|
183
280
|
break;
|
|
184
281
|
const job = JSON.parse(String(row.job)); // claimDue returns it as JSON text, on every driver
|
|
185
282
|
const attempt = Number(row.attempts);
|
|
186
283
|
try {
|
|
187
|
-
result[await
|
|
284
|
+
result[await run(job, { receipt, attempt }, runner)]++;
|
|
188
285
|
}
|
|
189
286
|
catch (e) {
|
|
190
287
|
runner.report(e, job, attempt); // its bookkeeping failed (a lost connection): its lease brings it back
|
|
@@ -196,8 +293,7 @@ export function storedJobs(sql, options, via) {
|
|
|
196
293
|
async retry(id) {
|
|
197
294
|
return (await query(sql.retry, [id])).length > 0;
|
|
198
295
|
},
|
|
199
|
-
|
|
200
|
-
fail: failedRows.fail,
|
|
296
|
+
...(via ? { consume } : {}),
|
|
201
297
|
failed: failedRows.failed,
|
|
202
298
|
forget: failedRows.forget,
|
|
203
299
|
};
|
package/dist/core/in-process.js
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
import { RUNS_NOW } from "./types.js";
|
|
2
|
+
/** The last job of each key, in this process: the next one starts after it. */
|
|
3
|
+
const lastOfKey = new Map();
|
|
2
4
|
const IN_PROCESS = {
|
|
3
5
|
[RUNS_NOW]: true,
|
|
4
6
|
name: "inProcess()",
|
|
7
|
+
orders: true,
|
|
8
|
+
queues: true, // nothing waits, so there's nothing to keep apart
|
|
5
9
|
async deliver(jobs, { run }) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
for (const job of jobs) {
|
|
11
|
+
// one try, whatever `attempts` says: what throws has failed, so onError never hears of a retry that won't come
|
|
12
|
+
const once = () => run({ ...job, attempts: 1 });
|
|
13
|
+
const key = job.kind === "delivery" ? job.key : undefined;
|
|
14
|
+
if (!key) {
|
|
15
|
+
await once();
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const mine = (lastOfKey.get(key) ?? Promise.resolve()).then(once);
|
|
19
|
+
const settled = mine.catch(() => { });
|
|
20
|
+
lastOfKey.set(key, settled);
|
|
21
|
+
try {
|
|
22
|
+
await mine;
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
if (lastOfKey.get(key) === settled)
|
|
26
|
+
lastOfKey.delete(key);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
9
29
|
},
|
|
10
30
|
};
|
|
11
31
|
/**
|
package/dist/core/jobs.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ActionCtx, Call, CallJob, CronJob, DatabaseAdapter, Delivery, FailedJobs, Envelope, FailureInfo, Fn, Job, JobsAdapter, JobScope, Jobs, RegisteredCron, RegisteredFn, RegisteredListener } from "./types.ts";
|
|
1
|
+
import type { ActionCtx, AdapterSetup, Call, CallJob, CronJob, DatabaseAdapter, Delivery, FailedJobs, Envelope, FailureInfo, Fn, Job, JobsAdapter, JobScope, Jobs, RegisteredCron, RegisteredFn, RegisteredListener } from "./types.ts";
|
|
2
2
|
export declare const isPlainObject: (v: unknown) => v is Record<string, unknown>;
|
|
3
3
|
/**
|
|
4
4
|
* What makes a scheduled call with `replace: true` replaceable: its function and its input, `<function id>@<input>`. A
|
|
@@ -6,10 +6,11 @@ export declare const isPlainObject: (v: unknown) => v is Record<string, unknown>
|
|
|
6
6
|
*/
|
|
7
7
|
export declare const replaceKeyOf: (job: Job) => string | undefined;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* large input still fits an index and the function
|
|
9
|
+
* The key adapters that keep one waiting job per key (`replaces: true`) store: a delivery's `unique` as it is, a call's
|
|
10
|
+
* `replaceKeyOf` as `<function id>@<sha-256 of the input>`, so a large input still fits an index and the function
|
|
11
|
+
* stays readable. Undefined for every other job.
|
|
11
12
|
*/
|
|
12
|
-
export declare function
|
|
13
|
+
export declare function storedUniqueKey(job: Job): Promise<string | undefined>;
|
|
13
14
|
/**
|
|
14
15
|
* Seconds to wait after a failed try: e^attempt, capped at e^10 (about 6 hours), the curve Graphile Worker uses. The
|
|
15
16
|
* core's, so every adapter that backs off (the jobs table, Cloudflare's redelivery) waits the same.
|
|
@@ -17,6 +18,8 @@ export declare function storedReplaceKey(job: Job): Promise<string | undefined>;
|
|
|
17
18
|
export declare const defaultBackoff: (attempt: number) => number;
|
|
18
19
|
/** Where the docs list each adapter and what it can do. */
|
|
19
20
|
export declare const ADAPTERS = "see Jobs, Adapters in the docs";
|
|
21
|
+
/** What an adapter's `connect` gets as `setup.workers`: does a loop here take this job, and wake them. */
|
|
22
|
+
export declare function workersOf(adapter: JobsAdapter): AdapterSetup["workers"];
|
|
20
23
|
/** One listener's share of an event, as a job: `<listener id>@<event id>`, with the listener's tries. */
|
|
21
24
|
export declare const deliveryOf: (l: RegisteredListener, e: Envelope) => Delivery;
|
|
22
25
|
/** What running jobs needs from the registry: its definitions, and how it runs a function or a listener's handler. */
|
|
@@ -31,6 +34,8 @@ export interface JobsDeps {
|
|
|
31
34
|
/** `jobs.failed`: where failed jobs are kept when the adapter keeps none. */
|
|
32
35
|
failedStore: FailedJobs | undefined;
|
|
33
36
|
maxEventDepth: number;
|
|
37
|
+
/** `jobs.queues`: the queues besides `"default"`. */
|
|
38
|
+
queues: readonly string[];
|
|
34
39
|
now(): Date;
|
|
35
40
|
wrap(call: Call, next: () => Promise<unknown>): Promise<unknown>;
|
|
36
41
|
report(error: unknown, info: FailureInfo): void;
|
|
@@ -39,8 +44,8 @@ export interface JobsDeps {
|
|
|
39
44
|
id: string;
|
|
40
45
|
attempt: number;
|
|
41
46
|
}): Promise<unknown>;
|
|
42
|
-
/** The ctx a listener's or cron's handler gets, as `scope`, in an event chain `eventDepth` deep. */
|
|
43
|
-
handlerCtx(scope: object, eventDepth: number, id: string, queued: Job[] | undefined): ActionCtx<any>;
|
|
47
|
+
/** The ctx a listener's or cron's handler gets, as `scope`, in an event chain `eventDepth` deep. `stop`: its timeout. */
|
|
48
|
+
handlerCtx(scope: object, eventDepth: number, id: string, queued: Job[] | undefined, stop?: AbortSignal): ActionCtx<any>;
|
|
44
49
|
}
|
|
45
50
|
/**
|
|
46
51
|
* Jobs, for one registry: a listener's share of an event, a cron's run, a scheduled call. Turns a commit's events and
|
|
@@ -53,5 +58,6 @@ export declare function makeJobs(deps: JobsDeps): {
|
|
|
53
58
|
due: (at: Date, only?: string) => CronJob[];
|
|
54
59
|
checkHold: (what: string, seconds: number) => void;
|
|
55
60
|
checkRetries: (what: string, attempts: number) => void;
|
|
61
|
+
checkQueue: (what: string, queue: string | undefined) => void;
|
|
56
62
|
yours: string;
|
|
57
63
|
};
|