@plitzi/sdk-server 0.38.20 → 0.38.21
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 +117 -0
- package/README.md +2 -2
- package/dist/actions.d.ts +6 -0
- package/dist/actions.js +2 -1
- package/dist/core/configSeam.js +2 -2
- package/dist/core/services/functionRoutes.js +7 -2
- package/dist/core/services/realtime.js +5 -2
- package/dist/functions-api.d.ts +155 -0
- 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/modules/actions/index.js +16 -3
- 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/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 +7 -1
- package/dist/modules/actions/types.d.ts +18 -4
- package/dist/modules/functions/capabilities.js +59 -1
- package/dist/modules/functions/config.d.ts +9 -0
- package/dist/modules/functions/context.d.ts +2 -2
- package/dist/modules/functions/context.js +14 -1
- package/dist/modules/functions/contract.d.ts +84 -1
- package/dist/modules/functions/driver.js +94 -2
- 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.js +36 -3
- package/dist/modules/functions/native.js +2 -1
- package/dist/modules/functions/oauth.d.ts +2 -1
- package/dist/modules/functions/oauth.js +15 -2
- package/dist/modules/functions/protocol.d.ts +71 -4
- package/dist/modules/functions/protocol.js +2 -1
- package/dist/modules/functions/routes.d.ts +9 -1
- package/dist/modules/functions/routes.js +48 -23
- package/dist/modules/functions/runner/messages.d.ts +0 -1
- package/dist/modules/functions/runner/messages.js +40 -2
- package/dist/modules/functions/runner/remote.js +58 -3
- package/dist/modules/functions/runner/service.js +53 -7
- package/dist/modules/functions/sandbox/isolate.js +5 -3
- 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
|
@@ -129,6 +129,50 @@ var fetchInitOf = (value) => {
|
|
|
129
129
|
};
|
|
130
130
|
};
|
|
131
131
|
var optionalString = (value, what) => value === void 0 || value === null ? void 0 : stringArg(value, what);
|
|
132
|
+
/** A call to `ctx.jobs`, read into the one method it names: a name and an object for `start`, an id for the rest. */
|
|
133
|
+
var jobsCallOf = (call) => {
|
|
134
|
+
switch (call.method) {
|
|
135
|
+
case "start": {
|
|
136
|
+
const key = optionalString(call.key, "A key");
|
|
137
|
+
if (call.input !== void 0 && !isRecord(call.input)) throw new CallError("A job is handed an object as its input");
|
|
138
|
+
return {
|
|
139
|
+
op: "jobs",
|
|
140
|
+
method: "start",
|
|
141
|
+
name: stringArg(call.name, "A job"),
|
|
142
|
+
input: isRecord(call.input) ? call.input : {},
|
|
143
|
+
...key === void 0 ? {} : { key }
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
case "get":
|
|
147
|
+
case "pause":
|
|
148
|
+
case "resume":
|
|
149
|
+
case "cancel": return {
|
|
150
|
+
op: "jobs",
|
|
151
|
+
method: call.method,
|
|
152
|
+
id: stringArg(call.id, "A job id")
|
|
153
|
+
};
|
|
154
|
+
default: throw new CallError("Not a jobs method the platform answers");
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
/** A job's `save` or `report`, from its slice. */
|
|
158
|
+
var jobCallOf = (call) => {
|
|
159
|
+
if (call.method === "save") return {
|
|
160
|
+
op: "job",
|
|
161
|
+
method: "save",
|
|
162
|
+
state: call.state
|
|
163
|
+
};
|
|
164
|
+
if (call.method === "report") {
|
|
165
|
+
if (typeof call.fraction !== "number" || !Number.isFinite(call.fraction)) throw new CallError("job.report takes how far it is, a number from 0 to 1");
|
|
166
|
+
const step = optionalString(call.step, "A step");
|
|
167
|
+
return {
|
|
168
|
+
op: "job",
|
|
169
|
+
method: "report",
|
|
170
|
+
fraction: call.fraction,
|
|
171
|
+
...step === void 0 ? {} : { step }
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
throw new CallError("Not a job method the platform answers");
|
|
175
|
+
};
|
|
132
176
|
/** A call to `ctx.oauth`, read into the one method it names: what each method takes, and an owner only as a string. */
|
|
133
177
|
var oauthCallOf = (call) => {
|
|
134
178
|
const connection = stringArg(call.connection, "An OAuth connection");
|
|
@@ -143,7 +187,8 @@ var oauthCallOf = (call) => {
|
|
|
143
187
|
...own
|
|
144
188
|
};
|
|
145
189
|
case "status":
|
|
146
|
-
case "disconnect":
|
|
190
|
+
case "disconnect":
|
|
191
|
+
case "token": return {
|
|
147
192
|
op: "oauth",
|
|
148
193
|
method: call.method,
|
|
149
194
|
connection,
|
|
@@ -249,6 +294,8 @@ var readCall = (value) => {
|
|
|
249
294
|
file: stringArg(call.file, "A file")
|
|
250
295
|
};
|
|
251
296
|
case "oauth": return oauthCallOf(call);
|
|
297
|
+
case "jobs": return jobsCallOf(call);
|
|
298
|
+
case "job": return jobCallOf(call);
|
|
252
299
|
case "log": return {
|
|
253
300
|
op: "log",
|
|
254
301
|
values: Array.isArray(call.values) ? call.values : []
|
|
@@ -285,12 +332,23 @@ var answerCall = async (ctx, call) => {
|
|
|
285
332
|
});
|
|
286
333
|
case "status": return ctx.oauth.status(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
287
334
|
case "disconnect": return ctx.oauth.disconnect(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
335
|
+
case "token": return ctx.oauth.token(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
288
336
|
case "fetch": return wireResponseOf(await ctx.oauth.fetch(call.connection, call.url, {
|
|
289
337
|
...call.init,
|
|
290
338
|
...call.owner === void 0 ? {} : { owner: call.owner }
|
|
291
339
|
}));
|
|
292
340
|
}
|
|
293
341
|
break;
|
|
342
|
+
case "jobs":
|
|
343
|
+
switch (call.method) {
|
|
344
|
+
case "start": return ctx.jobs.start(call.name, call.input, call.key === void 0 ? {} : { key: call.key });
|
|
345
|
+
case "get": return await ctx.jobs.get(call.id) ?? null;
|
|
346
|
+
case "pause": return ctx.jobs.pause(call.id);
|
|
347
|
+
case "resume": return ctx.jobs.resume(call.id);
|
|
348
|
+
case "cancel": return ctx.jobs.cancel(call.id);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
case "job": throw new CallError("job.save and job.report belong to a job, inside its run");
|
|
294
352
|
case "log":
|
|
295
353
|
ctx.log(...call.values);
|
|
296
354
|
return;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FunctionsDefinition } from './contract';
|
|
2
|
+
import { FunctionJobLimits } from './jobs';
|
|
2
3
|
import { FunctionLimits, FunctionRunner, FunctionUsage } from './protocol';
|
|
3
4
|
import { FunctionTimeLimits } from '@plitzi/sdk-shared';
|
|
4
5
|
/** One invocation of a space's task, as metering sees it. */
|
|
@@ -39,6 +40,14 @@ export type FunctionsConfig = {
|
|
|
39
40
|
admit?: (spaceId: number) => Promise<string | undefined>;
|
|
40
41
|
/** Told what each invocation spent, however it ended: what `admit` budgets by, and what a deployment meters. */
|
|
41
42
|
onUsage?: (record: FunctionUsageRecord) => void;
|
|
43
|
+
/** The spaces' jobs (`defineFunctions({ jobs })`): what each may spend. */
|
|
44
|
+
jobs?: {
|
|
45
|
+
/**
|
|
46
|
+
* What one space's jobs may spend — its plan's — each unset one `DEFAULT_FUNCTION_JOB_LIMITS`'. `pool` names the
|
|
47
|
+
* spaces whose running jobs count together: an account's.
|
|
48
|
+
*/
|
|
49
|
+
limitsFor?: (spaceId: number) => Promise<Partial<FunctionJobLimits> | undefined>;
|
|
50
|
+
};
|
|
42
51
|
};
|
|
43
52
|
/** What an invocation gets unless its task asks for more CPU or time. */
|
|
44
53
|
export declare const DEFAULT_FUNCTION_LIMITS: FunctionLimits;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FunctionContext } from './contract';
|
|
2
2
|
import { FunctionScope } from './scope';
|
|
3
3
|
import { ActionTaskContext } from '../actions/types';
|
|
4
|
-
import { FunctionOAuthConnection } from '@plitzi/sdk-shared';
|
|
4
|
+
import { FunctionJobManifest, FunctionOAuthConnection } from '@plitzi/sdk-shared';
|
|
5
5
|
/**
|
|
6
6
|
* Whether `hostname` is one the space declared: exactly, or under a `*.` wildcard (`*.example.com` is every subdomain of
|
|
7
7
|
* it, and not `example.com` itself).
|
|
@@ -17,4 +17,4 @@ export declare const lineOf: (values: readonly unknown[]) => string;
|
|
|
17
17
|
* A self-hosted server hands it to the code directly; the platform answers the sandbox's calls with it. With a `scope`,
|
|
18
18
|
* it is a plugin's code, and gets the plugin's corner of the space (`./scope`).
|
|
19
19
|
*/
|
|
20
|
-
export declare const functionContextFor: (ctx: ActionTaskContext, hosts: readonly string[], scope?: FunctionScope, oauth?: Readonly<Record<string, FunctionOAuthConnection>>) => FunctionContext;
|
|
20
|
+
export declare const functionContextFor: (ctx: ActionTaskContext, hosts: readonly string[], scope?: FunctionScope, oauth?: Readonly<Record<string, FunctionOAuthConnection>>, jobs?: Readonly<Record<string, FunctionJobManifest>>) => FunctionContext;
|
|
@@ -66,6 +66,17 @@ var lineOf = (values) => values.map((value) => {
|
|
|
66
66
|
var unavailable = (what) => () => Promise.reject(/* @__PURE__ */ new Error(`This server has no realtime channels (${what})`));
|
|
67
67
|
var unsigned = () => Promise.reject(/* @__PURE__ */ new Error("This server signs nothing: its actions were given no signingSecret"));
|
|
68
68
|
var noJobs = () => Promise.reject(/* @__PURE__ */ new Error("This server runs no jobs: nothing can be set to run later"));
|
|
69
|
+
/** `ctx.jobs` where there is none to give: a server with no queue, or a plugin's code — the space's jobs are the space's. */
|
|
70
|
+
var noFunctionJobs = (why) => {
|
|
71
|
+
const refused = () => Promise.reject(new Error(why));
|
|
72
|
+
return {
|
|
73
|
+
start: refused,
|
|
74
|
+
get: refused,
|
|
75
|
+
pause: refused,
|
|
76
|
+
resume: refused,
|
|
77
|
+
cancel: refused
|
|
78
|
+
};
|
|
79
|
+
};
|
|
69
80
|
/**
|
|
70
81
|
* A plugin reads none of its space's data. The space keeps there what its pages must not carry, and a plugin was trusted
|
|
71
82
|
* to draw, not with what the space keeps — the same line as its credentials: handed the data, its `fetch` could send
|
|
@@ -77,7 +88,7 @@ var noData = (file) => Promise.reject(/* @__PURE__ */ new Error(`The functions o
|
|
|
77
88
|
* A self-hosted server hands it to the code directly; the platform answers the sandbox's calls with it. With a `scope`,
|
|
78
89
|
* it is a plugin's code, and gets the plugin's corner of the space (`./scope`).
|
|
79
90
|
*/
|
|
80
|
-
var functionContextFor = (ctx, hosts, scope, oauth = {}) => {
|
|
91
|
+
var functionContextFor = (ctx, hosts, scope, oauth = {}, jobs = {}) => {
|
|
81
92
|
const shared = {
|
|
82
93
|
spaceId: ctx.spaceId,
|
|
83
94
|
environment: ctx.environment,
|
|
@@ -102,6 +113,7 @@ var functionContextFor = (ctx, hosts, scope, oauth = {}) => {
|
|
|
102
113
|
later: ctx.later ?? noJobs,
|
|
103
114
|
cancelLater: ctx.cancelLater ?? noJobs,
|
|
104
115
|
data: ctx.data,
|
|
116
|
+
jobs: ctx.jobs ? ctx.jobs(jobs) : noFunctionJobs("This server runs no jobs: its actions have no queue"),
|
|
105
117
|
oauth: oauthFor(ctx, oauth, (url) => {
|
|
106
118
|
if (!hostAllowed(url.hostname, hosts)) throw new Error(`"${url.hostname}" is not among the hosts this space's functions may reach (allow.hosts)`);
|
|
107
119
|
})
|
|
@@ -122,6 +134,7 @@ var functionContextFor = (ctx, hosts, scope, oauth = {}) => {
|
|
|
122
134
|
later: noLater,
|
|
123
135
|
cancelLater: noLater,
|
|
124
136
|
data: noData,
|
|
137
|
+
jobs: noFunctionJobs("The functions of a plugin start none of the jobs of its space"),
|
|
125
138
|
oauth: noOAuth("The functions of a plugin make none of the OAuth connections of its space")
|
|
126
139
|
};
|
|
127
140
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LaterAnswer, LaterRequest } from '../actions/jobs/later';
|
|
2
2
|
import { RateCount, RateLimit } from '../actions/runtime/rateLimit';
|
|
3
3
|
import { ActionKvStore } from '../actions/types';
|
|
4
|
-
import { Environment, FunctionOAuthConnection, FunctionTimeLimits, InteractionCallbackParam } from '@plitzi/sdk-shared';
|
|
4
|
+
import { ActionAccess, Environment, FunctionJobView, FunctionOAuthConnection, FunctionTimeLimits, InteractionCallbackParam } from '@plitzi/sdk-shared';
|
|
5
5
|
/**
|
|
6
6
|
* Who started the run, as a space's own code may know them: everything a function decides by — who, their roles and
|
|
7
7
|
* permissions — and never their session. A token inside the sandbox is a token a function could send anywhere.
|
|
@@ -40,6 +40,13 @@ export type FunctionOAuthStatus = {
|
|
|
40
40
|
scopes?: string[];
|
|
41
41
|
expiresAt?: number;
|
|
42
42
|
};
|
|
43
|
+
/** The access token `ctx.oauth.token` hands out — never the refresh token, which stays with the platform. */
|
|
44
|
+
export type FunctionOAuthToken = {
|
|
45
|
+
token: string;
|
|
46
|
+
type: string;
|
|
47
|
+
expiresAt?: number;
|
|
48
|
+
scopes?: string[];
|
|
49
|
+
};
|
|
43
50
|
/**
|
|
44
51
|
* OAuth 2.0 on behalf of whoever the space serves, through the connections it declared (`oauth` in its functions): the
|
|
45
52
|
* consent, the code exchanged, the token kept sealed and renewed by the platform. The code never holds a token, as it
|
|
@@ -64,6 +71,78 @@ export type FunctionOAuth = {
|
|
|
64
71
|
fetch: (connection: string, url: string | URL, init?: Omit<FunctionFetchInit, 'credential'> & FunctionOAuthOwner) => Promise<Response>;
|
|
65
72
|
/** Forgets the connection — revoked at the provider when it says where — and answers whether there was one. */
|
|
66
73
|
disconnect: (connection: string, options?: FunctionOAuthOwner) => Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* The access token itself, renewed first when it is about to lapse — only of a connection that declares
|
|
76
|
+
* `exposeToken: true`, for a provider's SDK in the page that asks for it (`getOAuthToken`). Whoever the code hands it
|
|
77
|
+
* to holds it until it lapses: answer it only to whom the connection is (its owner), from a route that checks so.
|
|
78
|
+
*/
|
|
79
|
+
token: (connection: string, options?: FunctionOAuthOwner) => Promise<FunctionOAuthToken>;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* The space's jobs, from its code: what starts one, reads it and stops it. Who may is the code's to decide — these are
|
|
83
|
+
* called from its routes and tasks, behind its own checks.
|
|
84
|
+
*/
|
|
85
|
+
export type FunctionJobs = {
|
|
86
|
+
/**
|
|
87
|
+
* Starts one of the jobs this code declares (`jobs` in `defineFunctions`), with `input`: answered at once, the job
|
|
88
|
+
* `pending`. With `key`, at most one of that key runs at a time — starting it again while one is under way answers that
|
|
89
|
+
* one (a button pressed twice), and once it ended starts it over with the new input.
|
|
90
|
+
*/
|
|
91
|
+
start: (name: string, input?: Record<string, unknown>, options?: {
|
|
92
|
+
key?: string;
|
|
93
|
+
}) => Promise<FunctionJobView>;
|
|
94
|
+
/** One of the space's jobs, as whoever may watch it sees it — or nothing, when there is none by that id. */
|
|
95
|
+
get: (id: string) => Promise<FunctionJobView | undefined>;
|
|
96
|
+
/** Sets it aside: it stops once its code saved, and waits as `paused`. Answers whether anything changed. */
|
|
97
|
+
pause: (id: string) => Promise<boolean>;
|
|
98
|
+
/** Puts a paused job back in the queue, to go on from what it saved. */
|
|
99
|
+
resume: (id: string) => Promise<boolean>;
|
|
100
|
+
/** Stops it for good: dropped if it waits, aborted if it runs. */
|
|
101
|
+
cancel: (id: string) => Promise<boolean>;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Why a slice of a job is asked to end (`job.stopping`): it was set aside (`pause`) or cancelled (`cancel`), its time in
|
|
105
|
+
* one go is up (`limit`) or the server is going away (`shutdown`). Whichever, what was saved is where it goes on from.
|
|
106
|
+
*/
|
|
107
|
+
export type FunctionJobStop = 'pause' | 'cancel' | 'limit' | 'shutdown';
|
|
108
|
+
/**
|
|
109
|
+
* What a job's `run` is handed besides its input: the job, and how it keeps where it is. A job runs in slices — a pause,
|
|
110
|
+
* a lost server or its time in one go ending one — and every slice but the first starts from what the one before saved.
|
|
111
|
+
*/
|
|
112
|
+
export type FunctionJobHandle<S = unknown> = {
|
|
113
|
+
id: string;
|
|
114
|
+
name: string;
|
|
115
|
+
/** What it last saved — `undefined` on its first slice, and whenever it never saved. */
|
|
116
|
+
state: S | undefined;
|
|
117
|
+
/** Which attempt this is, from 1: a slice after a failure is the next attempt, from the last save. */
|
|
118
|
+
attempt: number;
|
|
119
|
+
/**
|
|
120
|
+
* Keeps `state` durably: the next slice starts with it. JSON, within what the server keeps for one job. A method, not
|
|
121
|
+
* a property: so a job whose state has a type of its own is still one of `FunctionsDefinition['jobs']`.
|
|
122
|
+
*/
|
|
123
|
+
save(state: S): Promise<void>;
|
|
124
|
+
/** How far it is, from 0 to 1, and what it is doing — for whoever watches it. Never fails the job. */
|
|
125
|
+
report: (fraction: number, step?: string) => void;
|
|
126
|
+
/** Why this slice is asked to end, once it is (`signal` aborted): save, and return. */
|
|
127
|
+
readonly stopping: FunctionJobStop | undefined;
|
|
128
|
+
/** Aborted when the slice is asked to end — the same as `ctx.signal`. Some seconds later it is cut. */
|
|
129
|
+
signal: AbortSignal;
|
|
130
|
+
};
|
|
131
|
+
/** A job of the space's: work longer than a request, run on the server slice by slice until it answers. */
|
|
132
|
+
export type FunctionJob<I extends Record<string, unknown> = Record<string, unknown>, S = unknown, R = unknown> = {
|
|
133
|
+
/**
|
|
134
|
+
* Who may watch it (`$job:<id>`, `useJob`) besides whoever started it — a channel's access, read against the visitor's
|
|
135
|
+
* permissions in the space. Absent, only whoever started it.
|
|
136
|
+
*/
|
|
137
|
+
watch?: ActionAccess;
|
|
138
|
+
/** How long it may run, every slice together, in seconds — within what the server allows. */
|
|
139
|
+
maxSeconds?: number;
|
|
140
|
+
/**
|
|
141
|
+
* The work. What it answers is the job's result; an `ActionRefusal` fails it without a retry. A method, not a property:
|
|
142
|
+
* so a job typed with an input and a state of its own is still one of `FunctionsDefinition['jobs']`, and one written
|
|
143
|
+
* in place there gets an object for its input and `unknown` for its state.
|
|
144
|
+
*/
|
|
145
|
+
run(input: I, job: FunctionJobHandle<S>, ctx: FunctionContext): R | Promise<R>;
|
|
67
146
|
};
|
|
68
147
|
/**
|
|
69
148
|
* Everything a space's code can do besides computing — one object, the same wherever it runs. In a self-hosted server
|
|
@@ -124,6 +203,8 @@ export type FunctionContext = {
|
|
|
124
203
|
log: (...values: unknown[]) => void;
|
|
125
204
|
/** OAuth 2.0 through the space's declared connections — see {@link FunctionOAuth}. */
|
|
126
205
|
oauth: FunctionOAuth;
|
|
206
|
+
/** The space's jobs — see {@link FunctionJobs}. */
|
|
207
|
+
jobs: FunctionJobs;
|
|
127
208
|
/** Progress for a caller that asked for a stream — a no-op when nobody did. */
|
|
128
209
|
emit: (chunk: unknown) => void;
|
|
129
210
|
/** Aborted when the run is: cancelled, or out of time. Hand it to what can stop early. */
|
|
@@ -176,6 +257,8 @@ export type FunctionsDefinition = {
|
|
|
176
257
|
* (OpenID) or its endpoints, the credential with the app's id and secret, the scopes.
|
|
177
258
|
*/
|
|
178
259
|
oauth?: Record<string, FunctionOAuthConnection>;
|
|
260
|
+
/** Its jobs, by name (`ctx.jobs.start(name, input)`): work that outlives a request, run slice by slice. */
|
|
261
|
+
jobs?: Record<string, FunctionJob>;
|
|
179
262
|
};
|
|
180
263
|
/**
|
|
181
264
|
* What a function throws to refuse, with a reason written for whoever asked — a wrong password, a board that is
|
|
@@ -19,7 +19,11 @@ var describeFunctions = (definition) => {
|
|
|
19
19
|
} : {}),
|
|
20
20
|
routes: isRecord(found.routes) ? Object.keys(found.routes) : [],
|
|
21
21
|
limits: found.limits,
|
|
22
|
-
oauth: found.oauth
|
|
22
|
+
oauth: found.oauth,
|
|
23
|
+
jobs: isRecord(found.jobs) ? Object.fromEntries(Object.entries(found.jobs).map(([name, job]) => [name, isRecord(job) ? {
|
|
24
|
+
watch: job.watch,
|
|
25
|
+
maxSeconds: job.maxSeconds
|
|
26
|
+
} : job])) : found.jobs
|
|
23
27
|
};
|
|
24
28
|
};
|
|
25
29
|
/**
|
|
@@ -130,6 +134,35 @@ var createFunctionsDriver = (environment) => {
|
|
|
130
134
|
op: "data",
|
|
131
135
|
file
|
|
132
136
|
}),
|
|
137
|
+
jobs: {
|
|
138
|
+
start: (name, input = {}, options = {}) => call({
|
|
139
|
+
op: "jobs",
|
|
140
|
+
method: "start",
|
|
141
|
+
name,
|
|
142
|
+
input,
|
|
143
|
+
key: options.key
|
|
144
|
+
}),
|
|
145
|
+
get: (id) => call({
|
|
146
|
+
op: "jobs",
|
|
147
|
+
method: "get",
|
|
148
|
+
id
|
|
149
|
+
}).then((found) => found ?? void 0),
|
|
150
|
+
pause: (id) => call({
|
|
151
|
+
op: "jobs",
|
|
152
|
+
method: "pause",
|
|
153
|
+
id
|
|
154
|
+
}),
|
|
155
|
+
resume: (id) => call({
|
|
156
|
+
op: "jobs",
|
|
157
|
+
method: "resume",
|
|
158
|
+
id
|
|
159
|
+
}),
|
|
160
|
+
cancel: (id) => call({
|
|
161
|
+
op: "jobs",
|
|
162
|
+
method: "cancel",
|
|
163
|
+
id
|
|
164
|
+
})
|
|
165
|
+
},
|
|
133
166
|
oauth: {
|
|
134
167
|
connect: (connection, options = {}) => call({
|
|
135
168
|
op: "oauth",
|
|
@@ -150,6 +183,12 @@ var createFunctionsDriver = (environment) => {
|
|
|
150
183
|
connection,
|
|
151
184
|
owner: options.owner
|
|
152
185
|
}),
|
|
186
|
+
token: (connection, options = {}) => call({
|
|
187
|
+
op: "oauth",
|
|
188
|
+
method: "token",
|
|
189
|
+
connection,
|
|
190
|
+
owner: options.owner
|
|
191
|
+
}),
|
|
153
192
|
fetch: async (connection, url, init = {}) => {
|
|
154
193
|
const { owner, ...rest } = init;
|
|
155
194
|
const answer = await call({
|
|
@@ -173,10 +212,63 @@ var createFunctionsDriver = (environment) => {
|
|
|
173
212
|
signal
|
|
174
213
|
};
|
|
175
214
|
};
|
|
176
|
-
|
|
215
|
+
const STOPS = [
|
|
216
|
+
"pause",
|
|
217
|
+
"cancel",
|
|
218
|
+
"limit",
|
|
219
|
+
"shutdown"
|
|
220
|
+
];
|
|
221
|
+
/**
|
|
222
|
+
* Why the slice is asked to end, once it is: the reason the signal was aborted with — said as itself in a runtime, and
|
|
223
|
+
* as the message of the `AbortError` the sandbox raises — and a cancel when it said none of them.
|
|
224
|
+
*/
|
|
225
|
+
const stoppingOf = () => {
|
|
226
|
+
const current = signal;
|
|
227
|
+
if (typeof current !== "object" || current === null || !("aborted" in current) || current.aborted !== true) return;
|
|
228
|
+
const reason = "reason" in current ? current.reason : void 0;
|
|
229
|
+
const said = typeof reason === "string" ? reason : typeof reason === "object" && reason !== null && "message" in reason && typeof reason.message === "string" ? reason.message : void 0;
|
|
230
|
+
return said !== void 0 && STOPS.includes(said) ? said : "cancel";
|
|
231
|
+
};
|
|
232
|
+
/** A job's `run` and what it is handed: the slice, how it saves and reports, and why it is asked to end. */
|
|
233
|
+
const runJob = async (found, invocation, ctx) => {
|
|
234
|
+
const jobs = isRecord(found.jobs) ? found.jobs : {};
|
|
235
|
+
const name = String(invocation.name);
|
|
236
|
+
const job = Object.hasOwn(jobs, name) ? jobs[name] : void 0;
|
|
237
|
+
if (!isRecord(job) || !isCallable(job.run)) throw new Error(`This space's functions have no job "${name}"`);
|
|
238
|
+
const slice = isRecord(invocation.job) ? invocation.job : {};
|
|
239
|
+
const handle = {
|
|
240
|
+
id: slice.id,
|
|
241
|
+
name,
|
|
242
|
+
state: slice.state,
|
|
243
|
+
attempt: slice.attempt,
|
|
244
|
+
save: async (state) => {
|
|
245
|
+
await call({
|
|
246
|
+
op: "job",
|
|
247
|
+
method: "save",
|
|
248
|
+
state
|
|
249
|
+
});
|
|
250
|
+
},
|
|
251
|
+
report: (fraction, step) => {
|
|
252
|
+
call({
|
|
253
|
+
op: "job",
|
|
254
|
+
method: "report",
|
|
255
|
+
fraction,
|
|
256
|
+
step
|
|
257
|
+
}).catch(() => void 0);
|
|
258
|
+
},
|
|
259
|
+
get stopping() {
|
|
260
|
+
return stoppingOf();
|
|
261
|
+
},
|
|
262
|
+
signal
|
|
263
|
+
};
|
|
264
|
+
const value = await job.run(isRecord(invocation.input) ? invocation.input : {}, handle, ctx);
|
|
265
|
+
return value === void 0 ? null : value;
|
|
266
|
+
};
|
|
267
|
+
/** One invocation: what a task or a job's slice returned (`null` for nothing), or the wire response a route answered. */
|
|
177
268
|
const invoke = async (definition, invocation) => {
|
|
178
269
|
const found = isRecord(definition) ? definition : {};
|
|
179
270
|
const ctx = contextOf(invocation);
|
|
271
|
+
if (invocation.kind === "job") return runJob(found, invocation, ctx);
|
|
180
272
|
if (invocation.kind === "task") {
|
|
181
273
|
const task = listOf(found.tasks).find((entry) => isRecord(entry) && `${String(entry.namespace)}.${String(entry.action)}` === invocation.name);
|
|
182
274
|
if (!isRecord(task) || !isCallable(task.run)) throw new Error(`This space's functions have no task "${String(invocation.name)}"`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ActionKvAdapter } from '../actions/types';
|
|
2
|
+
import { JobQueue, PubSubAdapter } from '@plitzi/sdk-shared';
|
|
3
|
+
export type JobControlOptions = {
|
|
4
|
+
queue: JobQueue;
|
|
5
|
+
/**
|
|
6
|
+
* The page server's realtime — the pub/sub its replicas listen on and the `kv` its retained messages are kept in — so
|
|
7
|
+
* whoever watches a job hears what an operator did to it. Absent, the queue changes and nobody is told until the job
|
|
8
|
+
* moves again.
|
|
9
|
+
*/
|
|
10
|
+
realtime?: {
|
|
11
|
+
pubsub: PubSubAdapter;
|
|
12
|
+
kv: ActionKvAdapter;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export type JobControl = Pick<JobQueue, 'pause' | 'resume' | 'cancel' | 'requeue'>;
|
|
16
|
+
/**
|
|
17
|
+
* An operator's hold on the jobs, from a process that serves no pages — a deployment's API behind its Queue panel: the
|
|
18
|
+
* queue's own `pause`, `resume`, `cancel` and `requeue`, each said on `$job:<id>` when it changed a job of the spaces'
|
|
19
|
+
* functions, as the page server says it when its code does the same.
|
|
20
|
+
*/
|
|
21
|
+
export declare const createJobControl: ({ queue, realtime }: JobControlOptions) => JobControl;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { serverLog } from "../../helpers/serverLog.js";
|
|
2
|
+
import { jobViewOf } from "./jobs.js";
|
|
3
|
+
import { createRealtimeHub } from "../realtime/hub.js";
|
|
4
|
+
import { publishJobView } from "../realtime/jobs.js";
|
|
5
|
+
import { createRealtimeRetained } from "../realtime/retained.js";
|
|
6
|
+
//#region src/modules/functions/jobControl.ts
|
|
7
|
+
/**
|
|
8
|
+
* An operator's hold on the jobs, from a process that serves no pages — a deployment's API behind its Queue panel: the
|
|
9
|
+
* queue's own `pause`, `resume`, `cancel` and `requeue`, each said on `$job:<id>` when it changed a job of the spaces'
|
|
10
|
+
* functions, as the page server says it when its code does the same.
|
|
11
|
+
*/
|
|
12
|
+
var createJobControl = ({ queue, realtime }) => {
|
|
13
|
+
const said = realtime ? {
|
|
14
|
+
retained: createRealtimeRetained(realtime.kv),
|
|
15
|
+
pubsub: realtime.pubsub
|
|
16
|
+
} : void 0;
|
|
17
|
+
const hub = said ? createRealtimeHub(said.pubsub, said.retained) : void 0;
|
|
18
|
+
const announce = async (spaceIds, jobId) => {
|
|
19
|
+
if (!hub || !said) return;
|
|
20
|
+
try {
|
|
21
|
+
const job = await queue.getJob(spaceIds, jobId);
|
|
22
|
+
if (job?.target.kind === "function") await publishJobView(hub, said.retained, {
|
|
23
|
+
spaceId: job.spaceId,
|
|
24
|
+
environment: job.environment
|
|
25
|
+
}, jobId, jobViewOf(job));
|
|
26
|
+
} catch (error) {
|
|
27
|
+
serverLog.warn("Actions", `job ${jobId}: its watchers were not told what an operator did`, error);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const told = (change) => async (spaceIds, jobId, ...rest) => {
|
|
31
|
+
const changed = await change(spaceIds, jobId, ...rest);
|
|
32
|
+
if (changed) await announce(spaceIds, jobId);
|
|
33
|
+
return changed;
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
pause: told((spaceIds, jobId) => queue.pause(spaceIds, jobId)),
|
|
37
|
+
resume: told((spaceIds, jobId) => queue.resume(spaceIds, jobId)),
|
|
38
|
+
cancel: told((spaceIds, jobId) => queue.cancel(spaceIds, jobId)),
|
|
39
|
+
requeue: told((spaceIds, jobId, options) => queue.requeue(spaceIds, jobId, options))
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { createJobControl };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { FunctionsConfig } from './config';
|
|
2
|
+
import { FunctionJobs, FunctionJobStop, FunctionsDefinition } from './contract';
|
|
3
|
+
import { SpaceFunctions } from './protocol';
|
|
4
|
+
import { TaskContextSource } from '../actions/runtime/runAction';
|
|
5
|
+
import { Job, JobQueue, Environment, FunctionJobManifest, FunctionJobView, RunUser, SpaceRevision } from '@plitzi/sdk-shared';
|
|
6
|
+
/**
|
|
7
|
+
* A space's jobs (`defineFunctions({ jobs })`): work longer than a request, kept in the actions' durable queue and run
|
|
8
|
+
* slice by slice — in the sandbox, in a space's runtime, or natively in a self-hosted server.
|
|
9
|
+
*
|
|
10
|
+
* A slice ends when the code returns (the job is done), throws (an attempt failed: the next starts from the last save),
|
|
11
|
+
* or is asked to stop: paused, cancelled, its time in one go up, the replica going away. `job.save` is the one
|
|
12
|
+
* checkpoint, and the rule that keeps a job from looping forever is that a slice cut by its time goes on only if it saved
|
|
13
|
+
* since it began.
|
|
14
|
+
*/
|
|
15
|
+
/** What a server lets a space's jobs spend: a deployment sets it per space (its plan), each unset one the default's. */
|
|
16
|
+
export type FunctionJobLimits = {
|
|
17
|
+
/** Jobs running at once, across `pool`. */
|
|
18
|
+
running: number;
|
|
19
|
+
/** The spaces whose jobs `running` counts together — an account's. Default: the space alone. */
|
|
20
|
+
pool?: number[];
|
|
21
|
+
/** Jobs waiting (`pending` or `paused`) in one space. */
|
|
22
|
+
waiting: number;
|
|
23
|
+
/** How long one job may run, every slice together, in seconds. */
|
|
24
|
+
maxSeconds: number;
|
|
25
|
+
/** How long one slice runs in the sandbox before it is asked to save and go on in the next. */
|
|
26
|
+
sliceMs: number;
|
|
27
|
+
/** CPU one slice in the sandbox may use. */
|
|
28
|
+
sliceCpuMs: number;
|
|
29
|
+
/** Calls to `ctx` one slice in the sandbox may make. */
|
|
30
|
+
sliceCalls: number;
|
|
31
|
+
/** The most a job keeps of what it saves — and of its input — as JSON, in bytes. */
|
|
32
|
+
stateBytes: number;
|
|
33
|
+
};
|
|
34
|
+
export declare const DEFAULT_FUNCTION_JOB_LIMITS: Omit<FunctionJobLimits, 'pool'>;
|
|
35
|
+
/** How long a slice asked to end has to save and return before it is cut. */
|
|
36
|
+
export declare const JOB_GRACE_MS = 5000;
|
|
37
|
+
/** How a slice ended, for the worker to settle the job by. */
|
|
38
|
+
export type JobSliceOutcome = {
|
|
39
|
+
kind: 'done';
|
|
40
|
+
result: unknown;
|
|
41
|
+
spentMs: number;
|
|
42
|
+
} | {
|
|
43
|
+
kind: 'stopped';
|
|
44
|
+
stop: FunctionJobStop;
|
|
45
|
+
saved: boolean;
|
|
46
|
+
spentMs: number;
|
|
47
|
+
}
|
|
48
|
+
/** `final`: no attempt would change it — a refusal, a job no code declares, a job out of its time. */
|
|
49
|
+
| {
|
|
50
|
+
kind: 'failed';
|
|
51
|
+
error: string;
|
|
52
|
+
final: boolean;
|
|
53
|
+
refused: boolean;
|
|
54
|
+
spentMs: number;
|
|
55
|
+
}
|
|
56
|
+
/** Not started: the account runs as many jobs as it may, or may not run code right now. */
|
|
57
|
+
| {
|
|
58
|
+
kind: 'wait';
|
|
59
|
+
delayMs: number;
|
|
60
|
+
why: string;
|
|
61
|
+
};
|
|
62
|
+
/** Who and where a run of a space's code is, as `ctx.jobs` starts a job from it. */
|
|
63
|
+
export type JobRunScope = {
|
|
64
|
+
spaceId: number;
|
|
65
|
+
environment: Environment;
|
|
66
|
+
at?: SpaceRevision;
|
|
67
|
+
user?: RunUser;
|
|
68
|
+
};
|
|
69
|
+
export type FunctionJobsService = {
|
|
70
|
+
/** `ctx.jobs` for one run of a space's code, over the jobs that code declares. */
|
|
71
|
+
forRun: (run: JobRunScope, declared: Readonly<Record<string, FunctionJobManifest>>) => FunctionJobs;
|
|
72
|
+
/** Runs one slice of `job`, `signal` aborting with why (`pause`, `cancel`, `shutdown`) when its worker is told to. */
|
|
73
|
+
slice: (job: Job, signal: AbortSignal, workerId: string) => Promise<JobSliceOutcome>;
|
|
74
|
+
/** Says how the job is now to whoever watches it (`$job:<id>`). Never fails: a watcher missing a beat is not a fault. */
|
|
75
|
+
announce: (spaceId: number, jobId: string) => Promise<void>;
|
|
76
|
+
};
|
|
77
|
+
export type FunctionJobsDeps = {
|
|
78
|
+
/** Read when asked: the queue is made after the runner, and a server may run none. */
|
|
79
|
+
queue: () => JobQueue | undefined;
|
|
80
|
+
config: FunctionsConfig;
|
|
81
|
+
getFunctions?: (spaceId: number, at?: SpaceRevision) => Promise<SpaceFunctions | undefined>;
|
|
82
|
+
/** Read when a slice runs: the runner that makes it is made after this. */
|
|
83
|
+
taskContext: () => TaskContextSource;
|
|
84
|
+
/** Says the job as it is on `$job:<id>` — absent when the server has no realtime channels. */
|
|
85
|
+
publish?: (space: {
|
|
86
|
+
spaceId: number;
|
|
87
|
+
environment: string;
|
|
88
|
+
}, jobId: string, view: FunctionJobView) => Promise<void>;
|
|
89
|
+
maxAttempts: number;
|
|
90
|
+
};
|
|
91
|
+
/** What a job's declaration says besides its code, as a manifest holds it. */
|
|
92
|
+
export declare const jobsManifestOf: (definition: FunctionsDefinition) => Record<string, FunctionJobManifest>;
|
|
93
|
+
/**
|
|
94
|
+
* A job as whoever may watch it sees it: never its input or what it saved, and of a failure only the code's own refusal
|
|
95
|
+
* — anything else it threw can carry what a visitor must not read, and stays in the Queue for an operator.
|
|
96
|
+
*/
|
|
97
|
+
export declare const jobViewOf: (job: Job) => FunctionJobView;
|
|
98
|
+
export declare const createFunctionJobs: ({ queue, config, getFunctions, taskContext, publish, maxAttempts }: FunctionJobsDeps) => FunctionJobsService;
|