@lango-project/lango-boss 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +111 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +725 -0
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/boss.mjs +136 -0
- package/dist/boss.mjs.map +1 -0
- package/dist/claim_watch.mjs +99 -0
- package/dist/claim_watch.mjs.map +1 -0
- package/dist/cli.mjs +143 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/configuration.mjs +58 -0
- package/dist/configuration.mjs.map +1 -0
- package/dist/dispatcher.mjs +108 -0
- package/dist/dispatcher.mjs.map +1 -0
- package/dist/driver.mjs +98 -0
- package/dist/driver.mjs.map +1 -0
- package/dist/envelope.mjs +92 -0
- package/dist/envelope.mjs.map +1 -0
- package/dist/library.mjs +139 -0
- package/dist/library.mjs.map +1 -0
- package/dist/management.mjs +360 -0
- package/dist/management.mjs.map +1 -0
- package/dist/management_transaction.mjs +26 -0
- package/dist/management_transaction.mjs.map +1 -0
- package/dist/namespace.mjs +27 -0
- package/dist/namespace.mjs.map +1 -0
- package/dist/queue.mjs +161 -0
- package/dist/queue.mjs.map +1 -0
- package/dist/reads.mjs +359 -0
- package/dist/reads.mjs.map +1 -0
- package/dist/retention.mjs +133 -0
- package/dist/retention.mjs.map +1 -0
- package/dist/schedule_sync.mjs +222 -0
- package/dist/schedule_sync.mjs.map +1 -0
- package/dist/schedule_transaction.mjs +24 -0
- package/dist/schedule_transaction.mjs.map +1 -0
- package/dist/schedules.mjs +201 -0
- package/dist/schedules.mjs.map +1 -0
- package/dist/transaction.mjs +16 -0
- package/dist/transaction.mjs.map +1 -0
- package/dist/validation.mjs +90 -0
- package/dist/validation.mjs.map +1 -0
- package/dist/worker.mjs +316 -0
- package/dist/worker.mjs.map +1 -0
- package/docs/guide.md +88 -0
- package/docs/management.md +93 -0
- package/docs/producer.md +115 -0
- package/docs/reads.md +107 -0
- package/docs/reference.md +77 -0
- package/docs/release.md +103 -0
- package/docs/retention.md +47 -0
- package/docs/scheduling.md +79 -0
- package/docs/worker.md +90 -0
- package/examples/reports/README.md +118 -0
- package/examples/reports/app.lango +133 -0
- package/examples/reports/deploy.xml +6 -0
- package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
- package/examples/reports/dist/.lango/localization.mjs +108 -0
- package/examples/reports/dist/.lango/runtime.mjs +725 -0
- package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
- package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
- package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
- package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
- package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
- package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
- package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
- package/examples/reports/dist/.lango/xmldom/index.js +41 -0
- package/examples/reports/dist/.lango/xmldom/package.json +1 -0
- package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
- package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
- package/examples/reports/dist/app.mjs +137 -0
- package/examples/reports/dist/app.mjs.map +1 -0
- package/examples/reports/dist/handlers.mjs +202 -0
- package/examples/reports/dist/handlers.mjs.map +1 -0
- package/examples/reports/dist/setup.mjs +14 -0
- package/examples/reports/dist/setup.mjs.map +1 -0
- package/examples/reports/handlers.lango +153 -0
- package/examples/reports/jobs.xml +5 -0
- package/examples/reports/package.json +23 -0
- package/examples/reports/routes.xml +4 -0
- package/examples/reports/setup.lango +17 -0
- package/package.json +60 -4
- package/src/boss.lango +92 -0
- package/src/claim_watch.lango +58 -0
- package/src/cli.lango +103 -0
- package/src/configuration.lango +43 -0
- package/src/dispatcher.lango +75 -0
- package/src/driver.lango +72 -0
- package/src/envelope.lango +56 -0
- package/src/library.lango +66 -0
- package/src/management.lango +302 -0
- package/src/management_transaction.lango +28 -0
- package/src/namespace.lango +15 -0
- package/src/queue.lango +115 -0
- package/src/reads.lango +277 -0
- package/src/retention.lango +107 -0
- package/src/schedule_sync.lango +190 -0
- package/src/schedule_transaction.lango +22 -0
- package/src/schedules.lango +165 -0
- package/src/transaction.lango +23 -0
- package/src/validation.lango +45 -0
- package/src/worker.lango +230 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { schemaName } from "./namespace.mjs";
|
|
2
|
+
import { usable, manager, uuid } from "./reads.mjs";
|
|
3
|
+
import { name } from "./validation.mjs";
|
|
4
|
+
import { initializeSchedules, operation as scheduleOperation } from "./schedule_sync.mjs";
|
|
5
|
+
import * as server from "@lango-project/lango-server";
|
|
6
|
+
import {
|
|
7
|
+
hostGlobal as __hostGlobal,
|
|
8
|
+
contextGet as __contextGet,
|
|
9
|
+
contextSet as __contextSet,
|
|
10
|
+
contextRestore as __contextRestore,
|
|
11
|
+
bindContexts as __bindContexts,
|
|
12
|
+
fromJson as __fromJson,
|
|
13
|
+
toJson as __toJson,
|
|
14
|
+
isNumber as __isNumber,
|
|
15
|
+
isString as __isString,
|
|
16
|
+
isBool as __isBool,
|
|
17
|
+
isObject as __isObject,
|
|
18
|
+
isArray as __isArray,
|
|
19
|
+
entries as __entries,
|
|
20
|
+
requireBoolean as __requireBoolean,
|
|
21
|
+
normalize as __normalize,
|
|
22
|
+
finite as __finite,
|
|
23
|
+
numericAdd as __numericAdd,
|
|
24
|
+
boundary as __boundary,
|
|
25
|
+
startTrans as __startTrans,
|
|
26
|
+
commitTrans as __commitTrans,
|
|
27
|
+
rollbackTrans as __rollbackTrans,
|
|
28
|
+
transState as __transState,
|
|
29
|
+
query as __query,
|
|
30
|
+
requireArity as __requireArity,
|
|
31
|
+
} from "./.lango/runtime.mjs";
|
|
32
|
+
|
|
33
|
+
export function prepareManagement(config) {
|
|
34
|
+
__requireArity(arguments.length, [0], "prepareManagement");
|
|
35
|
+
config = __boundary(config);
|
|
36
|
+
let action = __bindContexts(() => {
|
|
37
|
+
return initializeManagement(config);
|
|
38
|
+
return null;
|
|
39
|
+
});
|
|
40
|
+
return __normalize(server.withDatabase(__normalize(config.root), action));
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function initializeManagement(config) {
|
|
45
|
+
__requireArity(arguments.length, [0], "initializeManagement");
|
|
46
|
+
config = __boundary(config);
|
|
47
|
+
let queues;
|
|
48
|
+
let audit;
|
|
49
|
+
let schema = schemaName(__normalize(config.application));
|
|
50
|
+
__startTrans();
|
|
51
|
+
try {
|
|
52
|
+
scheduleLock(__normalize(config.application));
|
|
53
|
+
initializeSchedules(schema);
|
|
54
|
+
queues = __query("create table if not exists " + String(schema) + ".lango_managed_queues (\n name text primary key, created_at timestamptz not null default now()\n)", []);
|
|
55
|
+
__normalize(queues.execute());
|
|
56
|
+
audit = __query("create table if not exists " + String(schema) + ".lango_management_audit (\n id bigserial primary key, occurred_at timestamptz not null default now(),\n application text not null, action text not null, target text not null,\n actor jsonb not null, outcome jsonb not null\n)", []);
|
|
57
|
+
__normalize(audit.execute());
|
|
58
|
+
for (const item of __normalize(config.targets)) {
|
|
59
|
+
let store = __query("insert into " + String(schema) + ".lango_managed_queues (name) values (:name) on conflict do nothing", ["name"]);
|
|
60
|
+
store.name = __normalize(item.queue);
|
|
61
|
+
__normalize(store.execute());
|
|
62
|
+
}
|
|
63
|
+
__commitTrans();
|
|
64
|
+
} catch (error) {
|
|
65
|
+
__rollbackTrans();
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function scheduleLock(application) {
|
|
73
|
+
__requireArity(arguments.length, [0], "scheduleLock");
|
|
74
|
+
application = __boundary(application);
|
|
75
|
+
let query = __query("select pg_advisory_xact_lock(hashtextextended(:name, 0))", ["name"]);
|
|
76
|
+
query.name = `lango-boss:schedules:${application}`;
|
|
77
|
+
__normalize(query.execute());
|
|
78
|
+
return null;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function actor(token) {
|
|
83
|
+
__requireArity(arguments.length, [0], "actor");
|
|
84
|
+
token = __boundary(token);
|
|
85
|
+
let result = { };
|
|
86
|
+
for (const field of ["clientId", "userId", "grantId", "identity"]) {
|
|
87
|
+
let value = __normalize(token.get(field));
|
|
88
|
+
if (__requireBoolean(__isString(value))) {
|
|
89
|
+
result[field] = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function manage(settings, scope, token, action, target = null, options = null) {
|
|
97
|
+
const __contextScope = [];
|
|
98
|
+
try {
|
|
99
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "manage");
|
|
100
|
+
settings = __boundary(settings);
|
|
101
|
+
scope = __boundary(scope);
|
|
102
|
+
token = __boundary(token);
|
|
103
|
+
action = __boundary(action);
|
|
104
|
+
target = __boundary(target);
|
|
105
|
+
options = __boundary(options);
|
|
106
|
+
let result;
|
|
107
|
+
usable(scope);
|
|
108
|
+
__contextScope.push(__contextSet("db", scope));
|
|
109
|
+
try {
|
|
110
|
+
manager(token);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
__normalize(__contextGet("log").warn(`Boss management denied action=${__toJson(action)} actor=${__toJson(actor(token))}`));
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
let schema = schemaName(__normalize(settings.application));
|
|
116
|
+
if (__requireBoolean(__requireBoolean(action === "queues") || __requireBoolean(action === "schedules"))) {
|
|
117
|
+
return inventory(__normalize(settings.application), schema, action, options);
|
|
118
|
+
}
|
|
119
|
+
let operations = ["retry", "cancel", "pauseQueue", "resumeQueue", "pauseSchedule", "resumeSchedule"];
|
|
120
|
+
if (__requireBoolean(!__requireBoolean(__normalize(operations.includes(action))))) {
|
|
121
|
+
throw "Unknown Boss management operation";
|
|
122
|
+
}
|
|
123
|
+
if (__requireBoolean(__requireBoolean(action === "retry") || __requireBoolean(action === "cancel"))) {
|
|
124
|
+
if (__requireBoolean(!__requireBoolean(uuid(target)))) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
name(target, "Management target");
|
|
129
|
+
}
|
|
130
|
+
let own = __normalize(__contextGet("db").transactionState()) === null;
|
|
131
|
+
if (__requireBoolean(own)) {
|
|
132
|
+
__startTrans();
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
result = null;
|
|
136
|
+
if (action === "retry" || action === "cancel") {
|
|
137
|
+
result = changeJob(settings, schema, action, target);
|
|
138
|
+
} else if (action === "pauseQueue" || action === "resumeQueue") {
|
|
139
|
+
result = changeQueue(schema, action, target);
|
|
140
|
+
} else if (action === "pauseSchedule" || action === "resumeSchedule") {
|
|
141
|
+
scheduleLock(__normalize(settings.application));
|
|
142
|
+
result = changeSchedule(settings, schema, action, target);
|
|
143
|
+
}
|
|
144
|
+
appendAudit(__normalize(settings.application), schema, token, action, target, result);
|
|
145
|
+
if (__requireBoolean(own)) {
|
|
146
|
+
__commitTrans();
|
|
147
|
+
}
|
|
148
|
+
__normalize(__contextGet("log").debug(`Boss management staged action=${action} target=${__toJson(target)} actor=${__toJson(actor(token))}`));
|
|
149
|
+
return result;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (__requireBoolean(own)) {
|
|
152
|
+
__rollbackTrans();
|
|
153
|
+
}
|
|
154
|
+
__normalize(__contextGet("log").err(`Boss management failed action=${action} target=${__toJson(target)} actor=${__toJson(actor(token))}: ${error}`));
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
} finally {
|
|
159
|
+
__contextRestore(__contextScope);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function native(schema, action, queue, id = null) {
|
|
164
|
+
const __contextScope = [];
|
|
165
|
+
try {
|
|
166
|
+
__requireArity(arguments.length, [0, 1, 2], "native");
|
|
167
|
+
schema = __boundary(schema);
|
|
168
|
+
action = __boundary(action);
|
|
169
|
+
queue = __boundary(queue);
|
|
170
|
+
id = __boundary(id);
|
|
171
|
+
return __normalize(__contextGet("db").invoke({ module: "@lango-project/lango-boss/management-transaction", handler: "apply" }, { schema: schema, action: action, queue: queue, id: id }));
|
|
172
|
+
return null;
|
|
173
|
+
} finally {
|
|
174
|
+
__contextRestore(__contextScope);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function changeJob(settings, schema, action, id) {
|
|
179
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "changeJob");
|
|
180
|
+
settings = __boundary(settings);
|
|
181
|
+
schema = __boundary(schema);
|
|
182
|
+
action = __boundary(action);
|
|
183
|
+
id = __boundary(id);
|
|
184
|
+
let available;
|
|
185
|
+
let query = __query("select name, state::text, data->>'path' as path, retry_limit\nfrom " + String(schema) + ".job where id = :id::uuid\n and data->>'application' = :application and data->'version' = '1'::jsonb\norder by name limit 2 for update", ["id","application"]);
|
|
186
|
+
query.id = id;
|
|
187
|
+
query.application = __normalize(settings.application);
|
|
188
|
+
__normalize(query.execute());
|
|
189
|
+
if (__requireBoolean(!__requireBoolean(__normalize(query.next())))) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
let queue = __normalize(query.name);
|
|
193
|
+
let before = __normalize(query.state);
|
|
194
|
+
let path = __normalize(query.path);
|
|
195
|
+
let limit = __normalize(query.retry_limit);
|
|
196
|
+
if (__requireBoolean(__normalize(query.next()))) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
let changed = false;
|
|
200
|
+
let reason = null;
|
|
201
|
+
let state = before;
|
|
202
|
+
if (__requireBoolean(__requireBoolean(action === "retry") && __requireBoolean(before === "failed"))) {
|
|
203
|
+
available = false;
|
|
204
|
+
for (const item of __normalize(settings.targets)) {
|
|
205
|
+
if (__requireBoolean(__requireBoolean(__normalize(item.path) === path) && __requireBoolean(__normalize(item.queue) === queue))) {
|
|
206
|
+
available = true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (__requireBoolean(!__requireBoolean(available))) {
|
|
210
|
+
reason = "TargetUnavailable";
|
|
211
|
+
} else {
|
|
212
|
+
if (__requireBoolean(limit === 2147483647)) {
|
|
213
|
+
reason = "RetryLimitExceeded";
|
|
214
|
+
} else {
|
|
215
|
+
native(schema, action, queue, id);
|
|
216
|
+
state = "retry";
|
|
217
|
+
changed = true;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (__requireBoolean(__requireBoolean(action === "cancel") && __requireBoolean(__requireBoolean(__requireBoolean(before === "created") || __requireBoolean(before === "retry")) || __requireBoolean(before === "active")))) {
|
|
222
|
+
native(schema, action, queue, id);
|
|
223
|
+
state = "cancelled";
|
|
224
|
+
changed = true;
|
|
225
|
+
}
|
|
226
|
+
return { id: id, queue: queue, previousState: before, state: state, changed: changed, cancellationRequested: __requireBoolean(changed) && __requireBoolean(before === "active"), reason: reason };
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function changeQueue(schema, action, target) {
|
|
231
|
+
__requireArity(arguments.length, [0, 1, 2], "changeQueue");
|
|
232
|
+
schema = __boundary(schema);
|
|
233
|
+
action = __boundary(action);
|
|
234
|
+
target = __boundary(target);
|
|
235
|
+
let query = __query("select q.paused_on is not null as paused from " + String(schema) + ".queue q\njoin " + String(schema) + ".lango_managed_queues m on q.name = m.name\nwhere q.name = :name for update of q", ["name"]);
|
|
236
|
+
query.name = target;
|
|
237
|
+
__normalize(query.execute());
|
|
238
|
+
if (__requireBoolean(!__requireBoolean(__normalize(query.next())))) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
let paused = action === "pauseQueue";
|
|
242
|
+
let changed = __normalize(query.paused) !== paused;
|
|
243
|
+
if (__requireBoolean(changed)) {
|
|
244
|
+
native(schema, action, target);
|
|
245
|
+
}
|
|
246
|
+
return { name: target, paused: paused, changed: changed };
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function changeSchedule(settings, schema, action, target) {
|
|
251
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "changeSchedule");
|
|
252
|
+
settings = __boundary(settings);
|
|
253
|
+
schema = __boundary(schema);
|
|
254
|
+
action = __boundary(action);
|
|
255
|
+
target = __boundary(target);
|
|
256
|
+
let store;
|
|
257
|
+
let query = __query("select queue, key, paused, definition from " + String(schema) + ".lango_schedule_definitions\nwhere name = :name and present = true for update", ["name"]);
|
|
258
|
+
query.name = target;
|
|
259
|
+
__normalize(query.execute());
|
|
260
|
+
if (__requireBoolean(!__requireBoolean(__normalize(query.next())))) {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
let paused = action === "pauseSchedule";
|
|
264
|
+
let changed = __normalize(query.paused) !== paused;
|
|
265
|
+
let item = __normalize(__hostGlobal(["Object"]).assign({ }, __normalize(query.definition), { application: __normalize(settings.application) }));
|
|
266
|
+
if (__requireBoolean(changed)) {
|
|
267
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(paused)) && __requireBoolean(__normalize(item.enabled)))) {
|
|
268
|
+
scheduleOperation(schema, "upsert", __normalize(query.queue), __normalize(query.key), item);
|
|
269
|
+
} else {
|
|
270
|
+
scheduleOperation(schema, "remove", __normalize(query.queue), __normalize(query.key));
|
|
271
|
+
}
|
|
272
|
+
store = __query("update " + String(schema) + ".lango_schedule_definitions set paused = :paused, updated_at = now() where name = :name", ["paused","name"]);
|
|
273
|
+
store.name = target;
|
|
274
|
+
store.paused = paused;
|
|
275
|
+
__normalize(store.execute());
|
|
276
|
+
}
|
|
277
|
+
return { name: target, paused: paused, enabled: __normalize(item.enabled), effectiveEnabled: __requireBoolean(__normalize(item.enabled)) && __requireBoolean(!__requireBoolean(paused)), changed: changed };
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function appendAudit(application, schema, token, action, target, result) {
|
|
282
|
+
__requireArity(arguments.length, [0, 1, 2, 3, 4, 5], "appendAudit");
|
|
283
|
+
application = __boundary(application);
|
|
284
|
+
schema = __boundary(schema);
|
|
285
|
+
token = __boundary(token);
|
|
286
|
+
action = __boundary(action);
|
|
287
|
+
target = __boundary(target);
|
|
288
|
+
result = __boundary(result);
|
|
289
|
+
let query = __query("insert into " + String(schema) + ".lango_management_audit (application, action, target, actor, outcome)\nvalues (:application, :action, :target, :actor::jsonb, :outcome::jsonb)", ["application","action","target","actor","outcome"]);
|
|
290
|
+
query.application = application;
|
|
291
|
+
query.action = action;
|
|
292
|
+
query.target = target;
|
|
293
|
+
query.actor = __toJson(actor(token));
|
|
294
|
+
query.outcome = __toJson(result);
|
|
295
|
+
__normalize(query.execute());
|
|
296
|
+
return null;
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function inventory(application, schema, action, options) {
|
|
301
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "inventory");
|
|
302
|
+
application = __boundary(application);
|
|
303
|
+
schema = __boundary(schema);
|
|
304
|
+
action = __boundary(action);
|
|
305
|
+
options = __boundary(options);
|
|
306
|
+
let queues;
|
|
307
|
+
let schedules;
|
|
308
|
+
let last;
|
|
309
|
+
if (__requireBoolean(options === null)) {
|
|
310
|
+
options = { };
|
|
311
|
+
}
|
|
312
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(options))) || __requireBoolean(__isArray(options)))) {
|
|
313
|
+
throw "Inventory options must be a map";
|
|
314
|
+
}
|
|
315
|
+
for (const [field, value] of __entries(options)) {
|
|
316
|
+
if (__requireBoolean(__requireBoolean(field !== "limit") && __requireBoolean(field !== "cursor"))) {
|
|
317
|
+
throw `Unknown inventory option: ${field}`;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
let limit = __normalize(options.limit);
|
|
321
|
+
if (__requireBoolean(limit === null)) {
|
|
322
|
+
limit = 50;
|
|
323
|
+
}
|
|
324
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(limit)))) || __requireBoolean(limit < 1)) || __requireBoolean(limit > 200))) {
|
|
325
|
+
throw "Inventory limit must be between 1 and 200";
|
|
326
|
+
}
|
|
327
|
+
let after = __normalize(options.cursor);
|
|
328
|
+
if (__requireBoolean(after !== null)) {
|
|
329
|
+
name(after, "Inventory cursor");
|
|
330
|
+
}
|
|
331
|
+
let rows = [];
|
|
332
|
+
if (__requireBoolean(action === "queues")) {
|
|
333
|
+
queues = __query("select q.name, q.paused_on is not null as paused,\n (select count(*)::integer from " + String(schema) + ".job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state in ('created', 'retry')) as queued,\n (select count(*)::integer from " + String(schema) + ".job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state = 'active') as active,\n (select count(*)::integer from " + String(schema) + ".job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state = 'failed') as failed\nfrom " + String(schema) + ".queue q join " + String(schema) + ".lango_managed_queues m on m.name = q.name\nwhere (:after::text is null or q.name > :after) order by q.name limit :limit", ["application","after","limit"]);
|
|
334
|
+
queues.after = after;
|
|
335
|
+
queues.application = application;
|
|
336
|
+
queues.limit = __numericAdd(limit, 1);
|
|
337
|
+
__normalize(queues.execute());
|
|
338
|
+
__loop1: while (__requireBoolean(__normalize(queues.next()))) {
|
|
339
|
+
rows.push({ name: __normalize(queues.name), paused: __normalize(queues.paused), queued: __normalize(queues.queued), active: __normalize(queues.active), failed: __normalize(queues.failed) });
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
schedules = __query("select name, queue, paused, definition from " + String(schema) + ".lango_schedule_definitions\nwhere present = true and (:after::text is null or name > :after) order by name limit :limit", ["after","limit"]);
|
|
343
|
+
schedules.after = after;
|
|
344
|
+
schedules.limit = __numericAdd(limit, 1);
|
|
345
|
+
__normalize(schedules.execute());
|
|
346
|
+
__loop2: while (__requireBoolean(__normalize(schedules.next()))) {
|
|
347
|
+
let item = __normalize(schedules.definition);
|
|
348
|
+
rows.push({ name: __normalize(schedules.name), queue: __normalize(schedules.queue), path: __normalize(item.path), cron: __normalize(item.cron), timeZone: __normalize(item.timeZone), enabled: __normalize(item.enabled), paused: __normalize(schedules.paused), effectiveEnabled: __requireBoolean(__normalize(item.enabled)) && __requireBoolean(!__requireBoolean(__normalize(schedules.paused))) });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
let cursor = null;
|
|
352
|
+
if (__requireBoolean(__normalize(rows.length) > limit)) {
|
|
353
|
+
__normalize(rows.pop());
|
|
354
|
+
last = __normalize(rows[__finite(__normalize(rows.length) - 1)]);
|
|
355
|
+
cursor = __normalize(last.name);
|
|
356
|
+
}
|
|
357
|
+
return { items: rows, nextCursor: cursor };
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
//# sourceMappingURL=management.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"management.mjs","sources":["management.lango"],"sourcesContent":["import \"./namespace\"\n schemaName(application)\nimport \"./reads\"\n usable(db)\n manager(token)\n uuid(value)\nimport \"./validation\"\n name(value, label)\nimport \"./schedule_sync\"\n initializeSchedules(schema)\n alias scheduleOperation = operation(schema, action, queue, key, item = null)\nimport js \"@lango-project/lango-server\" as server\nimport js global Object\nimport js global Number\n\nexport prepareManagement(config)\nexport manage(settings, scope, token, action, target = null, options = null)\n\nprepareManagement(config)\n action = ()\n capture config\n return initializeManagement(config)\n return server/withDatabase(config.root, action)\n\ninitializeManagement(config)\n schema = schemaName(config.application)\n start_trans()\n try\n scheduleLock(config.application)\n initializeSchedules(schema)\n queues = SQL\n create table if not exists ${schema}.lango_managed_queues (\n name text primary key, created_at timestamptz not null default now()\n )\n queues.execute()\n audit = SQL\n create table if not exists ${schema}.lango_management_audit (\n id bigserial primary key, occurred_at timestamptz not null default now(),\n application text not null, action text not null, target text not null,\n actor jsonb not null, outcome jsonb not null\n )\n audit.execute()\n for config.targets as item\n store = SQL\n insert into ${schema}.lango_managed_queues (name) values (:name) on conflict do nothing\n store.name = item.queue\n store.execute()\n commit_trans()\n catch error\n rollback_trans()\n throw error\n return null\n\nscheduleLock(application)\n query = SQL\n select pg_advisory_xact_lock(hashtextextended(:name, 0))\n query.name = \"lango-boss:schedules:${application}\"\n query.execute()\n return null\n\nactor(token)\n result = {}\n for [\"clientId\", \"userId\", \"grantId\", \"identity\"] as field\n value = token.get(field)\n if is_string(value)\n result[field] = value\n return result\n\nmanage(settings, scope, token, action, target = null, options = null)\n usable(scope)\n context db = scope\n context log\n try\n manager(token)\n catch error\n log.warn(\"Boss management denied action=${to_json(action)} actor=${to_json(actor(token))}\")\n throw error\n schema = schemaName(settings.application)\n if action == \"queues\" or action == \"schedules\"\n return inventory(settings.application, schema, action, options)\n operations = [\"retry\", \"cancel\", \"pauseQueue\", \"resumeQueue\", \"pauseSchedule\", \"resumeSchedule\"]\n if !operations.includes(action)\n throw \"Unknown Boss management operation\"\n if action == \"retry\" or action == \"cancel\"\n if !uuid(target)\n return null\n else\n name(target, \"Management target\")\n own = db.transactionState() == null\n if own\n start_trans()\n try\n result = null\n switch action\n case \"retry\"\n case \"cancel\"\n result = changeJob(settings, schema, action, target)\n case \"pauseQueue\"\n case \"resumeQueue\"\n result = changeQueue(schema, action, target)\n case \"pauseSchedule\"\n case \"resumeSchedule\"\n scheduleLock(settings.application)\n result = changeSchedule(settings, schema, action, target)\n appendAudit(settings.application, schema, token, action, target, result)\n if own\n commit_trans()\n log.debug(\"Boss management staged action=${action} target=${to_json(target)} actor=${to_json(actor(token))}\")\n return result\n catch error\n if own\n rollback_trans()\n log.err(\"Boss management failed action=${action} target=${to_json(target)} actor=${to_json(actor(token))}: ${error}\")\n throw error\n\nnative(schema, action, queue, id = null)\n context db\n return db.invoke({\n module: \"@lango-project/lango-boss/management-transaction\"\n handler: \"apply\"\n }, {\n schema: schema\n action: action\n queue: queue\n id: id\n })\n\nchangeJob(settings, schema, action, id)\n query = SQL\n select name, state::text, data->>'path' as path, retry_limit\n from ${schema}.job where id = :id::uuid\n and data->>'application' = :application and data->'version' = '1'::jsonb\n order by name limit 2 for update\n query.id = id\n query.application = settings.application\n query.execute()\n if !query.next()\n return null\n queue = query.name\n before = query.state\n path = query.path\n limit = query.retry_limit\n if query.next()\n return null\n changed = false\n reason = null\n state = before\n if action == \"retry\" and before == \"failed\"\n available = false\n for settings.targets as item\n if item.path == path and item.queue == queue\n available = true\n if !available\n reason = \"TargetUnavailable\"\n else\n if limit == 2147483647\n reason = \"RetryLimitExceeded\"\n else\n native(schema, action, queue, id)\n state = \"retry\"\n changed = true\n if action == \"cancel\" and (before == \"created\" or before == \"retry\" or before == \"active\")\n native(schema, action, queue, id)\n state = \"cancelled\"\n changed = true\n return {\n id: id\n queue: queue\n previousState: before\n state: state\n changed: changed\n cancellationRequested: changed and before == \"active\"\n reason: reason\n }\n\nchangeQueue(schema, action, target)\n query = SQL\n select q.paused_on is not null as paused from ${schema}.queue q\n join ${schema}.lango_managed_queues m on q.name = m.name\n where q.name = :name for update of q\n query.name = target\n query.execute()\n if !query.next()\n return null\n paused = action == \"pauseQueue\"\n changed = query.paused != paused\n if changed\n native(schema, action, target)\n return {\n name: target\n paused: paused\n changed: changed\n }\n\nchangeSchedule(settings, schema, action, target)\n query = SQL\n select queue, key, paused, definition from ${schema}.lango_schedule_definitions\n where name = :name and present = true for update\n query.name = target\n query.execute()\n if !query.next()\n return null\n paused = action == \"pauseSchedule\"\n changed = query.paused != paused\n item = Object/assign({}, query.definition, {\n application: settings.application\n })\n if changed\n if !paused and item.enabled\n scheduleOperation(schema, \"upsert\", query.queue, query.key, item)\n else\n scheduleOperation(schema, \"remove\", query.queue, query.key)\n store = SQL\n update ${schema}.lango_schedule_definitions set paused = :paused, updated_at = now() where name = :name\n store.name = target\n store.paused = paused\n store.execute()\n return {\n name: target\n paused: paused\n enabled: item.enabled\n effectiveEnabled: item.enabled and !paused\n changed: changed\n }\n\nappendAudit(application, schema, token, action, target, result)\n query = SQL\n insert into ${schema}.lango_management_audit (application, action, target, actor, outcome)\n values (:application, :action, :target, :actor::jsonb, :outcome::jsonb)\n query.application = application\n query.action = action\n query.target = target\n query.actor = to_json(actor(token))\n query.outcome = to_json(result)\n query.execute()\n return null\n\ninventory(application, schema, action, options)\n if options == null\n options = {}\n if !is_object(options) or is_array(options)\n throw \"Inventory options must be a map\"\n for options as field: value\n if field != \"limit\" and field != \"cursor\"\n throw \"Unknown inventory option: ${field}\"\n limit = options.limit\n if limit == null\n limit = 50\n if !Number/isSafeInteger(limit) or limit < 1 or limit > 200\n throw \"Inventory limit must be between 1 and 200\"\n after = options.cursor\n if after != null\n name(after, \"Inventory cursor\")\n rows = []\n if action == \"queues\"\n queues = SQL\n select q.name, q.paused_on is not null as paused,\n (select count(*)::integer from ${schema}.job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state in ('created', 'retry')) as queued,\n (select count(*)::integer from ${schema}.job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state = 'active') as active,\n (select count(*)::integer from ${schema}.job j where j.name = q.name and j.data->>'application' = :application and j.data->'version' = '1'::jsonb and j.state = 'failed') as failed\n from ${schema}.queue q join ${schema}.lango_managed_queues m on m.name = q.name\n where (:after::text is null or q.name > :after) order by q.name limit :limit\n queues.after = after\n queues.application = application\n queues.limit = limit + 1\n queues.execute()\n loop while queues.next()\n rows.add({\n name: queues.name\n paused: queues.paused\n queued: queues.queued\n active: queues.active\n failed: queues.failed\n })\n else\n schedules = SQL\n select name, queue, paused, definition from ${schema}.lango_schedule_definitions\n where present = true and (:after::text is null or name > :after) order by name limit :limit\n schedules.after = after\n schedules.limit = limit + 1\n schedules.execute()\n loop while schedules.next()\n item = schedules.definition\n rows.add({\n name: schedules.name\n queue: schedules.queue\n path: item.path\n cron: item.cron\n timeZone: item.timeZone\n enabled: item.enabled\n paused: schedules.paused\n effectiveEnabled: item.enabled and !schedules.paused\n })\n cursor = null\n if rows.length > limit\n rows.pop()\n last = rows[rows.length - 1]\n cursor = last.name\n return {\n items: rows\n nextCursor: cursor\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA;;;;;;;;;;AAOA;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;;;;;;;;;;;;;AAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as pgBoss from "pg-boss";
|
|
2
|
+
import {
|
|
3
|
+
normalize as __normalize,
|
|
4
|
+
boundary as __boundary,
|
|
5
|
+
requireArity as __requireArity,
|
|
6
|
+
} from "./.lango/runtime.mjs";
|
|
7
|
+
|
|
8
|
+
export function apply(db, input) {
|
|
9
|
+
__requireArity(arguments.length, [0, 1], "apply");
|
|
10
|
+
db = __boundary(db);
|
|
11
|
+
input = __boundary(input);
|
|
12
|
+
let boss = new pgBoss.PgBoss({ db: db, schema: __normalize(input.schema), migrate: false, supervise: false, schedule: false, registerInstance: false });
|
|
13
|
+
if (__normalize(input.action) === "retry") {
|
|
14
|
+
return __normalize(boss.retry(__normalize(input.queue), __normalize(input.id), { db: db }));
|
|
15
|
+
} else if (__normalize(input.action) === "cancel") {
|
|
16
|
+
return __normalize(boss.cancel(__normalize(input.queue), __normalize(input.id), { db: db }));
|
|
17
|
+
} else if (__normalize(input.action) === "pauseQueue") {
|
|
18
|
+
return __normalize(boss.pauseQueue(__normalize(input.queue)));
|
|
19
|
+
} else if (__normalize(input.action) === "resumeQueue") {
|
|
20
|
+
return __normalize(boss.resumeQueue(__normalize(input.queue)));
|
|
21
|
+
} else {
|
|
22
|
+
throw "Unknown management operation";
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=management_transaction.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"management_transaction.mjs","sources":["management_transaction.lango"],"sourcesContent":["import js \"pg-boss\" as pgBoss\n\nexport apply(db, input)\n\napply(db, input)\n boss = new pgBoss/PgBoss({\n db: db\n schema: input.schema\n migrate: false\n supervise: false\n schedule: false\n registerInstance: false\n })\n switch input.action\n case \"retry\"\n return boss.retry(input.queue, input.id, {\n db: db\n })\n case \"cancel\"\n return boss.cancel(input.queue, input.id, {\n db: db\n })\n case \"pauseQueue\"\n return boss.pauseQueue(input.queue)\n case \"resumeQueue\"\n return boss.resumeQueue(input.queue)\n else\n throw \"Unknown management operation\"\n"],"names":[],"mappings":";;;;;;;AAIA;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as crypto from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
normalize as __normalize,
|
|
4
|
+
boundary as __boundary,
|
|
5
|
+
requireArity as __requireArity,
|
|
6
|
+
} from "./.lango/runtime.mjs";
|
|
7
|
+
|
|
8
|
+
export function schemaName(application) {
|
|
9
|
+
__requireArity(arguments.length, [0], "schemaName");
|
|
10
|
+
application = __boundary(application);
|
|
11
|
+
let hash = __normalize(crypto.createHash("sha256"));
|
|
12
|
+
__normalize(hash.update(application));
|
|
13
|
+
let suffix = __normalize(__normalize(hash.digest("hex")).slice(0, 32));
|
|
14
|
+
return `lango_${suffix}`;
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function scheduleKey(application, name) {
|
|
19
|
+
__requireArity(arguments.length, [0, 1], "scheduleKey");
|
|
20
|
+
application = __boundary(application);
|
|
21
|
+
name = __boundary(name);
|
|
22
|
+
let hash = __normalize(crypto.createHash("sha256"));
|
|
23
|
+
__normalize(hash.update(name));
|
|
24
|
+
return `${schemaName(application)}/${__normalize(hash.digest("hex"))}`;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=namespace.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"namespace.mjs","sources":["namespace.lango"],"sourcesContent":["import js \"node:crypto\" as crypto\n\nexport schemaName(application)\nexport scheduleKey(application, name)\n\nschemaName(application)\n hash = crypto/createHash(\"sha256\")\n hash.update(application)\n suffix = hash.digest(\"hex\").slice(0, 32)\n return \"lango_${suffix}\"\n\nscheduleKey(application, name)\n hash = crypto/createHash(\"sha256\")\n hash.update(name)\n return \"${schemaName(application)}/${hash.digest(\"hex\")}\"\n"],"names":[],"mappings":";;;;;;;AAKA;;;;;;;;;;AAMA;;;;;;;;;;"}
|
package/dist/queue.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { readJob, readJobs } from "./reads.mjs";
|
|
2
|
+
import { manage as management } from "./management.mjs";
|
|
3
|
+
import {
|
|
4
|
+
hostGlobal as __hostGlobal,
|
|
5
|
+
contextGet as __contextGet,
|
|
6
|
+
contextSet as __contextSet,
|
|
7
|
+
contextRestore as __contextRestore,
|
|
8
|
+
fromJson as __fromJson,
|
|
9
|
+
toJson as __toJson,
|
|
10
|
+
isNumber as __isNumber,
|
|
11
|
+
isString as __isString,
|
|
12
|
+
isBool as __isBool,
|
|
13
|
+
isObject as __isObject,
|
|
14
|
+
isArray as __isArray,
|
|
15
|
+
requireBoolean as __requireBoolean,
|
|
16
|
+
normalize as __normalize,
|
|
17
|
+
finite as __finite,
|
|
18
|
+
boundary as __boundary,
|
|
19
|
+
requireArity as __requireArity,
|
|
20
|
+
} from "./.lango/runtime.mjs";
|
|
21
|
+
|
|
22
|
+
class QueueSender {
|
|
23
|
+
#settings;
|
|
24
|
+
#db;
|
|
25
|
+
#token;
|
|
26
|
+
#lango;
|
|
27
|
+
constructor(settings, db, token) {
|
|
28
|
+
__requireArity(arguments.length, [0, 1, 2], "constructor");
|
|
29
|
+
settings = __boundary(settings);
|
|
30
|
+
db = __boundary(db);
|
|
31
|
+
token = __boundary(token);
|
|
32
|
+
this.#settings = __fromJson(__toJson(settings));
|
|
33
|
+
this.#db = db;
|
|
34
|
+
this.#token = token;
|
|
35
|
+
}
|
|
36
|
+
get(id) {
|
|
37
|
+
__requireArity(arguments.length, [0], "get");
|
|
38
|
+
id = __boundary(id);
|
|
39
|
+
return readJob(__normalize(this.#settings), __normalize(this.#db), __normalize(this.#token), id);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
job(id) {
|
|
43
|
+
__requireArity(arguments.length, [0], "job");
|
|
44
|
+
id = __boundary(id);
|
|
45
|
+
return readJob(__normalize(this.#settings), __normalize(this.#db), __normalize(this.#token), id, true);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
jobs(options = null) {
|
|
49
|
+
options = __boundary(options);
|
|
50
|
+
return readJobs(__normalize(this.#settings), __normalize(this.#db), __normalize(this.#token), options);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
manage(action, target = null, options = null) {
|
|
54
|
+
__requireArity(arguments.length, [0], "manage");
|
|
55
|
+
action = __boundary(action);
|
|
56
|
+
target = __boundary(target);
|
|
57
|
+
options = __boundary(options);
|
|
58
|
+
return management(__normalize(this.#settings), __normalize(this.#db), __normalize(this.#token), action, target, options);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
send(path, input) {
|
|
62
|
+
const __contextScope = [];
|
|
63
|
+
try {
|
|
64
|
+
__requireArity(arguments.length, [0, 1], "send");
|
|
65
|
+
path = __boundary(path);
|
|
66
|
+
input = __boundary(input);
|
|
67
|
+
let target = null;
|
|
68
|
+
for (const candidate of __normalize(this.#settings.targets)) {
|
|
69
|
+
if (__requireBoolean(__normalize(candidate.path) === path)) {
|
|
70
|
+
target = candidate;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (__requireBoolean(target === null)) {
|
|
74
|
+
throw `Unknown job path: ${path}`;
|
|
75
|
+
}
|
|
76
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(input === null) || __requireBoolean(!__requireBoolean(__isObject(input)))) || __requireBoolean(__isArray(input)))) {
|
|
77
|
+
throw "Queue input must be a JSON map";
|
|
78
|
+
}
|
|
79
|
+
if (__requireBoolean(__requireBoolean(__normalize(target.policy) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(this.#token.hasPolicy(__normalize(target.policy))))))) {
|
|
80
|
+
throw `Queue target policy rejected: ${__normalize(target.policy)}`;
|
|
81
|
+
}
|
|
82
|
+
if (__requireBoolean(__normalize(target.reauthSeconds) > 0)) {
|
|
83
|
+
let stamp = __normalize(this.#token.get("authenticatedAt"));
|
|
84
|
+
let now = __normalize(__hostGlobal(["Date"]).now());
|
|
85
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(stamp)))) || __requireBoolean(stamp < 0)) || __requireBoolean(stamp > now)) || __requireBoolean(__finite(now - stamp) >= __finite(__normalize(target.reauthSeconds) * 1000)))) {
|
|
86
|
+
throw "Queue target requires fresh authentication";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
let payload = { version: 1, application: __normalize(this.#settings.application), path: __normalize(target.path), input: __fromJson(__toJson(input)), source: source(__normalize(this.#token)), failedRetentionSeconds: 604800 };
|
|
90
|
+
let state = __normalize(this.#db.transactionState());
|
|
91
|
+
if (__requireBoolean(__requireBoolean(state !== null) && __requireBoolean(state !== "active"))) {
|
|
92
|
+
throw "Queue submission requires a usable transaction";
|
|
93
|
+
}
|
|
94
|
+
let own = state === null;
|
|
95
|
+
if (__requireBoolean(own)) {
|
|
96
|
+
__normalize(this.#db.begin());
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
let id = __normalize(this.#db.invoke({ module: "@lango-project/lango-boss/transaction", handler: "send" }, { schema: __normalize(this.#settings.schema), queue: __normalize(target.queue), payload: payload }));
|
|
100
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(id))) || __requireBoolean(id === ""))) {
|
|
101
|
+
throw "Queue submission did not return a job id";
|
|
102
|
+
}
|
|
103
|
+
if (__requireBoolean(own)) {
|
|
104
|
+
__normalize(this.#db.commit());
|
|
105
|
+
}
|
|
106
|
+
return id;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (__requireBoolean(own)) {
|
|
109
|
+
try {
|
|
110
|
+
__normalize(this.#db.rollback());
|
|
111
|
+
} catch (rollbackError) {
|
|
112
|
+
__normalize(__contextGet("log").err("Queue transaction cleanup failed"));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
} finally {
|
|
119
|
+
__contextRestore(__contextScope);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export function createQueue(settings) {
|
|
124
|
+
const __contextScope = [];
|
|
125
|
+
try {
|
|
126
|
+
__requireArity(arguments.length, [0], "createQueue");
|
|
127
|
+
settings = __boundary(settings);
|
|
128
|
+
if (__requireBoolean(__requireBoolean(__contextGet("db") === null) || __requireBoolean(__normalize(__contextGet("db").parameterStyle) !== "postgresql"))) {
|
|
129
|
+
throw "Lango Boss queue requires an active PostgreSQL scope";
|
|
130
|
+
}
|
|
131
|
+
return new QueueSender(settings, __contextGet("db"), __contextGet("token"));
|
|
132
|
+
return null;
|
|
133
|
+
} finally {
|
|
134
|
+
__contextRestore(__contextScope);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function source(token) {
|
|
139
|
+
__requireArity(arguments.length, [0], "source");
|
|
140
|
+
token = __boundary(token);
|
|
141
|
+
let profile = __normalize(token.get("identity"));
|
|
142
|
+
if (__requireBoolean(__normalize(token.clientId) === null)) {
|
|
143
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(profile))) || __requireBoolean(profile === ""))) {
|
|
144
|
+
throw "Queue submission requires a trusted source identity";
|
|
145
|
+
}
|
|
146
|
+
return { kind: "system", identity: profile };
|
|
147
|
+
}
|
|
148
|
+
let result = { kind: "user", clientId: __normalize(token.clientId) };
|
|
149
|
+
for (const field of ["userId", "grantId"]) {
|
|
150
|
+
let value = __normalize(token.get(field));
|
|
151
|
+
if (__requireBoolean(value !== null)) {
|
|
152
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(value === ""))) {
|
|
153
|
+
throw "Queue identity references must be nonempty strings";
|
|
154
|
+
}
|
|
155
|
+
result[field] = value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=queue.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.mjs","sources":["queue.lango"],"sourcesContent":["import js global Date\nimport js global Number\nimport \"./reads\"\n readJob(settings, db, token, id, management = false)\n readJobs(settings, db, token, options)\nimport \"./management\"\n alias management = manage(settings, scope, token, action, target = null, options = null)\n\nexport createQueue(settings)\n\nclass QueueSender(settings, db, token)\n methods\n send(path, input)\n get(id)\n job(id)\n jobs(options = null)\n manage(action, target = null, options = null)\n\ncreateQueue(settings)\n context db\n context token\n if db == null or db.parameterStyle != \"postgresql\"\n throw \"Lango Boss queue requires an active PostgreSQL scope\"\n return new QueueSender(settings, db, token)\n\nQueueSender(settings, db, token)\n @settings = from_json(to_json(settings))\n @db = db\n @token = token\n\nQueueSender:get(id)\n return readJob(@settings, @db, @token, id)\n\nQueueSender:job(id)\n return readJob(@settings, @db, @token, id, true)\n\nQueueSender:jobs(options = null)\n return readJobs(@settings, @db, @token, options)\n\nQueueSender:manage(action, target = null, options = null)\n return management(@settings, @db, @token, action, target, options)\n\nQueueSender:send(path, input)\n target = null\n for @settings.targets as candidate\n if candidate.path == path\n target = candidate\n if target == null\n throw \"Unknown job path: ${path}\"\n if input == null or !is_object(input) or is_array(input)\n throw \"Queue input must be a JSON map\"\n if target.policy != \"anonymous\" and !@token.hasPolicy(target.policy)\n throw \"Queue target policy rejected: ${target.policy}\"\n if target.reauthSeconds > 0\n stamp = @token.get(\"authenticatedAt\")\n now = Date/now()\n if !Number/isSafeInteger(stamp) or stamp < 0 or stamp > now or now - stamp >= target.reauthSeconds * 1000\n throw \"Queue target requires fresh authentication\"\n payload = {\n version: 1\n application: @settings.application\n path: target.path\n input: from_json(to_json(input))\n source: source(@token)\n failedRetentionSeconds: 604800\n }\n state = @db.transactionState()\n if state != null and state != \"active\"\n throw \"Queue submission requires a usable transaction\"\n own = state == null\n if own\n @db.begin()\n try\n id = @db.invoke({\n module: \"@lango-project/lango-boss/transaction\"\n handler: \"send\"\n }, {\n schema: @settings.schema\n queue: target.queue\n payload: payload\n })\n if !is_string(id) or id == \"\"\n throw \"Queue submission did not return a job id\"\n if own\n @db.commit()\n return id\n catch error\n if own\n try\n @db.rollback()\n catch rollbackError\n context log\n log.err(\"Queue transaction cleanup failed\")\n throw error\n\nsource(token)\n profile = token.get(\"identity\")\n if token.clientId? == null\n if !is_string(profile) or profile == \"\"\n throw \"Queue submission requires a trusted source identity\"\n return {\n kind: \"system\"\n identity: profile\n }\n result = {\n kind: \"user\"\n clientId: token.clientId?\n }\n for [\"userId\", \"grantId\"] as field\n value = token.get(field)\n if value != null\n if !is_string(value) or value == \"\"\n throw \"Queue identity references must be nonempty strings\"\n result[field] = value\n return result\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;AA6EA;;;;;;;;;;;;;;;;;;;;;;;;"}
|