@plitzi/sdk-server 0.38.24 → 0.39.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/CHANGELOG.md +152 -1
- package/README.md +55 -9
- package/dist/actions.d.ts +4 -1
- package/dist/actions.js +3 -2
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js +5 -4
- package/dist/core/auth/api/accounts.d.ts +11 -0
- package/dist/core/auth/api/accounts.js +175 -0
- package/dist/core/auth/api/admin.d.ts +41 -0
- package/dist/core/auth/api/admin.js +146 -0
- package/dist/core/auth/api/context.d.ts +119 -0
- package/dist/core/auth/api/context.js +262 -0
- package/dist/core/auth/api/factors.d.ts +31 -0
- package/dist/core/auth/api/factors.js +209 -0
- package/dist/core/auth/api/index.d.ts +94 -0
- package/dist/core/auth/api/index.js +43 -0
- package/dist/core/auth/api/profile.d.ts +43 -0
- package/dist/core/auth/api/profile.js +243 -0
- package/dist/core/auth/api/session.d.ts +62 -0
- package/dist/core/auth/api/session.js +312 -0
- package/dist/core/auth/{api.d.ts → api/types.d.ts} +41 -195
- package/dist/core/auth/api/values.d.ts +67 -0
- package/dist/core/auth/api/values.js +90 -0
- package/dist/core/auth/createAuth.d.ts +49 -39
- package/dist/core/auth/createAuth.js +21 -1
- package/dist/core/auth/oauth/client.d.ts +2 -2
- package/dist/core/auth/oauth/client.js +6 -1
- package/dist/core/auth/oauth/index.d.ts +30 -7
- package/dist/core/auth/oauth/index.js +33 -14
- package/dist/core/auth/oauth/providers/github.js +1 -1
- package/dist/core/auth/oauth/providers/google.js +1 -1
- package/dist/core/auth/oauth/providers/oidc.d.ts +40 -0
- package/dist/core/auth/oauth/providers/oidc.js +197 -0
- package/dist/core/auth/oauth/state.d.ts +2 -0
- package/dist/core/auth/oauth/state.js +2 -0
- package/dist/core/auth/oauth/types.d.ts +21 -3
- package/dist/core/auth/oauth/types.js +4 -1
- package/dist/core/auth/routes.js +1 -1
- package/dist/core/auth/tokens.d.ts +3 -1
- package/dist/core/auth/tokens.js +2 -1
- package/dist/core/configSeam.d.ts +0 -4
- package/dist/core/configSeam.js +12 -1
- package/dist/core/createServer.js +16 -4
- package/dist/core/freePort.d.ts +10 -0
- package/dist/core/freePort.js +39 -1
- package/dist/core/handlers/socialRouteHandlers.d.ts +9 -1
- package/dist/core/handlers/socialRouteHandlers.js +46 -12
- package/dist/core/health.d.ts +1 -0
- package/dist/core/health.js +2 -1
- package/dist/core/http/stages/authRoutes.d.ts +6 -0
- package/dist/core/http/stages/authRoutes.js +42 -1
- package/dist/core/http/stages/images.js +5 -3
- package/dist/core/http/stages/livePages.js +1 -1
- package/dist/core/server/fleet/relay.js +9 -1
- package/dist/core/server/fleet/stores.d.ts +7 -0
- package/dist/core/server/fleet/stores.js +15 -2
- package/dist/core/server/pageServer.js +5 -1
- package/dist/core/services/realtime.js +4 -2
- package/dist/core/services/registry.js +7 -1
- package/dist/core/services/robotsTxt.d.ts +16 -0
- package/dist/core/services/robotsTxt.js +50 -0
- package/dist/core/services/servedSpace.d.ts +8 -0
- package/dist/core/services/servedSpace.js +16 -0
- package/dist/core/services/sitemapXml.d.ts +7 -0
- package/dist/core/services/sitemapXml.js +37 -0
- package/dist/functions-api.d.ts +69 -2
- package/dist/helpers/fileStore.d.ts +60 -0
- package/dist/helpers/fileStore.js +127 -0
- package/dist/helpers/outboundGuard.d.ts +32 -3
- package/dist/helpers/outboundGuard.js +50 -5
- package/dist/hosts/project/agentRuns.d.ts +24 -0
- package/dist/hosts/project/agentRuns.js +107 -0
- package/dist/hosts/project/laterAsks.d.ts +41 -0
- package/dist/hosts/project/laterAsks.js +117 -0
- package/dist/hosts/project/plugins.js +11 -4
- package/dist/hosts/project/serveProject.d.ts +8 -1
- package/dist/hosts/project/serveProject.js +81 -24
- package/dist/hosts/project/spaceFailure.d.ts +4 -0
- package/dist/hosts/project/spaceFailure.js +10 -0
- package/dist/hosts/project/watchSpace.d.ts +5 -5
- package/dist/hosts/project/watchSpace.js +15 -6
- package/dist/index.js +2 -2
- package/dist/kernel.js +1 -1
- package/dist/modules/actions/connectorWrite.js +3 -1
- package/dist/modules/actions/index.js +1 -1
- package/dist/modules/actions/jobs/fileQueue.d.ts +34 -0
- package/dist/modules/actions/jobs/fileQueue.js +72 -0
- package/dist/modules/actions/jobs/index.js +3 -3
- package/dist/modules/actions/jobs/memoryQueue.js +6 -215
- package/dist/modules/actions/jobs/queueDocument.d.ts +14 -0
- package/dist/modules/actions/jobs/queueDocument.js +22 -0
- package/dist/modules/actions/jobs/queueRules.d.ts +39 -0
- package/dist/modules/actions/jobs/queueRules.js +270 -0
- package/dist/modules/actions/moduleFor.js +2 -0
- package/dist/modules/actions/runtime/fileKv.d.ts +3 -1
- package/dist/modules/actions/runtime/fileKv.js +12 -98
- package/dist/modules/actions/runtime/guards.d.ts +19 -1
- package/dist/modules/actions/runtime/guards.js +10 -1
- package/dist/modules/actions/runtime/kvChange.d.ts +40 -0
- package/dist/modules/actions/runtime/kvChange.js +23 -1
- package/dist/modules/actions/runtime/kvList.d.ts +4 -0
- package/dist/modules/actions/runtime/kvList.js +5 -1
- package/dist/modules/actions/runtime/kvStore.js +15 -1
- package/dist/modules/actions/runtime/namespaceKv.js +2 -0
- package/dist/modules/actions/runtime/renderResolver.js +31 -5
- package/dist/modules/actions/runtime/runAction.js +16 -6
- package/dist/modules/actions/runtime/scope.js +11 -0
- package/dist/modules/actions/runtime/taskContext.js +11 -32
- package/dist/modules/actions/tasks/auth.js +2 -0
- package/dist/modules/actions/tasks/connector.js +6 -2
- package/dist/modules/actions/tasks/flow.js +5 -0
- package/dist/modules/actions/tasks/http.js +2 -1
- package/dist/modules/actions/tasks/kv.js +5 -0
- package/dist/modules/actions/tasks/list.js +11 -2
- package/dist/modules/actions/tasks/transform.js +2 -0
- package/dist/modules/actions/transport/callHandler.js +25 -4
- package/dist/modules/actions/types.d.ts +46 -1
- package/dist/modules/connectors/engine.d.ts +7 -2
- package/dist/modules/connectors/engine.js +4 -4
- package/dist/modules/connectors/resolver.d.ts +2 -1
- package/dist/modules/connectors/resolver.js +3 -2
- package/dist/modules/functions/build.d.ts +7 -2
- package/dist/modules/functions/build.js +90 -6
- package/dist/modules/functions/capabilities.js +16 -0
- package/dist/modules/functions/context.js +6 -2
- package/dist/modules/functions/contract.d.ts +39 -1
- package/dist/modules/functions/driver.d.ts +2 -1
- package/dist/modules/functions/driver.js +35 -0
- package/dist/modules/functions/load.d.ts +1 -1
- package/dist/modules/functions/load.js +2 -2
- package/dist/modules/functions/manifest.js +7 -1
- package/dist/modules/functions/oauth.d.ts +1 -1
- package/dist/modules/functions/oauth.js +4 -4
- package/dist/modules/functions/protocol.d.ts +6 -0
- package/dist/modules/functions/sandbox/guest/bytes.d.ts +27 -0
- package/dist/modules/functions/sandbox/guest/bytes.js +108 -0
- package/dist/modules/functions/sandbox/guest/crypto.d.ts +47 -0
- package/dist/modules/functions/sandbox/guest/crypto.js +144 -0
- package/dist/modules/functions/sandbox/guest/events.d.ts +152 -0
- package/dist/modules/functions/sandbox/guest/events.js +143 -0
- package/dist/modules/functions/sandbox/guest/fetch.d.ts +305 -0
- package/dist/modules/functions/sandbox/guest/fetch.js +237 -0
- package/dist/modules/functions/sandbox/guest/index.d.ts +28 -0
- package/dist/modules/functions/sandbox/guest/index.js +130 -0
- package/dist/modules/functions/sandbox/{guest.d.ts → guest/types.d.ts} +5 -12
- package/dist/modules/functions/sandbox/isolate.js +20 -6
- package/dist/modules/functions/space.js +2 -8
- package/dist/modules/images/imageProxy.d.ts +3 -2
- package/dist/modules/images/imageProxy.js +2 -2
- package/dist/modules/mongo/jobQueue.js +0 -19
- package/dist/modules/mysql/accounts.js +12 -5
- package/dist/modules/mysql/jobs/index.d.ts +1 -1
- package/dist/modules/mysql/jobs/schema.d.ts +0 -6
- package/dist/modules/mysql/jobs/schema.js +2 -46
- package/dist/modules/mysql/schema.js +26 -1
- package/dist/modules/mysql/sessions.js +2 -1
- package/dist/modules/realtime/handlers.d.ts +9 -4
- package/dist/modules/realtime/handlers.js +20 -3
- package/dist/modules/realtime/hub.d.ts +4 -2
- package/dist/modules/realtime/hub.js +36 -4
- package/dist/modules/realtime/index.d.ts +14 -0
- package/dist/modules/realtime/index.js +14 -1
- package/dist/modules/realtime/presence.d.ts +33 -0
- package/dist/modules/realtime/presence.js +56 -0
- package/dist/modules/realtime/socket.d.ts +3 -1
- package/dist/modules/realtime/socket.js +2 -2
- package/dist/modules/rsc/connectorRscData.d.ts +6 -1
- package/dist/modules/rsc/connectorRscData.js +27 -6
- package/dist/modules/rsc/externalQueryReader.d.ts +28 -0
- package/dist/modules/rsc/externalQueryReader.js +43 -0
- package/dist/modules/rsc/handler.js +1 -1
- package/dist/modules/rsc/resolveRscData.d.ts +11 -1
- package/dist/modules/rsc/resolveRscData.js +86 -16
- package/dist/modules/runtime/inside.js +1 -1
- package/dist/modules/runtime/runner.js +2 -1
- package/dist/modules/sqlite/jobQueue.d.ts +35 -0
- package/dist/modules/sqlite/jobQueue.js +56 -0
- package/dist/modules/sqlite/kv.d.ts +1 -1
- package/dist/modules/sqlite/kv.js +2 -10
- package/dist/modules/sqlite/open.d.ts +3 -0
- package/dist/modules/sqlite/open.js +13 -0
- package/dist/modules/ssr/Component.d.ts +5 -1
- package/dist/modules/ssr/Component.js +3 -1
- package/dist/modules/ssr/applySSRResult.d.ts +8 -1
- package/dist/modules/ssr/applySSRResult.js +23 -3
- package/dist/modules/ssr/handler.js +30 -5
- package/dist/modules/ssr/prepareRender.js +27 -5
- package/dist/modules/ssr/preview.d.ts +2 -0
- package/dist/modules/ssr/preview.js +6 -3
- package/dist/modules/ssr/registerExternalPlugins.d.ts +2 -6
- package/dist/modules/ssr/registerExternalPlugins.js +2 -2
- package/dist/modules/ssr/resolvePageSeo.d.ts +11 -0
- package/dist/modules/ssr/resolvePageSeo.js +13 -2
- package/dist/modules/ssr/streamBody.js +2 -2
- package/dist/modules/ssr/views/template.ejs +21 -17
- package/dist/mysql.d.ts +1 -1
- package/dist/mysql.js +2 -2
- package/dist/plugins/manager.d.ts +5 -0
- package/dist/plugins/manager.js +9 -0
- package/dist/sqlite.d.ts +8 -1
- package/dist/sqlite.js +3 -1
- package/package.json +5 -5
- package/skills/plitzi-authoring/CHEATSHEET.md +6 -6
- package/skills/plitzi-authoring/SKILL.md +4 -2
- package/skills/plitzi-authoring/recipes/accordion.ts +2 -1
- package/skills/plitzi-authoring/recipes/carousel.ts +24 -3
- package/skills/plitzi-authoring/recipes/detail-page.ts +0 -1
- package/skills/plitzi-authoring/recipes/embed-and-svg.ts +3 -1
- package/skills/plitzi-authoring/recipes/filter-a-list.ts +0 -1
- package/skills/plitzi-authoring/recipes/show-data.ts +0 -1
- package/skills/plitzi-authoring/recipes/typed-data.ts +0 -1
- package/skills/plitzi-authoring/reference/accessibility.md +7 -2
- package/skills/plitzi-authoring/reference/auth.md +15 -4
- package/skills/plitzi-authoring/reference/authoring-errors.md +51 -10
- package/skills/plitzi-authoring/reference/colours-and-motion.md +7 -1
- package/skills/plitzi-authoring/reference/components.md +4 -1
- package/skills/plitzi-authoring/reference/data-and-visibility.md +31 -22
- package/skills/plitzi-authoring/reference/efficiency.md +2 -1
- package/skills/plitzi-authoring/reference/elements-and-styles.md +16 -15
- package/skills/plitzi-authoring/reference/flows.md +20 -8
- package/skills/plitzi-authoring/reference/kept-state.md +8 -0
- package/skills/plitzi-authoring/reference/layouts.md +18 -2
- package/skills/plitzi-authoring/reference/lists.md +20 -4
- package/skills/plitzi-authoring/reference/plugin-reach.md +12 -0
- package/skills/plitzi-authoring/reference/plugins.md +13 -2
- package/skills/plitzi-authoring/reference/realtime.md +8 -5
- package/skills/plitzi-authoring/reference/structure.md +17 -1
- package/skills/plitzi-authoring/reference/templates.md +11 -3
- package/skills/plitzi-authoring/reference/testing.md +82 -5
- package/skills/plitzi-authoring/reference/typed-sources.md +6 -0
- package/skills/plitzi-authoring/reference/validation.md +1 -1
- package/dist/core/auth/api.js +0 -1323
- package/dist/modules/functions/sandbox/guest.js +0 -662
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { applyRule, emptyState, queueOver, queueRules } from "./queueRules.js";
|
|
2
2
|
//#region src/modules/actions/jobs/memoryQueue.ts
|
|
3
3
|
/** Every method of the queue, for the copy a server's workers share (see `fleet/stores.ts`). */
|
|
4
4
|
var JOB_QUEUE_METHODS = {
|
|
@@ -32,220 +32,11 @@ var JOB_QUEUE_METHODS = {
|
|
|
32
32
|
* is testing the rules a shared store has to keep too.
|
|
33
33
|
*/
|
|
34
34
|
var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const evict = () => {
|
|
41
|
-
const finished = [...jobs.values()].filter((job) => isTerminalJobStatus(job.status)).sort((a, b) => a.updatedAt - b.updatedAt);
|
|
42
|
-
for (const job of finished.slice(0, finished.length - retainFinished)) jobs.delete(job.id);
|
|
43
|
-
};
|
|
44
|
-
const owned = (spaceIds, jobId) => {
|
|
45
|
-
const job = jobs.get(jobId);
|
|
46
|
-
return job && spaceIds.includes(job.spaceId) ? job : void 0;
|
|
47
|
-
};
|
|
48
|
-
/** Numbered by the history itself, not by `attempts`: an operator who repeats a finished job starts its attempt
|
|
49
|
-
* count again, and the record of what has already been tried must not start again with it. */
|
|
50
|
-
const appendAttempt = (job, attempt) => {
|
|
51
|
-
job.history = [...job.history, {
|
|
52
|
-
...attempt,
|
|
53
|
-
attempt: job.history.length + 1
|
|
54
|
-
}];
|
|
55
|
-
};
|
|
56
|
-
return {
|
|
57
|
-
now: () => Promise.resolve(new Date(now())),
|
|
58
|
-
enqueue: (job) => {
|
|
59
|
-
if (jobs.has(job.id)) return Promise.resolve(false);
|
|
60
|
-
const at = now();
|
|
61
|
-
jobs.set(job.id, {
|
|
62
|
-
...job,
|
|
63
|
-
status: "pending",
|
|
64
|
-
runAt: job.dueAt,
|
|
65
|
-
attempts: 0,
|
|
66
|
-
createdAt: at,
|
|
67
|
-
updatedAt: at,
|
|
68
|
-
history: []
|
|
69
|
-
});
|
|
70
|
-
evict();
|
|
71
|
-
return Promise.resolve(true);
|
|
72
|
-
},
|
|
73
|
-
claim: ({ workerId, leaseMs, limit }) => {
|
|
74
|
-
const at = now();
|
|
75
|
-
const taken = [];
|
|
76
|
-
const candidates = [...jobs.values()].filter((job) => job.status === "pending" && job.runAt <= at || job.status === "running" && (job.leaseUntil ?? 0) < at).sort((a, b) => a.runAt - b.runAt);
|
|
77
|
-
for (const job of candidates.slice(0, limit)) {
|
|
78
|
-
if (job.status === "running") appendAttempt(job, {
|
|
79
|
-
status: "lost",
|
|
80
|
-
startedAt: job.updatedAt,
|
|
81
|
-
endedAt: at,
|
|
82
|
-
workerId: job.workerId ?? "unknown",
|
|
83
|
-
error: "the worker holding this job stopped reporting"
|
|
84
|
-
});
|
|
85
|
-
job.status = "running";
|
|
86
|
-
job.attempts += 1;
|
|
87
|
-
job.workerId = workerId;
|
|
88
|
-
job.leaseUntil = at + leaseMs;
|
|
89
|
-
job.updatedAt = at;
|
|
90
|
-
taken.push({ ...job });
|
|
91
|
-
}
|
|
92
|
-
return Promise.resolve(taken);
|
|
93
|
-
},
|
|
94
|
-
heartbeat: ({ jobIds, workerId, leaseMs }) => {
|
|
95
|
-
const at = now();
|
|
96
|
-
const stops = [];
|
|
97
|
-
for (const id of jobIds) {
|
|
98
|
-
const job = jobs.get(id);
|
|
99
|
-
if (!job || job.workerId !== workerId || job.status !== "running") continue;
|
|
100
|
-
job.leaseUntil = at + leaseMs;
|
|
101
|
-
if (job.cancelRequested) stops.push({
|
|
102
|
-
jobId: id,
|
|
103
|
-
stop: "cancel"
|
|
104
|
-
});
|
|
105
|
-
else if (job.pauseRequested) stops.push({
|
|
106
|
-
jobId: id,
|
|
107
|
-
stop: "pause"
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
return Promise.resolve(stops);
|
|
111
|
-
},
|
|
112
|
-
write: ({ jobId, workerId, state, progress }) => {
|
|
113
|
-
const job = jobs.get(jobId);
|
|
114
|
-
if (!job || job.workerId !== workerId || job.status !== "running") return Promise.resolve(false);
|
|
115
|
-
const at = now();
|
|
116
|
-
if (state !== void 0) job.state = state;
|
|
117
|
-
if (progress) job.progress = {
|
|
118
|
-
...progress,
|
|
119
|
-
at
|
|
120
|
-
};
|
|
121
|
-
job.updatedAt = at;
|
|
122
|
-
return Promise.resolve(true);
|
|
123
|
-
},
|
|
124
|
-
settle: ({ jobId, workerId, status, delayMs, runId, error, attempt, result, spentMs, refund = status === "pending" && !attempt }) => {
|
|
125
|
-
const job = jobs.get(jobId);
|
|
126
|
-
if (!job || job.workerId !== workerId) return Promise.resolve();
|
|
127
|
-
const at = now();
|
|
128
|
-
if (attempt) appendAttempt(job, {
|
|
129
|
-
...attempt,
|
|
130
|
-
startedAt: job.updatedAt,
|
|
131
|
-
endedAt: at
|
|
132
|
-
});
|
|
133
|
-
job.status = status === "pending" && job.cancelRequested ? "cancelled" : status === "pending" && job.pauseRequested ? "paused" : status;
|
|
134
|
-
job.updatedAt = at;
|
|
135
|
-
job.leaseUntil = void 0;
|
|
136
|
-
job.error = error;
|
|
137
|
-
job.cancelRequested = void 0;
|
|
138
|
-
job.pauseRequested = void 0;
|
|
139
|
-
if (runId) job.runId = runId;
|
|
140
|
-
if (result !== void 0) job.result = result;
|
|
141
|
-
if (spentMs) job.activeMs = (job.activeMs ?? 0) + spentMs;
|
|
142
|
-
if (refund) job.attempts = Math.max(0, job.attempts - 1);
|
|
143
|
-
if (job.status === "pending") job.runAt = at + (delayMs ?? 0);
|
|
144
|
-
if (job.status === "pending" || job.status === "paused") job.workerId = void 0;
|
|
145
|
-
evict();
|
|
146
|
-
return Promise.resolve();
|
|
147
|
-
},
|
|
148
|
-
dueSchedules: (limit) => {
|
|
149
|
-
const at = now();
|
|
150
|
-
return Promise.resolve([...schedules.values()].filter((schedule) => schedule.enabled && schedule.nextRunAt <= at).sort((a, b) => a.nextRunAt - b.nextRunAt).slice(0, limit).map((schedule) => ({ ...schedule })));
|
|
151
|
-
},
|
|
152
|
-
advanceSchedule: ({ spaceId, actionId, from, to, missed }) => {
|
|
153
|
-
const schedule = schedules.get(key(spaceId, actionId));
|
|
154
|
-
if (!schedule || schedule.nextRunAt !== from) return Promise.resolve(false);
|
|
155
|
-
schedule.nextRunAt = to;
|
|
156
|
-
schedule.lastFireAt = from;
|
|
157
|
-
schedule.missed += missed;
|
|
158
|
-
schedule.updatedAt = now();
|
|
159
|
-
return Promise.resolve(true);
|
|
160
|
-
},
|
|
161
|
-
putSchedules: ({ spaceId, schedules: incoming }) => {
|
|
162
|
-
const at = now();
|
|
163
|
-
const keep = new Set(incoming.map((schedule) => key(spaceId, schedule.actionId)));
|
|
164
|
-
for (const existing of schedules.keys()) if (existing.startsWith(`${spaceId}:`) && !keep.has(existing)) schedules.delete(existing);
|
|
165
|
-
for (const incomingSchedule of incoming) {
|
|
166
|
-
const id = key(spaceId, incomingSchedule.actionId);
|
|
167
|
-
const existing = schedules.get(id);
|
|
168
|
-
const unchanged = existing && existing.cron === incomingSchedule.cron && existing.timezone === incomingSchedule.timezone && existing.environment === incomingSchedule.environment;
|
|
169
|
-
schedules.set(id, {
|
|
170
|
-
...incomingSchedule,
|
|
171
|
-
missed: existing?.missed ?? 0,
|
|
172
|
-
...existing?.lastFireAt === void 0 ? {} : { lastFireAt: existing.lastFireAt },
|
|
173
|
-
nextRunAt: unchanged ? existing.nextRunAt : incomingSchedule.nextRunAt,
|
|
174
|
-
updatedAt: at
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
return Promise.resolve();
|
|
178
|
-
},
|
|
179
|
-
listJobs: ({ spaceIds, name, status, kind, limit, offset }) => {
|
|
180
|
-
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);
|
|
181
|
-
return Promise.resolve({
|
|
182
|
-
jobs: matching.slice(offset, offset + limit).map((job) => ({ ...job })),
|
|
183
|
-
total: matching.length
|
|
184
|
-
});
|
|
185
|
-
},
|
|
186
|
-
getJob: (spaceIds, jobId) => Promise.resolve(owned(spaceIds, jobId)),
|
|
187
|
-
listSchedules: (spaceIds) => Promise.resolve([...schedules.values()].filter((schedule) => spaceIds.includes(schedule.spaceId))),
|
|
188
|
-
requeue: (spaceIds, jobId, { input, fresh = false } = {}) => {
|
|
189
|
-
const job = owned(spaceIds, jobId);
|
|
190
|
-
if (!job || !isTerminalJobStatus(job.status)) return Promise.resolve(false);
|
|
191
|
-
job.status = "pending";
|
|
192
|
-
job.runAt = now();
|
|
193
|
-
job.attempts = 0;
|
|
194
|
-
job.workerId = void 0;
|
|
195
|
-
job.leaseUntil = void 0;
|
|
196
|
-
job.cancelRequested = void 0;
|
|
197
|
-
job.pauseRequested = void 0;
|
|
198
|
-
job.error = void 0;
|
|
199
|
-
job.result = void 0;
|
|
200
|
-
if (input) job.input = input;
|
|
201
|
-
if (fresh) {
|
|
202
|
-
job.state = void 0;
|
|
203
|
-
job.progress = void 0;
|
|
204
|
-
job.activeMs = void 0;
|
|
205
|
-
}
|
|
206
|
-
job.updatedAt = now();
|
|
207
|
-
return Promise.resolve(true);
|
|
208
|
-
},
|
|
209
|
-
cancel: (spaceIds, jobId) => {
|
|
210
|
-
const job = owned(spaceIds, jobId);
|
|
211
|
-
if (!job || isTerminalJobStatus(job.status)) return Promise.resolve(false);
|
|
212
|
-
if (job.status === "pending" || job.status === "paused") {
|
|
213
|
-
job.status = "cancelled";
|
|
214
|
-
job.workerId = void 0;
|
|
215
|
-
job.pauseRequested = void 0;
|
|
216
|
-
} else job.cancelRequested = true;
|
|
217
|
-
job.updatedAt = now();
|
|
218
|
-
return Promise.resolve(true);
|
|
219
|
-
},
|
|
220
|
-
pause: (spaceIds, jobId) => {
|
|
221
|
-
const job = owned(spaceIds, jobId);
|
|
222
|
-
if (job?.target.kind !== "function" || job.status !== "pending" && job.status !== "running" || job.pauseRequested) return Promise.resolve(false);
|
|
223
|
-
if (job.status === "pending") job.status = "paused";
|
|
224
|
-
else job.pauseRequested = true;
|
|
225
|
-
job.updatedAt = now();
|
|
226
|
-
return Promise.resolve(true);
|
|
227
|
-
},
|
|
228
|
-
resume: (spaceIds, jobId) => {
|
|
229
|
-
const job = owned(spaceIds, jobId);
|
|
230
|
-
if (job?.status !== "paused") return Promise.resolve(false);
|
|
231
|
-
job.status = "pending";
|
|
232
|
-
job.runAt = now();
|
|
233
|
-
job.updatedAt = now();
|
|
234
|
-
return Promise.resolve(true);
|
|
235
|
-
},
|
|
236
|
-
cancelPending: ({ spaceId, key, olderThan }) => {
|
|
237
|
-
const newest = olderThan === void 0 ? void 0 : jobs.get(olderThan);
|
|
238
|
-
const isOlder = (job) => newest === void 0 || job.createdAt < newest.createdAt || job.createdAt === newest.createdAt && job.id < newest.id;
|
|
239
|
-
const at = now();
|
|
240
|
-
let dropped = 0;
|
|
241
|
-
for (const job of jobs.values()) if (job.spaceId === spaceId && job.key === key && job.status === "pending" && job.id !== olderThan && isOlder(job)) {
|
|
242
|
-
job.status = "cancelled";
|
|
243
|
-
job.updatedAt = at;
|
|
244
|
-
dropped += 1;
|
|
245
|
-
}
|
|
246
|
-
return Promise.resolve(dropped);
|
|
247
|
-
}
|
|
248
|
-
};
|
|
35
|
+
const rules = queueRules(emptyState(), {
|
|
36
|
+
retainFinished,
|
|
37
|
+
clock
|
|
38
|
+
});
|
|
39
|
+
return queueOver((method, args) => Promise.resolve(applyRule(rules, method, args)));
|
|
249
40
|
};
|
|
250
41
|
//#endregion
|
|
251
42
|
export { JOB_QUEUE_METHODS, createMemoryJobQueue };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { QueueState } from './queueRules';
|
|
2
|
+
import { ActionSchedule, Job } from '@plitzi/sdk-shared';
|
|
3
|
+
/**
|
|
4
|
+
* A queue's state as one document — what `createFileJobQueue` writes to its file and `createSqliteJobQueue` to its row —
|
|
5
|
+
* versioned so a later layout can tell an old one from a broken one.
|
|
6
|
+
*/
|
|
7
|
+
export type QueueDocument = {
|
|
8
|
+
format: 1;
|
|
9
|
+
jobs: Job[];
|
|
10
|
+
schedules: ActionSchedule[];
|
|
11
|
+
};
|
|
12
|
+
export declare const documentOf: ({ jobs, schedules }: QueueState) => QueueDocument;
|
|
13
|
+
/** The state a document holds — or why it is no queue's, for the store to say with its fix. */
|
|
14
|
+
export declare const stateOf: (parsed: unknown) => QueueState | string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { scheduleKey } from "./queueRules.js";
|
|
2
|
+
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
3
|
+
//#region src/modules/actions/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
|
+
//#endregion
|
|
22
|
+
export { documentOf, stateOf };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Job, JobQueue, ActionSchedule } from '@plitzi/sdk-shared';
|
|
2
|
+
/**
|
|
3
|
+
* The queue's rules — what `enqueue`, `claim`, `settle`… do to the jobs and schedules — over a state handed to them,
|
|
4
|
+
* kept apart from WHERE the state is: a process's Maps (`createMemoryJobQueue`), a JSON file (`createFileJobQueue`), a
|
|
5
|
+
* SQLite table (`createSqliteJobQueue`). One set of rules, so a store kept anywhere keeps the contract to the letter: a
|
|
6
|
+
* test that passes against one is testing all of them.
|
|
7
|
+
*/
|
|
8
|
+
/** The jobs and the schedules, by id: what a queue keeps. */
|
|
9
|
+
export type QueueState = {
|
|
10
|
+
jobs: Map<string, Job>;
|
|
11
|
+
schedules: Map<string, ActionSchedule>;
|
|
12
|
+
};
|
|
13
|
+
/** The queue's methods, answering at once: what each store wraps in its own way of reading and keeping the state. */
|
|
14
|
+
export type QueueRules = {
|
|
15
|
+
[K in keyof JobQueue]: (...args: Parameters<JobQueue[K]>) => Awaited<ReturnType<JobQueue[K]>>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Whether a call changed the state, by its method and what it answered — what a store keeping the state elsewhere writes
|
|
19
|
+
* back after. A worker claims every second: a claim that took nothing, a refused `write` or `cancel` change nothing, and
|
|
20
|
+
* are not written.
|
|
21
|
+
*/
|
|
22
|
+
export declare const changedBy: <K extends keyof JobQueue>(method: K, answer: Awaited<ReturnType<JobQueue[K]>>) => boolean;
|
|
23
|
+
/** What a schedule is kept by: its space and its action. */
|
|
24
|
+
export declare const scheduleKey: (spaceId: number, actionId: string) => string;
|
|
25
|
+
export type QueueRulesOptions = {
|
|
26
|
+
/** How many finished jobs to keep before the oldest are dropped. */
|
|
27
|
+
retainFinished: number;
|
|
28
|
+
/** What the queue calls now, in epoch ms: the one clock everything here reads. */
|
|
29
|
+
clock: () => number;
|
|
30
|
+
};
|
|
31
|
+
export declare const queueRules: ({ jobs, schedules }: QueueState, { retainFinished, clock }: QueueRulesOptions) => QueueRules;
|
|
32
|
+
/** A state with nothing in it. */
|
|
33
|
+
export declare const emptyState: () => QueueState;
|
|
34
|
+
/** How a store runs one method of the rules: reading its state, and keeping it after one that changes it. */
|
|
35
|
+
export type QueueAccess = <K extends keyof JobQueue>(method: K, args: Parameters<JobQueue[K]>) => Promise<Awaited<ReturnType<JobQueue[K]>>>;
|
|
36
|
+
/** One method of the rules, called with the arguments its contract types. */
|
|
37
|
+
export declare const applyRule: <K extends keyof JobQueue>(rules: QueueRules, method: K, args: Parameters<JobQueue[K]>) => Awaited<ReturnType<JobQueue[K]>>;
|
|
38
|
+
/** The contract, each method run through `access`: what every store answers with. */
|
|
39
|
+
export declare const queueOver: (access: QueueAccess) => JobQueue;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { isTerminalJobStatus } from "@plitzi/sdk-shared/actions";
|
|
2
|
+
//#region src/modules/actions/jobs/queueRules.ts
|
|
3
|
+
/**
|
|
4
|
+
* Whether a call changed the state, by its method and what it answered — what a store keeping the state elsewhere writes
|
|
5
|
+
* back after. A worker claims every second: a claim that took nothing, a refused `write` or `cancel` change nothing, and
|
|
6
|
+
* are not written.
|
|
7
|
+
*/
|
|
8
|
+
var changedBy = (method, answer) => {
|
|
9
|
+
switch (method) {
|
|
10
|
+
case "claim": return Array.isArray(answer) && answer.length > 0;
|
|
11
|
+
case "enqueue":
|
|
12
|
+
case "write":
|
|
13
|
+
case "advanceSchedule":
|
|
14
|
+
case "requeue":
|
|
15
|
+
case "cancel":
|
|
16
|
+
case "pause":
|
|
17
|
+
case "resume": return answer === true;
|
|
18
|
+
case "cancelPending": return typeof answer === "number" && answer > 0;
|
|
19
|
+
case "heartbeat":
|
|
20
|
+
case "settle":
|
|
21
|
+
case "putSchedules": return true;
|
|
22
|
+
default: return false;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/** What a schedule is kept by: its space and its action. */
|
|
26
|
+
var scheduleKey = (spaceId, actionId) => `${String(spaceId)}:${actionId}`;
|
|
27
|
+
var queueRules = ({ jobs, schedules }, { retainFinished, clock }) => {
|
|
28
|
+
const key = scheduleKey;
|
|
29
|
+
const now = () => clock();
|
|
30
|
+
/** Oldest finished jobs first, so the bound drops history rather than anything anybody is waiting on. */
|
|
31
|
+
const evict = () => {
|
|
32
|
+
const finished = [...jobs.values()].filter((job) => isTerminalJobStatus(job.status)).sort((a, b) => a.updatedAt - b.updatedAt);
|
|
33
|
+
for (const job of finished.slice(0, finished.length - retainFinished)) jobs.delete(job.id);
|
|
34
|
+
};
|
|
35
|
+
const owned = (spaceIds, jobId) => {
|
|
36
|
+
const job = jobs.get(jobId);
|
|
37
|
+
return job && spaceIds.includes(job.spaceId) ? job : void 0;
|
|
38
|
+
};
|
|
39
|
+
/** Numbered by the history itself, not by `attempts`: an operator who repeats a finished job starts its attempt
|
|
40
|
+
* count again, and the record of what has already been tried must not start again with it. */
|
|
41
|
+
const appendAttempt = (job, attempt) => {
|
|
42
|
+
job.history = [...job.history, {
|
|
43
|
+
...attempt,
|
|
44
|
+
attempt: job.history.length + 1
|
|
45
|
+
}];
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
now: () => new Date(now()),
|
|
49
|
+
enqueue: (job) => {
|
|
50
|
+
if (jobs.has(job.id)) return false;
|
|
51
|
+
const at = now();
|
|
52
|
+
jobs.set(job.id, {
|
|
53
|
+
...job,
|
|
54
|
+
status: "pending",
|
|
55
|
+
runAt: job.dueAt,
|
|
56
|
+
attempts: 0,
|
|
57
|
+
createdAt: at,
|
|
58
|
+
updatedAt: at,
|
|
59
|
+
history: []
|
|
60
|
+
});
|
|
61
|
+
evict();
|
|
62
|
+
return true;
|
|
63
|
+
},
|
|
64
|
+
claim: ({ workerId, leaseMs, limit }) => {
|
|
65
|
+
const at = now();
|
|
66
|
+
const taken = [];
|
|
67
|
+
const candidates = [...jobs.values()].filter((job) => job.status === "pending" && job.runAt <= at || job.status === "running" && (job.leaseUntil ?? 0) < at).sort((a, b) => a.runAt - b.runAt);
|
|
68
|
+
for (const job of candidates.slice(0, limit)) {
|
|
69
|
+
if (job.status === "running") appendAttempt(job, {
|
|
70
|
+
status: "lost",
|
|
71
|
+
startedAt: job.updatedAt,
|
|
72
|
+
endedAt: at,
|
|
73
|
+
workerId: job.workerId ?? "unknown",
|
|
74
|
+
error: "the worker holding this job stopped reporting"
|
|
75
|
+
});
|
|
76
|
+
job.status = "running";
|
|
77
|
+
job.attempts += 1;
|
|
78
|
+
job.workerId = workerId;
|
|
79
|
+
job.leaseUntil = at + leaseMs;
|
|
80
|
+
job.updatedAt = at;
|
|
81
|
+
taken.push({ ...job });
|
|
82
|
+
}
|
|
83
|
+
return taken;
|
|
84
|
+
},
|
|
85
|
+
heartbeat: ({ jobIds, workerId, leaseMs }) => {
|
|
86
|
+
const at = now();
|
|
87
|
+
const stops = [];
|
|
88
|
+
for (const id of jobIds) {
|
|
89
|
+
const job = jobs.get(id);
|
|
90
|
+
if (!job || job.workerId !== workerId || job.status !== "running") continue;
|
|
91
|
+
job.leaseUntil = at + leaseMs;
|
|
92
|
+
if (job.cancelRequested) stops.push({
|
|
93
|
+
jobId: id,
|
|
94
|
+
stop: "cancel"
|
|
95
|
+
});
|
|
96
|
+
else if (job.pauseRequested) stops.push({
|
|
97
|
+
jobId: id,
|
|
98
|
+
stop: "pause"
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return stops;
|
|
102
|
+
},
|
|
103
|
+
write: ({ jobId, workerId, state, progress }) => {
|
|
104
|
+
const job = jobs.get(jobId);
|
|
105
|
+
if (!job || job.workerId !== workerId || job.status !== "running") return false;
|
|
106
|
+
const at = now();
|
|
107
|
+
if (state !== void 0) job.state = state;
|
|
108
|
+
if (progress) job.progress = {
|
|
109
|
+
...progress,
|
|
110
|
+
at
|
|
111
|
+
};
|
|
112
|
+
job.updatedAt = at;
|
|
113
|
+
return true;
|
|
114
|
+
},
|
|
115
|
+
settle: ({ jobId, workerId, status, delayMs, runId, error, attempt, result, spentMs, refund = status === "pending" && !attempt }) => {
|
|
116
|
+
const job = jobs.get(jobId);
|
|
117
|
+
if (!job || job.workerId !== workerId) return;
|
|
118
|
+
const at = now();
|
|
119
|
+
if (attempt) appendAttempt(job, {
|
|
120
|
+
...attempt,
|
|
121
|
+
startedAt: job.updatedAt,
|
|
122
|
+
endedAt: at
|
|
123
|
+
});
|
|
124
|
+
job.status = status === "pending" && job.cancelRequested ? "cancelled" : status === "pending" && job.pauseRequested ? "paused" : status;
|
|
125
|
+
job.updatedAt = at;
|
|
126
|
+
job.leaseUntil = void 0;
|
|
127
|
+
job.error = error;
|
|
128
|
+
job.cancelRequested = void 0;
|
|
129
|
+
job.pauseRequested = void 0;
|
|
130
|
+
if (runId) job.runId = runId;
|
|
131
|
+
if (result !== void 0) job.result = result;
|
|
132
|
+
if (spentMs) job.activeMs = (job.activeMs ?? 0) + spentMs;
|
|
133
|
+
if (refund) job.attempts = Math.max(0, job.attempts - 1);
|
|
134
|
+
if (job.status === "pending") job.runAt = at + (delayMs ?? 0);
|
|
135
|
+
if (job.status === "pending" || job.status === "paused") job.workerId = void 0;
|
|
136
|
+
evict();
|
|
137
|
+
},
|
|
138
|
+
dueSchedules: (limit) => {
|
|
139
|
+
const at = now();
|
|
140
|
+
return [...schedules.values()].filter((schedule) => schedule.enabled && schedule.nextRunAt <= at).sort((a, b) => a.nextRunAt - b.nextRunAt).slice(0, limit).map((schedule) => ({ ...schedule }));
|
|
141
|
+
},
|
|
142
|
+
advanceSchedule: ({ spaceId, actionId, from, to, missed }) => {
|
|
143
|
+
const schedule = schedules.get(key(spaceId, actionId));
|
|
144
|
+
if (!schedule || schedule.nextRunAt !== from) return false;
|
|
145
|
+
schedule.nextRunAt = to;
|
|
146
|
+
schedule.lastFireAt = from;
|
|
147
|
+
schedule.missed += missed;
|
|
148
|
+
schedule.updatedAt = now();
|
|
149
|
+
return true;
|
|
150
|
+
},
|
|
151
|
+
putSchedules: ({ spaceId, schedules: incoming }) => {
|
|
152
|
+
const at = now();
|
|
153
|
+
const keep = new Set(incoming.map((schedule) => key(spaceId, schedule.actionId)));
|
|
154
|
+
for (const existing of schedules.keys()) if (existing.startsWith(`${spaceId}:`) && !keep.has(existing)) schedules.delete(existing);
|
|
155
|
+
for (const incomingSchedule of incoming) {
|
|
156
|
+
const id = key(spaceId, incomingSchedule.actionId);
|
|
157
|
+
const existing = schedules.get(id);
|
|
158
|
+
const unchanged = existing && existing.cron === incomingSchedule.cron && existing.timezone === incomingSchedule.timezone && existing.environment === incomingSchedule.environment;
|
|
159
|
+
schedules.set(id, {
|
|
160
|
+
...incomingSchedule,
|
|
161
|
+
missed: existing?.missed ?? 0,
|
|
162
|
+
...existing?.lastFireAt === void 0 ? {} : { lastFireAt: existing.lastFireAt },
|
|
163
|
+
nextRunAt: unchanged ? existing.nextRunAt : incomingSchedule.nextRunAt,
|
|
164
|
+
updatedAt: at
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
listJobs: ({ spaceIds, name, status, kind, limit, offset }) => {
|
|
169
|
+
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);
|
|
170
|
+
return {
|
|
171
|
+
jobs: matching.slice(offset, offset + limit).map((job) => ({ ...job })),
|
|
172
|
+
total: matching.length
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
getJob: (spaceIds, jobId) => owned(spaceIds, jobId),
|
|
176
|
+
listSchedules: (spaceIds) => [...schedules.values()].filter((schedule) => spaceIds.includes(schedule.spaceId)),
|
|
177
|
+
requeue: (spaceIds, jobId, { input, fresh = false } = {}) => {
|
|
178
|
+
const job = owned(spaceIds, jobId);
|
|
179
|
+
if (!job || !isTerminalJobStatus(job.status)) return false;
|
|
180
|
+
job.status = "pending";
|
|
181
|
+
job.runAt = now();
|
|
182
|
+
job.attempts = 0;
|
|
183
|
+
job.workerId = void 0;
|
|
184
|
+
job.leaseUntil = void 0;
|
|
185
|
+
job.cancelRequested = void 0;
|
|
186
|
+
job.pauseRequested = void 0;
|
|
187
|
+
job.error = void 0;
|
|
188
|
+
job.result = void 0;
|
|
189
|
+
if (input) job.input = input;
|
|
190
|
+
if (fresh) {
|
|
191
|
+
job.state = void 0;
|
|
192
|
+
job.progress = void 0;
|
|
193
|
+
job.activeMs = void 0;
|
|
194
|
+
}
|
|
195
|
+
job.updatedAt = now();
|
|
196
|
+
return true;
|
|
197
|
+
},
|
|
198
|
+
cancel: (spaceIds, jobId) => {
|
|
199
|
+
const job = owned(spaceIds, jobId);
|
|
200
|
+
if (!job || isTerminalJobStatus(job.status)) return false;
|
|
201
|
+
if (job.status === "pending" || job.status === "paused") {
|
|
202
|
+
job.status = "cancelled";
|
|
203
|
+
job.workerId = void 0;
|
|
204
|
+
job.pauseRequested = void 0;
|
|
205
|
+
} else job.cancelRequested = true;
|
|
206
|
+
job.updatedAt = now();
|
|
207
|
+
return true;
|
|
208
|
+
},
|
|
209
|
+
pause: (spaceIds, jobId) => {
|
|
210
|
+
const job = owned(spaceIds, jobId);
|
|
211
|
+
if (job?.target.kind !== "function" || job.status !== "pending" && job.status !== "running" || job.pauseRequested) return false;
|
|
212
|
+
if (job.status === "pending") job.status = "paused";
|
|
213
|
+
else job.pauseRequested = true;
|
|
214
|
+
job.updatedAt = now();
|
|
215
|
+
return true;
|
|
216
|
+
},
|
|
217
|
+
resume: (spaceIds, jobId) => {
|
|
218
|
+
const job = owned(spaceIds, jobId);
|
|
219
|
+
if (job?.status !== "paused") return false;
|
|
220
|
+
job.status = "pending";
|
|
221
|
+
job.runAt = now();
|
|
222
|
+
job.updatedAt = now();
|
|
223
|
+
return true;
|
|
224
|
+
},
|
|
225
|
+
cancelPending: ({ spaceId, key, olderThan }) => {
|
|
226
|
+
const newest = olderThan === void 0 ? void 0 : jobs.get(olderThan);
|
|
227
|
+
const isOlder = (job) => newest === void 0 || job.createdAt < newest.createdAt || job.createdAt === newest.createdAt && job.id < newest.id;
|
|
228
|
+
const at = now();
|
|
229
|
+
let dropped = 0;
|
|
230
|
+
for (const job of jobs.values()) if (job.spaceId === spaceId && job.key === key && job.status === "pending" && job.id !== olderThan && isOlder(job)) {
|
|
231
|
+
job.status = "cancelled";
|
|
232
|
+
job.updatedAt = at;
|
|
233
|
+
dropped += 1;
|
|
234
|
+
}
|
|
235
|
+
return dropped;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
/** A state with nothing in it. */
|
|
240
|
+
var emptyState = () => ({
|
|
241
|
+
jobs: /* @__PURE__ */ new Map(),
|
|
242
|
+
schedules: /* @__PURE__ */ new Map()
|
|
243
|
+
});
|
|
244
|
+
/** One method of the rules, called with the arguments its contract types. */
|
|
245
|
+
var applyRule = (rules, method, args) => {
|
|
246
|
+
const rule = rules[method];
|
|
247
|
+
return rule(...args);
|
|
248
|
+
};
|
|
249
|
+
/** The contract, each method run through `access`: what every store answers with. */
|
|
250
|
+
var queueOver = (access) => ({
|
|
251
|
+
now: (...args) => access("now", args),
|
|
252
|
+
enqueue: (...args) => access("enqueue", args),
|
|
253
|
+
claim: (...args) => access("claim", args),
|
|
254
|
+
heartbeat: (...args) => access("heartbeat", args),
|
|
255
|
+
settle: (...args) => access("settle", args),
|
|
256
|
+
write: (...args) => access("write", args),
|
|
257
|
+
dueSchedules: (...args) => access("dueSchedules", args),
|
|
258
|
+
advanceSchedule: (...args) => access("advanceSchedule", args),
|
|
259
|
+
putSchedules: (...args) => access("putSchedules", args),
|
|
260
|
+
listJobs: (...args) => access("listJobs", args),
|
|
261
|
+
getJob: (...args) => access("getJob", args),
|
|
262
|
+
listSchedules: (...args) => access("listSchedules", args),
|
|
263
|
+
requeue: (...args) => access("requeue", args),
|
|
264
|
+
cancel: (...args) => access("cancel", args),
|
|
265
|
+
pause: (...args) => access("pause", args),
|
|
266
|
+
resume: (...args) => access("resume", args),
|
|
267
|
+
cancelPending: (...args) => access("cancelPending", args)
|
|
268
|
+
});
|
|
269
|
+
//#endregion
|
|
270
|
+
export { applyRule, changedBy, emptyState, queueOver, queueRules, scheduleKey };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { actionLookupsOf, dbDriversOf, functionsConfigOf } from "../../core/configSeam.js";
|
|
2
|
+
import { privateOriginsOf } from "../../helpers/outboundGuard.js";
|
|
2
3
|
import { createActionsModule } from "./index.js";
|
|
3
4
|
import { realtimeModuleFor } from "../realtime/index.js";
|
|
4
5
|
//#region src/modules/actions/moduleFor.ts
|
|
@@ -32,6 +33,7 @@ var actionsModuleFor = (config) => {
|
|
|
32
33
|
signingSecret: config.action?.signingSecret,
|
|
33
34
|
dbDrivers: dbDriversOf(config.action?.dbDrivers),
|
|
34
35
|
email: config.action?.email,
|
|
36
|
+
outbound: { privateOrigins: privateOriginsOf(config.outbound?.privateOrigins) },
|
|
35
37
|
onRun: config.action?.onRun,
|
|
36
38
|
realtime: realtimeModuleFor(config)
|
|
37
39
|
});
|
|
@@ -4,6 +4,8 @@ export type FileKvOptions = {
|
|
|
4
4
|
file: string;
|
|
5
5
|
/** Who holds it, said to a second process that tries: `this project's server, on port 8080`. */
|
|
6
6
|
owner?: string;
|
|
7
|
+
/** Where the process that holds it answers, for a second one to be pointed there: `http://localhost:8080`. */
|
|
8
|
+
serves?: string;
|
|
7
9
|
};
|
|
8
10
|
export type FileKv = ActionKvAdapter & {
|
|
9
11
|
/** Resolves once everything written so far is on disk: for a shutdown, or a test that reads the file. */
|
|
@@ -30,4 +32,4 @@ export type FileKv = ActionKvAdapter & {
|
|
|
30
32
|
* primary's through the fleet (`fleetKv`); replicas use `createSqliteKv` (`@plitzi/sdk-server/sqlite`) or a database.
|
|
31
33
|
* And a whole file per change: right for what a site keeps, not for a counter bumped thousands of times a second.
|
|
32
34
|
*/
|
|
33
|
-
export declare const createFileKv: ({ file, owner }: FileKvOptions) => FileKv;
|
|
35
|
+
export declare const createFileKv: ({ file, owner, serves }: FileKvOptions) => FileKv;
|