@plitzi/sdk-actions 0.0.0-stage → 0.39.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/CHANGELOG.md +18 -0
- package/README.md +54 -2
- package/dist/chunks/credentials-DwxZeAvH.mjs +27 -0
- package/dist/chunks/errors-bCBfIe7c.mjs +54 -0
- package/dist/chunks/grants-BsD-WlLE.mjs +168 -0
- package/dist/chunks/load-DBWRZn7G.mjs +48 -0
- package/dist/chunks/module-WdDzbjHy.mjs +7032 -0
- package/dist/chunks/queueDocument-xYz-KZ5V.mjs +21 -0
- package/dist/chunks/queueRules-BCCVgeUE.mjs +487 -0
- package/dist/chunks/remote-DopPgCMK.mjs +404 -0
- package/dist/chunks/storeClock-Btm7EaVm.mjs +31 -0
- package/dist/chunks/taskCatalog-S1IozUTD.mjs +201 -0
- package/dist/connectors/connectors.d.ts +7 -0
- package/dist/connectors/engine.d.ts +47 -0
- package/dist/connectors/getByPath.d.ts +8 -0
- package/dist/connectors/projection.d.ts +18 -0
- package/dist/connectors/resolver.d.ts +27 -0
- package/dist/connectors/types.d.ts +19 -0
- package/dist/engine/configSeam.d.ts +10 -0
- package/dist/engine/connectorWrite.d.ts +12 -0
- package/dist/engine/fileStore.d.ts +60 -0
- package/dist/engine/fleet.d.ts +20 -0
- package/dist/engine/jobs/calls.d.ts +49 -0
- package/dist/engine/jobs/claimSpaces.d.ts +10 -0
- package/dist/engine/jobs/codeMarks.d.ts +10 -0
- package/dist/engine/jobs/endings.d.ts +42 -0
- package/dist/engine/jobs/failureStreak.d.ts +15 -0
- package/dist/engine/jobs/fileQueue.d.ts +34 -0
- package/dist/engine/jobs/flow.d.ts +11 -0
- package/dist/engine/jobs/jobs.d.ts +36 -0
- package/dist/engine/jobs/later.d.ts +27 -0
- package/dist/engine/jobs/memoryNeeds.d.ts +24 -0
- package/dist/engine/jobs/memoryQueue.d.ts +33 -0
- package/dist/engine/jobs/memoryWatch.d.ts +21 -0
- package/dist/engine/jobs/notNow.d.ts +39 -0
- package/dist/engine/jobs/queueDocument.d.ts +15 -0
- package/dist/engine/jobs/queueRules.d.ts +64 -0
- package/dist/engine/jobs/scheduler.d.ts +70 -0
- package/dist/engine/jobs/schedules.d.ts +24 -0
- package/dist/engine/jobs/storeClock.d.ts +16 -0
- package/dist/engine/jobs/wake.d.ts +6 -0
- package/dist/engine/jobs/worker.d.ts +99 -0
- package/dist/engine/module.d.ts +96 -0
- package/dist/engine/moduleFor.d.ts +3 -0
- package/dist/engine/runtime/check.d.ts +24 -0
- package/dist/engine/runtime/credentials.d.ts +8 -0
- package/dist/engine/runtime/deadline.d.ts +19 -0
- package/dist/engine/runtime/email.d.ts +38 -0
- package/dist/engine/runtime/errors.d.ts +28 -0
- package/dist/engine/runtime/fileKv.d.ts +35 -0
- package/dist/engine/runtime/flags.d.ts +16 -0
- package/dist/engine/runtime/guards.d.ts +190 -0
- package/dist/engine/runtime/kvList.d.ts +28 -0
- package/dist/engine/runtime/kvStore.d.ts +36 -0
- package/dist/engine/runtime/limits.d.ts +12 -0
- package/dist/engine/runtime/memoryKv.d.ts +28 -0
- package/dist/engine/runtime/namespaceKv.d.ts +9 -0
- package/dist/engine/runtime/precheck.d.ts +28 -0
- package/dist/engine/runtime/rateLimit.d.ts +9 -0
- package/dist/engine/runtime/redisKv.d.ts +27 -0
- package/dist/engine/runtime/renderFailure.d.ts +24 -0
- package/dist/engine/runtime/renderResolver.d.ts +16 -0
- package/dist/engine/runtime/renderShare.d.ts +12 -0
- package/dist/engine/runtime/report.d.ts +15 -0
- package/dist/engine/runtime/runAction.d.ts +26 -0
- package/dist/engine/runtime/runLogger.d.ts +37 -0
- package/dist/engine/runtime/runStats.d.ts +8 -0
- package/dist/engine/runtime/scope.d.ts +40 -0
- package/dist/engine/runtime/signing.d.ts +27 -0
- package/dist/engine/runtime/taskContext.d.ts +54 -0
- package/dist/engine/runtime/triggers.d.ts +12 -0
- package/dist/engine/taskCatalog.d.ts +28 -0
- package/dist/engine/tasks/auth.d.ts +2 -0
- package/dist/engine/tasks/builtins.d.ts +10 -0
- package/dist/engine/tasks/connector.d.ts +2 -0
- package/dist/engine/tasks/db.d.ts +2 -0
- package/dist/engine/tasks/email.d.ts +2 -0
- package/dist/engine/tasks/flow.d.ts +3 -0
- package/dist/engine/tasks/helpers.d.ts +16 -0
- package/dist/engine/tasks/http.d.ts +2 -0
- package/dist/engine/tasks/kv.d.ts +2 -0
- package/dist/engine/tasks/list.d.ts +2 -0
- package/dist/engine/tasks/names.d.ts +8 -0
- package/dist/engine/tasks/realtime.d.ts +3 -0
- package/dist/engine/tasks/registry.d.ts +19 -0
- package/dist/engine/tasks/transform.d.ts +2 -0
- package/dist/engine/transport/callHandler.d.ts +34 -0
- package/dist/engine/transport/cancelHandler.d.ts +23 -0
- package/dist/engine/transport/catalogHandler.d.ts +22 -0
- package/dist/engine/transport/stream.d.ts +22 -0
- package/dist/engine/transport/verifySignature.d.ts +16 -0
- package/dist/engine/transport/webhookHandler.d.ts +26 -0
- package/dist/engine/types.d.ts +442 -0
- package/dist/functions/build.d.ts +27 -0
- package/dist/functions/capabilities.d.ts +11 -0
- package/dist/functions/config.d.ts +111 -0
- package/dist/functions/context.d.ts +15 -0
- package/dist/functions/index.d.ts +17 -0
- package/dist/functions/index.mjs +461 -0
- package/dist/functions/jobControl.d.ts +22 -0
- package/dist/functions/jobs.d.ts +104 -0
- package/dist/functions/load.d.ts +22 -0
- package/dist/functions/local.d.ts +36 -0
- package/dist/functions/manifest.d.ts +32 -0
- package/dist/functions/native.d.ts +13 -0
- package/dist/functions/oauth.d.ts +33 -0
- package/dist/functions/routes.d.ts +50 -0
- package/dist/functions/runner/remote.d.ts +26 -0
- package/dist/functions/scope.d.ts +28 -0
- package/dist/functions/space.d.ts +41 -0
- package/dist/functions/tryEntry.d.ts +9 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.mjs +545 -0
- package/dist/mongo/claimable.d.ts +5 -0
- package/dist/mongo/index.d.ts +4 -0
- package/dist/mongo/index.mjs +779 -0
- package/dist/mongo/jobQueue.d.ts +38 -0
- package/dist/mongo/kv.d.ts +15 -0
- package/dist/mysql/index.d.ts +5 -0
- package/dist/mysql/index.mjs +808 -0
- package/dist/mysql/jobQueue.d.ts +40 -0
- package/dist/mysql/kv.d.ts +18 -0
- package/dist/mysql/rows.d.ts +64 -0
- package/dist/mysql/schema.d.ts +28 -0
- package/dist/mysql/summary.d.ts +7 -0
- package/dist/realtime/declarations.d.ts +11 -0
- package/dist/realtime/failure.d.ts +6 -0
- package/dist/realtime/grants.d.ts +22 -0
- package/dist/realtime/handlers.d.ts +86 -0
- package/dist/realtime/hub.d.ts +71 -0
- package/dist/realtime/index.d.ts +105 -0
- package/dist/realtime/index.mjs +824 -0
- package/dist/realtime/jobs.d.ts +18 -0
- package/dist/realtime/message.d.ts +3 -0
- package/dist/realtime/presence.d.ts +33 -0
- package/dist/realtime/retained.d.ts +21 -0
- package/dist/realtime/socket.d.ts +30 -0
- package/dist/render/index.d.ts +10 -0
- package/dist/render/index.mjs +1 -0
- package/dist/render/protocol.d.ts +47 -0
- package/dist/render/remote.d.ts +16 -0
- package/dist/runtimes/bundle.d.ts +29 -0
- package/dist/runtimes/index.d.ts +3 -0
- package/dist/runtimes/index.mjs +229 -0
- package/dist/runtimes/stages.d.ts +52 -0
- package/dist/sqlite/index.d.ts +4 -0
- package/dist/sqlite/index.mjs +160 -0
- package/dist/sqlite/jobQueue.d.ts +35 -0
- package/dist/sqlite/kv.d.ts +34 -0
- package/dist/sqlite/open.d.ts +3 -0
- package/dist/types/config.d.ts +293 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/jobs.d.ts +317 -0
- package/dist/worker/elsewhere.d.ts +16 -0
- package/dist/worker/index.d.ts +5 -0
- package/dist/worker/index.mjs +1703 -0
- package/dist/worker/origin.d.ts +12 -0
- package/dist/worker/pageEngine.d.ts +11 -0
- package/dist/worker/stages/action.d.ts +24 -0
- package/dist/worker/stages/cors.d.ts +9 -0
- package/dist/worker/stages/forward.d.ts +12 -0
- package/dist/worker/stages/functionRoutes.d.ts +10 -0
- package/dist/worker/stages/queuedCalls.d.ts +15 -0
- package/dist/worker/stages/realtime.d.ts +9 -0
- package/dist/worker/stages/render.d.ts +9 -0
- package/dist/worker/worker.d.ts +39 -0
- package/package.json +76 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { l as scheduleKey } from "./queueRules-BCCVgeUE.mjs";
|
|
2
|
+
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
3
|
+
// src/engine/jobs/queueDocument.ts
|
|
4
|
+
var documentOf = ({ jobs, schedules }) => ({
|
|
5
|
+
format: 1,
|
|
6
|
+
jobs: [...jobs.values()],
|
|
7
|
+
schedules: [...schedules.values()]
|
|
8
|
+
});
|
|
9
|
+
var isJob = (value) => isRecord(value) && typeof value.id === "string" && typeof value.spaceId === "number" && typeof value.status === "string" && typeof value.runAt === "number" && isRecord(value.target) && Array.isArray(value.history);
|
|
10
|
+
var isSchedule = (value) => isRecord(value) && typeof value.spaceId === "number" && typeof value.actionId === "string" && typeof value.nextRunAt === "number";
|
|
11
|
+
/** The state a document holds — or why it is no queue's, for the store to say with its fix. */
|
|
12
|
+
var stateOf = (parsed) => {
|
|
13
|
+
if (!isRecord(parsed) || parsed.format !== 1) return "no `format: 1`";
|
|
14
|
+
if (!Array.isArray(parsed.jobs) || !parsed.jobs.every(isJob)) return "its `jobs` are not jobs";
|
|
15
|
+
if (!Array.isArray(parsed.schedules) || !parsed.schedules.every(isSchedule)) return "its `schedules` are not schedules";
|
|
16
|
+
return {
|
|
17
|
+
jobs: new Map(parsed.jobs.map((job) => [job.id, job])),
|
|
18
|
+
schedules: new Map(parsed.schedules.map((schedule) => [scheduleKey(schedule.spaceId, schedule.actionId), schedule]))
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export { stateOf as n, documentOf as t };
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { isTerminalJobStatus } from "@plitzi/sdk-shared/actions";
|
|
2
|
+
// src/engine/jobs/flow.ts
|
|
3
|
+
var MINUTE = 6e4;
|
|
4
|
+
var p95 = (values) => {
|
|
5
|
+
if (values.length === 0) return;
|
|
6
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
7
|
+
return sorted[Math.min(sorted.length - 1, Math.ceil(sorted.length * .95) - 1)];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The queue's flow minute by minute over `[from, to)`, from the jobs that touch it: what fell due in each, what was
|
|
11
|
+
* started and ended, and how long a job waited from due to its first attempt — what tells a queue that keeps up from
|
|
12
|
+
* one that falls behind. A job running now started its attempt when it was claimed (`updatedAt`). Every store reads
|
|
13
|
+
* the jobs it may have to count — due in the span, or written since it began — and hands them here.
|
|
14
|
+
*/
|
|
15
|
+
var flowOf = (jobs, from, to) => {
|
|
16
|
+
const first = Math.floor(from / MINUTE);
|
|
17
|
+
const last = Math.ceil(to / MINUTE);
|
|
18
|
+
const minutes = Array.from({ length: Math.max(0, last - first) }, (_item, index) => ({
|
|
19
|
+
minute: first + index,
|
|
20
|
+
due: 0,
|
|
21
|
+
started: 0,
|
|
22
|
+
ended: 0,
|
|
23
|
+
waits: []
|
|
24
|
+
}));
|
|
25
|
+
const at = (instant) => instant >= from && instant < to ? minutes[Math.floor(instant / MINUTE) - first] : void 0;
|
|
26
|
+
for (const job of jobs) {
|
|
27
|
+
const dueIn = at(job.dueAt);
|
|
28
|
+
if (dueIn) dueIn.due += 1;
|
|
29
|
+
const starts = [...job.history.map((attempt) => ({
|
|
30
|
+
attempt: attempt.attempt,
|
|
31
|
+
startedAt: attempt.startedAt
|
|
32
|
+
})), ...job.status === "running" ? [{
|
|
33
|
+
attempt: job.attempts,
|
|
34
|
+
startedAt: job.updatedAt
|
|
35
|
+
}] : []];
|
|
36
|
+
for (const { attempt, startedAt } of starts) {
|
|
37
|
+
const startedIn = at(startedAt);
|
|
38
|
+
if (startedIn) {
|
|
39
|
+
startedIn.started += 1;
|
|
40
|
+
if (attempt === 1) startedIn.waits.push(Math.max(0, startedAt - job.dueAt));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const attempt of job.history) {
|
|
44
|
+
const endedIn = at(attempt.endedAt);
|
|
45
|
+
if (endedIn) endedIn.ended += 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return minutes.map(({ waits, ...counts }) => {
|
|
49
|
+
const waitP95Ms = p95(waits);
|
|
50
|
+
return waitP95Ms === void 0 ? counts : {
|
|
51
|
+
...counts,
|
|
52
|
+
waitP95Ms
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
// src/engine/jobs/memoryNeeds.ts
|
|
57
|
+
/**
|
|
58
|
+
* What a job needs once the worker running it died, in the words every store writes: the in-process ones through
|
|
59
|
+
* `queueRules`, Mongo and MySQL in their own queries — each composing the same sentences from these parts.
|
|
60
|
+
*
|
|
61
|
+
* A worker dies for many reasons — a node lost, a deploy past its grace — and the job is simply run again. But one that
|
|
62
|
+
* died near its memory (it said so: `heartbeat`'s `nearMemoryLimit`), or a second in a row to die running the same job,
|
|
63
|
+
* was taken down by the job: run again on the same size, it ends that one too, and the next. That job needs more than
|
|
64
|
+
* the worker could hold, when the claims say what their workers may (`JobClaimMemory`).
|
|
65
|
+
*/
|
|
66
|
+
/** What the history says of an attempt nobody reported. */
|
|
67
|
+
var LOST_ERROR = "the worker holding this job stopped reporting";
|
|
68
|
+
/** {@link lostError}'s words around what the worker could hold. */
|
|
69
|
+
var LOST_MEMORY_WORDS = [`${LOST_ERROR} near its memory — it could hold `, " MiB, so the job runs next on a worker that may hold more"];
|
|
70
|
+
/** {@link setAsideError}'s words around what the job needs more than, and what the largest worker may hold. */
|
|
71
|
+
var SET_ASIDE_WORDS = [
|
|
72
|
+
"Set aside: a worker died running it — it needs more than ",
|
|
73
|
+
" MiB, and the largest worker here may hold ",
|
|
74
|
+
" MiB. Give a worker more memory, then resume it."
|
|
75
|
+
];
|
|
76
|
+
/** Whether the job took its worker down: it neared its memory holding it, or the attempt before was lost too. */
|
|
77
|
+
var outgrewWorker = (job) => job.nearMemoryLimit === true || job.history.at(-1)?.status === "lost";
|
|
78
|
+
/** The least a job needs after the worker holding it died: more than that one could hold when the job took it down. */
|
|
79
|
+
var needsAfterLoss = (needsMb, holderMb, outgrew) => holderMb === void 0 || !outgrew ? needsMb : Math.max(needsMb ?? 0, holderMb + 1);
|
|
80
|
+
/** Why a job waits after its worker died: for a larger one, when it took that worker down and it said its size. */
|
|
81
|
+
var lostError = (holderMb, outgrew) => holderMb === void 0 || !outgrew ? LOST_ERROR : `${LOST_MEMORY_WORDS[0]}${String(holderMb)}${LOST_MEMORY_WORDS[1]}`;
|
|
82
|
+
/** Why a job is set aside: no worker of the deployment may hold what it needs. */
|
|
83
|
+
var setAsideError = (needsMb, ceilingMb) => `${SET_ASIDE_WORDS[0]}${String(needsMb - 1)}${SET_ASIDE_WORDS[1]}${String(ceilingMb)}${SET_ASIDE_WORDS[2]}`;
|
|
84
|
+
// src/engine/jobs/queueRules.ts
|
|
85
|
+
/** The kind of work a job is (`JobTake`): what a claim's `takes` picks by. */
|
|
86
|
+
var takeOf = (target) => target.kind === "function" ? "functionJobs" : target.code ? "codeActions" : "light";
|
|
87
|
+
/** Whether a job of `spaceId` is one a claim with `spaces` takes. */
|
|
88
|
+
var takesSpace = (spaces, spaceId) => !spaces || ("only" in spaces ? spaces.only.includes(spaceId) : !spaces.except.includes(spaceId));
|
|
89
|
+
/**
|
|
90
|
+
* Whether a claim takes a job: one of its spaces — or, for a job a worker died running, one this worker may hold,
|
|
91
|
+
* whatever spaces it is kept for: the larger workers are few, and the job's own space's may all be the size that died.
|
|
92
|
+
*/
|
|
93
|
+
var fits = (job, spaces, memory) => job.needsMemoryMb === void 0 ? takesSpace(spaces, job.spaceId) : memory !== void 0 && memory.takesNeeding !== false && memory.mb >= job.needsMemoryMb;
|
|
94
|
+
/**
|
|
95
|
+
* Whether a call changed the state, by its method and what it answered — what a store keeping the state elsewhere writes
|
|
96
|
+
* back after. A worker claims every second: a claim that took nothing, a refused `write` or `cancel` change nothing, and
|
|
97
|
+
* are not written.
|
|
98
|
+
*/
|
|
99
|
+
var changedBy = (method, answer) => {
|
|
100
|
+
switch (method) {
|
|
101
|
+
case "claim": return Array.isArray(answer) && answer.length > 0;
|
|
102
|
+
case "enqueue":
|
|
103
|
+
case "write":
|
|
104
|
+
case "advanceSchedule":
|
|
105
|
+
case "requeue":
|
|
106
|
+
case "cancel":
|
|
107
|
+
case "pause":
|
|
108
|
+
case "resume": return answer === true;
|
|
109
|
+
case "cancelPending": return typeof answer === "number" && answer > 0;
|
|
110
|
+
case "heartbeat":
|
|
111
|
+
case "settle":
|
|
112
|
+
case "putSchedules": return true;
|
|
113
|
+
default: return false;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/** What a schedule is kept by: its space and its action. */
|
|
117
|
+
var scheduleKey = (spaceId, actionId) => `${String(spaceId)}:${actionId}`;
|
|
118
|
+
var queueRules = ({ jobs, schedules }, { retainFinished, clock, onUnansweredChange }) => {
|
|
119
|
+
const key = scheduleKey;
|
|
120
|
+
const now = () => clock();
|
|
121
|
+
/** Oldest finished jobs first, so the bound drops history rather than anything anybody is waiting on. */
|
|
122
|
+
const evict = () => {
|
|
123
|
+
const finished = [...jobs.values()].filter((job) => isTerminalJobStatus(job.status)).sort((a, b) => a.updatedAt - b.updatedAt);
|
|
124
|
+
for (const job of finished.slice(0, finished.length - retainFinished)) jobs.delete(job.id);
|
|
125
|
+
};
|
|
126
|
+
const owned = (spaceIds, jobId) => {
|
|
127
|
+
const job = jobs.get(jobId);
|
|
128
|
+
return job && spaceIds.includes(job.spaceId) ? job : void 0;
|
|
129
|
+
};
|
|
130
|
+
/** Numbered by the history itself, not by `attempts`: an operator who repeats a finished job starts its attempt
|
|
131
|
+
* count again, and the record of what has already been tried must not start again with it. */
|
|
132
|
+
const appendAttempt = (job, attempt) => {
|
|
133
|
+
job.history = [...job.history, {
|
|
134
|
+
...attempt,
|
|
135
|
+
attempt: job.history.length + 1
|
|
136
|
+
}];
|
|
137
|
+
};
|
|
138
|
+
return {
|
|
139
|
+
now: () => new Date(now()),
|
|
140
|
+
enqueue: (job) => {
|
|
141
|
+
if (jobs.has(job.id)) return false;
|
|
142
|
+
const at = now();
|
|
143
|
+
jobs.set(job.id, {
|
|
144
|
+
...job,
|
|
145
|
+
status: "pending",
|
|
146
|
+
runAt: job.dueAt,
|
|
147
|
+
attempts: 0,
|
|
148
|
+
createdAt: at,
|
|
149
|
+
updatedAt: at,
|
|
150
|
+
history: []
|
|
151
|
+
});
|
|
152
|
+
evict();
|
|
153
|
+
return true;
|
|
154
|
+
},
|
|
155
|
+
claim: ({ workerId, leaseMs, limit, spaces, memory, takes }) => {
|
|
156
|
+
const at = now();
|
|
157
|
+
for (const job of jobs.values()) {
|
|
158
|
+
if (job.status === "running" && (job.leaseUntil ?? 0) < at) {
|
|
159
|
+
const holderMb = job.holderMemoryMb;
|
|
160
|
+
const outgrew = outgrewWorker(job);
|
|
161
|
+
appendAttempt(job, {
|
|
162
|
+
status: "lost",
|
|
163
|
+
startedAt: job.updatedAt,
|
|
164
|
+
endedAt: at,
|
|
165
|
+
workerId: job.workerId ?? "unknown",
|
|
166
|
+
error: LOST_ERROR,
|
|
167
|
+
...holderMb === void 0 ? {} : { memoryMb: holderMb },
|
|
168
|
+
...job.nearMemoryLimit ? { nearMemoryLimit: job.nearMemoryLimit } : {}
|
|
169
|
+
});
|
|
170
|
+
job.status = "pending";
|
|
171
|
+
job.workerId = void 0;
|
|
172
|
+
job.leaseUntil = void 0;
|
|
173
|
+
job.holderMemoryMb = void 0;
|
|
174
|
+
job.nearMemoryLimit = void 0;
|
|
175
|
+
job.needsMemoryMb = needsAfterLoss(job.needsMemoryMb, holderMb, outgrew);
|
|
176
|
+
job.error = lostError(holderMb, outgrew);
|
|
177
|
+
job.updatedAt = at;
|
|
178
|
+
onUnansweredChange?.();
|
|
179
|
+
}
|
|
180
|
+
if (memory && job.status === "pending" && (job.needsMemoryMb ?? 0) > memory.ceilingMb) {
|
|
181
|
+
job.status = "paused";
|
|
182
|
+
job.error = setAsideError(job.needsMemoryMb ?? 0, memory.ceilingMb);
|
|
183
|
+
job.updatedAt = at;
|
|
184
|
+
onUnansweredChange?.();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const taken = [];
|
|
188
|
+
const candidates = [...jobs.values()].filter((job) => job.status === "pending" && job.runAt <= at && fits(job, spaces, memory) && (!takes || takeOf(job.target) === takes)).sort((a, b) => a.runAt - b.runAt);
|
|
189
|
+
for (const job of candidates.slice(0, limit)) {
|
|
190
|
+
job.status = "running";
|
|
191
|
+
job.attempts += 1;
|
|
192
|
+
job.workerId = workerId;
|
|
193
|
+
job.leaseUntil = at + leaseMs;
|
|
194
|
+
job.holderMemoryMb = memory?.mb;
|
|
195
|
+
job.nearMemoryLimit = void 0;
|
|
196
|
+
job.updatedAt = at;
|
|
197
|
+
taken.push({ ...job });
|
|
198
|
+
}
|
|
199
|
+
return taken;
|
|
200
|
+
},
|
|
201
|
+
heartbeat: ({ jobIds, workerId, leaseMs, nearMemoryLimit }) => {
|
|
202
|
+
const at = now();
|
|
203
|
+
const stops = [];
|
|
204
|
+
for (const id of jobIds) {
|
|
205
|
+
const job = jobs.get(id);
|
|
206
|
+
if (!job || job.workerId !== workerId || job.status !== "running") continue;
|
|
207
|
+
job.leaseUntil = at + leaseMs;
|
|
208
|
+
if (nearMemoryLimit) job.nearMemoryLimit = true;
|
|
209
|
+
if (job.cancelRequested) stops.push({
|
|
210
|
+
jobId: id,
|
|
211
|
+
stop: "cancel"
|
|
212
|
+
});
|
|
213
|
+
else if (job.pauseRequested) stops.push({
|
|
214
|
+
jobId: id,
|
|
215
|
+
stop: "pause"
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return stops;
|
|
219
|
+
},
|
|
220
|
+
write: ({ jobId, workerId, state, progress }) => {
|
|
221
|
+
const job = jobs.get(jobId);
|
|
222
|
+
if (!job || job.workerId !== workerId || job.status !== "running") return false;
|
|
223
|
+
const at = now();
|
|
224
|
+
if (state !== void 0) job.state = state;
|
|
225
|
+
if (progress) job.progress = {
|
|
226
|
+
...progress,
|
|
227
|
+
at
|
|
228
|
+
};
|
|
229
|
+
job.updatedAt = at;
|
|
230
|
+
return true;
|
|
231
|
+
},
|
|
232
|
+
settle: ({ jobId, workerId, status, delayMs, runId, error, attempt, result, spentMs, refund = status === "pending" && !attempt }) => {
|
|
233
|
+
const job = jobs.get(jobId);
|
|
234
|
+
if (!job || job.workerId !== workerId) return;
|
|
235
|
+
const at = now();
|
|
236
|
+
if (attempt) appendAttempt(job, {
|
|
237
|
+
...attempt,
|
|
238
|
+
startedAt: job.updatedAt,
|
|
239
|
+
endedAt: at
|
|
240
|
+
});
|
|
241
|
+
job.status = status === "pending" && job.cancelRequested ? "cancelled" : status === "pending" && job.pauseRequested ? "paused" : status;
|
|
242
|
+
job.updatedAt = at;
|
|
243
|
+
job.leaseUntil = void 0;
|
|
244
|
+
job.error = error;
|
|
245
|
+
job.cancelRequested = void 0;
|
|
246
|
+
job.pauseRequested = void 0;
|
|
247
|
+
if (runId) job.runId = runId;
|
|
248
|
+
if (result !== void 0) job.result = result;
|
|
249
|
+
if (spentMs) job.activeMs = (job.activeMs ?? 0) + spentMs;
|
|
250
|
+
if (refund) job.attempts = Math.max(0, job.attempts - 1);
|
|
251
|
+
if (job.status === "pending") job.runAt = at + (delayMs ?? 0);
|
|
252
|
+
if (job.status === "pending" || job.status === "paused") job.workerId = void 0;
|
|
253
|
+
evict();
|
|
254
|
+
},
|
|
255
|
+
dueSchedules: (limit) => {
|
|
256
|
+
const at = now();
|
|
257
|
+
return [...schedules.values()].filter((schedule) => schedule.enabled && schedule.nextRunAt <= at).sort((a, b) => a.nextRunAt - b.nextRunAt).slice(0, limit).map((schedule) => ({ ...schedule }));
|
|
258
|
+
},
|
|
259
|
+
advanceSchedule: ({ spaceId, actionId, from, to, missed }) => {
|
|
260
|
+
const schedule = schedules.get(key(spaceId, actionId));
|
|
261
|
+
if (!schedule || schedule.nextRunAt !== from) return false;
|
|
262
|
+
schedule.nextRunAt = to;
|
|
263
|
+
schedule.lastFireAt = from;
|
|
264
|
+
schedule.missed += missed;
|
|
265
|
+
schedule.updatedAt = now();
|
|
266
|
+
return true;
|
|
267
|
+
},
|
|
268
|
+
putSchedules: ({ spaceId, schedules: incoming }) => {
|
|
269
|
+
const at = now();
|
|
270
|
+
const keep = new Set(incoming.map((schedule) => key(spaceId, schedule.actionId)));
|
|
271
|
+
for (const existing of schedules.keys()) if (existing.startsWith(`${spaceId}:`) && !keep.has(existing)) schedules.delete(existing);
|
|
272
|
+
for (const incomingSchedule of incoming) {
|
|
273
|
+
const id = key(spaceId, incomingSchedule.actionId);
|
|
274
|
+
const existing = schedules.get(id);
|
|
275
|
+
const unchanged = existing && existing.cron === incomingSchedule.cron && existing.timezone === incomingSchedule.timezone && existing.environment === incomingSchedule.environment;
|
|
276
|
+
schedules.set(id, {
|
|
277
|
+
...incomingSchedule,
|
|
278
|
+
missed: existing?.missed ?? 0,
|
|
279
|
+
...existing?.lastFireAt === void 0 ? {} : { lastFireAt: existing.lastFireAt },
|
|
280
|
+
nextRunAt: unchanged ? existing.nextRunAt : incomingSchedule.nextRunAt,
|
|
281
|
+
updatedAt: at
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
listJobs: ({ spaceIds, name, status, kind, limit, offset }) => {
|
|
286
|
+
const matching = [...jobs.values()].filter((job) => spaceIds.includes(job.spaceId) && (name === void 0 || job.target.name === name) && (status === void 0 || job.status === status) && (kind === void 0 || job.target.kind === kind)).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
287
|
+
return {
|
|
288
|
+
jobs: matching.slice(offset, offset + limit).map((job) => ({ ...job })),
|
|
289
|
+
total: matching.length
|
|
290
|
+
};
|
|
291
|
+
},
|
|
292
|
+
listAcross: ({ statuses, limit, order }) => [...jobs.values()].filter((job) => statuses.includes(job.status)).sort((a, b) => order === "due" ? a.runAt - b.runAt : b.updatedAt - a.updatedAt).slice(0, limit).map((job) => ({ ...job })),
|
|
293
|
+
summarize: ({ spaces = 10 } = {}) => summarizeJobs([...jobs.values()], spaces),
|
|
294
|
+
countDue: ({ spaces, memoryMb } = {}) => {
|
|
295
|
+
const at = now();
|
|
296
|
+
const memory = memoryMb === void 0 ? void 0 : {
|
|
297
|
+
mb: memoryMb,
|
|
298
|
+
ceilingMb: memoryMb
|
|
299
|
+
};
|
|
300
|
+
return [...jobs.values()].filter((job) => job.status === "pending" && job.runAt <= at && fits(job, spaces, memory)).length;
|
|
301
|
+
},
|
|
302
|
+
flow: ({ from, to }) => flowOf(jobs.values(), from, to),
|
|
303
|
+
getJob: (spaceIds, jobId) => owned(spaceIds, jobId),
|
|
304
|
+
listSchedules: (spaceIds) => [...schedules.values()].filter((schedule) => spaceIds.includes(schedule.spaceId)),
|
|
305
|
+
requeue: (spaceIds, jobId, { input, fresh = false } = {}) => {
|
|
306
|
+
const job = owned(spaceIds, jobId);
|
|
307
|
+
if (!job || !isTerminalJobStatus(job.status)) return false;
|
|
308
|
+
job.status = "pending";
|
|
309
|
+
job.runAt = now();
|
|
310
|
+
job.attempts = 0;
|
|
311
|
+
job.workerId = void 0;
|
|
312
|
+
job.leaseUntil = void 0;
|
|
313
|
+
job.cancelRequested = void 0;
|
|
314
|
+
job.pauseRequested = void 0;
|
|
315
|
+
job.error = void 0;
|
|
316
|
+
job.result = void 0;
|
|
317
|
+
if (input) job.input = input;
|
|
318
|
+
if (fresh) {
|
|
319
|
+
job.state = void 0;
|
|
320
|
+
job.progress = void 0;
|
|
321
|
+
job.activeMs = void 0;
|
|
322
|
+
}
|
|
323
|
+
job.updatedAt = now();
|
|
324
|
+
return true;
|
|
325
|
+
},
|
|
326
|
+
cancel: (spaceIds, jobId) => {
|
|
327
|
+
const job = owned(spaceIds, jobId);
|
|
328
|
+
if (!job || isTerminalJobStatus(job.status)) return false;
|
|
329
|
+
if (job.status === "pending" || job.status === "paused") {
|
|
330
|
+
job.status = "cancelled";
|
|
331
|
+
job.workerId = void 0;
|
|
332
|
+
job.pauseRequested = void 0;
|
|
333
|
+
} else job.cancelRequested = true;
|
|
334
|
+
job.updatedAt = now();
|
|
335
|
+
return true;
|
|
336
|
+
},
|
|
337
|
+
pause: (spaceIds, jobId) => {
|
|
338
|
+
const job = owned(spaceIds, jobId);
|
|
339
|
+
if (job?.target.kind !== "function" || job.status !== "pending" && job.status !== "running" || job.pauseRequested) return false;
|
|
340
|
+
if (job.status === "pending") job.status = "paused";
|
|
341
|
+
else job.pauseRequested = true;
|
|
342
|
+
job.updatedAt = now();
|
|
343
|
+
return true;
|
|
344
|
+
},
|
|
345
|
+
resume: (spaceIds, jobId) => {
|
|
346
|
+
const job = owned(spaceIds, jobId);
|
|
347
|
+
if (job?.status !== "paused") return false;
|
|
348
|
+
job.status = "pending";
|
|
349
|
+
job.runAt = now();
|
|
350
|
+
job.updatedAt = now();
|
|
351
|
+
if (job.attempts >= job.maxAttempts) job.attempts = 0;
|
|
352
|
+
return true;
|
|
353
|
+
},
|
|
354
|
+
cancelPending: ({ spaceId, key, olderThan }) => {
|
|
355
|
+
const newest = olderThan === void 0 ? void 0 : jobs.get(olderThan);
|
|
356
|
+
const isOlder = (job) => newest === void 0 || job.createdAt < newest.createdAt || job.createdAt === newest.createdAt && job.id < newest.id;
|
|
357
|
+
const at = now();
|
|
358
|
+
let dropped = 0;
|
|
359
|
+
for (const job of jobs.values()) if (job.spaceId === spaceId && job.key === key && job.status === "pending" && job.id !== olderThan && isOlder(job)) {
|
|
360
|
+
job.status = "cancelled";
|
|
361
|
+
job.updatedAt = at;
|
|
362
|
+
dropped += 1;
|
|
363
|
+
}
|
|
364
|
+
return dropped;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
/** A state with nothing in it. */
|
|
369
|
+
/** Which of {@link WaitingJobs} a job is, by its target's kind and trigger — as a store's own query reads them. */
|
|
370
|
+
var waitingKindOf = (kind, trigger) => {
|
|
371
|
+
if (kind === "function") return "functionJobs";
|
|
372
|
+
switch (trigger) {
|
|
373
|
+
case "call": return "calls";
|
|
374
|
+
case "schedule": return "schedules";
|
|
375
|
+
case "later": return "later";
|
|
376
|
+
default: return "other";
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
/** Every status at zero: what a summary counts up from, so a status nothing is in still says 0. */
|
|
380
|
+
var noJobsByStatus = () => ({
|
|
381
|
+
pending: 0,
|
|
382
|
+
running: 0,
|
|
383
|
+
paused: 0,
|
|
384
|
+
succeeded: 0,
|
|
385
|
+
failed: 0,
|
|
386
|
+
dead: 0,
|
|
387
|
+
cancelled: 0
|
|
388
|
+
});
|
|
389
|
+
/**
|
|
390
|
+
* The status a job waits for the queue in. One set aside (`paused`) waits for a person and is said apart
|
|
391
|
+
* (`byStatus.paused`): counted as waiting, 32 jobs set aside at their limits read as a queue falling behind for
|
|
392
|
+
* minutes, the oldest of them its wait, with nothing left to run.
|
|
393
|
+
*/
|
|
394
|
+
var WAITING_STATUSES = ["pending"];
|
|
395
|
+
/** The statuses a job a worker died running is in while it needs a larger worker: waiting for one, or set aside. */
|
|
396
|
+
var NEEDING_STATUSES = ["pending", "paused"];
|
|
397
|
+
/**
|
|
398
|
+
* The queue at a glance, from its jobs: what `summarize` answers for a store that holds them all in hand — the database
|
|
399
|
+
* ones count the same in their own query.
|
|
400
|
+
*/
|
|
401
|
+
var summarizeJobs = (all, spaces) => {
|
|
402
|
+
const byStatus = noJobsByStatus();
|
|
403
|
+
const waiting = {
|
|
404
|
+
calls: 0,
|
|
405
|
+
schedules: 0,
|
|
406
|
+
later: 0,
|
|
407
|
+
functionJobs: 0,
|
|
408
|
+
other: 0
|
|
409
|
+
};
|
|
410
|
+
const bySpace = /* @__PURE__ */ new Map();
|
|
411
|
+
let oldestDueAt;
|
|
412
|
+
const needingMemory = {
|
|
413
|
+
waiting: 0,
|
|
414
|
+
paused: 0
|
|
415
|
+
};
|
|
416
|
+
all.forEach((job) => {
|
|
417
|
+
byStatus[job.status] += 1;
|
|
418
|
+
if (job.needsMemoryMb !== void 0 && NEEDING_STATUSES.includes(job.status)) {
|
|
419
|
+
if (job.status === "paused") needingMemory.paused += 1;
|
|
420
|
+
else {
|
|
421
|
+
needingMemory.waiting += 1;
|
|
422
|
+
needingMemory.mostMb = Math.max(needingMemory.mostMb ?? 0, job.needsMemoryMb);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (!WAITING_STATUSES.includes(job.status)) return;
|
|
426
|
+
const kind = waitingKindOf(job.target.kind, job.target.kind === "action" ? job.target.trigger : void 0);
|
|
427
|
+
waiting[kind] += 1;
|
|
428
|
+
oldestDueAt = oldestDueAt === void 0 ? job.dueAt : Math.min(oldestDueAt, job.dueAt);
|
|
429
|
+
const space = bySpace.get(job.spaceId) ?? {
|
|
430
|
+
spaceId: job.spaceId,
|
|
431
|
+
waiting: 0,
|
|
432
|
+
calls: 0,
|
|
433
|
+
oldestDueAt: job.dueAt,
|
|
434
|
+
lastAt: Number.NEGATIVE_INFINITY
|
|
435
|
+
};
|
|
436
|
+
space.waiting += 1;
|
|
437
|
+
space.calls += kind === "calls" ? 1 : 0;
|
|
438
|
+
space.oldestDueAt = Math.min(space.oldestDueAt, job.dueAt);
|
|
439
|
+
if (job.updatedAt >= space.lastAt) {
|
|
440
|
+
space.lastAt = job.updatedAt;
|
|
441
|
+
if (job.error) space.lastError = job.error;
|
|
442
|
+
else delete space.lastError;
|
|
443
|
+
}
|
|
444
|
+
bySpace.set(job.spaceId, space);
|
|
445
|
+
});
|
|
446
|
+
return {
|
|
447
|
+
byStatus,
|
|
448
|
+
waiting,
|
|
449
|
+
...oldestDueAt === void 0 ? {} : { oldestDueAt },
|
|
450
|
+
needingMemory,
|
|
451
|
+
spaces: [...bySpace.values()].sort((a, b) => b.waiting - a.waiting || a.oldestDueAt - b.oldestDueAt).slice(0, spaces).map(({ lastAt: _lastAt, ...space }) => space)
|
|
452
|
+
};
|
|
453
|
+
};
|
|
454
|
+
var emptyState = () => ({
|
|
455
|
+
jobs: /* @__PURE__ */ new Map(),
|
|
456
|
+
schedules: /* @__PURE__ */ new Map()
|
|
457
|
+
});
|
|
458
|
+
/** One method of the rules, called with the arguments its contract types. */
|
|
459
|
+
var applyRule = (rules, method, args) => {
|
|
460
|
+
const rule = rules[method];
|
|
461
|
+
return rule(...args);
|
|
462
|
+
};
|
|
463
|
+
/** The contract, each method run through `access`: what every store answers with. */
|
|
464
|
+
var queueOver = (access) => ({
|
|
465
|
+
now: (...args) => access("now", args),
|
|
466
|
+
enqueue: (...args) => access("enqueue", args),
|
|
467
|
+
claim: (...args) => access("claim", args),
|
|
468
|
+
heartbeat: (...args) => access("heartbeat", args),
|
|
469
|
+
settle: (...args) => access("settle", args),
|
|
470
|
+
write: (...args) => access("write", args),
|
|
471
|
+
dueSchedules: (...args) => access("dueSchedules", args),
|
|
472
|
+
advanceSchedule: (...args) => access("advanceSchedule", args),
|
|
473
|
+
putSchedules: (...args) => access("putSchedules", args),
|
|
474
|
+
listJobs: (...args) => access("listJobs", args),
|
|
475
|
+
listAcross: (...args) => access("listAcross", args),
|
|
476
|
+
summarize: (...args) => access("summarize", args),
|
|
477
|
+
countDue: (...args) => access("countDue", args),
|
|
478
|
+
flow: (...args) => access("flow", args),
|
|
479
|
+
getJob: (...args) => access("getJob", args),
|
|
480
|
+
listSchedules: (...args) => access("listSchedules", args),
|
|
481
|
+
requeue: (...args) => access("requeue", args),
|
|
482
|
+
cancel: (...args) => access("cancel", args),
|
|
483
|
+
pause: (...args) => access("pause", args),
|
|
484
|
+
resume: (...args) => access("resume", args),
|
|
485
|
+
cancelPending: (...args) => access("cancelPending", args)
|
|
486
|
+
});
|
|
487
|
+
export { flowOf as _, emptyState as a, queueRules as c, LOST_ERROR as d, LOST_MEMORY_WORDS as f, outgrewWorker as g, needsAfterLoss as h, changedBy as i, scheduleKey as l, lostError as m, WAITING_STATUSES as n, noJobsByStatus as o, SET_ASIDE_WORDS as p, applyRule as r, queueOver as s, NEEDING_STATUSES as t, waitingKindOf as u };
|