@lango-project/lango-server 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -3
- package/dist/body_reader.mjs.map +1 -1
- package/dist/builder.mjs +26 -2
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/server.mjs +3 -835
- package/dist/cli/server.mjs.map +1 -1
- package/dist/client_identity.mjs.map +1 -1
- package/dist/database.mjs.map +1 -1
- package/dist/database_action.mjs +71 -0
- package/dist/database_action.mjs.map +1 -0
- package/dist/database_options.mjs.map +1 -1
- package/dist/database_scope.mjs +11 -0
- package/dist/database_scope.mjs.map +1 -1
- package/dist/endpoint.mjs.map +1 -1
- package/dist/error_details.mjs.map +1 -1
- package/dist/execution_failure.mjs +30 -0
- package/dist/execution_failure.mjs.map +1 -0
- package/dist/execution_identity.mjs +103 -0
- package/dist/execution_identity.mjs.map +1 -0
- package/dist/execution_log.mjs +47 -0
- package/dist/execution_log.mjs.map +1 -0
- package/dist/execution_runtime.mjs +201 -0
- package/dist/execution_runtime.mjs.map +1 -0
- package/dist/execution_worker.mjs +41 -0
- package/dist/execution_worker.mjs.map +1 -0
- package/dist/executor.mjs +384 -0
- package/dist/executor.mjs.map +1 -0
- package/dist/group.mjs +15 -0
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs.map +1 -1
- package/dist/job_configuration.mjs +37 -0
- package/dist/job_configuration.mjs.map +1 -0
- package/dist/job_context.mjs +61 -0
- package/dist/job_context.mjs.map +1 -0
- package/dist/library.mjs +31 -0
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs +44 -0
- package/dist/module_loader.mjs.map +1 -0
- package/dist/options.mjs +12 -1
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs.map +1 -1
- package/dist/policy.mjs.map +1 -1
- package/dist/postgres_adapter.mjs +12 -1
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +67 -5
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs +52 -0
- package/dist/queue_provider.mjs.map +1 -0
- package/dist/request_identity.mjs.map +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/route_pattern.mjs.map +1 -1
- package/dist/routes.mjs +36 -1
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +65 -25
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +4 -1
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs.map +1 -1
- package/dist/sqlite_adapter.mjs.map +1 -1
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs.map +1 -1
- package/dist/trace.mjs.map +1 -1
- package/dist/worker.mjs.map +1 -1
- package/docs/jobs.md +123 -0
- package/examples/security/package.json +1 -1
- package/package.json +10 -6
- package/src/builder.lango +22 -0
- package/src/database_action.lango +30 -0
- package/src/database_scope.lango +7 -0
- package/src/execution_failure.lango +14 -0
- package/src/execution_identity.lango +71 -0
- package/src/execution_log.lango +28 -0
- package/src/execution_runtime.lango +135 -0
- package/src/execution_worker.lango +32 -0
- package/src/executor.lango +284 -0
- package/src/group.lango +11 -0
- package/src/job_configuration.lango +29 -0
- package/src/job_context.lango +44 -0
- package/src/library.lango +21 -0
- package/src/middleware.lango +1 -1
- package/src/module_loader.lango +25 -0
- package/src/options.lango +7 -1
- package/src/postgres_adapter.lango +12 -0
- package/src/postgres_worker.lango +54 -3
- package/src/queue_provider.lango +30 -0
- package/src/routes.lango +44 -0
- package/src/runtime.lango +45 -17
- package/src/server.lango +3 -0
- package/dist/cli/.lango/bignumber.mjs +0 -3119
- package/dist/cli/.lango/localization.mjs +0 -108
- package/dist/cli/.lango/runtime.mjs +0 -725
- package/dist/cli/.lango/vendor/i18next.LICENSE +0 -21
- package/dist/cli/.lango/vendor/i18next.mjs +0 -2434
- package/dist/cli/.lango/xmldom/conventions.js +0 -429
- package/dist/cli/.lango/xmldom/dom-parser.js +0 -591
- package/dist/cli/.lango/xmldom/dom.js +0 -3759
- package/dist/cli/.lango/xmldom/entities.js +0 -2171
- package/dist/cli/.lango/xmldom/errors.js +0 -205
- package/dist/cli/.lango/xmldom/grammar.js +0 -559
- package/dist/cli/.lango/xmldom/index.js +0 -41
- package/dist/cli/.lango/xmldom/package.json +0 -1
- package/dist/cli/.lango/xmldom/sax.js +0 -983
- package/dist/cli/.lango/xmldom.LICENSE.md +0 -8
- package/dist/cli/body_reader.mjs +0 -180
- package/dist/cli/body_reader.mjs.map +0 -1
- package/dist/cli/client_identity.mjs +0 -250
- package/dist/cli/client_identity.mjs.map +0 -1
- package/dist/cli/database.mjs +0 -116
- package/dist/cli/database.mjs.map +0 -1
- package/dist/cli/database_options.mjs +0 -83
- package/dist/cli/database_options.mjs.map +0 -1
- package/dist/cli/database_scope.mjs +0 -82
- package/dist/cli/database_scope.mjs.map +0 -1
- package/dist/cli/endpoint.mjs +0 -50
- package/dist/cli/endpoint.mjs.map +0 -1
- package/dist/cli/error_details.mjs +0 -45
- package/dist/cli/error_details.mjs.map +0 -1
- package/dist/cli/longpoll_broker.mjs +0 -215
- package/dist/cli/longpoll_broker.mjs.map +0 -1
- package/dist/cli/longpoll_options.mjs +0 -103
- package/dist/cli/longpoll_options.mjs.map +0 -1
- package/dist/cli/longpoll_session.mjs +0 -136
- package/dist/cli/longpoll_session.mjs.map +0 -1
- package/dist/cli/middleware.mjs +0 -25
- package/dist/cli/middleware.mjs.map +0 -1
- package/dist/cli/options.mjs +0 -203
- package/dist/cli/options.mjs.map +0 -1
- package/dist/cli/origin.mjs +0 -87
- package/dist/cli/origin.mjs.map +0 -1
- package/dist/cli/policy.mjs +0 -121
- package/dist/cli/policy.mjs.map +0 -1
- package/dist/cli/postgres_adapter.mjs +0 -167
- package/dist/cli/postgres_adapter.mjs.map +0 -1
- package/dist/cli/route_pattern.mjs +0 -129
- package/dist/cli/route_pattern.mjs.map +0 -1
- package/dist/cli/routes.mjs +0 -325
- package/dist/cli/routes.mjs.map +0 -1
- package/dist/cli/sqlite_adapter.mjs +0 -163
- package/dist/cli/sqlite_adapter.mjs.map +0 -1
- package/dist/cli/static_files.mjs +0 -224
- package/dist/cli/static_files.mjs.map +0 -1
- package/dist/cli/trace.mjs +0 -53
- package/dist/cli/trace.mjs.map +0 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ModuleLoader } from "./module_loader.mjs";
|
|
2
|
+
import { Database } from "./database.mjs";
|
|
3
|
+
import { executionSource, executionToken } from "./execution_identity.mjs";
|
|
4
|
+
import { ExecutionFailure } from "./execution_failure.mjs";
|
|
5
|
+
import { JobContext } from "./job_context.mjs";
|
|
6
|
+
import { ExecutionLog } from "./execution_log.mjs";
|
|
7
|
+
import { errorDetails } from "./error_details.mjs";
|
|
8
|
+
import {
|
|
9
|
+
hostGlobal as __hostGlobal,
|
|
10
|
+
contextGet as __contextGet,
|
|
11
|
+
contextSet as __contextSet,
|
|
12
|
+
contextRestore as __contextRestore,
|
|
13
|
+
bindContexts as __bindContexts,
|
|
14
|
+
fromJson as __fromJson,
|
|
15
|
+
toJson as __toJson,
|
|
16
|
+
getClass as __getClass,
|
|
17
|
+
isKindof as __isKindof,
|
|
18
|
+
isNumber as __isNumber,
|
|
19
|
+
isString as __isString,
|
|
20
|
+
isBool as __isBool,
|
|
21
|
+
isObject as __isObject,
|
|
22
|
+
isArray as __isArray,
|
|
23
|
+
requireBoolean as __requireBoolean,
|
|
24
|
+
normalize as __normalize,
|
|
25
|
+
call as __call,
|
|
26
|
+
boundary as __boundary,
|
|
27
|
+
requireArity as __requireArity,
|
|
28
|
+
withArity as __withArity,
|
|
29
|
+
} from "./.lango/runtime.mjs";
|
|
30
|
+
|
|
31
|
+
export class ExecutionRuntime {
|
|
32
|
+
#definition;
|
|
33
|
+
#loader;
|
|
34
|
+
#database;
|
|
35
|
+
#handlers;
|
|
36
|
+
#identity;
|
|
37
|
+
#queueFactory;
|
|
38
|
+
constructor(definition) {
|
|
39
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
40
|
+
definition = __boundary(definition);
|
|
41
|
+
this.#definition = definition;
|
|
42
|
+
this.#loader = new ModuleLoader();
|
|
43
|
+
this.#database = new Database(__normalize(definition.database), __normalize(definition.root));
|
|
44
|
+
this.#handlers = __normalize(__hostGlobal(["Object"]).create(null));
|
|
45
|
+
this.#identity = null;
|
|
46
|
+
this.#queueFactory = null;
|
|
47
|
+
}
|
|
48
|
+
preload() {
|
|
49
|
+
let pending = __normalize(this.#database.open());
|
|
50
|
+
for (const route of __normalize(this.#definition.targets)) {
|
|
51
|
+
pending = this.#prepare(pending, route);
|
|
52
|
+
}
|
|
53
|
+
return pending.then(__withArity(__bindContexts((ignored) => {
|
|
54
|
+
ignored = __boundary(ignored);
|
|
55
|
+
return __normalize(this.#loader.target(__normalize(this.#definition.identity)));
|
|
56
|
+
return null;
|
|
57
|
+
}), [0], "closure")).then(__withArity(__bindContexts((handler) => {
|
|
58
|
+
handler = __boundary(handler);
|
|
59
|
+
this.#identity = handler;
|
|
60
|
+
if (__requireBoolean(__normalize(this.#definition.queueProvider) !== null)) {
|
|
61
|
+
return __normalize(this.#loader.target(__normalize(this.#definition.queueProvider)));
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
return null;
|
|
65
|
+
}), [0], "closure")).then(__withArity(__bindContexts((factory) => {
|
|
66
|
+
factory = __boundary(factory);
|
|
67
|
+
this.#queueFactory = factory;
|
|
68
|
+
return null;
|
|
69
|
+
return null;
|
|
70
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
71
|
+
error = __boundary(error);
|
|
72
|
+
this.close();
|
|
73
|
+
throw error;
|
|
74
|
+
return null;
|
|
75
|
+
}), [0], "closure"));
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
#prepare(pending, route) {
|
|
79
|
+
__requireArity(arguments.length, [0, 1], "prepare");
|
|
80
|
+
pending = __boundary(pending);
|
|
81
|
+
route = __boundary(route);
|
|
82
|
+
return pending.then(__withArity(__bindContexts((ignored) => {
|
|
83
|
+
ignored = __boundary(ignored);
|
|
84
|
+
return __normalize(this.#loader.target(route));
|
|
85
|
+
return null;
|
|
86
|
+
}), [0], "closure")).then(__withArity(__bindContexts((handler) => {
|
|
87
|
+
handler = __boundary(handler);
|
|
88
|
+
this.#handlers[__normalize(route.path)] = handler;
|
|
89
|
+
return null;
|
|
90
|
+
return null;
|
|
91
|
+
}), [0], "closure"));
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
execute(task) {
|
|
95
|
+
const __contextScope = [];
|
|
96
|
+
try {
|
|
97
|
+
__requireArity(arguments.length, [0], "execute");
|
|
98
|
+
task = __boundary(task);
|
|
99
|
+
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
100
|
+
let route = null;
|
|
101
|
+
for (const target of __normalize(this.#definition.targets)) {
|
|
102
|
+
if (__requireBoolean(__normalize(target.path) === __normalize(task.path))) {
|
|
103
|
+
route = target;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (__requireBoolean(route === null)) {
|
|
107
|
+
if (__requireBoolean(__contextGet("db") !== null)) {
|
|
108
|
+
__normalize(__contextGet("db").finish());
|
|
109
|
+
}
|
|
110
|
+
throw "Unregistered execution path";
|
|
111
|
+
}
|
|
112
|
+
__contextScope.push(__contextSet("job", new JobContext(task, route)));
|
|
113
|
+
let sink = __contextGet("log");
|
|
114
|
+
__contextScope.push(__contextSet("log", new ExecutionLog(sink, __contextGet("job"))));
|
|
115
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
|
|
116
|
+
const __contextScope = [];
|
|
117
|
+
try {
|
|
118
|
+
ignored = __boundary(ignored);
|
|
119
|
+
let source = executionSource(__normalize(task.source));
|
|
120
|
+
__normalize(__hostGlobal(["Object"]).freeze(source));
|
|
121
|
+
let target = __normalize(__hostGlobal(["Object"]).freeze({ path: __normalize(route.path), queue: __normalize(route.queue), policy: __normalize(route.policy), reauthSeconds: __normalize(route.reauthSeconds) }));
|
|
122
|
+
let handler = __normalize(this.#identity);
|
|
123
|
+
return __call(handler, { source: source, route: target });
|
|
124
|
+
return null;
|
|
125
|
+
} finally {
|
|
126
|
+
__contextRestore(__contextScope);
|
|
127
|
+
}
|
|
128
|
+
}), [0], "closure")).then(__withArity(__bindContexts((identity) => {
|
|
129
|
+
const __contextScope = [];
|
|
130
|
+
try {
|
|
131
|
+
identity = __boundary(identity);
|
|
132
|
+
__contextScope.push(__contextSet("token", executionToken(identity, __normalize(task.source), route)));
|
|
133
|
+
if (__requireBoolean(__normalize(task.authorize) === true)) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
let sender = null;
|
|
137
|
+
if (__requireBoolean(__normalize(this.#queueFactory) !== null)) {
|
|
138
|
+
let factory = __normalize(this.#queueFactory);
|
|
139
|
+
sender = __call(factory, __normalize(this.#definition.queueProvider.options));
|
|
140
|
+
}
|
|
141
|
+
__contextScope.push(__contextSet("queue", sender));
|
|
142
|
+
let handler = __normalize(this.#handlers[__normalize(route.path)]);
|
|
143
|
+
return __call(handler, __normalize(task.input));
|
|
144
|
+
return null;
|
|
145
|
+
} finally {
|
|
146
|
+
__contextRestore(__contextScope);
|
|
147
|
+
}
|
|
148
|
+
}), [0], "closure")).then(__withArity(__bindContexts((output) => {
|
|
149
|
+
const __contextScope = [];
|
|
150
|
+
try {
|
|
151
|
+
output = __boundary(output);
|
|
152
|
+
let body = __toJson(output);
|
|
153
|
+
if (__requireBoolean(!__requireBoolean(__isString(body)))) {
|
|
154
|
+
throw "Execution output is not JSON serializable";
|
|
155
|
+
}
|
|
156
|
+
if (__requireBoolean(__requireBoolean(__contextGet("db") !== null) && __requireBoolean(__normalize(__contextGet("db").finish())))) {
|
|
157
|
+
throw "UnclosedTransaction";
|
|
158
|
+
}
|
|
159
|
+
return { ok: true, output: __fromJson(body) };
|
|
160
|
+
return null;
|
|
161
|
+
} finally {
|
|
162
|
+
__contextRestore(__contextScope);
|
|
163
|
+
}
|
|
164
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
165
|
+
const __contextScope = [];
|
|
166
|
+
try {
|
|
167
|
+
error = __boundary(error);
|
|
168
|
+
let permanent = __isKindof(error, ExecutionFailure);
|
|
169
|
+
let code = "ExecutionFailed";
|
|
170
|
+
if (__requireBoolean(permanent)) {
|
|
171
|
+
code = __normalize(error.code);
|
|
172
|
+
}
|
|
173
|
+
__normalize(__contextGet("log").err(`code=${code}: ${errorDetails(error)}`));
|
|
174
|
+
return { ok: false, code: code, permanent: permanent, message: errorDetails(error) };
|
|
175
|
+
return null;
|
|
176
|
+
} finally {
|
|
177
|
+
__contextRestore(__contextScope);
|
|
178
|
+
}
|
|
179
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
180
|
+
const __contextScope = [];
|
|
181
|
+
try {
|
|
182
|
+
if (__requireBoolean(__contextGet("db") !== null)) {
|
|
183
|
+
__normalize(__contextGet("db").finish());
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
} finally {
|
|
187
|
+
__contextRestore(__contextScope);
|
|
188
|
+
}
|
|
189
|
+
}));
|
|
190
|
+
return null;
|
|
191
|
+
} finally {
|
|
192
|
+
__contextRestore(__contextScope);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
close() {
|
|
196
|
+
__normalize(this.#database.close());
|
|
197
|
+
return null;
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=execution_runtime.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_runtime.mjs","sources":["execution_runtime.lango"],"sourcesContent":["import \"./module_loader\"\n class ModuleLoader\nimport \"./database\"\n class Database\nimport \"./execution_identity\"\n executionSource(source)\n executionToken(identity, source, route)\nimport \"./execution_failure\"\n class ExecutionFailure\nimport \"./job_context\"\n class JobContext\nimport \"./execution_log\"\n class ExecutionLog\nimport \"./error_details\"\n errorDetails(error)\nimport js global Promise\nimport js global Object\n\nexport class ExecutionRuntime(definition)\n methods\n preload()\n execute(task)\n close()\n\nExecutionRuntime(definition)\n @definition = definition\n @loader = new ModuleLoader()\n @database = new Database(definition.database, definition.root)\n @handlers = Object/create(null)\n @identity = null\n @queueFactory = null\n\nExecutionRuntime:preload()\n pending = @database.open()\n for @definition.targets as route\n pending = prepare(pending, route)\n return chain pending\n then(ignored)\n return @loader.target(@definition.identity)\n then(handler)\n @identity = handler\n if @definition.queueProvider != null\n return @loader.target(@definition.queueProvider)\n return null\n then(factory)\n @queueFactory = factory\n return null\n catch(error)\n close()\n throw error\n\nExecutionRuntime:prepare(pending, route)\n return chain pending\n then(ignored)\n capture route\n return @loader.target(route)\n then(handler)\n capture route\n @handlers[route.path] = handler\n return null\n\nExecutionRuntime:execute(task)\n context db = @database.scope()\n context log\n route = null\n for @definition.targets as target\n if target.path == task.path\n route = target\n if route == null\n if db != null\n db.finish()\n throw \"Unregistered execution path\"\n context job = new JobContext(task, route)\n sink = log\n context log = new ExecutionLog(sink, job)\n return chain Promise/resolve(null)\n then(ignored)\n capture task\n capture route\n source = executionSource(task.source)\n Object/freeze(source)\n target = Object/freeze({\n path: route.path\n queue: route.queue\n policy: route.policy\n reauthSeconds: route.reauthSeconds\n })\n handler = @identity\n return handler({\n source: source\n route: target\n })\n then(identity)\n capture task\n capture route\n context token = executionToken(identity, task.source, route)\n if task.authorize == true\n return null\n sender = null\n if @queueFactory != null\n factory = @queueFactory\n sender = factory(@definition.queueProvider.options)\n context queue = sender\n handler = @handlers[route.path]\n return handler(task.input)\n then(output)\n body = to_json(output)\n if !is_string(body)\n throw \"Execution output is not JSON serializable\"\n if db != null and db.finish()\n throw \"UnclosedTransaction\"\n return {\n ok: true\n output: from_json(body)\n }\n catch(error)\n capture ExecutionFailure\n permanent = is_kindof(error, ExecutionFailure)\n code = \"ExecutionFailed\"\n if permanent\n code = error.code?\n log.err(\"code=${code}: ${errorDetails(error)}\")\n return {\n ok: false\n code: code\n permanent: permanent\n message: errorDetails(error)\n }\n finally\n if db != null\n db.finish()\n\nExecutionRuntime:close()\n @database.close()\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"8206a4ec90bbf74a729aaa54bd55077b09ef6d6862dc6c74851c094461775428","outputHash":"bc9082abb7c7d2d5812a97a6ad8ccc9363c68901c5bfec5945f3c0ecde2cb347","mapHash":"d6aa9fb37e87200908873628ca752ea0d8287af787534ae7692eaa0af874cda0"}}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ExecutionRuntime } from "./execution_runtime.mjs";
|
|
2
|
+
import * as threads from "node:worker_threads";
|
|
3
|
+
import {
|
|
4
|
+
hostGlobal as __hostGlobal,
|
|
5
|
+
bindContexts as __bindContexts,
|
|
6
|
+
normalize as __normalize,
|
|
7
|
+
boundary as __boundary,
|
|
8
|
+
requireArity as __requireArity,
|
|
9
|
+
withArity as __withArity,
|
|
10
|
+
} from "./.lango/runtime.mjs";
|
|
11
|
+
|
|
12
|
+
let runtime = new ExecutionRuntime(__normalize(threads.workerData));
|
|
13
|
+
|
|
14
|
+
export let execute = initialize();
|
|
15
|
+
function initialize() {
|
|
16
|
+
__normalize(__hostGlobal(["process"]).setSourceMapsEnabled(true));
|
|
17
|
+
__normalize(__hostGlobal(["process"]).once("exit", __bindContexts(() => {
|
|
18
|
+
__normalize(runtime.close());
|
|
19
|
+
return null;
|
|
20
|
+
})));
|
|
21
|
+
return __normalize(runtime.preload()).then(__withArity(__bindContexts((ignored) => {
|
|
22
|
+
ignored = __boundary(ignored);
|
|
23
|
+
__normalize(threads.parentPort.postMessage({ kind: "ready", threadId: __normalize(threads.threadId) }));
|
|
24
|
+
return workerHandler();
|
|
25
|
+
return null;
|
|
26
|
+
}), [0], "closure"));
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function workerHandler() {
|
|
31
|
+
return run;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function run(task) {
|
|
36
|
+
__requireArity(arguments.length, [0], "run");
|
|
37
|
+
task = __boundary(task);
|
|
38
|
+
return __normalize(runtime.execute(task));
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=execution_worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_worker.mjs","sources":["execution_worker.lango"],"sourcesContent":["import \"./execution_runtime\"\n class ExecutionRuntime\nimport js \"node:worker_threads\" as threads\nimport js global process\n\nexport global execute\n\nglobal runtime = new ExecutionRuntime(threads.workerData)\nglobal execute = initialize()\n\ninitialize()\n global runtime\n process.setSourceMapsEnabled(true)\n process.once(\"exit\", ()\n global runtime\n runtime.close()\n )\n return chain runtime.preload()\n then(ignored)\n capture threads\n threads/parentPort/postMessage({\n kind: \"ready\"\n threadId: threads.threadId\n })\n return workerHandler()\n\nworkerHandler()\n return run\n\nrun(task)\n global runtime\n return runtime.execute(task)\n"],"names":[],"mappings":";;;;;;;;;;;AAOA;;AACA;AAEA;;;;;;;;;;;;;;;AAgBA;;;;;AAGA;;;;;;;","x_lango_build":{"fingerprint":"7dbc9e2444c7d6ef70e697efa133e7c26fcf208a674c53f4425401f494e478f7","outputHash":"cd86946a7992331bc108c786892523e4351d9f6c310d273b6267fdf02984f2f6","mapHash":"675fa2caa6ac99dd1f222270d5ca7fd7db8e0469c4b23a8b89c9379eb4790358"}}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { readDatabaseConfiguration, readJobRoutes } from "./job_configuration.mjs";
|
|
2
|
+
import { executionSource } from "./execution_identity.mjs";
|
|
3
|
+
import { queueProvider } from "./queue_provider.mjs";
|
|
4
|
+
import { errorDetails } from "./error_details.mjs";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import * as modules from "node:module";
|
|
7
|
+
import * as piscina from "piscina";
|
|
8
|
+
import {
|
|
9
|
+
hostGlobal as __hostGlobal,
|
|
10
|
+
hostGlobalCall as __hostGlobalCall,
|
|
11
|
+
contextGet as __contextGet,
|
|
12
|
+
contextSet as __contextSet,
|
|
13
|
+
contextRestore as __contextRestore,
|
|
14
|
+
bindContexts as __bindContexts,
|
|
15
|
+
fromJson as __fromJson,
|
|
16
|
+
toJson as __toJson,
|
|
17
|
+
getClass as __getClass,
|
|
18
|
+
isKindof as __isKindof,
|
|
19
|
+
isNumber as __isNumber,
|
|
20
|
+
isString as __isString,
|
|
21
|
+
isBool as __isBool,
|
|
22
|
+
isObject as __isObject,
|
|
23
|
+
isArray as __isArray,
|
|
24
|
+
entries as __entries,
|
|
25
|
+
add as __add,
|
|
26
|
+
requireBoolean as __requireBoolean,
|
|
27
|
+
normalize as __normalize,
|
|
28
|
+
boundary as __boundary,
|
|
29
|
+
requireArity as __requireArity,
|
|
30
|
+
withArity as __withArity,
|
|
31
|
+
} from "./.lango/runtime.mjs";
|
|
32
|
+
|
|
33
|
+
export class Executor {
|
|
34
|
+
#definition;
|
|
35
|
+
#workers;
|
|
36
|
+
#maxQueue;
|
|
37
|
+
#timeout;
|
|
38
|
+
#startupTimeout;
|
|
39
|
+
#shutdownTimeout;
|
|
40
|
+
#state;
|
|
41
|
+
#pool;
|
|
42
|
+
#closing;
|
|
43
|
+
#closeError;
|
|
44
|
+
#ready;
|
|
45
|
+
#readyIds;
|
|
46
|
+
#lango;
|
|
47
|
+
constructor(directory, settings) {
|
|
48
|
+
__requireArity(arguments.length, [0, 1], "constructor");
|
|
49
|
+
directory = __boundary(directory);
|
|
50
|
+
settings = __boundary(settings);
|
|
51
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(directory))) || __requireBoolean(directory === ""))) {
|
|
52
|
+
throw "Executor requires an application directory";
|
|
53
|
+
}
|
|
54
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(settings === null) || __requireBoolean(!__requireBoolean(__isObject(settings)))) || __requireBoolean(__isArray(settings)))) {
|
|
55
|
+
throw "Executor requires settings and an identity provider";
|
|
56
|
+
}
|
|
57
|
+
let allowed = ["identity", "workers", "maxQueue", "timeoutMs", "startupTimeoutMs", "shutdownTimeoutMs", "queueProvider"];
|
|
58
|
+
for (const [key, value] of __entries(settings)) {
|
|
59
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
60
|
+
throw `Unknown executor option: ${key}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let root = __normalize(path.resolve(directory));
|
|
64
|
+
this.#definition = { root: root, database: readDatabaseConfiguration(root), targets: readJobRoutes(root), identity: provider(root, __normalize(settings.identity)), queueProvider: queueProvider(root, __normalize(settings.queueProvider)) };
|
|
65
|
+
this.#workers = limit(__normalize(settings.workers), 2, 1, 128, "workers");
|
|
66
|
+
this.#maxQueue = limit(__normalize(settings.maxQueue), 64, 0, 2147483647, "maxQueue");
|
|
67
|
+
this.#timeout = limit(__normalize(settings.timeoutMs), 900000, 1, 2147483647, "timeoutMs");
|
|
68
|
+
this.#startupTimeout = limit(__normalize(settings.startupTimeoutMs), 30000, 1, 2147483647, "startupTimeoutMs");
|
|
69
|
+
this.#shutdownTimeout = limit(__normalize(settings.shutdownTimeoutMs), 30000, 1, 2147483647, "shutdownTimeoutMs");
|
|
70
|
+
this.#state = "new";
|
|
71
|
+
this.#pool = null;
|
|
72
|
+
this.#closing = null;
|
|
73
|
+
this.#closeError = null;
|
|
74
|
+
this.#ready = __normalize(__hostGlobal(["Promise"]).withResolvers());
|
|
75
|
+
this.#readyIds = [];
|
|
76
|
+
}
|
|
77
|
+
start() {
|
|
78
|
+
if (__requireBoolean(__normalize(this.#state) !== "new")) {
|
|
79
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Executor can only be started once"));
|
|
80
|
+
}
|
|
81
|
+
this.#state = "starting";
|
|
82
|
+
try {
|
|
83
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(__normalize(this.#definition.root), "package.json"))));
|
|
84
|
+
let entry = __normalize(resolver.resolve("@lango-project/lango-server"));
|
|
85
|
+
this.#pool = new piscina.Piscina({ filename: __normalize(path.join(__normalize(path.dirname(entry)), "execution_worker.mjs")), name: "execute", minThreads: __normalize(this.#workers), maxThreads: __normalize(this.#workers), maxQueue: __normalize(this.#maxQueue), concurrentTasksPerWorker: 1, closeTimeout: __normalize(this.#shutdownTimeout), workerData: __normalize(this.#definition) });
|
|
86
|
+
let pool = __normalize(this.#pool);
|
|
87
|
+
__normalize(pool.on("message", __withArity(__bindContexts((message) => {
|
|
88
|
+
message = __boundary(message);
|
|
89
|
+
if (__requireBoolean(__requireBoolean(__normalize(message.kind) === "ready") && __requireBoolean(!__requireBoolean(__normalize(this.#readyIds.includes(__normalize(message.threadId))))))) {
|
|
90
|
+
__add(this.#readyIds, __normalize(message.threadId));
|
|
91
|
+
if (__requireBoolean(__normalize(this.#readyIds.length) === __normalize(this.#workers))) {
|
|
92
|
+
__normalize(this.#ready.resolve(null));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}), [0], "closure")));
|
|
97
|
+
__normalize(pool.on("error", __withArity(__bindContexts((error) => {
|
|
98
|
+
const __contextScope = [];
|
|
99
|
+
try {
|
|
100
|
+
error = __boundary(error);
|
|
101
|
+
__normalize(this.#ready.reject(error));
|
|
102
|
+
if (__requireBoolean(__normalize(this.#state) === "closing")) {
|
|
103
|
+
this.#closeError = error;
|
|
104
|
+
}
|
|
105
|
+
__normalize(__contextGet("log").err(`Execution worker error: ${errorDetails(error)}`));
|
|
106
|
+
return null;
|
|
107
|
+
} finally {
|
|
108
|
+
__contextRestore(__contextScope);
|
|
109
|
+
}
|
|
110
|
+
}), [0], "closure")));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
this.#state = "closed";
|
|
113
|
+
return __normalize(__hostGlobal(["Promise"]).reject(error));
|
|
114
|
+
}
|
|
115
|
+
let expire = __bindContexts(() => {
|
|
116
|
+
__normalize(this.#ready.reject("Executor startup timed out"));
|
|
117
|
+
return null;
|
|
118
|
+
});
|
|
119
|
+
let timer = __hostGlobalCall(["setTimeout"], expire, __normalize(this.#startupTimeout));
|
|
120
|
+
return __normalize(this.#ready.promise).then(__withArity(__bindContexts((ignored) => {
|
|
121
|
+
ignored = __boundary(ignored);
|
|
122
|
+
if (__requireBoolean(__normalize(this.#state) !== "starting")) {
|
|
123
|
+
throw "Executor closed during startup";
|
|
124
|
+
}
|
|
125
|
+
this.#state = "running";
|
|
126
|
+
return null;
|
|
127
|
+
return null;
|
|
128
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
129
|
+
error = __boundary(error);
|
|
130
|
+
return this.#destroy().then(__withArity(__bindContexts((ignored) => {
|
|
131
|
+
ignored = __boundary(ignored);
|
|
132
|
+
this.#state = "closed";
|
|
133
|
+
throw error;
|
|
134
|
+
return null;
|
|
135
|
+
}), [0], "closure"));
|
|
136
|
+
return null;
|
|
137
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
138
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
139
|
+
return null;
|
|
140
|
+
}));
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
execute(task, options = null) {
|
|
144
|
+
__requireArity(arguments.length, [0], "execute");
|
|
145
|
+
task = __boundary(task);
|
|
146
|
+
options = __boundary(options);
|
|
147
|
+
return this.#run(task, options, false);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
authorize(task, options = null) {
|
|
151
|
+
__requireArity(arguments.length, [0], "authorize");
|
|
152
|
+
task = __boundary(task);
|
|
153
|
+
options = __boundary(options);
|
|
154
|
+
return this.#run(task, options, true);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
#run(task, options, authorize) {
|
|
158
|
+
__requireArity(arguments.length, [0, 1, 2], "run");
|
|
159
|
+
task = __boundary(task);
|
|
160
|
+
options = __boundary(options);
|
|
161
|
+
authorize = __boundary(authorize);
|
|
162
|
+
if (__requireBoolean(__normalize(this.#state) !== "running")) {
|
|
163
|
+
return __normalize(__hostGlobal(["Promise"]).reject("Executor is not running"));
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
task = validateTask(task, __normalize(this.#definition.targets));
|
|
167
|
+
task.authorize = authorize;
|
|
168
|
+
options = executionOptions(options, __normalize(this.#timeout));
|
|
169
|
+
} catch (error) {
|
|
170
|
+
return __normalize(__hostGlobal(["Promise"]).reject(error));
|
|
171
|
+
}
|
|
172
|
+
let controller = new (__hostGlobal(["AbortController"]))();
|
|
173
|
+
let status = { expired: false };
|
|
174
|
+
let signal = __normalize(options.signal);
|
|
175
|
+
let abort = __bindContexts(() => {
|
|
176
|
+
__normalize(controller.abort());
|
|
177
|
+
return null;
|
|
178
|
+
});
|
|
179
|
+
if (__requireBoolean(signal !== null)) {
|
|
180
|
+
__normalize(signal.addEventListener("abort", abort, { once: true }));
|
|
181
|
+
if (__requireBoolean(__normalize(signal.aborted))) {
|
|
182
|
+
__normalize(controller.abort());
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
let expire = __bindContexts(() => {
|
|
186
|
+
if (__requireBoolean(__normalize(status.expired))) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
status.expired = true;
|
|
190
|
+
__normalize(controller.abort());
|
|
191
|
+
return null;
|
|
192
|
+
});
|
|
193
|
+
let timer = __hostGlobalCall(["setTimeout"], expire, __normalize(options.timeoutMs));
|
|
194
|
+
return __normalize(this.#pool.run(task, { signal: __normalize(controller.signal) })).then(__withArity(__bindContexts((result) => {
|
|
195
|
+
result = __boundary(result);
|
|
196
|
+
if (__requireBoolean(!__requireBoolean(__normalize(result.ok)))) {
|
|
197
|
+
let error = new (__hostGlobal(["Error"]))(__normalize(result.message));
|
|
198
|
+
error.code = __normalize(result.code);
|
|
199
|
+
error.permanent = __normalize(result.permanent);
|
|
200
|
+
error.logged = true;
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
return __normalize(result.output);
|
|
204
|
+
return null;
|
|
205
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
206
|
+
const __contextScope = [];
|
|
207
|
+
try {
|
|
208
|
+
error = __boundary(error);
|
|
209
|
+
if (__requireBoolean(__normalize(status.expired))) {
|
|
210
|
+
error = new (__hostGlobal(["Error"]))("Execution attempt timed out");
|
|
211
|
+
error.code = "ExecutionTimeout";
|
|
212
|
+
error.permanent = false;
|
|
213
|
+
} else {
|
|
214
|
+
if (__requireBoolean(__requireBoolean(signal !== null) && __requireBoolean(__normalize(signal.aborted)))) {
|
|
215
|
+
error = new (__hostGlobal(["Error"]))("Execution attempt was aborted");
|
|
216
|
+
error.code = "ExecutionAborted";
|
|
217
|
+
error.permanent = false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (__requireBoolean(!__requireBoolean(__isObject(error)))) {
|
|
221
|
+
error = new (__hostGlobal(["Error"]))(`${error}`);
|
|
222
|
+
}
|
|
223
|
+
if (__requireBoolean(__normalize(error.permanent) !== true)) {
|
|
224
|
+
error.permanent = false;
|
|
225
|
+
}
|
|
226
|
+
if (__requireBoolean(__normalize(error.logged) !== true)) {
|
|
227
|
+
let source = __normalize(task.source);
|
|
228
|
+
__normalize(__contextGet("log").err(`[jobId=${__normalize(task.id)}] path=${__normalize(task.path)} attempt=${__normalize(task.attempt)} clientId=${__normalize(source.clientId)} userId=${__normalize(source.userId)} identity=${__normalize(source.identity)}: ${errorDetails(error)}`));
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
return null;
|
|
232
|
+
} finally {
|
|
233
|
+
__contextRestore(__contextScope);
|
|
234
|
+
}
|
|
235
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
236
|
+
__hostGlobalCall(["clearTimeout"], timer);
|
|
237
|
+
if (__requireBoolean(signal !== null)) {
|
|
238
|
+
__normalize(signal.removeEventListener("abort", abort));
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}));
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
close() {
|
|
245
|
+
if (__requireBoolean(__normalize(this.#closing) !== null)) {
|
|
246
|
+
return __normalize(this.#closing);
|
|
247
|
+
}
|
|
248
|
+
if (__requireBoolean(__requireBoolean(__normalize(this.#state) === "closed") || __requireBoolean(__normalize(this.#state) === "new"))) {
|
|
249
|
+
this.#state = "closed";
|
|
250
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
251
|
+
}
|
|
252
|
+
let state = __normalize(this.#state);
|
|
253
|
+
this.#state = "closing";
|
|
254
|
+
if (__requireBoolean(state === "starting")) {
|
|
255
|
+
__normalize(this.#ready.reject("Executor closed during startup"));
|
|
256
|
+
this.#closing = this.#destroy();
|
|
257
|
+
} else {
|
|
258
|
+
this.#closing = this.#drain();
|
|
259
|
+
}
|
|
260
|
+
return __normalize(this.#closing);
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
#drain() {
|
|
264
|
+
return __normalize(this.#pool.close()).then(__withArity(__bindContexts((ignored) => {
|
|
265
|
+
ignored = __boundary(ignored);
|
|
266
|
+
if (__requireBoolean(__normalize(this.#closeError) !== null)) {
|
|
267
|
+
throw __normalize(this.#closeError);
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
return null;
|
|
271
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
272
|
+
return this.#destroy();
|
|
273
|
+
return null;
|
|
274
|
+
})).finally(__bindContexts(() => {
|
|
275
|
+
this.#state = "closed";
|
|
276
|
+
return null;
|
|
277
|
+
}));
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
#destroy() {
|
|
281
|
+
if (__requireBoolean(__normalize(this.#pool) === null)) {
|
|
282
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
283
|
+
}
|
|
284
|
+
let pool = __normalize(this.#pool);
|
|
285
|
+
this.#pool = null;
|
|
286
|
+
return __normalize(pool.destroy());
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function provider(root, spec) {
|
|
291
|
+
__requireArity(arguments.length, [0, 1], "provider");
|
|
292
|
+
root = __boundary(root);
|
|
293
|
+
spec = __boundary(spec);
|
|
294
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(spec === null) || __requireBoolean(!__requireBoolean(__isObject(spec)))) || __requireBoolean(__isArray(spec)))) {
|
|
295
|
+
throw "Executor identity provider requires module and handler";
|
|
296
|
+
}
|
|
297
|
+
for (const [key, value] of __entries(spec)) {
|
|
298
|
+
if (__requireBoolean(__requireBoolean(key !== "module") && __requireBoolean(key !== "handler"))) {
|
|
299
|
+
throw `Unknown identity provider option: ${key}`;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.module)))) || __requireBoolean(__normalize(spec.module) === "")) || __requireBoolean(__normalize(spec.module.startsWith("node:")))) || __requireBoolean(!__requireBoolean(__isString(__normalize(spec.handler))))) || __requireBoolean(__normalize(spec.handler) === ""))) {
|
|
303
|
+
throw "Executor identity provider requires module and handler";
|
|
304
|
+
}
|
|
305
|
+
let resolver = __normalize(modules.createRequire(__normalize(path.join(root, "package.json"))));
|
|
306
|
+
let file = __normalize(resolver.resolve(__normalize(spec.module)));
|
|
307
|
+
if (__requireBoolean(!__requireBoolean(__normalize(path.isAbsolute(file))))) {
|
|
308
|
+
throw "Identity provider must resolve to an application module";
|
|
309
|
+
}
|
|
310
|
+
return { module: file, handler: __normalize(spec.handler) };
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function limit(value, fallback, minimum, maximum, name) {
|
|
315
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4], "limit");
|
|
316
|
+
value = __boundary(value);
|
|
317
|
+
fallback = __boundary(fallback);
|
|
318
|
+
minimum = __boundary(minimum);
|
|
319
|
+
maximum = __boundary(maximum);
|
|
320
|
+
name = __boundary(name);
|
|
321
|
+
if (__requireBoolean(value === null)) {
|
|
322
|
+
return fallback;
|
|
323
|
+
}
|
|
324
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value)))) || __requireBoolean(value < minimum)) || __requireBoolean(value > maximum))) {
|
|
325
|
+
throw `Invalid executor option: ${name}`;
|
|
326
|
+
}
|
|
327
|
+
return value;
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function executionOptions(options, timeout) {
|
|
332
|
+
__requireArity(arguments.length, [0, 1], "executionOptions");
|
|
333
|
+
options = __boundary(options);
|
|
334
|
+
timeout = __boundary(timeout);
|
|
335
|
+
if (__requireBoolean(options === null)) {
|
|
336
|
+
options = { };
|
|
337
|
+
}
|
|
338
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(options))) || __requireBoolean(__isArray(options)))) {
|
|
339
|
+
throw "Execution options must be a map";
|
|
340
|
+
}
|
|
341
|
+
for (const [key, value] of __entries(options)) {
|
|
342
|
+
if (__requireBoolean(__requireBoolean(key !== "signal") && __requireBoolean(key !== "timeoutMs"))) {
|
|
343
|
+
throw `Unknown execution option: ${key}`;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (__requireBoolean(__requireBoolean(__normalize(options.signal) !== null) && __requireBoolean(!__requireBoolean(__isKindof(__normalize(options.signal), __normalize(__hostGlobal(["AbortSignal"]))))))) {
|
|
347
|
+
throw "Execution signal must be an AbortSignal";
|
|
348
|
+
}
|
|
349
|
+
return { signal: __normalize(options.signal), timeoutMs: limit(__normalize(options.timeoutMs), timeout, 1, 2147483647, "timeoutMs") };
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function validateTask(task, targets) {
|
|
354
|
+
__requireArity(arguments.length, [0, 1], "validateTask");
|
|
355
|
+
task = __boundary(task);
|
|
356
|
+
targets = __boundary(targets);
|
|
357
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(task === null) || __requireBoolean(!__requireBoolean(__isObject(task)))) || __requireBoolean(__isArray(task)))) {
|
|
358
|
+
throw "Execution task must be a map";
|
|
359
|
+
}
|
|
360
|
+
let allowed = ["id", "path", "input", "source", "attempt"];
|
|
361
|
+
for (const [key, value] of __entries(task)) {
|
|
362
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
363
|
+
throw `Unknown execution task option: ${key}`;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(task.id)))) || __requireBoolean(__normalize(task.id) === "")) || __requireBoolean(task.id.length > 200))) {
|
|
367
|
+
throw "Execution task requires a job id";
|
|
368
|
+
}
|
|
369
|
+
let found = false;
|
|
370
|
+
for (const route of targets) {
|
|
371
|
+
if (__requireBoolean(__normalize(route.path) === __normalize(task.path))) {
|
|
372
|
+
found = true;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (__requireBoolean(!__requireBoolean(found))) {
|
|
376
|
+
throw "Unregistered execution path";
|
|
377
|
+
}
|
|
378
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(task.input) === null) || __requireBoolean(!__requireBoolean(__isObject(__normalize(task.input))))) || __requireBoolean(__isArray(__normalize(task.input))))) {
|
|
379
|
+
throw "Execution input must be a JSON map";
|
|
380
|
+
}
|
|
381
|
+
return { id: __normalize(task.id), path: __normalize(task.path), input: __fromJson(__toJson(__normalize(task.input))), source: executionSource(__normalize(task.source)), attempt: limit(__normalize(task.attempt), 1, 1, 2147483647, "attempt") };
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=executor.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.mjs","sources":["executor.lango"],"sourcesContent":["import \"./job_configuration\"\n readDatabaseConfiguration(directory)\n readJobRoutes(directory)\nimport \"./execution_identity\"\n executionSource(source)\nimport \"./queue_provider\"\n queueProvider(root, spec)\nimport \"./error_details\"\n errorDetails(error)\nimport js \"node:path\" as path\nimport js \"node:module\" as modules\nimport js \"piscina\" as piscina\nimport js global Object\nimport js global Number\nimport js global Promise\nimport js global Error\nimport js global AbortController\nimport js global AbortSignal\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class Executor(directory, settings)\n methods\n start()\n execute(task, options = null)\n authorize(task, options = null)\n close()\n\nExecutor(directory, settings)\n if !is_string(directory) or directory == \"\"\n throw \"Executor requires an application directory\"\n if settings == null or !is_object(settings) or is_array(settings)\n throw \"Executor requires settings and an identity provider\"\n allowed = [\"identity\", \"workers\", \"maxQueue\", \"timeoutMs\", \"startupTimeoutMs\", \"shutdownTimeoutMs\", \"queueProvider\"]\n for settings as key: value\n if !allowed.includes(key)\n throw \"Unknown executor option: ${key}\"\n root = path/resolve(directory)\n @definition = {\n root: root\n database: readDatabaseConfiguration(root)\n targets: readJobRoutes(root)\n identity: provider(root, settings.identity)\n queueProvider: queueProvider(root, settings.queueProvider)\n }\n @workers = limit(settings.workers, 2, 1, 128, \"workers\")\n @maxQueue = limit(settings.maxQueue, 64, 0, 2147483647, \"maxQueue\")\n @timeout = limit(settings.timeoutMs, 900000, 1, 2147483647, \"timeoutMs\")\n @startupTimeout = limit(settings.startupTimeoutMs, 30000, 1, 2147483647, \"startupTimeoutMs\")\n @shutdownTimeout = limit(settings.shutdownTimeoutMs, 30000, 1, 2147483647, \"shutdownTimeoutMs\")\n @state = \"new\"\n @pool = null\n @closing = null\n @closeError = null\n @ready = Promise/withResolvers()\n @readyIds = []\n\nprovider(root, spec)\n if spec == null or !is_object(spec) or is_array(spec)\n throw \"Executor identity provider requires module and handler\"\n for spec as key: value\n if key != \"module\" and key != \"handler\"\n throw \"Unknown identity provider option: ${key}\"\n if !is_string(spec.module) or spec.module == \"\" or spec.module.startsWith(\"node:\") or !is_string(spec.handler) or spec.handler == \"\"\n throw \"Executor identity provider requires module and handler\"\n resolver = modules/createRequire(path/join(root, \"package.json\"))\n file = resolver.resolve(spec.module)\n if !path/isAbsolute(file)\n throw \"Identity provider must resolve to an application module\"\n return {\n module: file\n handler: spec.handler\n }\n\nlimit(value, fallback, minimum, maximum, name)\n if value == null\n return fallback\n if !Number/isSafeInteger(value) or value < minimum or value > maximum\n throw \"Invalid executor option: ${name}\"\n return value\n\nExecutor:start()\n if @state != \"new\"\n return Promise/reject(\"Executor can only be started once\")\n @state = \"starting\"\n try\n resolver = modules/createRequire(path/join(@definition.root, \"package.json\"))\n entry = resolver.resolve(\"@lango-project/lango-server\")\n @pool = new piscina/Piscina({\n filename: path/join(path/dirname(entry), \"execution_worker.mjs\")\n name: \"execute\"\n minThreads: @workers\n maxThreads: @workers\n maxQueue: @maxQueue\n concurrentTasksPerWorker: 1\n closeTimeout: @shutdownTimeout\n workerData: @definition\n })\n pool = @pool\n pool.on(\"message\", (message)\n if message.kind == \"ready\" and !@readyIds.includes(message.threadId)\n @readyIds.add(message.threadId)\n if @readyIds.length == @workers\n @ready.resolve(null)\n )\n pool.on(\"error\", (error)\n @ready.reject(error)\n if @state == \"closing\"\n @closeError = error\n context log\n log.err(\"Execution worker error: ${errorDetails(error)}\")\n )\n catch error\n @state = \"closed\"\n return Promise/reject(error)\n expire = ()\n @ready.reject(\"Executor startup timed out\")\n timer = setTimeout(expire, @startupTimeout)\n return chain @ready.promise\n then(ignored)\n if @state != \"starting\"\n throw \"Executor closed during startup\"\n @state = \"running\"\n return null\n catch(error)\n return chain destroy()\n then(ignored)\n capture error\n @state = \"closed\"\n throw error\n finally\n capture timer\n clearTimeout(timer)\n\nExecutor:execute(task, options = null)\n return run(task, options, false)\n\nExecutor:authorize(task, options = null)\n return run(task, options, true)\n\nExecutor:run(task, options, authorize)\n if @state != \"running\"\n return Promise/reject(\"Executor is not running\")\n try\n task = validateTask(task, @definition.targets)\n task.authorize = authorize\n options = executionOptions(options, @timeout)\n catch error\n return Promise/reject(error)\n controller = new AbortController()\n status = {\n expired: false\n }\n signal = options.signal\n abort = ()\n capture controller\n controller.abort()\n if signal != null\n signal.addEventListener(\"abort\", abort, {\n once: true\n })\n if signal.aborted\n controller.abort()\n expire = ()\n capture controller\n capture status\n if status.expired\n return null\n status.expired = true\n controller.abort()\n timer = setTimeout(expire, options.timeoutMs)\n return chain @pool.run(task, {\n signal: controller.signal\n })\n then(result)\n if !result.ok\n error = new Error(result.message)\n error.code = result.code\n error.permanent = result.permanent\n error.logged = true\n throw error\n return result.output\n catch(error)\n capture status\n capture task\n capture signal\n if status.expired\n error = new Error(\"Execution attempt timed out\")\n error.code = \"ExecutionTimeout\"\n error.permanent = false\n else\n if signal != null and signal.aborted\n error = new Error(\"Execution attempt was aborted\")\n error.code = \"ExecutionAborted\"\n error.permanent = false\n if !is_object(error)\n error = new Error(\"${error}\")\n if error.permanent != true\n error.permanent = false\n context log\n if error.logged != true\n source = task.source\n log.err(\"[jobId=${task.id}] path=${task.path} attempt=${task.attempt} clientId=${source.clientId} userId=${source.userId} identity=${source.identity}: ${errorDetails(error)}\")\n throw error\n finally\n capture timer\n capture signal\n capture abort\n clearTimeout(timer)\n if signal != null\n signal.removeEventListener(\"abort\", abort)\n\nexecutionOptions(options, timeout)\n if options == null\n options = {}\n if !is_object(options) or is_array(options)\n throw \"Execution options must be a map\"\n for options as key: value\n if key != \"signal\" and key != \"timeoutMs\"\n throw \"Unknown execution option: ${key}\"\n if options.signal != null and !is_kindof(options.signal, AbortSignal)\n throw \"Execution signal must be an AbortSignal\"\n return {\n signal: options.signal\n timeoutMs: limit(options.timeoutMs, timeout, 1, 2147483647, \"timeoutMs\")\n }\n\nvalidateTask(task, targets)\n if task == null or !is_object(task) or is_array(task)\n throw \"Execution task must be a map\"\n allowed = [\"id\", \"path\", \"input\", \"source\", \"attempt\"]\n for task as key: value\n if !allowed.includes(key)\n throw \"Unknown execution task option: ${key}\"\n if !is_string(task.id) or task.id == \"\" or task.id.length > 200\n throw \"Execution task requires a job id\"\n found = false\n for targets as route\n if route.path == task.path\n found = true\n if !found\n throw \"Unregistered execution path\"\n if task.input == null or !is_object(task.input) or is_array(task.input)\n throw \"Execution input must be a JSON map\"\n return {\n id: task.id\n path: task.path\n input: from_json(to_json(task.input))\n source: executionSource(task.source)\n attempt: limit(task.attempt, 1, 1, 2147483647, \"attempt\")\n }\n\nExecutor:close()\n if @closing != null\n return @closing\n if @state == \"closed\" or @state == \"new\"\n @state = \"closed\"\n return Promise/resolve(null)\n state = @state\n @state = \"closing\"\n if state == \"starting\"\n @ready.reject(\"Executor closed during startup\")\n @closing = destroy()\n else\n @closing = drain()\n return @closing\n\nExecutor:drain()\n return chain @pool.close()\n then(ignored)\n if @closeError != null\n throw @closeError\n return null\n finally\n return destroy()\n finally\n @state = \"closed\"\n\nExecutor:destroy()\n if @pool == null\n return Promise/resolve(null)\n pool = @pool\n @pool = null\n return pool.destroy()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA;;;;;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;;;;;AA0IA;;;;;;;;;;;;;;;;;;;;;;AAeA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"9b79b4002c95b68c8f567c3b9b13513a2f2dabef62561f5e39a7e1227a5b6619","outputHash":"9d3249297fbdba4c809ace5ca6bad54136934de93723358eccf8f69d9cd0af2c","mapHash":"59c6fd6c30de302e9420291c886df83643fb2ce5441a65e5e2491d9865e268b2"}}
|