@plitzi/sdk-server 0.38.20 → 0.38.22
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 +141 -0
- package/README.md +2 -2
- package/dist/actions.d.ts +6 -0
- package/dist/actions.js +2 -1
- package/dist/core/auth/createAuth.d.ts +1 -1
- package/dist/core/auth/tokens.d.ts +1 -1
- package/dist/core/configSeam.js +12 -2
- package/dist/core/services/functionRoutes.js +7 -2
- package/dist/core/services/realtime.js +5 -2
- package/dist/functions-api.d.ts +176 -3
- package/dist/functions.d.ts +3 -1
- package/dist/functions.js +2 -1
- package/dist/helpers/serverLog.d.ts +1 -1
- package/dist/helpers/serverLog.js +1 -1
- package/dist/hosts/project/plugins.js +20 -3
- package/dist/hosts/project/serveProject.js +16 -1
- package/dist/modules/actions/index.js +17 -4
- package/dist/modules/actions/jobs/index.d.ts +4 -3
- package/dist/modules/actions/jobs/index.js +2 -1
- package/dist/modules/actions/jobs/later.d.ts +2 -2
- package/dist/modules/actions/jobs/later.js +5 -2
- package/dist/modules/actions/jobs/memoryQueue.d.ts +4 -4
- package/dist/modules/actions/jobs/memoryQueue.js +69 -25
- package/dist/modules/actions/jobs/scheduler.d.ts +2 -2
- package/dist/modules/actions/jobs/scheduler.js +5 -2
- package/dist/modules/actions/jobs/worker.d.ts +6 -3
- package/dist/modules/actions/jobs/worker.js +104 -10
- package/dist/modules/actions/runtime/errors.d.ts +9 -1
- package/dist/modules/actions/runtime/errors.js +12 -2
- package/dist/modules/actions/runtime/runAction.d.ts +5 -2
- package/dist/modules/actions/runtime/runAction.js +5 -3
- package/dist/modules/actions/runtime/taskContext.d.ts +12 -3
- package/dist/modules/actions/runtime/taskContext.js +9 -1
- package/dist/modules/actions/types.d.ts +22 -6
- package/dist/modules/functions/capabilities.js +124 -13
- package/dist/modules/functions/config.d.ts +20 -0
- package/dist/modules/functions/context.d.ts +2 -2
- package/dist/modules/functions/context.js +16 -3
- package/dist/modules/functions/contract.d.ts +97 -3
- package/dist/modules/functions/driver.js +136 -9
- package/dist/modules/functions/jobControl.d.ts +21 -0
- package/dist/modules/functions/jobControl.js +43 -0
- package/dist/modules/functions/jobs.d.ts +98 -0
- package/dist/modules/functions/jobs.js +438 -0
- package/dist/modules/functions/manifest.d.ts +2 -0
- package/dist/modules/functions/manifest.js +37 -4
- package/dist/modules/functions/native.js +2 -1
- package/dist/modules/functions/oauth.d.ts +3 -2
- package/dist/modules/functions/oauth.js +53 -19
- package/dist/modules/functions/protocol.d.ts +98 -7
- package/dist/modules/functions/protocol.js +15 -1
- package/dist/modules/functions/routes.d.ts +9 -1
- package/dist/modules/functions/routes.js +50 -25
- package/dist/modules/functions/runner/messages.d.ts +0 -1
- package/dist/modules/functions/runner/messages.js +46 -5
- package/dist/modules/functions/runner/remote.js +60 -8
- package/dist/modules/functions/runner/service.js +54 -8
- package/dist/modules/functions/sandbox/guest.js +5 -3
- package/dist/modules/functions/sandbox/isolate.js +7 -8
- package/dist/modules/functions/space.js +4 -4
- package/dist/modules/mongo/jobQueue.d.ts +3 -3
- package/dist/modules/mongo/jobQueue.js +137 -26
- package/dist/modules/mysql/jobs/jobQueue.d.ts +3 -3
- package/dist/modules/mysql/jobs/jobQueue.js +97 -33
- package/dist/modules/mysql/jobs/schema.js +38 -11
- package/dist/modules/realtime/handlers.d.ts +5 -2
- package/dist/modules/realtime/handlers.js +15 -4
- package/dist/modules/realtime/index.d.ts +11 -1
- package/dist/modules/realtime/index.js +8 -1
- package/dist/modules/realtime/jobs.d.ts +20 -0
- package/dist/modules/realtime/jobs.js +41 -0
- package/dist/modules/realtime/retained.d.ts +3 -2
- package/dist/modules/realtime/retained.js +2 -2
- package/dist/modules/realtime/socket.d.ts +4 -1
- package/dist/modules/realtime/socket.js +2 -2
- package/dist/modules/rsc/resolveRscData.js +2 -1
- package/dist/modules/runtime/host.js +1 -1
- package/dist/modules/runtime/runner.d.ts +1 -1
- package/dist/modules/runtime/runner.js +12 -2
- package/dist/modules/runtime/stages.js +1 -1
- package/dist/modules/ssr/applySSRResult.d.ts +5 -0
- package/dist/modules/ssr/applySSRResult.js +8 -1
- package/dist/modules/ssr/buildBody.js +2 -1
- package/dist/modules/ssr/prepareRender.d.ts +3 -0
- package/dist/modules/ssr/prepareRender.js +17 -5
- package/dist/modules/ssr/spaceDocument.d.ts +2 -1
- package/dist/modules/ssr/spaceDocument.js +14 -5
- package/dist/modules/ssr/streamBody.js +2 -1
- package/package.json +3 -3
- package/skills/plitzi-authoring/reference/auth.md +6 -0
- package/skills/plitzi-authoring/reference/authoring-errors.md +1 -0
- package/skills/plitzi-authoring/reference/plugin-reach.md +3 -0
- package/skills/plitzi-authoring/reference/realtime.md +4 -0
- package/dist/adapters/cloudAdapters.test.d.ts +0 -1
- package/dist/adapters/jsonAdapters.test.d.ts +0 -1
- package/dist/adapters/space/space.test.d.ts +0 -1
- package/dist/core/auth/api.test.d.ts +0 -1
- package/dist/core/auth/authorize.test.d.ts +0 -1
- package/dist/core/auth/createAuth.test.d.ts +0 -1
- package/dist/core/auth/csrf.test.d.ts +0 -1
- package/dist/core/auth/domains.test.d.ts +0 -1
- package/dist/core/auth/identity.test.d.ts +0 -1
- package/dist/core/auth/lifecycle.test.d.ts +0 -1
- package/dist/core/auth/oauth/oauth.test.d.ts +0 -1
- package/dist/core/auth/redirects.test.d.ts +0 -1
- package/dist/core/auth/routes.test.d.ts +0 -1
- package/dist/core/auth/session.test.d.ts +0 -1
- package/dist/core/auth/spaceTokens.test.d.ts +0 -1
- package/dist/core/auth/throttle.test.d.ts +0 -1
- package/dist/core/auth/tokens.test.d.ts +0 -1
- package/dist/core/auth/totp.test.d.ts +0 -1
- package/dist/core/configSeam.test.d.ts +0 -1
- package/dist/core/createServer.actions.test.d.ts +0 -1
- package/dist/core/createServer.functions.test.d.ts +0 -1
- package/dist/core/createServer.livePages.test.d.ts +0 -1
- package/dist/core/createServer.notFound.test.d.ts +0 -1
- package/dist/core/createServer.oauth.test.d.ts +0 -1
- package/dist/core/createServer.publicData.test.d.ts +0 -1
- package/dist/core/createServer.test.d.ts +0 -1
- package/dist/core/fontStore.test.d.ts +0 -1
- package/dist/core/freePort.test.d.ts +0 -1
- package/dist/core/handlers/csrfMiddleware.test.d.ts +0 -1
- package/dist/core/handlers/handlers.test.d.ts +0 -1
- package/dist/core/handlers/socialRouteHandlers.test.d.ts +0 -1
- package/dist/core/http/dispatcher.test.d.ts +0 -1
- package/dist/core/http/stages/crossSiteWrites.test.d.ts +0 -1
- package/dist/core/http/stages/exchangeRoute.test.d.ts +0 -1
- package/dist/core/http/stages/images.test.d.ts +0 -1
- package/dist/core/http/stages/middlewares.test.d.ts +0 -1
- package/dist/core/http/stages/sessionRenewal.test.d.ts +0 -1
- package/dist/core/http/stages/signInRoute.test.d.ts +0 -1
- package/dist/core/oauth/authorize.test.d.ts +0 -1
- package/dist/core/oauth/register.test.d.ts +0 -1
- package/dist/core/oauth/token.test.d.ts +0 -1
- package/dist/core/requestParser.test.d.ts +0 -1
- package/dist/core/server/baseServer.test.d.ts +0 -1
- package/dist/core/server/closeOnSignals.test.d.ts +0 -1
- package/dist/core/server/drain.test.d.ts +0 -1
- package/dist/core/server/fleet/channel.test.d.ts +0 -1
- package/dist/core/server/fleet/fixtures/fleetServer.d.ts +0 -1
- package/dist/core/server/fleet/fleet.integration.test.d.ts +0 -1
- package/dist/core/server/fleet/relay.test.d.ts +0 -1
- package/dist/core/server/fleet/role.test.d.ts +0 -1
- package/dist/core/server/fleet/supervisor.test.d.ts +0 -1
- package/dist/core/services/resolve.test.d.ts +0 -1
- package/dist/core/staticFiles.test.d.ts +0 -1
- package/dist/env.test.d.ts +0 -1
- package/dist/helpers/atomicFile.test.d.ts +0 -1
- package/dist/helpers/buildResponseHelpers.test.d.ts +0 -1
- package/dist/helpers/buildServerInfo.test.d.ts +0 -1
- package/dist/helpers/cache/CompressedFileCache.test.d.ts +0 -1
- package/dist/helpers/cache/cacheManager.test.d.ts +0 -1
- package/dist/helpers/cache/keys.test.d.ts +0 -1
- package/dist/helpers/compress.test.d.ts +0 -1
- package/dist/helpers/hydrationPayload.test.d.ts +0 -1
- package/dist/helpers/offlineDataLoader.test.d.ts +0 -1
- package/dist/helpers/onAbort.test.d.ts +0 -1
- package/dist/helpers/outboundGuard.test.d.ts +0 -1
- package/dist/helpers/reportReactBuild.test.d.ts +0 -1
- package/dist/helpers/resolveDebugMode.test.d.ts +0 -1
- package/dist/helpers/serverLog.test.d.ts +0 -1
- package/dist/hosts/authoringHost/index.test.d.ts +0 -1
- package/dist/hosts/project/serveProject.test.d.ts +0 -1
- package/dist/layers.test.d.ts +0 -1
- package/dist/middlewares/spaceDeployment.test.d.ts +0 -1
- package/dist/modules/actions/connectorWrite.test.d.ts +0 -1
- package/dist/modules/actions/jobs/failureStreak.test.d.ts +0 -1
- package/dist/modules/actions/jobs/later.test.d.ts +0 -1
- package/dist/modules/actions/jobs/memoryStores.test.d.ts +0 -1
- package/dist/modules/actions/jobs/scheduler.test.d.ts +0 -1
- package/dist/modules/actions/jobs/testing/jobQueueContract.d.ts +0 -25
- package/dist/modules/actions/jobs/worker.test.d.ts +0 -1
- package/dist/modules/actions/moduleFor.test.d.ts +0 -1
- package/dist/modules/actions/runtime/check.test.d.ts +0 -1
- package/dist/modules/actions/runtime/credentials.test.d.ts +0 -1
- package/dist/modules/actions/runtime/email.test.d.ts +0 -1
- package/dist/modules/actions/runtime/fileKv.test.d.ts +0 -1
- package/dist/modules/actions/runtime/flags.test.d.ts +0 -1
- package/dist/modules/actions/runtime/guards.test.d.ts +0 -1
- package/dist/modules/actions/runtime/kvStore.test.d.ts +0 -1
- package/dist/modules/actions/runtime/namespaceKv.test.d.ts +0 -1
- package/dist/modules/actions/runtime/redisKv.integration.test.d.ts +0 -1
- package/dist/modules/actions/runtime/renderResolver.test.d.ts +0 -1
- package/dist/modules/actions/runtime/renderShare.test.d.ts +0 -1
- package/dist/modules/actions/runtime/runAction.test.d.ts +0 -1
- package/dist/modules/actions/runtime/runLogger.test.d.ts +0 -1
- package/dist/modules/actions/runtime/signing.test.d.ts +0 -1
- package/dist/modules/actions/taskCatalog.test.d.ts +0 -1
- package/dist/modules/actions/tasks/db.test.d.ts +0 -1
- package/dist/modules/actions/tasks/email.test.d.ts +0 -1
- package/dist/modules/actions/tasks/http.test.d.ts +0 -1
- package/dist/modules/actions/tasks/kv.test.d.ts +0 -1
- package/dist/modules/actions/transport/callHandler.test.d.ts +0 -1
- package/dist/modules/actions/transport/webhookHandler.test.d.ts +0 -1
- package/dist/modules/connectors/engine.test.d.ts +0 -1
- package/dist/modules/connectors/projection.test.d.ts +0 -1
- package/dist/modules/connectors/resolver.test.d.ts +0 -1
- package/dist/modules/functions/build.test.d.ts +0 -1
- package/dist/modules/functions/load.test.d.ts +0 -1
- package/dist/modules/functions/local.test.d.ts +0 -1
- package/dist/modules/functions/manifest.test.d.ts +0 -1
- package/dist/modules/functions/native.test.d.ts +0 -1
- package/dist/modules/functions/oauth.test.d.ts +0 -1
- package/dist/modules/functions/plugins.test.d.ts +0 -1
- package/dist/modules/functions/routes.test.d.ts +0 -1
- package/dist/modules/functions/runner/remote.test.d.ts +0 -1
- package/dist/modules/functions/sandbox/isolate.test.d.ts +0 -1
- package/dist/modules/functions/testing/tasksOf.d.ts +0 -4
- package/dist/modules/images/imageProxy.test.d.ts +0 -1
- package/dist/modules/images/transform.test.d.ts +0 -1
- package/dist/modules/mongo/stores.integration.test.d.ts +0 -1
- package/dist/modules/mysql/accounts.test.d.ts +0 -1
- package/dist/modules/mysql/jobs/stores.integration.test.d.ts +0 -1
- package/dist/modules/mysql/schema.test.d.ts +0 -1
- package/dist/modules/mysql/store.integration.test.d.ts +0 -1
- package/dist/modules/realtime/realtime.test.d.ts +0 -1
- package/dist/modules/realtime/redisPubSub.test.d.ts +0 -1
- package/dist/modules/realtime/retained.test.d.ts +0 -1
- package/dist/modules/realtime/socket.test.d.ts +0 -1
- package/dist/modules/rsc/connectorRscData.test.d.ts +0 -1
- package/dist/modules/rsc/handler.test.d.ts +0 -1
- package/dist/modules/rsc/publicFileResolver.test.d.ts +0 -1
- package/dist/modules/rsc/resolveRscData.test.d.ts +0 -1
- package/dist/modules/runtime/elsewhere.test.d.ts +0 -1
- package/dist/modules/runtime/entry.test.d.ts +0 -1
- package/dist/modules/runtime/inside.test.d.ts +0 -1
- package/dist/modules/runtime/ownServer.test.d.ts +0 -1
- package/dist/modules/runtime/runtime.test.d.ts +0 -1
- package/dist/modules/sqlite/kv.test.d.ts +0 -1
- package/dist/modules/ssr/draftSession.test.d.ts +0 -1
- package/dist/modules/ssr/loadPluginComponents.test.d.ts +0 -1
- package/dist/modules/ssr/metering.test.d.ts +0 -1
- package/dist/modules/ssr/prepareRender.test.d.ts +0 -1
- package/dist/modules/ssr/redirect.test.d.ts +0 -1
- package/dist/modules/ssr/registerExternalPlugins.test.d.ts +0 -1
- package/dist/modules/ssr/reportMissingPlugins.test.d.ts +0 -1
- package/dist/modules/ssr/resolvePageSeo.test.d.ts +0 -1
- package/dist/modules/ssr/spaceDocument.test.d.ts +0 -1
- package/dist/modules/ssr/template.test.d.ts +0 -1
- package/dist/modules/ssr/testing/offlineData.d.ts +0 -5
- package/dist/modules/ssr/vendor.test.d.ts +0 -1
- package/dist/plugins/compile.test.d.ts +0 -1
- package/dist/plugins/copy.test.d.ts +0 -1
- package/dist/plugins/manager.test.d.ts +0 -1
- package/dist/plugins/validate.test.d.ts +0 -1
|
@@ -2,9 +2,11 @@ import { loadFunctions, loadFunctionsSource } from "../../modules/functions/load
|
|
|
2
2
|
import { PLUGIN_FUNCTIONS_SOURCE } from "@plitzi/sdk-shared/actions";
|
|
3
3
|
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import { existsSync, readFileSync, readdirSync, watch } from "node:fs";
|
|
5
|
+
import { existsSync, readFileSync, readdirSync, unwatchFile, watch, watchFile } from "node:fs";
|
|
6
6
|
import { PLUGINS_DIR, PLUGIN_FUNCTIONS_DIR, VENDOR_PLUGINS_DIR, checkPluginFolder, layoutFindingText, pluginEntry, pluginTypeOf, readVendorPlugin } from "@plitzi/sdk-shared/project";
|
|
7
7
|
//#region src/hosts/project/plugins.ts
|
|
8
|
+
/** How often a project with no `src/plugins` looks for it — the time between `plitzi plugin add` and the page. */
|
|
9
|
+
var FOLDER_POLL_MS = 500;
|
|
8
10
|
/**
|
|
9
11
|
* `action: 'compile'` is what makes a folder's plugin SERVER-rendered: the server builds the entry with esbuild, keeps
|
|
10
12
|
* React external so the plugin runs on the one copy the page already has, serves the bundle to the browser AND
|
|
@@ -94,12 +96,26 @@ var projectPlugins = async (root) => {
|
|
|
94
96
|
*/
|
|
95
97
|
var watchProjectPlugins = (root, server, plugins) => {
|
|
96
98
|
const dir = path.join(root, PLUGINS_DIR);
|
|
97
|
-
if (
|
|
99
|
+
if (existsSync(dir)) return watchPluginsFolder(root, dir, server, plugins, false);
|
|
100
|
+
let stop;
|
|
101
|
+
const appeared = (now) => {
|
|
102
|
+
if (stop || !now.isDirectory()) return;
|
|
103
|
+
unwatchFile(dir, appeared);
|
|
104
|
+
stop = watchPluginsFolder(root, dir, server, plugins, true);
|
|
105
|
+
};
|
|
106
|
+
watchFile(dir, { interval: FOLDER_POLL_MS }, appeared);
|
|
107
|
+
return () => {
|
|
108
|
+
unwatchFile(dir, appeared);
|
|
109
|
+
stop?.();
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
/** `late`: the folder appeared after boot, so what it holds already is new — said and registered at once. */
|
|
113
|
+
var watchPluginsFolder = (root, dir, server, plugins, late) => {
|
|
98
114
|
const { names, fromFolders } = plugins;
|
|
99
115
|
let timer;
|
|
100
116
|
const reloading = /* @__PURE__ */ new Map();
|
|
101
117
|
/** What was said of each folder last — at boot, by the layout check — so a save says only what changed. */
|
|
102
|
-
const said = new Map(foldersOf(dir).map((folder) => [folder, checkPluginFolder(root, folder).map(layoutFindingText).join("\n")]));
|
|
118
|
+
const said = new Map(late ? [] : foldersOf(dir).map((folder) => [folder, checkPluginFolder(root, folder).map(layoutFindingText).join("\n")]));
|
|
103
119
|
const sayOf = (folder) => {
|
|
104
120
|
const findings = checkPluginFolder(root, folder);
|
|
105
121
|
const text = findings.map(layoutFindingText).join("\n");
|
|
@@ -151,6 +167,7 @@ var watchProjectPlugins = (root, server, plugins) => {
|
|
|
151
167
|
clearTimeout(timer);
|
|
152
168
|
timer = setTimeout(sync, 300);
|
|
153
169
|
});
|
|
170
|
+
if (late) timer = setTimeout(sync, 300);
|
|
154
171
|
return () => {
|
|
155
172
|
clearTimeout(timer);
|
|
156
173
|
reloading.forEach(clearTimeout);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { consoleLogger } from "../../helpers/serverLog.js";
|
|
2
2
|
import { isFleetWorker } from "../../core/server/fleet/role.js";
|
|
3
|
+
import { OAUTH_CALLBACK_PATH } from "../../modules/functions/oauth.js";
|
|
3
4
|
import { loadFunctions } from "../../modules/functions/load.js";
|
|
4
5
|
import { createServer as createServer$1 } from "../../core/createServer.js";
|
|
5
6
|
import { closeOnSignals } from "../../core/server/closeOnSignals.js";
|
|
@@ -15,6 +16,7 @@ import { projectPlugins, watchProjectPlugins } from "./plugins.js";
|
|
|
15
16
|
import { createSpaceFailure } from "./spaceFailure.js";
|
|
16
17
|
import { watchSpace } from "./watchSpace.js";
|
|
17
18
|
import { fleetKv } from "../../core/server/fleet/stores.js";
|
|
19
|
+
import { FUNCTION_ROUTES_PREFIX } from "@plitzi/sdk-shared/actions";
|
|
18
20
|
import { X509Certificate } from "node:crypto";
|
|
19
21
|
import { hostname, networkInterfaces } from "node:os";
|
|
20
22
|
import { isIP } from "node:net";
|
|
@@ -70,6 +72,16 @@ var LOOPBACK = /* @__PURE__ */ new Set([
|
|
|
70
72
|
"localhost"
|
|
71
73
|
]);
|
|
72
74
|
var ANY_ADDRESS = /* @__PURE__ */ new Set(["0.0.0.0", "::"]);
|
|
75
|
+
/**
|
|
76
|
+
* Where OAuth providers send a visitor back (`ctx.oauth`), the one address to register whichever this server was
|
|
77
|
+
* opened on: where people reach it, unless that is plain HTTP past this machine — a provider takes `http` on loopback
|
|
78
|
+
* alone — and then this machine's loopback, never `localhost`, which providers refuse too (RFC 8252).
|
|
79
|
+
*/
|
|
80
|
+
var oauthCallbackOrigin = (publicUrl, localUrl) => {
|
|
81
|
+
const reached = new URL(publicUrl);
|
|
82
|
+
const loopback = reached.hostname === "127.0.0.1" || reached.hostname === "[::1]";
|
|
83
|
+
return reached.protocol === "https:" || loopback ? reached.origin : new URL(localUrl).origin;
|
|
84
|
+
};
|
|
73
85
|
/** An address as a URL writes it: an IPv6 one in brackets. */
|
|
74
86
|
var urlHost = (address) => address.includes(":") ? `[${address}]` : address;
|
|
75
87
|
/** Where this machine reaches a server listening on `host`: its loopback, unless it listens on one address only. */
|
|
@@ -205,6 +217,7 @@ var startProject = async (options) => {
|
|
|
205
217
|
}) : cloudAdapters(plugins.names);
|
|
206
218
|
const functionsDir = path.join(root, FUNCTIONS_DIR);
|
|
207
219
|
const functions = existsSync(functionsDir) ? await loadFunctions(functionsDir) : [];
|
|
220
|
+
const callbackOrigin = oauthCallbackOrigin(publicUrl, localUrl);
|
|
208
221
|
const runtimeBundle = path.join(root, RUNTIME_BUNDLE);
|
|
209
222
|
const spaceRuntime = await loadRuntimeModule(projectModule(root, RUNTIME_ENTRY, process.argv[1])) ?? (existsSync(runtimeBundle) ? await loadRuntime(readFileSync(runtimeBundle), path.join(root, PROJECT_TMP, "runtime")) : void 0);
|
|
210
223
|
const runtime = spaceRuntime && await serveRuntime(spaceRuntime, {
|
|
@@ -234,7 +247,8 @@ var startProject = async (options) => {
|
|
|
234
247
|
dataDir: path.join(root, DATA_DIR),
|
|
235
248
|
functions: {
|
|
236
249
|
native: [...functions, ...runtime?.native ?? []],
|
|
237
|
-
plugins: plugins.functions
|
|
250
|
+
plugins: plugins.functions,
|
|
251
|
+
oauth: { callbackOrigin }
|
|
238
252
|
},
|
|
239
253
|
action: {
|
|
240
254
|
signingSecret: process.env.PLITZI_SIGNING_SECRET,
|
|
@@ -259,6 +273,7 @@ var startProject = async (options) => {
|
|
|
259
273
|
console.log(`pages on ${localUrl}/`);
|
|
260
274
|
if (network.length > 0) console.log(`also on ${network.map((address) => `${address}/`).join(", ")} — anyone on this network can open it (HOST=${host})`);
|
|
261
275
|
else if (developing && LOOPBACK.has(host)) console.log("only this machine opens it: HOST=0.0.0.0 in .env opens it to your network (a phone on the Wi-Fi)");
|
|
276
|
+
if ([...functions, ...runtime?.native ?? []].some((definition) => Object.keys(definition.oauth ?? {}).length > 0)) console.log(`OAuth providers send visitors back to ${callbackOrigin}${FUNCTION_ROUTES_PREFIX}${OAUTH_CALLBACK_PATH} — the address to register with each (PUBLIC_URL changes it)`);
|
|
262
277
|
if (reached.unnamed.length > 0) console.warn(`[tls] the certificate does not name ${reached.unnamed.join(", ")}: a device that opens it there is refused — \`plitzi cert\` makes one for this machine as it is now`);
|
|
263
278
|
}
|
|
264
279
|
const watching = developing ? [...held ? [watchSpace(root, (authored) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import "./jobs/memoryQueue.js";
|
|
2
|
+
import "./jobs/schedules.js";
|
|
2
3
|
import "./jobs/scheduler.js";
|
|
3
4
|
import "./runtime/errors.js";
|
|
4
5
|
import "./jobs/worker.js";
|
|
@@ -11,9 +12,10 @@ import { KV_METHODS, createMemoryKv } from "./runtime/memoryKv.js";
|
|
|
11
12
|
import { namespaceKv, spaceKvPatterns } from "./runtime/namespaceKv.js";
|
|
12
13
|
import "./runtime/precheck.js";
|
|
13
14
|
import { createActionRunner } from "./runtime/runAction.js";
|
|
14
|
-
import { createTaskRegistry } from "./tasks/registry.js";
|
|
15
|
-
import { functionCeilings } from "../functions/config.js";
|
|
16
15
|
import { readManifest } from "../functions/manifest.js";
|
|
16
|
+
import { functionCeilings } from "../functions/config.js";
|
|
17
|
+
import { createFunctionJobs } from "../functions/jobs.js";
|
|
18
|
+
import { createTaskRegistry } from "./tasks/registry.js";
|
|
17
19
|
import { createRoutes } from "../functions/routes.js";
|
|
18
20
|
import { createSpaceRegistries, prepareFunctions } from "../functions/space.js";
|
|
19
21
|
import "./runtime/check.js";
|
|
@@ -65,7 +67,16 @@ var createActionsModule = (given) => {
|
|
|
65
67
|
};
|
|
66
68
|
const registryForRun = (request) => Object.values(request.entry.document.nodes).every((node) => node.type !== "task" || !node.action || registry.get(node.action)) ? registry : registryFor(request.spaceId, request.at);
|
|
67
69
|
let jobsQueue;
|
|
68
|
-
const {
|
|
70
|
+
const { realtime } = config;
|
|
71
|
+
const functionJobs = createFunctionJobs({
|
|
72
|
+
queue: () => jobsQueue,
|
|
73
|
+
config: config.functions ?? {},
|
|
74
|
+
...config.lookups.getFunctions ? { getFunctions: config.lookups.getFunctions } : {},
|
|
75
|
+
taskContext: () => taskContext,
|
|
76
|
+
...realtime?.publishJob ? { publish: realtime.publishJob } : {},
|
|
77
|
+
maxAttempts: (config.jobs === false ? void 0 : config.jobs?.maxAttempts) ?? 3
|
|
78
|
+
});
|
|
79
|
+
const { runAction, taskContext } = createActionRunner(config, registryForRun, config.fetchImpl, () => jobsQueue, functionJobs);
|
|
69
80
|
const routes = createRoutes({
|
|
70
81
|
config: config.functions ?? {},
|
|
71
82
|
plugins: () => plugins,
|
|
@@ -107,8 +118,10 @@ var createActionsModule = (given) => {
|
|
|
107
118
|
}
|
|
108
119
|
};
|
|
109
120
|
if (config.jobs !== false && config.lookups.listActions) {
|
|
110
|
-
module.jobs = createActionJobs(config.jobs ?? {}, module, config.lookups);
|
|
121
|
+
module.jobs = createActionJobs(config.jobs ?? {}, module, config.lookups, functionJobs);
|
|
111
122
|
jobsQueue = module.jobs.queue;
|
|
123
|
+
const queue = module.jobs.queue;
|
|
124
|
+
realtime?.watchJobs?.((spaceId, jobId) => queue.getJob([spaceId], jobId));
|
|
112
125
|
}
|
|
113
126
|
return module;
|
|
114
127
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Scheduler } from './scheduler';
|
|
2
2
|
import { JobWorker } from './worker';
|
|
3
|
+
import { FunctionJobsService } from '../../functions/jobs';
|
|
3
4
|
import { ActionsModule } from '../index';
|
|
4
5
|
import { ActionJobsConfig, ActionLookups } from '../types';
|
|
5
|
-
import {
|
|
6
|
+
import { JobQueue } from '@plitzi/sdk-shared';
|
|
6
7
|
export type ActionJobs = {
|
|
7
|
-
queue:
|
|
8
|
+
queue: JobQueue;
|
|
8
9
|
scheduler: Scheduler;
|
|
9
10
|
worker: JobWorker;
|
|
10
11
|
start: () => void;
|
|
@@ -23,7 +24,7 @@ export type ActionJobs = {
|
|
|
23
24
|
* works, and the nightly email goes out once per replica. The workers of one server are one replica: they share
|
|
24
25
|
* the primary's queue, and only one of them runs the scheduler and the jobs (`runsFleetJobs`).
|
|
25
26
|
*/
|
|
26
|
-
export declare const createActionJobs: (config: ActionJobsConfig, module: ActionsModule, lookups: ActionLookups) => ActionJobs;
|
|
27
|
+
export declare const createActionJobs: (config: ActionJobsConfig, module: ActionsModule, lookups: ActionLookups, functionJobs?: FunctionJobsService) => ActionJobs;
|
|
27
28
|
export { createMemoryJobQueue } from './memoryQueue';
|
|
28
29
|
export { createScheduler } from './scheduler';
|
|
29
30
|
export { createJobWorker } from './worker';
|
|
@@ -17,7 +17,7 @@ import { fleetStore } from "../../../core/server/fleet/link.js";
|
|
|
17
17
|
* works, and the nightly email goes out once per replica. The workers of one server are one replica: they share
|
|
18
18
|
* the primary's queue, and only one of them runs the scheduler and the jobs (`runsFleetJobs`).
|
|
19
19
|
*/
|
|
20
|
-
var createActionJobs = (config, module, lookups) => {
|
|
20
|
+
var createActionJobs = (config, module, lookups, functionJobs) => {
|
|
21
21
|
const queue = config.queue ?? fleetStore("actions.jobs", JOB_QUEUE_METHODS) ?? createMemoryJobQueue();
|
|
22
22
|
const { produce = true, workers = 4 } = config;
|
|
23
23
|
const scheduler = createScheduler({
|
|
@@ -34,6 +34,7 @@ var createActionJobs = (config, module, lookups) => {
|
|
|
34
34
|
queue,
|
|
35
35
|
lookups,
|
|
36
36
|
module,
|
|
37
|
+
...functionJobs ? { functionJobs } : {},
|
|
37
38
|
workers,
|
|
38
39
|
...config.pollMs ? { pollMs: config.pollMs } : {},
|
|
39
40
|
...config.leaseMs ? { leaseMs: config.leaseMs } : {},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ActionLookups } from '../types';
|
|
2
|
-
import {
|
|
2
|
+
import { JobQueue, Environment, SpaceRevision } from '@plitzi/sdk-shared';
|
|
3
3
|
/** A month: past it, a run set for later is a schedule, and a cron says it better. */
|
|
4
4
|
export declare const MAX_LATER_SECONDS: number;
|
|
5
5
|
/** What a run asks to start later: one of the space's actions, in so many seconds, with its input — and a name. */
|
|
@@ -22,7 +22,7 @@ export type LaterAnswer = {
|
|
|
22
22
|
};
|
|
23
23
|
export type LaterDeps = {
|
|
24
24
|
/** Read when asked: the queue is made after the runner, and a server may run none. */
|
|
25
|
-
queue: () =>
|
|
25
|
+
queue: () => JobQueue | undefined;
|
|
26
26
|
lookups: Pick<ActionLookups, 'getAction'>;
|
|
27
27
|
maxAttempts: number;
|
|
28
28
|
};
|
|
@@ -37,9 +37,12 @@ var laterFor = ({ queue, lookups, maxAttempts }, { spaceId, environment, at }) =
|
|
|
37
37
|
await jobs.enqueue({
|
|
38
38
|
id,
|
|
39
39
|
spaceId,
|
|
40
|
-
actionId: entry.id,
|
|
41
40
|
environment,
|
|
42
|
-
|
|
41
|
+
target: {
|
|
42
|
+
kind: "action",
|
|
43
|
+
name: entry.id,
|
|
44
|
+
trigger: "later"
|
|
45
|
+
},
|
|
43
46
|
input,
|
|
44
47
|
dueAt,
|
|
45
48
|
maxAttempts,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StoreMethods } from '../../../core/server/fleet/channel';
|
|
2
|
-
import {
|
|
2
|
+
import { JobQueue } from '@plitzi/sdk-shared';
|
|
3
3
|
export type MemoryJobQueueOptions = {
|
|
4
4
|
/**
|
|
5
5
|
* How many finished jobs to keep before the oldest are dropped.
|
|
@@ -18,16 +18,16 @@ export type MemoryJobQueueOptions = {
|
|
|
18
18
|
clock?: () => number;
|
|
19
19
|
};
|
|
20
20
|
/** Every method of the queue, for the copy a server's workers share (see `fleet/stores.ts`). */
|
|
21
|
-
export declare const JOB_QUEUE_METHODS: StoreMethods<
|
|
21
|
+
export declare const JOB_QUEUE_METHODS: StoreMethods<JobQueue>;
|
|
22
22
|
/**
|
|
23
23
|
* The default queue: one process's Maps.
|
|
24
24
|
*
|
|
25
25
|
* Correct for a deployment that runs ONE replica, which is most self-hosted ones, and honest about the rest — a
|
|
26
26
|
* second replica running on this gets its own schedules and its own jobs, so a nightly digest goes out twice. The
|
|
27
|
-
* cure is any store the replicas share, behind the same {@link
|
|
27
|
+
* cure is any store the replicas share, behind the same {@link JobQueue} seam.
|
|
28
28
|
*
|
|
29
29
|
* It keeps the contract exactly, rather than approximately: enqueue is idempotent by id, a claim reaps lapsed
|
|
30
30
|
* leases, and every instant is read here rather than accepted from a caller. A test that passes against this one
|
|
31
31
|
* is testing the rules a shared store has to keep too.
|
|
32
32
|
*/
|
|
33
|
-
export declare const createMemoryJobQueue: ({ retainFinished, clock }?: MemoryJobQueueOptions) =>
|
|
33
|
+
export declare const createMemoryJobQueue: ({ retainFinished, clock }?: MemoryJobQueueOptions) => JobQueue;
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
+
import { isTerminalJobStatus } from "@plitzi/sdk-shared/actions";
|
|
1
2
|
//#region src/modules/actions/jobs/memoryQueue.ts
|
|
2
|
-
var TERMINAL = /* @__PURE__ */ new Set([
|
|
3
|
-
"succeeded",
|
|
4
|
-
"failed",
|
|
5
|
-
"dead",
|
|
6
|
-
"cancelled"
|
|
7
|
-
]);
|
|
8
3
|
/** Every method of the queue, for the copy a server's workers share (see `fleet/stores.ts`). */
|
|
9
4
|
var JOB_QUEUE_METHODS = {
|
|
10
5
|
now: true,
|
|
@@ -12,6 +7,7 @@ var JOB_QUEUE_METHODS = {
|
|
|
12
7
|
claim: true,
|
|
13
8
|
heartbeat: true,
|
|
14
9
|
settle: true,
|
|
10
|
+
write: true,
|
|
15
11
|
dueSchedules: true,
|
|
16
12
|
advanceSchedule: true,
|
|
17
13
|
putSchedules: true,
|
|
@@ -20,6 +16,8 @@ var JOB_QUEUE_METHODS = {
|
|
|
20
16
|
listSchedules: true,
|
|
21
17
|
requeue: true,
|
|
22
18
|
cancel: true,
|
|
19
|
+
pause: true,
|
|
20
|
+
resume: true,
|
|
23
21
|
cancelPending: true
|
|
24
22
|
};
|
|
25
23
|
/**
|
|
@@ -27,7 +25,7 @@ var JOB_QUEUE_METHODS = {
|
|
|
27
25
|
*
|
|
28
26
|
* Correct for a deployment that runs ONE replica, which is most self-hosted ones, and honest about the rest — a
|
|
29
27
|
* second replica running on this gets its own schedules and its own jobs, so a nightly digest goes out twice. The
|
|
30
|
-
* cure is any store the replicas share, behind the same {@link
|
|
28
|
+
* cure is any store the replicas share, behind the same {@link JobQueue} seam.
|
|
31
29
|
*
|
|
32
30
|
* It keeps the contract exactly, rather than approximately: enqueue is idempotent by id, a claim reaps lapsed
|
|
33
31
|
* leases, and every instant is read here rather than accepted from a caller. A test that passes against this one
|
|
@@ -40,7 +38,7 @@ var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) =>
|
|
|
40
38
|
const now = () => clock();
|
|
41
39
|
/** Oldest finished jobs first, so the bound drops history rather than anything anybody is waiting on. */
|
|
42
40
|
const evict = () => {
|
|
43
|
-
const finished = [...jobs.values()].filter((job) =>
|
|
41
|
+
const finished = [...jobs.values()].filter((job) => isTerminalJobStatus(job.status)).sort((a, b) => a.updatedAt - b.updatedAt);
|
|
44
42
|
for (const job of finished.slice(0, finished.length - retainFinished)) jobs.delete(job.id);
|
|
45
43
|
};
|
|
46
44
|
const owned = (spaceIds, jobId) => {
|
|
@@ -95,16 +93,35 @@ var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) =>
|
|
|
95
93
|
},
|
|
96
94
|
heartbeat: ({ jobIds, workerId, leaseMs }) => {
|
|
97
95
|
const at = now();
|
|
98
|
-
const
|
|
96
|
+
const stops = [];
|
|
99
97
|
for (const id of jobIds) {
|
|
100
98
|
const job = jobs.get(id);
|
|
101
99
|
if (!job || job.workerId !== workerId || job.status !== "running") continue;
|
|
102
100
|
job.leaseUntil = at + leaseMs;
|
|
103
|
-
if (job.cancelRequested)
|
|
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
|
+
});
|
|
104
109
|
}
|
|
105
|
-
return Promise.resolve(
|
|
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);
|
|
106
123
|
},
|
|
107
|
-
settle: ({ jobId, workerId, status, delayMs, runId, error, attempt }) => {
|
|
124
|
+
settle: ({ jobId, workerId, status, delayMs, runId, error, attempt, result, spentMs, refund = status === "pending" && !attempt }) => {
|
|
108
125
|
const job = jobs.get(jobId);
|
|
109
126
|
if (!job || job.workerId !== workerId) return Promise.resolve();
|
|
110
127
|
const at = now();
|
|
@@ -113,16 +130,18 @@ var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) =>
|
|
|
113
130
|
startedAt: job.updatedAt,
|
|
114
131
|
endedAt: at
|
|
115
132
|
});
|
|
116
|
-
job.status = status;
|
|
133
|
+
job.status = status === "pending" && job.cancelRequested ? "cancelled" : status === "pending" && job.pauseRequested ? "paused" : status;
|
|
117
134
|
job.updatedAt = at;
|
|
118
135
|
job.leaseUntil = void 0;
|
|
119
136
|
job.error = error;
|
|
137
|
+
job.cancelRequested = void 0;
|
|
138
|
+
job.pauseRequested = void 0;
|
|
120
139
|
if (runId) job.runId = runId;
|
|
121
|
-
if (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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;
|
|
126
145
|
evict();
|
|
127
146
|
return Promise.resolve();
|
|
128
147
|
},
|
|
@@ -157,8 +176,8 @@ var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) =>
|
|
|
157
176
|
}
|
|
158
177
|
return Promise.resolve();
|
|
159
178
|
},
|
|
160
|
-
listJobs: ({ spaceIds,
|
|
161
|
-
const matching = [...jobs.values()].filter((job) => spaceIds.includes(job.spaceId) && (
|
|
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);
|
|
162
181
|
return Promise.resolve({
|
|
163
182
|
jobs: matching.slice(offset, offset + limit).map((job) => ({ ...job })),
|
|
164
183
|
total: matching.length
|
|
@@ -166,29 +185,54 @@ var createMemoryJobQueue = ({ retainFinished = 500, clock = Date.now } = {}) =>
|
|
|
166
185
|
},
|
|
167
186
|
getJob: (spaceIds, jobId) => Promise.resolve(owned(spaceIds, jobId)),
|
|
168
187
|
listSchedules: (spaceIds) => Promise.resolve([...schedules.values()].filter((schedule) => spaceIds.includes(schedule.spaceId))),
|
|
169
|
-
requeue: (spaceIds, jobId) => {
|
|
188
|
+
requeue: (spaceIds, jobId, { input, fresh = false } = {}) => {
|
|
170
189
|
const job = owned(spaceIds, jobId);
|
|
171
|
-
if (!job || !
|
|
190
|
+
if (!job || !isTerminalJobStatus(job.status)) return Promise.resolve(false);
|
|
172
191
|
job.status = "pending";
|
|
173
192
|
job.runAt = now();
|
|
174
193
|
job.attempts = 0;
|
|
175
194
|
job.workerId = void 0;
|
|
176
195
|
job.leaseUntil = void 0;
|
|
177
|
-
job.cancelRequested =
|
|
196
|
+
job.cancelRequested = void 0;
|
|
197
|
+
job.pauseRequested = void 0;
|
|
178
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
|
+
}
|
|
179
206
|
job.updatedAt = now();
|
|
180
207
|
return Promise.resolve(true);
|
|
181
208
|
},
|
|
182
209
|
cancel: (spaceIds, jobId) => {
|
|
183
210
|
const job = owned(spaceIds, jobId);
|
|
184
|
-
if (!job ||
|
|
185
|
-
if (job.status === "pending") {
|
|
211
|
+
if (!job || isTerminalJobStatus(job.status)) return Promise.resolve(false);
|
|
212
|
+
if (job.status === "pending" || job.status === "paused") {
|
|
186
213
|
job.status = "cancelled";
|
|
187
214
|
job.workerId = void 0;
|
|
215
|
+
job.pauseRequested = void 0;
|
|
188
216
|
} else job.cancelRequested = true;
|
|
189
217
|
job.updatedAt = now();
|
|
190
218
|
return Promise.resolve(true);
|
|
191
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
|
+
},
|
|
192
236
|
cancelPending: ({ spaceId, key, olderThan }) => {
|
|
193
237
|
const newest = olderThan === void 0 ? void 0 : jobs.get(olderThan);
|
|
194
238
|
const isOlder = (job) => newest === void 0 || job.createdAt < newest.createdAt || job.createdAt === newest.createdAt && job.id < newest.id;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ActionLookups } from '../types';
|
|
2
|
-
import {
|
|
2
|
+
import { JobQueue, Environment } from '@plitzi/sdk-shared';
|
|
3
3
|
export type SchedulerOptions = {
|
|
4
|
-
queue:
|
|
4
|
+
queue: JobQueue;
|
|
5
5
|
lookups: ActionLookups;
|
|
6
6
|
/** Which environment scheduled runs execute in. Defaults to `main`, the document the builder edits. */
|
|
7
7
|
environment?: Environment;
|
|
@@ -37,9 +37,12 @@ var createScheduler = ({ queue, lookups, environment = "main", pollMs = 15e3, ba
|
|
|
37
37
|
(await queue.enqueue({
|
|
38
38
|
id,
|
|
39
39
|
spaceId,
|
|
40
|
-
actionId,
|
|
41
40
|
environment: schedule.environment,
|
|
42
|
-
|
|
41
|
+
target: {
|
|
42
|
+
kind: "action",
|
|
43
|
+
name: actionId,
|
|
44
|
+
trigger: "schedule"
|
|
45
|
+
},
|
|
43
46
|
input: {},
|
|
44
47
|
dueAt: fireAt,
|
|
45
48
|
maxAttempts: schedule.maxAttempts
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
import { FunctionJobsService } from '../../functions/jobs';
|
|
1
2
|
import { ActionsModule } from '../index';
|
|
2
3
|
import { ActionLookups } from '../types';
|
|
3
|
-
import {
|
|
4
|
+
import { JobQueue } from '@plitzi/sdk-shared';
|
|
4
5
|
export type JobWorkerOptions = {
|
|
5
|
-
queue:
|
|
6
|
+
queue: JobQueue;
|
|
6
7
|
lookups: ActionLookups;
|
|
7
8
|
module: ActionsModule;
|
|
9
|
+
/** What runs a job of a space's functions (`function` on the job), slice by slice. Absent, such a job waits. */
|
|
10
|
+
functionJobs?: FunctionJobsService;
|
|
8
11
|
/**
|
|
9
12
|
* How many jobs this replica runs AT ONCE.
|
|
10
13
|
*
|
|
@@ -61,4 +64,4 @@ export type JobWorker = {
|
|
|
61
64
|
stop: () => Promise<void>;
|
|
62
65
|
inFlight: () => number;
|
|
63
66
|
};
|
|
64
|
-
export declare const createJobWorker: ({ queue, lookups, module, workers, pollMs, leaseMs, backoff, workerId, onError: customOnError }: JobWorkerOptions) => JobWorker;
|
|
67
|
+
export declare const createJobWorker: ({ queue, lookups, module, functionJobs, workers, pollMs, leaseMs, backoff, workerId, onError: customOnError }: JobWorkerOptions) => JobWorker;
|
|
@@ -33,12 +33,14 @@ var GIVE_UP = /* @__PURE__ */ new Set([
|
|
|
33
33
|
"forbidden",
|
|
34
34
|
"recursion"
|
|
35
35
|
]);
|
|
36
|
-
var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leaseMs = 3e4, backoff = {}, workerId = `${process.pid}-${randomUUID().slice(0, 8)}`, onError: customOnError }) => {
|
|
36
|
+
var createJobWorker = ({ queue, lookups, module, functionJobs, workers = 4, pollMs = 1e3, leaseMs = 3e4, backoff = {}, workerId = `${process.pid}-${randomUUID().slice(0, 8)}`, onError: customOnError }) => {
|
|
37
37
|
const onError = customOnError ?? ((error) => serverLog.error("Actions", "job worker failed", error));
|
|
38
38
|
const claimFailures = createFailureStreak("Actions", "job claim");
|
|
39
39
|
const heartbeatFailures = createFailureStreak("Actions", "job heartbeat");
|
|
40
40
|
const { baseMs = 3e4, maxMs = 9e5 } = backoff;
|
|
41
41
|
const active = /* @__PURE__ */ new Map();
|
|
42
|
+
/** Of `active`, the jobs of a space's functions: what a stop asks to save and hand back, rather than waits for. */
|
|
43
|
+
const slicing = /* @__PURE__ */ new Set();
|
|
42
44
|
/** Every job this worker started, until it has settled — what `stop()` waits for. */
|
|
43
45
|
const inFlight = /* @__PURE__ */ new Set();
|
|
44
46
|
let timer;
|
|
@@ -66,7 +68,7 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
66
68
|
delayMs,
|
|
67
69
|
error
|
|
68
70
|
});
|
|
69
|
-
const fail = (job, runId, error) => {
|
|
71
|
+
const fail = (job, runId, error, { spentMs, refused = false } = {}) => {
|
|
70
72
|
const exhausted = job.attempts >= job.maxAttempts;
|
|
71
73
|
return settle({
|
|
72
74
|
jobId: job.id,
|
|
@@ -74,17 +76,108 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
74
76
|
status: exhausted ? "dead" : "pending",
|
|
75
77
|
...exhausted ? {} : { delayMs: retryDelay(job.attempts) },
|
|
76
78
|
...runId ? { runId } : {},
|
|
79
|
+
...spentMs ? { spentMs } : {},
|
|
77
80
|
error,
|
|
78
81
|
attempt: {
|
|
79
82
|
status: "failed",
|
|
80
83
|
workerId,
|
|
81
84
|
...runId ? { runId } : {},
|
|
82
|
-
error
|
|
85
|
+
error,
|
|
86
|
+
...refused ? { refused } : {}
|
|
83
87
|
}
|
|
84
88
|
});
|
|
85
89
|
};
|
|
90
|
+
/**
|
|
91
|
+
* One slice of a job of a space's functions, settled by how it ended: done, set aside, cancelled, handed back to go on
|
|
92
|
+
* in the next slice — or failed, to be tried again from what it saved.
|
|
93
|
+
*/
|
|
94
|
+
const executeSlice = async (job, jobs) => {
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
active.set(job.id, controller);
|
|
97
|
+
slicing.add(job.id);
|
|
98
|
+
try {
|
|
99
|
+
await jobs.announce(job.spaceId, job.id);
|
|
100
|
+
const outcome = await jobs.slice(job, controller.signal, workerId);
|
|
101
|
+
const base = {
|
|
102
|
+
jobId: job.id,
|
|
103
|
+
workerId
|
|
104
|
+
};
|
|
105
|
+
switch (outcome.kind) {
|
|
106
|
+
case "wait":
|
|
107
|
+
await release(job, outcome.delayMs, outcome.why);
|
|
108
|
+
break;
|
|
109
|
+
case "done":
|
|
110
|
+
await settle({
|
|
111
|
+
...base,
|
|
112
|
+
status: "succeeded",
|
|
113
|
+
result: outcome.result,
|
|
114
|
+
spentMs: outcome.spentMs,
|
|
115
|
+
attempt: {
|
|
116
|
+
status: "succeeded",
|
|
117
|
+
workerId
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
case "failed":
|
|
122
|
+
if (outcome.final) await settle({
|
|
123
|
+
...base,
|
|
124
|
+
status: "failed",
|
|
125
|
+
spentMs: outcome.spentMs,
|
|
126
|
+
error: outcome.error,
|
|
127
|
+
attempt: {
|
|
128
|
+
status: "failed",
|
|
129
|
+
workerId,
|
|
130
|
+
error: outcome.error,
|
|
131
|
+
...outcome.refused ? { refused: true } : {}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
else await fail(job, void 0, outcome.error, { spentMs: outcome.spentMs });
|
|
135
|
+
break;
|
|
136
|
+
case "stopped": if (outcome.stop === "pause") await settle({
|
|
137
|
+
...base,
|
|
138
|
+
status: "paused",
|
|
139
|
+
refund: true,
|
|
140
|
+
spentMs: outcome.spentMs,
|
|
141
|
+
attempt: {
|
|
142
|
+
status: "paused",
|
|
143
|
+
workerId
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
else if (outcome.stop === "cancel") await settle({
|
|
147
|
+
...base,
|
|
148
|
+
status: "cancelled",
|
|
149
|
+
spentMs: outcome.spentMs,
|
|
150
|
+
attempt: {
|
|
151
|
+
status: "cancelled",
|
|
152
|
+
workerId
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
else if (outcome.stop === "shutdown" || outcome.saved) await settle({
|
|
156
|
+
...base,
|
|
157
|
+
status: "pending",
|
|
158
|
+
delayMs: 0,
|
|
159
|
+
refund: true,
|
|
160
|
+
spentMs: outcome.spentMs
|
|
161
|
+
});
|
|
162
|
+
else await fail(job, void 0, "its slice ran out of time without saving progress — job.save where it is, to go on in the next one", { spentMs: outcome.spentMs });
|
|
163
|
+
}
|
|
164
|
+
} catch (error) {
|
|
165
|
+
await fail(job, void 0, messageOf(error));
|
|
166
|
+
} finally {
|
|
167
|
+
active.delete(job.id);
|
|
168
|
+
slicing.delete(job.id);
|
|
169
|
+
await jobs.announce(job.spaceId, job.id);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
86
172
|
const execute = async (job) => {
|
|
87
|
-
const
|
|
173
|
+
const { target } = job;
|
|
174
|
+
if (target.kind === "function") {
|
|
175
|
+
if (functionJobs) await executeSlice(job, functionJobs);
|
|
176
|
+
else await release(job, retryDelay(1), "not started: this replica runs no jobs of the spaces’ functions");
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const { name: actionId, trigger } = target;
|
|
180
|
+
const entry = await lookups.getAction(job.spaceId, actionId);
|
|
88
181
|
if (!entry) {
|
|
89
182
|
await settle({
|
|
90
183
|
jobId: job.id,
|
|
@@ -103,8 +196,8 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
103
196
|
try {
|
|
104
197
|
run = await module.guards.begin({
|
|
105
198
|
spaceId: job.spaceId,
|
|
106
|
-
actionId
|
|
107
|
-
callerId:
|
|
199
|
+
actionId,
|
|
200
|
+
callerId: trigger,
|
|
108
201
|
input: job.input,
|
|
109
202
|
idempotencyKey: job.id,
|
|
110
203
|
sharedKey: true,
|
|
@@ -138,8 +231,8 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
138
231
|
input: job.input,
|
|
139
232
|
spaceId: job.spaceId,
|
|
140
233
|
environment: job.environment,
|
|
141
|
-
trigger
|
|
142
|
-
callerId:
|
|
234
|
+
trigger,
|
|
235
|
+
callerId: trigger,
|
|
143
236
|
runId: run.runId,
|
|
144
237
|
signal: run.controller.signal
|
|
145
238
|
});
|
|
@@ -219,11 +312,11 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
219
312
|
const beat = async () => {
|
|
220
313
|
const held = [...active.keys()];
|
|
221
314
|
if (held.length === 0) return;
|
|
222
|
-
for (const jobId of await queue.heartbeat({
|
|
315
|
+
for (const { jobId, stop } of await queue.heartbeat({
|
|
223
316
|
jobIds: held,
|
|
224
317
|
workerId,
|
|
225
318
|
leaseMs
|
|
226
|
-
})) active.get(jobId)?.abort();
|
|
319
|
+
})) active.get(jobId)?.abort(stop);
|
|
227
320
|
};
|
|
228
321
|
/** One pass at a time, and the one under way is kept so `stop()` can wait for the claim it may be making. */
|
|
229
322
|
const guarded = async () => {
|
|
@@ -250,6 +343,7 @@ var createJobWorker = ({ queue, lookups, module, workers = 4, pollMs = 1e3, leas
|
|
|
250
343
|
timer = void 0;
|
|
251
344
|
await pass;
|
|
252
345
|
if (inFlight.size > 0) serverLog.info("Actions", `waiting for ${inFlight.size} running job(s) to finish before stopping`);
|
|
346
|
+
for (const jobId of slicing) active.get(jobId)?.abort("shutdown");
|
|
253
347
|
/**
|
|
254
348
|
* Drained rather than abandoned: a deploy finishes the jobs it started instead of leaving them to be retried.
|
|
255
349
|
*
|