@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.
Files changed (117) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +2 -0
  3. package/README.md +111 -2
  4. package/dist/.lango/bignumber.mjs +3119 -0
  5. package/dist/.lango/localization.mjs +108 -0
  6. package/dist/.lango/runtime.mjs +725 -0
  7. package/dist/.lango/vendor/i18next.LICENSE +21 -0
  8. package/dist/.lango/vendor/i18next.mjs +2434 -0
  9. package/dist/.lango/xmldom/conventions.js +429 -0
  10. package/dist/.lango/xmldom/dom-parser.js +591 -0
  11. package/dist/.lango/xmldom/dom.js +3759 -0
  12. package/dist/.lango/xmldom/entities.js +2171 -0
  13. package/dist/.lango/xmldom/errors.js +205 -0
  14. package/dist/.lango/xmldom/grammar.js +559 -0
  15. package/dist/.lango/xmldom/index.js +41 -0
  16. package/dist/.lango/xmldom/package.json +1 -0
  17. package/dist/.lango/xmldom/sax.js +983 -0
  18. package/dist/.lango/xmldom.LICENSE.md +8 -0
  19. package/dist/boss.mjs +136 -0
  20. package/dist/boss.mjs.map +1 -0
  21. package/dist/claim_watch.mjs +99 -0
  22. package/dist/claim_watch.mjs.map +1 -0
  23. package/dist/cli.mjs +143 -0
  24. package/dist/cli.mjs.map +1 -0
  25. package/dist/configuration.mjs +58 -0
  26. package/dist/configuration.mjs.map +1 -0
  27. package/dist/dispatcher.mjs +108 -0
  28. package/dist/dispatcher.mjs.map +1 -0
  29. package/dist/driver.mjs +98 -0
  30. package/dist/driver.mjs.map +1 -0
  31. package/dist/envelope.mjs +92 -0
  32. package/dist/envelope.mjs.map +1 -0
  33. package/dist/library.mjs +139 -0
  34. package/dist/library.mjs.map +1 -0
  35. package/dist/management.mjs +360 -0
  36. package/dist/management.mjs.map +1 -0
  37. package/dist/management_transaction.mjs +26 -0
  38. package/dist/management_transaction.mjs.map +1 -0
  39. package/dist/namespace.mjs +27 -0
  40. package/dist/namespace.mjs.map +1 -0
  41. package/dist/queue.mjs +161 -0
  42. package/dist/queue.mjs.map +1 -0
  43. package/dist/reads.mjs +359 -0
  44. package/dist/reads.mjs.map +1 -0
  45. package/dist/retention.mjs +133 -0
  46. package/dist/retention.mjs.map +1 -0
  47. package/dist/schedule_sync.mjs +222 -0
  48. package/dist/schedule_sync.mjs.map +1 -0
  49. package/dist/schedule_transaction.mjs +24 -0
  50. package/dist/schedule_transaction.mjs.map +1 -0
  51. package/dist/schedules.mjs +201 -0
  52. package/dist/schedules.mjs.map +1 -0
  53. package/dist/transaction.mjs +16 -0
  54. package/dist/transaction.mjs.map +1 -0
  55. package/dist/validation.mjs +90 -0
  56. package/dist/validation.mjs.map +1 -0
  57. package/dist/worker.mjs +316 -0
  58. package/dist/worker.mjs.map +1 -0
  59. package/docs/guide.md +88 -0
  60. package/docs/management.md +93 -0
  61. package/docs/producer.md +115 -0
  62. package/docs/reads.md +107 -0
  63. package/docs/reference.md +77 -0
  64. package/docs/release.md +103 -0
  65. package/docs/retention.md +47 -0
  66. package/docs/scheduling.md +79 -0
  67. package/docs/worker.md +90 -0
  68. package/examples/reports/README.md +118 -0
  69. package/examples/reports/app.lango +133 -0
  70. package/examples/reports/deploy.xml +6 -0
  71. package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
  72. package/examples/reports/dist/.lango/localization.mjs +108 -0
  73. package/examples/reports/dist/.lango/runtime.mjs +725 -0
  74. package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
  75. package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
  76. package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
  77. package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
  78. package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
  79. package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
  80. package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
  81. package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
  82. package/examples/reports/dist/.lango/xmldom/index.js +41 -0
  83. package/examples/reports/dist/.lango/xmldom/package.json +1 -0
  84. package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
  85. package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
  86. package/examples/reports/dist/app.mjs +137 -0
  87. package/examples/reports/dist/app.mjs.map +1 -0
  88. package/examples/reports/dist/handlers.mjs +202 -0
  89. package/examples/reports/dist/handlers.mjs.map +1 -0
  90. package/examples/reports/dist/setup.mjs +14 -0
  91. package/examples/reports/dist/setup.mjs.map +1 -0
  92. package/examples/reports/handlers.lango +153 -0
  93. package/examples/reports/jobs.xml +5 -0
  94. package/examples/reports/package.json +23 -0
  95. package/examples/reports/routes.xml +4 -0
  96. package/examples/reports/setup.lango +17 -0
  97. package/package.json +60 -4
  98. package/src/boss.lango +92 -0
  99. package/src/claim_watch.lango +58 -0
  100. package/src/cli.lango +103 -0
  101. package/src/configuration.lango +43 -0
  102. package/src/dispatcher.lango +75 -0
  103. package/src/driver.lango +72 -0
  104. package/src/envelope.lango +56 -0
  105. package/src/library.lango +66 -0
  106. package/src/management.lango +302 -0
  107. package/src/management_transaction.lango +28 -0
  108. package/src/namespace.lango +15 -0
  109. package/src/queue.lango +115 -0
  110. package/src/reads.lango +277 -0
  111. package/src/retention.lango +107 -0
  112. package/src/schedule_sync.lango +190 -0
  113. package/src/schedule_transaction.lango +22 -0
  114. package/src/schedules.lango +165 -0
  115. package/src/transaction.lango +23 -0
  116. package/src/validation.lango +45 -0
  117. package/src/worker.lango +230 -0
package/dist/reads.mjs ADDED
@@ -0,0 +1,359 @@
1
+ import { schemaName } from "./namespace.mjs";
2
+ import {
3
+ hostGlobal as __hostGlobal,
4
+ contextGet as __contextGet,
5
+ contextSet as __contextSet,
6
+ contextRestore as __contextRestore,
7
+ fromJson as __fromJson,
8
+ toJson as __toJson,
9
+ isNumber as __isNumber,
10
+ isString as __isString,
11
+ isBool as __isBool,
12
+ isObject as __isObject,
13
+ isArray as __isArray,
14
+ entries as __entries,
15
+ requireBoolean as __requireBoolean,
16
+ normalize as __normalize,
17
+ finite as __finite,
18
+ numericAdd as __numericAdd,
19
+ boundary as __boundary,
20
+ query as __query,
21
+ requireArity as __requireArity,
22
+ } from "./.lango/runtime.mjs";
23
+
24
+ export function usable(db) {
25
+ __requireArity(arguments.length, [0], "usable");
26
+ db = __boundary(db);
27
+ let state = __normalize(db.transactionState());
28
+ if (__requireBoolean(__requireBoolean(state !== null) && __requireBoolean(state !== "active"))) {
29
+ throw "Job reads require a usable transaction";
30
+ }
31
+ return null;
32
+ return null;
33
+ }
34
+
35
+ export function manager(token) {
36
+ __requireArity(arguments.length, [0], "manager");
37
+ token = __boundary(token);
38
+ if (__requireBoolean(!__requireBoolean(__normalize(token.hasPolicy("boss.manage"))))) {
39
+ throw "Boss management policy required: boss.manage";
40
+ }
41
+ return null;
42
+ return null;
43
+ }
44
+
45
+ export function uuid(value) {
46
+ __requireArity(arguments.length, [0], "uuid");
47
+ value = __boundary(value);
48
+ let pattern = new (__hostGlobal(["RegExp"]))("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
49
+ return __requireBoolean(__isString(value)) && __requireBoolean(__normalize(pattern.test(value)));
50
+ return null;
51
+ }
52
+
53
+ function owned(source, token) {
54
+ __requireArity(arguments.length, [0, 1], "owned");
55
+ source = __boundary(source);
56
+ token = __boundary(token);
57
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(source === null) || __requireBoolean(!__requireBoolean(__isObject(source)))) || __requireBoolean(__isArray(source))) || __requireBoolean(__normalize(source.kind) !== "user")) || __requireBoolean(__normalize(source.identity) !== null))) {
58
+ return false;
59
+ }
60
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(source.clientId)))) || __requireBoolean(__normalize(source.clientId) === ""))) {
61
+ return false;
62
+ }
63
+ if (__requireBoolean(__normalize(source.clientId) !== __normalize(token.clientId))) {
64
+ return false;
65
+ }
66
+ for (const key of ["userId", "grantId"]) {
67
+ let expected = __normalize(source[key]);
68
+ if (__requireBoolean(__requireBoolean(expected !== null) && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(expected))) || __requireBoolean(expected === "")))) {
69
+ return false;
70
+ }
71
+ if (__requireBoolean(expected !== __normalize(token.get(key)))) {
72
+ return false;
73
+ }
74
+ }
75
+ return true;
76
+ return null;
77
+ }
78
+
79
+ function permitted(settings, path, queue, token) {
80
+ __requireArity(arguments.length, [0, 1, 2, 3], "permitted");
81
+ settings = __boundary(settings);
82
+ path = __boundary(path);
83
+ queue = __boundary(queue);
84
+ token = __boundary(token);
85
+ for (const target of __normalize(settings.targets)) {
86
+ if (__requireBoolean(__requireBoolean(__normalize(target.path) === path) && __requireBoolean(__normalize(target.queue) === queue))) {
87
+ if (__requireBoolean(__requireBoolean(__normalize(target.policy) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(token.hasPolicy(__normalize(target.policy))))))) {
88
+ return false;
89
+ }
90
+ if (__requireBoolean(__normalize(target.reauthSeconds) > 0)) {
91
+ let stamp = __normalize(token.get("authenticatedAt"));
92
+ let now = __normalize(__hostGlobal(["Date"]).now());
93
+ 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)))) {
94
+ return false;
95
+ }
96
+ }
97
+ return true;
98
+ }
99
+ }
100
+ return false;
101
+ return null;
102
+ }
103
+
104
+ function invoke(settings, scope, action, input) {
105
+ const __contextScope = [];
106
+ try {
107
+ __requireArity(arguments.length, [0, 1, 2, 3], "invoke");
108
+ settings = __boundary(settings);
109
+ scope = __boundary(scope);
110
+ action = __boundary(action);
111
+ input = __boundary(input);
112
+ let single;
113
+ let result;
114
+ __contextScope.push(__contextSet("db", scope));
115
+ let schema = schemaName(__normalize(settings.application));
116
+ if (__requireBoolean(action === "job")) {
117
+ single = __query("select id::text, name as queue, data->>'path' as path, state::text,\n retry_count as \"retryCount\", retry_limit as \"retryLimit\",\n to_char(created_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"createdAt\",\n to_char(start_after at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startAfter\",\n to_char(started_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startedAt\",\n to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"completedAt\",\n data->'input' as input, data->'source' as source, output\nfrom " + String(schema) + ".job\nwhere id = :id::uuid and data->>'application' = :application\n and data->'version' = '1'::jsonb\nlimit 2", ["id","application"]);
118
+ single.id = __normalize(input.id);
119
+ single.application = __normalize(settings.application);
120
+ __normalize(single.execute());
121
+ if (__requireBoolean(!__requireBoolean(__normalize(single.next())))) {
122
+ return null;
123
+ }
124
+ result = summary(single);
125
+ result.input = __normalize(single.input);
126
+ result.source = __normalize(single.source);
127
+ result.output = __normalize(single.output);
128
+ if (__requireBoolean(__normalize(single.next()))) {
129
+ return null;
130
+ }
131
+ return result;
132
+ }
133
+ let listing = __query("select id::text, name as queue, data->>'path' as path, state::text,\n retry_count as \"retryCount\", retry_limit as \"retryLimit\",\n to_char(created_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"createdAt\",\n to_char(start_after at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startAfter\",\n to_char(started_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startedAt\",\n to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"completedAt\"\nfrom " + String(schema) + ".job\nwhere data->>'application' = :application and data->'version' = '1'::jsonb\n and (:queue::text is null or name = :queue)\n and (:path::text is null or data->>'path' = :path)\n and (:state::text is null or state::text = :state)\n and (:cursorTime::timestamptz is null or (created_on, id, name) < (:cursorTime::timestamptz, :cursorId::uuid, :cursorQueue::text))\norder by created_on desc, id desc, name desc\nlimit :limit", ["application","queue","path","state","cursorTime","cursorId","cursorQueue","limit"]);
134
+ listing.application = __normalize(settings.application);
135
+ listing.queue = __normalize(input.queue);
136
+ listing.path = __normalize(input.path);
137
+ listing.state = __normalize(input.state);
138
+ listing.limit = __normalize(input.limit);
139
+ listing.cursorTime = null;
140
+ listing.cursorId = null;
141
+ listing.cursorQueue = null;
142
+ if (__requireBoolean(__normalize(input.position) !== null)) {
143
+ listing.cursorTime = input.position.createdAt;
144
+ listing.cursorId = input.position.id;
145
+ listing.cursorQueue = input.position.name;
146
+ }
147
+ __normalize(listing.execute());
148
+ let rows = [];
149
+ __loop1: while (__requireBoolean(__normalize(listing.next()))) {
150
+ rows.push(summary(listing));
151
+ }
152
+ return rows;
153
+ return null;
154
+ } finally {
155
+ __contextRestore(__contextScope);
156
+ }
157
+ }
158
+
159
+ export function readJob(settings, db, token, id, management = false) {
160
+ __requireArity(arguments.length, [0, 1, 2, 3], "readJob");
161
+ settings = __boundary(settings);
162
+ db = __boundary(db);
163
+ token = __boundary(token);
164
+ id = __boundary(id);
165
+ management = __boundary(management);
166
+ usable(db);
167
+ if (__requireBoolean(management)) {
168
+ manager(token);
169
+ }
170
+ if (__requireBoolean(!__requireBoolean(uuid(id)))) {
171
+ return null;
172
+ }
173
+ let row = invoke(settings, db, "job", { id: id });
174
+ if (__requireBoolean(row === null)) {
175
+ return null;
176
+ }
177
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(management)) && __requireBoolean(__requireBoolean(!__requireBoolean(owned(__normalize(row.source), token))) || __requireBoolean(!__requireBoolean(permitted(settings, __normalize(row.path), __normalize(row.queue), token)))))) {
178
+ return null;
179
+ }
180
+ let result = summary(row);
181
+ result.resultAvailable = false;
182
+ result.result = null;
183
+ result.error = null;
184
+ let output = __normalize(row.output);
185
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(output !== null) && __requireBoolean(__isObject(output))) && __requireBoolean(!__requireBoolean(__isArray(output)))) && __requireBoolean(__normalize(output.version) === 1))) {
186
+ if (__requireBoolean(__requireBoolean(__normalize(row.state) === "completed") && __requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(output, "value"))))) {
187
+ result.resultAvailable = true;
188
+ result.result = __normalize(output.value);
189
+ }
190
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(row.state) === "failed") && __requireBoolean(__normalize(output.error) !== null)) && __requireBoolean(__isObject(__normalize(output.error))))) {
191
+ if (__requireBoolean(__requireBoolean(__isString(output.error.code)) && __requireBoolean(__isBool(output.error.permanent)))) {
192
+ result.error = { code: output.error.code, permanent: output.error.permanent };
193
+ }
194
+ }
195
+ }
196
+ if (__requireBoolean(management)) {
197
+ result.input = __normalize(row.input);
198
+ result.source = safeSource(__normalize(row.source));
199
+ }
200
+ return result;
201
+ return null;
202
+ }
203
+
204
+ function safeSource(source) {
205
+ __requireArity(arguments.length, [0], "safeSource");
206
+ source = __boundary(source);
207
+ let result = { };
208
+ if (__requireBoolean(__requireBoolean(__requireBoolean(source === null) || __requireBoolean(!__requireBoolean(__isObject(source)))) || __requireBoolean(__isArray(source)))) {
209
+ return null;
210
+ }
211
+ for (const field of ["kind", "clientId", "userId", "grantId", "identity"]) {
212
+ if (__requireBoolean(__isString(__normalize(source[field])))) {
213
+ result[field] = __normalize(source[field]);
214
+ }
215
+ }
216
+ return result;
217
+ return null;
218
+ }
219
+
220
+ function summary(row) {
221
+ __requireArity(arguments.length, [0], "summary");
222
+ row = __boundary(row);
223
+ return { id: __normalize(row.id), queue: __normalize(row.queue), path: __normalize(row.path), state: __normalize(row.state), retryCount: __normalize(row.retryCount), retryLimit: __normalize(row.retryLimit), createdAt: __normalize(row.createdAt), startAfter: __normalize(row.startAfter), startedAt: __normalize(row.startedAt), completedAt: __normalize(row.completedAt) };
224
+ return null;
225
+ }
226
+
227
+ function filters(options) {
228
+ __requireArity(arguments.length, [0], "filters");
229
+ options = __boundary(options);
230
+ if (__requireBoolean(options === null)) {
231
+ options = { };
232
+ }
233
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(options))) || __requireBoolean(__isArray(options)))) {
234
+ throw "Job list options must be a map";
235
+ }
236
+ let allowed = ["queue", "path", "state", "limit", "cursor"];
237
+ for (const [key, value] of __entries(options)) {
238
+ if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
239
+ throw `Unknown job list option: ${key}`;
240
+ }
241
+ }
242
+ let result = { queue: null, path: null, state: null, limit: 50, cursor: null };
243
+ for (const field of ["queue", "path", "state"]) {
244
+ let value = __normalize(options[field]);
245
+ if (__requireBoolean(value !== null)) {
246
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(value === "")) || __requireBoolean(__normalize(value.length) > 2048))) {
247
+ throw `Invalid job list filter: ${field}`;
248
+ }
249
+ result[field] = value;
250
+ }
251
+ }
252
+ let states = ["created", "retry", "active", "completed", "cancelled", "failed"];
253
+ if (__requireBoolean(__requireBoolean(__normalize(result.state) !== null) && __requireBoolean(!__requireBoolean(__normalize(states.includes(__normalize(result.state))))))) {
254
+ throw "Invalid job state filter";
255
+ }
256
+ if (__requireBoolean(__normalize(options.limit) !== null)) {
257
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(options.limit))))) || __requireBoolean(__normalize(options.limit) < 1)) || __requireBoolean(__normalize(options.limit) > 200))) {
258
+ throw "Job list limit must be between 1 and 200";
259
+ }
260
+ result.limit = __normalize(options.limit);
261
+ }
262
+ if (__requireBoolean(__normalize(options.cursor) !== null)) {
263
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(options.cursor)))) || __requireBoolean(options.cursor.length === 0)) || __requireBoolean(options.cursor.length > 8192))) {
264
+ throw "Invalid job cursor";
265
+ }
266
+ result.cursor = __normalize(options.cursor);
267
+ }
268
+ return result;
269
+ return null;
270
+ }
271
+
272
+ function decodeCursor(settings, selected) {
273
+ __requireArity(arguments.length, [0, 1], "decodeCursor");
274
+ settings = __boundary(settings);
275
+ selected = __boundary(selected);
276
+ let bytes;
277
+ let value;
278
+ let pattern;
279
+ let stamp;
280
+ let date;
281
+ if (__requireBoolean(__normalize(selected.cursor) === null)) {
282
+ return null;
283
+ }
284
+ try {
285
+ bytes = __normalize(__hostGlobal(["Buffer"]).from(__normalize(selected.cursor), "base64url"));
286
+ if (__requireBoolean(__normalize(bytes.toString("base64url")) !== __normalize(selected.cursor))) {
287
+ throw "Invalid encoding";
288
+ }
289
+ value = __fromJson(__normalize(bytes.toString("utf8")));
290
+ if (__requireBoolean(__requireBoolean(__requireBoolean(value === null) || __requireBoolean(!__requireBoolean(__isObject(value)))) || __requireBoolean(__isArray(value)))) {
291
+ throw "Invalid cursor";
292
+ }
293
+ if (__requireBoolean(__requireBoolean(__normalize(value.version) !== 1) || __requireBoolean(__normalize(value.application) !== __normalize(settings.application)))) {
294
+ throw "Wrong cursor scope";
295
+ }
296
+ for (const field of ["queue", "path", "state"]) {
297
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(value, field)))) || __requireBoolean(__normalize(value[field]) !== __normalize(selected[field])))) {
298
+ throw "Changed cursor filters";
299
+ }
300
+ }
301
+ pattern = new (__hostGlobal(["RegExp"]))("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{6}Z$");
302
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(value.createdAt)))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(__normalize(value.createdAt)))))) || __requireBoolean(!__requireBoolean(uuid(__normalize(value.id))))) || __requireBoolean(!__requireBoolean(__isString(__normalize(value.name)))))) {
303
+ throw "Invalid cursor position";
304
+ }
305
+ stamp = __normalize(__hostGlobal(["Date"]).parse(__normalize(value.createdAt)));
306
+ if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isFinite(stamp))))) {
307
+ throw "Invalid cursor time";
308
+ }
309
+ date = new (__hostGlobal(["Date"]))(stamp);
310
+ if (__requireBoolean(__normalize(date.toISOString()) !== `${__normalize(value.createdAt.slice(0, 23))}Z`)) {
311
+ throw "Invalid cursor time";
312
+ }
313
+ if (__requireBoolean(__requireBoolean(value.name.length === 0) || __requireBoolean(value.name.length > 200))) {
314
+ throw "Invalid cursor queue";
315
+ }
316
+ return value;
317
+ } catch (error) {
318
+ throw "Invalid job cursor";
319
+ }
320
+ return null;
321
+ }
322
+
323
+ function encodeCursor(settings, selected, row) {
324
+ __requireArity(arguments.length, [0, 1, 2], "encodeCursor");
325
+ settings = __boundary(settings);
326
+ selected = __boundary(selected);
327
+ row = __boundary(row);
328
+ return __normalize(__normalize(__hostGlobal(["Buffer"]).from(__toJson({ version: 1, application: __normalize(settings.application), queue: __normalize(selected.queue), path: __normalize(selected.path), state: __normalize(selected.state), createdAt: __normalize(row.createdAt), id: __normalize(row.id), name: __normalize(row.queue) }), "utf8")).toString("base64url"));
329
+ return null;
330
+ }
331
+
332
+ export function readJobs(settings, db, token, options) {
333
+ __requireArity(arguments.length, [0, 1, 2, 3], "readJobs");
334
+ settings = __boundary(settings);
335
+ db = __boundary(db);
336
+ token = __boundary(token);
337
+ options = __boundary(options);
338
+ usable(db);
339
+ manager(token);
340
+ let selected = filters(options);
341
+ let cursor = decodeCursor(settings, selected);
342
+ let rows = invoke(settings, db, "jobs", { queue: __normalize(selected.queue), path: __normalize(selected.path), state: __normalize(selected.state), limit: __numericAdd(__normalize(selected.limit), 1), position: cursor });
343
+ let items = [];
344
+ let last = null;
345
+ for (const row of rows) {
346
+ if (__requireBoolean(__normalize(items.length) === __normalize(selected.limit))) {
347
+ break;
348
+ }
349
+ items.push(summary(row));
350
+ last = row;
351
+ }
352
+ let next = null;
353
+ if (__requireBoolean(__normalize(rows.length) > __normalize(selected.limit))) {
354
+ next = encodeCursor(settings, selected, last);
355
+ }
356
+ return { items: items, nextCursor: next };
357
+ return null;
358
+ }
359
+ //# sourceMappingURL=reads.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reads.mjs","sources":["reads.lango"],"sourcesContent":["import js global Date\nimport js global Number\nimport js global Object\nimport js global RegExp\nimport js global Buffer\nimport \"./namespace\"\n schemaName(application)\n\nexport readJob(settings, db, token, id, management = false)\nexport readJobs(settings, db, token, options)\nexport usable(db)\nexport manager(token)\nexport uuid(value)\n\nusable(db)\n state = db.transactionState()\n if state != null and state != \"active\"\n throw \"Job reads require a usable transaction\"\n return null\n\nmanager(token)\n if !token.hasPolicy(\"boss.manage\")\n throw \"Boss management policy required: boss.manage\"\n return null\n\nuuid(value)\n pattern = new RegExp(\"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$\")\n return is_string(value) and pattern.test(value)\n\nowned(source, token)\n if source == null or !is_object(source) or is_array(source) or source.kind != \"user\" or source.identity != null\n return false\n if !is_string(source.clientId) or source.clientId == \"\"\n return false\n if source.clientId != token.clientId?\n return false\n for [\"userId\", \"grantId\"] as key\n expected = source[key]\n if expected != null and (!is_string(expected) or expected == \"\")\n return false\n if expected != token.get(key)\n return false\n return true\n\npermitted(settings, path, queue, token)\n for settings.targets as target\n if target.path == path and target.queue == queue\n if target.policy != \"anonymous\" and !token.hasPolicy(target.policy)\n return false\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 return false\n return true\n return false\n\ninvoke(settings, scope, action, input)\n context db = scope\n schema = schemaName(settings.application)\n if action == \"job\"\n single = SQL\n select id::text, name as queue, data->>'path' as path, state::text,\n retry_count as \"retryCount\", retry_limit as \"retryLimit\",\n to_char(created_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"createdAt\",\n to_char(start_after at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startAfter\",\n to_char(started_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startedAt\",\n to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"completedAt\",\n data->'input' as input, data->'source' as source, output\n from ${schema}.job\n where id = :id::uuid and data->>'application' = :application\n and data->'version' = '1'::jsonb\n limit 2\n single.id = input.id\n single.application = settings.application\n single.execute()\n if !single.next()\n return null\n result = summary(single)\n result.input = single.input\n result.source = single.source\n result.output = single.output\n if single.next()\n return null\n return result\n listing = SQL\n select id::text, name as queue, data->>'path' as path, state::text,\n retry_count as \"retryCount\", retry_limit as \"retryLimit\",\n to_char(created_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"createdAt\",\n to_char(start_after at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startAfter\",\n to_char(started_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"startedAt\",\n to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') as \"completedAt\"\n from ${schema}.job\n where data->>'application' = :application and data->'version' = '1'::jsonb\n and (:queue::text is null or name = :queue)\n and (:path::text is null or data->>'path' = :path)\n and (:state::text is null or state::text = :state)\n and (:cursorTime::timestamptz is null or (created_on, id, name) < (:cursorTime::timestamptz, :cursorId::uuid, :cursorQueue::text))\n order by created_on desc, id desc, name desc\n limit :limit\n listing.application = settings.application\n listing.queue = input.queue\n listing.path = input.path\n listing.state = input.state\n listing.limit = input.limit\n listing.cursorTime = null\n listing.cursorId = null\n listing.cursorQueue = null\n if input.position != null\n listing.cursorTime = input.position.createdAt\n listing.cursorId = input.position.id\n listing.cursorQueue = input.position.name\n listing.execute()\n rows = []\n loop while listing.next()\n rows.add(summary(listing))\n return rows\n\nreadJob(settings, db, token, id, management = false)\n usable(db)\n if management\n manager(token)\n if !uuid(id)\n return null\n row = invoke(settings, db, \"job\", {\n id: id\n })\n if row == null\n return null\n if !management and (!owned(row.source, token) or !permitted(settings, row.path, row.queue, token))\n return null\n result = summary(row)\n result.resultAvailable = false\n result.result = null\n result.error = null\n output = row.output\n if output != null and is_object(output) and !is_array(output) and output.version == 1\n if row.state == \"completed\" and Object/hasOwn(output, \"value\")\n result.resultAvailable = true\n result.result = output.value\n if row.state == \"failed\" and output.error != null and is_object(output.error)\n if is_string(output.error.code) and is_bool(output.error.permanent)\n result.error = {\n code: output.error.code\n permanent: output.error.permanent\n }\n if management\n result.input = row.input\n result.source = safeSource(row.source)\n return result\n\nsafeSource(source)\n result = {}\n if source == null or !is_object(source) or is_array(source)\n return null\n for [\"kind\", \"clientId\", \"userId\", \"grantId\", \"identity\"] as field\n if is_string(source[field])\n result[field] = source[field]\n return result\n\nsummary(row)\n return {\n id: row.id\n queue: row.queue\n path: row.path\n state: row.state\n retryCount: row.retryCount\n retryLimit: row.retryLimit\n createdAt: row.createdAt\n startAfter: row.startAfter\n startedAt: row.startedAt\n completedAt: row.completedAt\n }\n\nfilters(options)\n if options == null\n options = {}\n if !is_object(options) or is_array(options)\n throw \"Job list options must be a map\"\n allowed = [\"queue\", \"path\", \"state\", \"limit\", \"cursor\"]\n for options as key: value\n if !allowed.includes(key)\n throw \"Unknown job list option: ${key}\"\n result = {\n queue: null\n path: null\n state: null\n limit: 50\n cursor: null\n }\n for [\"queue\", \"path\", \"state\"] as field\n value = options[field]\n if value != null\n if !is_string(value) or value == \"\" or value.length > 2048\n throw \"Invalid job list filter: ${field}\"\n result[field] = value\n states = [\"created\", \"retry\", \"active\", \"completed\", \"cancelled\", \"failed\"]\n if result.state != null and !states.includes(result.state)\n throw \"Invalid job state filter\"\n if options.limit != null\n if !Number/isSafeInteger(options.limit) or options.limit < 1 or options.limit > 200\n throw \"Job list limit must be between 1 and 200\"\n result.limit = options.limit\n if options.cursor != null\n if !is_string(options.cursor) or options.cursor.length == 0 or options.cursor.length > 8192\n throw \"Invalid job cursor\"\n result.cursor = options.cursor\n return result\n\ndecodeCursor(settings, selected)\n if selected.cursor == null\n return null\n try\n bytes = Buffer/from(selected.cursor, \"base64url\")\n if bytes.toString(\"base64url\") != selected.cursor\n throw \"Invalid encoding\"\n value = from_json(bytes.toString(\"utf8\"))\n if value == null or !is_object(value) or is_array(value)\n throw \"Invalid cursor\"\n if value.version != 1 or value.application != settings.application\n throw \"Wrong cursor scope\"\n for [\"queue\", \"path\", \"state\"] as field\n if !Object/hasOwn(value, field) or value[field] != selected[field]\n throw \"Changed cursor filters\"\n pattern = new RegExp(\"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\\\.[0-9]{6}Z$\")\n if !is_string(value.createdAt) or !pattern.test(value.createdAt) or !uuid(value.id) or !is_string(value.name)\n throw \"Invalid cursor position\"\n stamp = Date/parse(value.createdAt)\n if !Number/isFinite(stamp)\n throw \"Invalid cursor time\"\n date = new Date(stamp)\n if date.toISOString() != \"${value.createdAt.slice(0, 23)}Z\"\n throw \"Invalid cursor time\"\n if value.name.length == 0 or value.name.length > 200\n throw \"Invalid cursor queue\"\n return value\n catch error\n throw \"Invalid job cursor\"\n\nencodeCursor(settings, selected, row)\n return Buffer/from(to_json({\n version: 1\n application: settings.application\n queue: selected.queue\n path: selected.path\n state: selected.state\n createdAt: row.createdAt\n id: row.id\n name: row.queue\n }), \"utf8\").toString(\"base64url\")\n\nreadJobs(settings, db, token, options)\n usable(db)\n manager(token)\n selected = filters(options)\n cursor = decodeCursor(settings, selected)\n rows = invoke(settings, db, \"jobs\", {\n queue: selected.queue\n path: selected.path\n state: selected.state\n limit: selected.limit + 1\n position: cursor\n })\n items = []\n last = null\n for rows as row\n if items.length == selected.limit\n break\n items.add(summary(row))\n last = row\n next = null\n if rows.length > selected.limit\n next = encodeCursor(settings, selected, last)\n return {\n items: items\n nextCursor: next\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAcA;;;;;;;;;;;AAMA;;;;;;;;;;AAKA;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA;;;;;;;;;;;;;;;;;;;;;;;;;AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA;;;;;;;;;;;;;;;;AASA;;;;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,133 @@
1
+ import { schemaName } from "./namespace.mjs";
2
+ import * as server from "@lango-project/lango-server";
3
+ import {
4
+ hostGlobal as __hostGlobal,
5
+ hostGlobalCall as __hostGlobalCall,
6
+ contextGet as __contextGet,
7
+ contextSet as __contextSet,
8
+ contextRestore as __contextRestore,
9
+ bindContexts as __bindContexts,
10
+ requireBoolean as __requireBoolean,
11
+ normalize as __normalize,
12
+ finite as __finite,
13
+ boundary as __boundary,
14
+ query as __query,
15
+ requireArity as __requireArity,
16
+ withArity as __withArity,
17
+ } from "./.lango/runtime.mjs";
18
+
19
+ export class Retention {
20
+ #config;
21
+ #interval;
22
+ #batchSize;
23
+ #timer;
24
+ #pending;
25
+ #closed;
26
+ constructor(config, intervalSeconds, batchSize) {
27
+ __requireArity(arguments.length, [0, 1, 2], "constructor");
28
+ config = __boundary(config);
29
+ intervalSeconds = __boundary(intervalSeconds);
30
+ batchSize = __boundary(batchSize);
31
+ this.#config = config;
32
+ this.#interval = __finite(intervalSeconds * 1000);
33
+ this.#batchSize = batchSize;
34
+ this.#timer = null;
35
+ this.#pending = null;
36
+ this.#closed = false;
37
+ }
38
+ start() {
39
+ this.#pending = this.#sweep();
40
+ return __normalize(this.#pending).then(__withArity(__bindContexts((ignored) => {
41
+ ignored = __boundary(ignored);
42
+ this.#arm();
43
+ return null;
44
+ }), [0], "closure"));
45
+ return null;
46
+ }
47
+ #arm() {
48
+ if (__requireBoolean(__normalize(this.#closed))) {
49
+ return null;
50
+ }
51
+ let fire = __bindContexts(() => {
52
+ this.#timer = null;
53
+ this.#pending = this.#sweep();
54
+ this.#pending = __normalize(this.#pending).catch(__withArity(__bindContexts((error) => {
55
+ const __contextScope = [];
56
+ try {
57
+ error = __boundary(error);
58
+ __normalize(__contextGet("log").err(`Boss retention cleanup failed: ${error}`));
59
+ return null;
60
+ } finally {
61
+ __contextRestore(__contextScope);
62
+ }
63
+ }), [0], "closure")).finally(__bindContexts(() => {
64
+ this.#arm();
65
+ return null;
66
+ }));
67
+ return null;
68
+ });
69
+ this.#timer = __hostGlobalCall(["setTimeout"], fire, __normalize(this.#interval));
70
+ __normalize(this.#timer.unref());
71
+ return null;
72
+ return null;
73
+ }
74
+ #sweep() {
75
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
76
+ ignored = __boundary(ignored);
77
+ return cleanRecords(__normalize(this.#config), __normalize(this.#batchSize));
78
+ return null;
79
+ }), [0], "closure")).then(__withArity(__bindContexts((count) => {
80
+ const __contextScope = [];
81
+ try {
82
+ count = __boundary(count);
83
+ __normalize(__contextGet("log").debug(`Boss retention cleanup removed ${count} terminal jobs`));
84
+ return count;
85
+ return null;
86
+ } finally {
87
+ __contextRestore(__contextScope);
88
+ }
89
+ }), [0], "closure"));
90
+ return null;
91
+ }
92
+ close() {
93
+ this.#closed = true;
94
+ if (__requireBoolean(__normalize(this.#timer) !== null)) {
95
+ __hostGlobalCall(["clearTimeout"], __normalize(this.#timer));
96
+ this.#timer = null;
97
+ }
98
+ return __normalize(__hostGlobal(["Promise"]).resolve(__normalize(this.#pending)));
99
+ return null;
100
+ }
101
+ }
102
+ function scope(config) {
103
+ __requireArity(arguments.length, [0], "scope");
104
+ config = __boundary(config);
105
+ let schema = schemaName(__normalize(config.application));
106
+ let query = __query("select set_config('search_path', :schema, false)", ["schema"]);
107
+ query.schema = schema;
108
+ __normalize(query.execute());
109
+ return null;
110
+ return null;
111
+ }
112
+
113
+ export function cleanRecords(config, batchSize) {
114
+ __requireArity(arguments.length, [0, 1], "cleanRecords");
115
+ config = __boundary(config);
116
+ batchSize = __boundary(batchSize);
117
+ let action = __bindContexts(() => {
118
+ scope(config);
119
+ let protect = __query("update job set deletion_seconds = 0\nwhere data->>'application' = :application and data->>'version' = '1'\n and deletion_seconds <> 0", ["application"]);
120
+ protect.application = __normalize(config.application);
121
+ __normalize(protect.execute());
122
+ let query = __query("with candidates as (\n select name, id from job\n where data->>'application' = :application and data->>'version' = '1'\n and state in ('failed', 'completed', 'cancelled')\n and completed_on is not null\n and completed_on + (\n case when state = 'failed' then\n case when jsonb_typeof(data->'failedRetentionSeconds') = 'number' then\n case when (data->>'failedRetentionSeconds')::numeric between 1 and 2147483647\n and trunc((data->>'failedRetentionSeconds')::numeric) = (data->>'failedRetentionSeconds')::numeric\n then (data->>'failedRetentionSeconds')::numeric else 604800 end\n else 604800 end\n else 604800 end\n ) * interval '1 second' <= now()\n order by completed_on, name, id\n limit :batchSize for update skip locked\n), removed as (\n delete from job j using candidates c\n where j.name = c.name and j.id = c.id\n returning j.id\n)\nselect count(*)::integer as total from removed", ["application","batchSize"]);
123
+ query.application = __normalize(config.application);
124
+ query.batchSize = batchSize;
125
+ __normalize(query.execute());
126
+ __normalize(query.next());
127
+ return __normalize(query.total);
128
+ return null;
129
+ });
130
+ return __normalize(server.withDatabase(__normalize(config.root), action));
131
+ return null;
132
+ }
133
+ //# sourceMappingURL=retention.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retention.mjs","sources":["retention.lango"],"sourcesContent":["import \"./namespace\"\n schemaName(application)\nimport js \"@lango-project/lango-server\" as server\nimport js global Promise\nimport js global setTimeout\nimport js global clearTimeout\n\nexport cleanRecords(config, batchSize)\nexport class Retention(config, intervalSeconds, batchSize)\n methods\n start()\n close()\n\nRetention(config, intervalSeconds, batchSize)\n @config = config\n @interval = intervalSeconds * 1000\n @batchSize = batchSize\n @timer = null\n @pending = null\n @closed = false\n\nRetention:start()\n @pending = sweep()\n return chain @pending\n then(ignored)\n arm()\n\nRetention:arm()\n if @closed\n return null\n fire = ()\n @timer = null\n @pending = sweep()\n @pending = chain @pending\n catch(error)\n context log\n log.err(\"Boss retention cleanup failed: ${error}\")\n finally\n arm()\n @timer = setTimeout(fire, @interval)\n @timer.unref()\n return null\n\nRetention:sweep()\n return chain Promise/resolve(null)\n then(ignored)\n return cleanRecords(@config, @batchSize)\n then(count)\n context log\n log.debug(\"Boss retention cleanup removed ${count} terminal jobs\")\n return count\n\nRetention:close()\n @closed = true\n if @timer != null\n clearTimeout(@timer)\n @timer = null\n return Promise/resolve(@pending)\n\nscope(config)\n schema = schemaName(config.application)\n query = SQL\n select set_config('search_path', :schema, false)\n query.schema = schema\n query.execute()\n return null\n\ncleanRecords(config, batchSize)\n action = ()\n capture config\n capture batchSize\n scope(config)\n protect = SQL\n update job set deletion_seconds = 0\n where data->>'application' = :application and data->>'version' = '1'\n and deletion_seconds <> 0\n protect.application = config.application\n protect.execute()\n query = SQL\n with candidates as (\n select name, id from job\n where data->>'application' = :application and data->>'version' = '1'\n and state in ('failed', 'completed', 'cancelled')\n and completed_on is not null\n and completed_on + (\n case when state = 'failed' then\n case when jsonb_typeof(data->'failedRetentionSeconds') = 'number' then\n case when (data->>'failedRetentionSeconds')::numeric between 1 and 2147483647\n and trunc((data->>'failedRetentionSeconds')::numeric) = (data->>'failedRetentionSeconds')::numeric\n then (data->>'failedRetentionSeconds')::numeric else 604800 end\n else 604800 end\n else 604800 end\n ) * interval '1 second' <= now()\n order by completed_on, name, id\n limit :batchSize for update skip locked\n ), removed as (\n delete from job j using candidates c\n where j.name = c.name and j.id = c.id\n returning j.id\n )\n select count(*)::integer as total from removed\n query.application = config.application\n query.batchSize = batchSize\n query.execute()\n query.next()\n return query.total\n return server/withDatabase(config.root, action)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;"}