@lango-project/lango-boss 0.0.0-stage → 0.1.1
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 +118 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/entries/cli.mjs +139 -0
- package/dist/.lango/entries/cli.mjs.map +1 -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 +7 -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/entries/app.mjs +133 -0
- package/examples/reports/dist/.lango/entries/app.mjs.map +1 -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 +7 -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 +62 -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,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostGlobal as __hostGlobal,
|
|
3
|
+
isNumber as __isNumber,
|
|
4
|
+
isString as __isString,
|
|
5
|
+
isBool as __isBool,
|
|
6
|
+
isObject as __isObject,
|
|
7
|
+
isArray as __isArray,
|
|
8
|
+
entries as __entries,
|
|
9
|
+
requireBoolean as __requireBoolean,
|
|
10
|
+
normalize as __normalize,
|
|
11
|
+
numericAdd as __numericAdd,
|
|
12
|
+
boundary as __boundary,
|
|
13
|
+
requireArity as __requireArity,
|
|
14
|
+
} from "./.lango/runtime.mjs";
|
|
15
|
+
|
|
16
|
+
function map(value, label) {
|
|
17
|
+
__requireArity(arguments.length, [0, 1], "map");
|
|
18
|
+
value = __boundary(value);
|
|
19
|
+
label = __boundary(label);
|
|
20
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(value === null) || __requireBoolean(!__requireBoolean(__isObject(value)))) || __requireBoolean(__isArray(value)))) {
|
|
21
|
+
throw `${label} must be a map`;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function fields(value, allowed) {
|
|
28
|
+
__requireArity(arguments.length, [0, 1], "fields");
|
|
29
|
+
value = __boundary(value);
|
|
30
|
+
allowed = __boundary(allowed);
|
|
31
|
+
for (const [key, item] of __entries(value)) {
|
|
32
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
33
|
+
throw `Unknown envelope field: ${key}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function executionTask(config, queue, job) {
|
|
41
|
+
__requireArity(arguments.length, [0, 1, 2], "executionTask");
|
|
42
|
+
config = __boundary(config);
|
|
43
|
+
queue = __boundary(queue);
|
|
44
|
+
job = __boundary(job);
|
|
45
|
+
let payload = __normalize(job.data);
|
|
46
|
+
map(payload, "Job envelope");
|
|
47
|
+
fields(payload, ["version", "application", "path", "input", "source", "failedRetentionSeconds"]);
|
|
48
|
+
if (__requireBoolean(__requireBoolean(__normalize(payload.version) !== 1) || __requireBoolean(__normalize(payload.application) !== __normalize(config.application)))) {
|
|
49
|
+
throw "Unsupported or foreign job envelope";
|
|
50
|
+
}
|
|
51
|
+
let target = null;
|
|
52
|
+
for (const item of __normalize(config.targets)) {
|
|
53
|
+
if (__requireBoolean(__requireBoolean(__normalize(item.path) === __normalize(payload.path)) && __requireBoolean(__normalize(item.queue) === queue))) {
|
|
54
|
+
target = item;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (__requireBoolean(target === null)) {
|
|
58
|
+
throw "Job path is not registered for this queue";
|
|
59
|
+
}
|
|
60
|
+
map(__normalize(payload.input), "Job input");
|
|
61
|
+
let source = __normalize(payload.source);
|
|
62
|
+
map(source, "Job source");
|
|
63
|
+
fields(source, ["kind", "clientId", "userId", "grantId", "identity"]);
|
|
64
|
+
for (const [key, value] of __entries(source)) {
|
|
65
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(__normalize(value.trim()) === ""))) {
|
|
66
|
+
throw "Job source references must be nonempty strings";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (__normalize(source.kind) === "user") {
|
|
70
|
+
if (__requireBoolean(__requireBoolean(__normalize(source.clientId) === null) || __requireBoolean(__normalize(source.identity) !== null))) {
|
|
71
|
+
throw "Invalid user job source";
|
|
72
|
+
}
|
|
73
|
+
} else if (__normalize(source.kind) === "system") {
|
|
74
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(source.identity) === null) || __requireBoolean(__normalize(source.clientId) !== null)) || __requireBoolean(__normalize(source.userId) !== null)) || __requireBoolean(__normalize(source.grantId) !== null))) {
|
|
75
|
+
throw "Invalid system job source";
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
throw "Unsupported job source";
|
|
79
|
+
}
|
|
80
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(payload.failedRetentionSeconds))))) || __requireBoolean(__normalize(payload.failedRetentionSeconds) < 1)) || __requireBoolean(__normalize(payload.failedRetentionSeconds) > 2147483647))) {
|
|
81
|
+
throw "Invalid failed retention metadata";
|
|
82
|
+
}
|
|
83
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(job.retryCount))))) || __requireBoolean(__normalize(job.retryCount) < 0))) {
|
|
84
|
+
throw "Invalid job attempt metadata";
|
|
85
|
+
}
|
|
86
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(job.expireInSeconds))))) || __requireBoolean(__normalize(job.expireInSeconds) < 1)) || __requireBoolean(__normalize(job.expireInSeconds) > 1209600))) {
|
|
87
|
+
throw "Invalid job expiration metadata";
|
|
88
|
+
}
|
|
89
|
+
return { id: __normalize(job.id), path: __normalize(target.path), input: __normalize(payload.input), source: source, attempt: __numericAdd(__normalize(job.retryCount), 1) };
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=envelope.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.mjs","sources":["envelope.lango"],"sourcesContent":["import js global Number\n\nexport executionTask(config, queue, job)\n\nmap(value, label)\n if value == null or !is_object(value) or is_array(value)\n throw \"${label} must be a map\"\n return null\n\nfields(value, allowed)\n for value as key: item\n if !allowed.includes(key)\n throw \"Unknown envelope field: ${key}\"\n return null\n\nexecutionTask(config, queue, job)\n payload = job.data\n map(payload, \"Job envelope\")\n fields(payload, [\"version\", \"application\", \"path\", \"input\", \"source\", \"failedRetentionSeconds\"])\n if payload.version != 1 or payload.application != config.application\n throw \"Unsupported or foreign job envelope\"\n target = null\n for config.targets as item\n if item.path == payload.path and item.queue == queue\n target = item\n if target == null\n throw \"Job path is not registered for this queue\"\n map(payload.input, \"Job input\")\n source = payload.source\n map(source, \"Job source\")\n fields(source, [\"kind\", \"clientId\", \"userId\", \"grantId\", \"identity\"])\n for source as key: value\n if !is_string(value) or value.trim() == \"\"\n throw \"Job source references must be nonempty strings\"\n switch source.kind\n case \"user\"\n if source.clientId == null or source.identity != null\n throw \"Invalid user job source\"\n case \"system\"\n if source.identity == null or source.clientId != null or source.userId != null or source.grantId != null\n throw \"Invalid system job source\"\n else\n throw \"Unsupported job source\"\n if !Number/isSafeInteger(payload.failedRetentionSeconds) or payload.failedRetentionSeconds < 1 or payload.failedRetentionSeconds > 2147483647\n throw \"Invalid failed retention metadata\"\n if !Number/isSafeInteger(job.retryCount) or job.retryCount < 0\n throw \"Invalid job attempt metadata\"\n if !Number/isSafeInteger(job.expireInSeconds) or job.expireInSeconds < 1 or job.expireInSeconds > 1209600\n throw \"Invalid job expiration metadata\"\n return {\n id: job.id\n path: target.path\n input: payload.input\n source: source\n attempt: job.retryCount + 1\n }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA;;;;;;;;;;;AAKA;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"ca6d12e160212fec63b419f357602911c8c4ab380d0459f69081b6e2f88bae3b","outputHash":"fef194972bd88a93f189cd9017381e8403900610edb4374430cd1c105e22d9e6","mapHash":"c58fa18dd8a40f281fdee35e1e284d6d9bc397c169298089c5006ec75cd81597"}}
|
package/dist/library.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { loadConfiguration as configuration } from "./configuration.mjs";
|
|
2
|
+
import { Boss as BossClass } from "./boss.mjs";
|
|
3
|
+
import { Worker as WorkerClass } from "./worker.mjs";
|
|
4
|
+
import {
|
|
5
|
+
contextGet as __contextGet,
|
|
6
|
+
contextSet as __contextSet,
|
|
7
|
+
contextRestore as __contextRestore,
|
|
8
|
+
normalize as __normalize,
|
|
9
|
+
boundary as __boundary,
|
|
10
|
+
requireArity as __requireArity,
|
|
11
|
+
} from "./.lango/runtime.mjs";
|
|
12
|
+
|
|
13
|
+
export let Boss = BossClass;
|
|
14
|
+
|
|
15
|
+
export let Worker = WorkerClass;
|
|
16
|
+
export function loadConfiguration(directory) {
|
|
17
|
+
__requireArity(arguments.length, [0], "loadConfiguration");
|
|
18
|
+
directory = __boundary(directory);
|
|
19
|
+
return configuration(directory);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function jobs(options = null) {
|
|
24
|
+
const __contextScope = [];
|
|
25
|
+
try {
|
|
26
|
+
options = __boundary(options);
|
|
27
|
+
return __normalize(__contextGet("queue").jobs(options));
|
|
28
|
+
return null;
|
|
29
|
+
} finally {
|
|
30
|
+
__contextRestore(__contextScope);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function job(id) {
|
|
35
|
+
const __contextScope = [];
|
|
36
|
+
try {
|
|
37
|
+
__requireArity(arguments.length, [0], "job");
|
|
38
|
+
id = __boundary(id);
|
|
39
|
+
return __normalize(__contextGet("queue").job(id));
|
|
40
|
+
return null;
|
|
41
|
+
} finally {
|
|
42
|
+
__contextRestore(__contextScope);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function retry(id) {
|
|
47
|
+
const __contextScope = [];
|
|
48
|
+
try {
|
|
49
|
+
__requireArity(arguments.length, [0], "retry");
|
|
50
|
+
id = __boundary(id);
|
|
51
|
+
return __normalize(__contextGet("queue").manage("retry", id));
|
|
52
|
+
return null;
|
|
53
|
+
} finally {
|
|
54
|
+
__contextRestore(__contextScope);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function cancel(id) {
|
|
59
|
+
const __contextScope = [];
|
|
60
|
+
try {
|
|
61
|
+
__requireArity(arguments.length, [0], "cancel");
|
|
62
|
+
id = __boundary(id);
|
|
63
|
+
return __normalize(__contextGet("queue").manage("cancel", id));
|
|
64
|
+
return null;
|
|
65
|
+
} finally {
|
|
66
|
+
__contextRestore(__contextScope);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function queues(options = null) {
|
|
71
|
+
const __contextScope = [];
|
|
72
|
+
try {
|
|
73
|
+
options = __boundary(options);
|
|
74
|
+
return __normalize(__contextGet("queue").manage("queues", null, options));
|
|
75
|
+
return null;
|
|
76
|
+
} finally {
|
|
77
|
+
__contextRestore(__contextScope);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function schedules(options = null) {
|
|
82
|
+
const __contextScope = [];
|
|
83
|
+
try {
|
|
84
|
+
options = __boundary(options);
|
|
85
|
+
return __normalize(__contextGet("queue").manage("schedules", null, options));
|
|
86
|
+
return null;
|
|
87
|
+
} finally {
|
|
88
|
+
__contextRestore(__contextScope);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function pauseQueue(name) {
|
|
93
|
+
const __contextScope = [];
|
|
94
|
+
try {
|
|
95
|
+
__requireArity(arguments.length, [0], "pauseQueue");
|
|
96
|
+
name = __boundary(name);
|
|
97
|
+
return __normalize(__contextGet("queue").manage("pauseQueue", name));
|
|
98
|
+
return null;
|
|
99
|
+
} finally {
|
|
100
|
+
__contextRestore(__contextScope);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function resumeQueue(name) {
|
|
105
|
+
const __contextScope = [];
|
|
106
|
+
try {
|
|
107
|
+
__requireArity(arguments.length, [0], "resumeQueue");
|
|
108
|
+
name = __boundary(name);
|
|
109
|
+
return __normalize(__contextGet("queue").manage("resumeQueue", name));
|
|
110
|
+
return null;
|
|
111
|
+
} finally {
|
|
112
|
+
__contextRestore(__contextScope);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function pauseSchedule(name) {
|
|
117
|
+
const __contextScope = [];
|
|
118
|
+
try {
|
|
119
|
+
__requireArity(arguments.length, [0], "pauseSchedule");
|
|
120
|
+
name = __boundary(name);
|
|
121
|
+
return __normalize(__contextGet("queue").manage("pauseSchedule", name));
|
|
122
|
+
return null;
|
|
123
|
+
} finally {
|
|
124
|
+
__contextRestore(__contextScope);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function resumeSchedule(name) {
|
|
129
|
+
const __contextScope = [];
|
|
130
|
+
try {
|
|
131
|
+
__requireArity(arguments.length, [0], "resumeSchedule");
|
|
132
|
+
name = __boundary(name);
|
|
133
|
+
return __normalize(__contextGet("queue").manage("resumeSchedule", name));
|
|
134
|
+
return null;
|
|
135
|
+
} finally {
|
|
136
|
+
__contextRestore(__contextScope);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=library.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"library.mjs","sources":["library.lango"],"sourcesContent":["import \"./configuration\"\n alias configuration = loadConfiguration(directory)\nimport \"./boss\"\n alias BossClass = class Boss\nimport \"./worker\"\n alias WorkerClass = class Worker\n\nexport loadConfiguration(directory)\nexport global Boss\nexport global Worker\nexport jobs(options = null)\nexport job(id)\nexport retry(id)\nexport cancel(id)\nexport queues(options = null)\nexport schedules(options = null)\nexport pauseQueue(name)\nexport resumeQueue(name)\nexport pauseSchedule(name)\nexport resumeSchedule(name)\n\nglobal Boss = BossClass\nglobal Worker = WorkerClass\n\nloadConfiguration(directory)\n return configuration(directory)\n\njobs(options = null)\n context queue\n return queue.jobs(options)\n\njob(id)\n context queue\n return queue.job(id)\n\nretry(id)\n context queue\n return queue.manage(\"retry\", id)\n\ncancel(id)\n context queue\n return queue.manage(\"cancel\", id)\n\nqueues(options = null)\n context queue\n return queue.manage(\"queues\", null, options)\n\nschedules(options = null)\n context queue\n return queue.manage(\"schedules\", null, options)\n\npauseQueue(name)\n context queue\n return queue.manage(\"pauseQueue\", name)\n\nresumeQueue(name)\n context queue\n return queue.manage(\"resumeQueue\", name)\n\npauseSchedule(name)\n context queue\n return queue.manage(\"pauseSchedule\", name)\n\nresumeSchedule(name)\n context queue\n return queue.manage(\"resumeSchedule\", name)\n"],"names":[],"mappings":";;;;;;;;;;;;AAqBA;;AACA;AAEA;;;;;;;AAGA;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;AAIA;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;;AAIA;;;;;;;;;;;;","x_lango_build":{"fingerprint":"9acb5d8f8b1155a2129aa56887f61f64bdebcc9ac291a105c81f890a14ba5fc5","outputHash":"22e826272e446067a61c7b8c361dfd08ec8fd6b490c1bea2241eeec2ca3d76c5","mapHash":"004e6fa4bb22111f6e69ab67da602a78e3ecd68893722983b7242fa0ff7661c3"}}
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"db8827bccc009af36e08fbd99bc985d5692a534cc275308485972b3f2b3f307f","outputHash":"6662911f25e98140de9b3270cf6c3f3ac02017be1de61dcef59602482f12ca9f","mapHash":"4148e69961fd6c50b0824d9a807ffd08c909d2532539c9ef158c1430649e1f54"}}
|
|
@@ -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;;;;;;;;;;;;;;;;;;;","x_lango_build":{"fingerprint":"f9866618260a8e6506c8d7b48b7ccbb3032e7df4d581521ca91a15934509f9ea","outputHash":"3de90e1aee624fba989ebbd4f116cf183671dd455b35bc00e3e1569ae80be99c","mapHash":"f19ec9d73674fab08a182a48e53e8b8fb36d8b6c26d3b851916cb79a3201f1f0"}}
|