@lango-project/lango-boss 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +111 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +725 -0
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/boss.mjs +136 -0
- package/dist/boss.mjs.map +1 -0
- package/dist/claim_watch.mjs +99 -0
- package/dist/claim_watch.mjs.map +1 -0
- package/dist/cli.mjs +143 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/configuration.mjs +58 -0
- package/dist/configuration.mjs.map +1 -0
- package/dist/dispatcher.mjs +108 -0
- package/dist/dispatcher.mjs.map +1 -0
- package/dist/driver.mjs +98 -0
- package/dist/driver.mjs.map +1 -0
- package/dist/envelope.mjs +92 -0
- package/dist/envelope.mjs.map +1 -0
- package/dist/library.mjs +139 -0
- package/dist/library.mjs.map +1 -0
- package/dist/management.mjs +360 -0
- package/dist/management.mjs.map +1 -0
- package/dist/management_transaction.mjs +26 -0
- package/dist/management_transaction.mjs.map +1 -0
- package/dist/namespace.mjs +27 -0
- package/dist/namespace.mjs.map +1 -0
- package/dist/queue.mjs +161 -0
- package/dist/queue.mjs.map +1 -0
- package/dist/reads.mjs +359 -0
- package/dist/reads.mjs.map +1 -0
- package/dist/retention.mjs +133 -0
- package/dist/retention.mjs.map +1 -0
- package/dist/schedule_sync.mjs +222 -0
- package/dist/schedule_sync.mjs.map +1 -0
- package/dist/schedule_transaction.mjs +24 -0
- package/dist/schedule_transaction.mjs.map +1 -0
- package/dist/schedules.mjs +201 -0
- package/dist/schedules.mjs.map +1 -0
- package/dist/transaction.mjs +16 -0
- package/dist/transaction.mjs.map +1 -0
- package/dist/validation.mjs +90 -0
- package/dist/validation.mjs.map +1 -0
- package/dist/worker.mjs +316 -0
- package/dist/worker.mjs.map +1 -0
- package/docs/guide.md +88 -0
- package/docs/management.md +93 -0
- package/docs/producer.md +115 -0
- package/docs/reads.md +107 -0
- package/docs/reference.md +77 -0
- package/docs/release.md +103 -0
- package/docs/retention.md +47 -0
- package/docs/scheduling.md +79 -0
- package/docs/worker.md +90 -0
- package/examples/reports/README.md +118 -0
- package/examples/reports/app.lango +133 -0
- package/examples/reports/deploy.xml +6 -0
- package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
- package/examples/reports/dist/.lango/localization.mjs +108 -0
- package/examples/reports/dist/.lango/runtime.mjs +725 -0
- package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
- package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
- package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
- package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
- package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
- package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
- package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
- package/examples/reports/dist/.lango/xmldom/index.js +41 -0
- package/examples/reports/dist/.lango/xmldom/package.json +1 -0
- package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
- package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
- package/examples/reports/dist/app.mjs +137 -0
- package/examples/reports/dist/app.mjs.map +1 -0
- package/examples/reports/dist/handlers.mjs +202 -0
- package/examples/reports/dist/handlers.mjs.map +1 -0
- package/examples/reports/dist/setup.mjs +14 -0
- package/examples/reports/dist/setup.mjs.map +1 -0
- package/examples/reports/handlers.lango +153 -0
- package/examples/reports/jobs.xml +5 -0
- package/examples/reports/package.json +23 -0
- package/examples/reports/routes.xml +4 -0
- package/examples/reports/setup.lango +17 -0
- package/package.json +60 -4
- package/src/boss.lango +92 -0
- package/src/claim_watch.lango +58 -0
- package/src/cli.lango +103 -0
- package/src/configuration.lango +43 -0
- package/src/dispatcher.lango +75 -0
- package/src/driver.lango +72 -0
- package/src/envelope.lango +56 -0
- package/src/library.lango +66 -0
- package/src/management.lango +302 -0
- package/src/management_transaction.lango +28 -0
- package/src/namespace.lango +15 -0
- package/src/queue.lango +115 -0
- package/src/reads.lango +277 -0
- package/src/retention.lango +107 -0
- package/src/schedule_sync.lango +190 -0
- package/src/schedule_transaction.lango +22 -0
- package/src/schedules.lango +165 -0
- package/src/transaction.lango +23 -0
- package/src/validation.lango +45 -0
- package/src/worker.lango +230 -0
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lango-project/lango-boss",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PostgreSQL-backed jobs and queues for Lango applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=24"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/library.mjs",
|
|
15
|
+
"./queue": "./dist/queue.mjs",
|
|
16
|
+
"./transaction": "./dist/transaction.mjs",
|
|
17
|
+
"./schedule-transaction": "./dist/schedule_transaction.mjs",
|
|
18
|
+
"./management-transaction": "./dist/management_transaction.mjs"
|
|
19
|
+
},
|
|
20
|
+
"lango": {
|
|
21
|
+
"source": "src/library.lango",
|
|
22
|
+
"output": "dist/library.mjs"
|
|
23
|
+
},
|
|
24
|
+
"bin": {
|
|
25
|
+
"lango-boss": "dist/cli.mjs"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist/**",
|
|
29
|
+
"src/*.lango",
|
|
30
|
+
"docs/**",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"NOTICE",
|
|
34
|
+
"examples/reports/*.lango",
|
|
35
|
+
"examples/reports/*.xml",
|
|
36
|
+
"examples/reports/package.json",
|
|
37
|
+
"examples/reports/README.md",
|
|
38
|
+
"examples/reports/dist/**"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "lango run tools/clean.lango && lango build src/library.lango --out dist/library.mjs && lango build src/queue.lango --out dist/queue.mjs && lango build src/transaction.lango --out dist/transaction.mjs && lango build src/schedule_transaction.lango --out dist/schedule_transaction.mjs && lango build src/management_transaction.lango --out dist/management_transaction.mjs && lango build src/cli.lango --out dist/cli.mjs --executable && lango build examples/reports/app.lango --out examples/reports/dist/app.mjs --executable && lango build examples/reports/handlers.lango --out examples/reports/dist/handlers.mjs",
|
|
42
|
+
"test": "npm run build && lango test src/test/configuration.lango --build-dir .test-output/configuration && lango test src/test/envelope.lango --build-dir .test-output/envelope",
|
|
43
|
+
"prepack": "npm run build",
|
|
44
|
+
"test:postgres": "npm run build && lango build src/test/producer_handlers.lango --out .test-output/producer_handlers.mjs && lango test src/test/producer.lango --build-dir .test-output/producer && lango build src/test/worker_handlers.lango --out .test-output/worker_handlers.mjs && lango test src/test/worker.lango --build-dir .test-output/worker && lango build src/test/schedule_handlers.lango --out .test-output/schedule_handlers.mjs && lango test src/test/scheduling.lango --build-dir .test-output/scheduling && lango test src/test/retention.lango --build-dir .test-output/retention && lango build src/test/read_handlers.lango --out .test-output/read_handlers.mjs && lango test src/test/reads.lango --build-dir .test-output/reads && lango build src/test/management_handlers.lango --out .test-output/management_handlers.mjs && lango test src/test/management.lango --build-dir .test-output/management",
|
|
45
|
+
"test:workers": "npm run build && lango build src/test/worker_handlers.lango --out .test-output/worker_handlers.mjs && lango test src/test/worker.lango --build-dir .test-output/worker",
|
|
46
|
+
"test:schedules": "npm run build && lango build src/test/schedule_handlers.lango --out .test-output/schedule_handlers.mjs && lango test src/test/scheduling.lango --build-dir .test-output/scheduling",
|
|
47
|
+
"test:retention": "npm run build && lango build src/test/schedule_handlers.lango --out .test-output/schedule_handlers.mjs && lango test src/test/retention.lango --build-dir .test-output/retention",
|
|
48
|
+
"test:reads": "npm run build && lango build src/test/read_handlers.lango --out .test-output/read_handlers.mjs && lango test src/test/reads.lango --build-dir .test-output/reads",
|
|
49
|
+
"test:management": "npm run build && lango build src/test/management_handlers.lango --out .test-output/management_handlers.mjs && lango test src/test/management.lango --build-dir .test-output/management",
|
|
50
|
+
"test:package": "npm run build && lango test src/test/package.lango --build-dir .test-output/package",
|
|
51
|
+
"stage:server": "lango run tools/stage_server.lango --"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@lango-project/lango-server": "^0.2.4",
|
|
55
|
+
"cron-parser": "5.10.7",
|
|
56
|
+
"pg-boss": "12.40.1"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@lango-project/lango-js": "^0.1.18",
|
|
60
|
+
"pg": "^8.23.1"
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/boss.lango
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import "./configuration"
|
|
2
|
+
loadConfiguration(directory)
|
|
3
|
+
import "./driver"
|
|
4
|
+
createDriver(config, consumer = false, scheduling = false, cronMonitorIntervalSeconds = 30)
|
|
5
|
+
prepareQueues(driver, targets)
|
|
6
|
+
queueProvider(config)
|
|
7
|
+
import js "@lango-project/lango-server" as server
|
|
8
|
+
import "./management"
|
|
9
|
+
prepareManagement(config)
|
|
10
|
+
import js global Promise
|
|
11
|
+
import js global Object
|
|
12
|
+
|
|
13
|
+
export class Boss(directory)
|
|
14
|
+
methods
|
|
15
|
+
start()
|
|
16
|
+
close()
|
|
17
|
+
configure(builder)
|
|
18
|
+
executor(settings)
|
|
19
|
+
|
|
20
|
+
Boss(directory)
|
|
21
|
+
@config = loadConfiguration(directory)
|
|
22
|
+
@state = "new"
|
|
23
|
+
@driver = null
|
|
24
|
+
@starting = null
|
|
25
|
+
@closing = null
|
|
26
|
+
|
|
27
|
+
Boss:start()
|
|
28
|
+
if @state != "new"
|
|
29
|
+
return Promise/reject("Boss can only be started once")
|
|
30
|
+
@state = "starting"
|
|
31
|
+
@driver = createDriver(@config)
|
|
32
|
+
@starting = prepare()
|
|
33
|
+
return @starting
|
|
34
|
+
|
|
35
|
+
Boss:prepare()
|
|
36
|
+
return chain @driver.start()
|
|
37
|
+
then(ignored)
|
|
38
|
+
return prepareQueues(@driver, @config.targets)
|
|
39
|
+
then(ignored)
|
|
40
|
+
return prepareManagement(@config)
|
|
41
|
+
then(ignored)
|
|
42
|
+
if @state != "starting"
|
|
43
|
+
throw "Boss closed during startup"
|
|
44
|
+
@state = "running"
|
|
45
|
+
return null
|
|
46
|
+
catch(error)
|
|
47
|
+
return chain @driver.stop()
|
|
48
|
+
then(ignored)
|
|
49
|
+
capture error
|
|
50
|
+
@state = "closed"
|
|
51
|
+
throw error
|
|
52
|
+
|
|
53
|
+
Boss:provider()
|
|
54
|
+
return queueProvider(@config)
|
|
55
|
+
|
|
56
|
+
Boss:configure(builder)
|
|
57
|
+
if @state != "running"
|
|
58
|
+
throw "Start Boss before configuring queue submission"
|
|
59
|
+
builder.provideQueue(provider())
|
|
60
|
+
return builder
|
|
61
|
+
|
|
62
|
+
Boss:executor(settings)
|
|
63
|
+
if @state != "running"
|
|
64
|
+
throw "Start Boss before creating an executor"
|
|
65
|
+
if settings == null or !is_object(settings) or is_array(settings) or settings.queueProvider != null
|
|
66
|
+
throw "Executor requires settings without a separate queue provider"
|
|
67
|
+
options = Object/assign({}, settings, {
|
|
68
|
+
queueProvider: provider()
|
|
69
|
+
})
|
|
70
|
+
return new server/Executor(@config.root, options)
|
|
71
|
+
|
|
72
|
+
Boss:close()
|
|
73
|
+
if @closing != null
|
|
74
|
+
return @closing
|
|
75
|
+
if @state == "new" or @state == "closed"
|
|
76
|
+
@state = "closed"
|
|
77
|
+
return Promise/resolve(null)
|
|
78
|
+
@state = "closing"
|
|
79
|
+
pending = Promise/resolve(null)
|
|
80
|
+
if @starting != null
|
|
81
|
+
pending = @starting
|
|
82
|
+
@closing = closeAfter(pending)
|
|
83
|
+
return @closing
|
|
84
|
+
|
|
85
|
+
Boss:closeAfter(pending)
|
|
86
|
+
return chain pending
|
|
87
|
+
catch(error)
|
|
88
|
+
return null
|
|
89
|
+
then(ignored)
|
|
90
|
+
return @driver.stop()
|
|
91
|
+
finally
|
|
92
|
+
@state = "closed"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import js global AbortController
|
|
2
|
+
import js global Promise
|
|
3
|
+
import js global setTimeout
|
|
4
|
+
import js global clearTimeout
|
|
5
|
+
|
|
6
|
+
export class ClaimWatch(driver, queue, job)
|
|
7
|
+
properties
|
|
8
|
+
signal?
|
|
9
|
+
methods
|
|
10
|
+
start()
|
|
11
|
+
close()
|
|
12
|
+
|
|
13
|
+
ClaimWatch(driver, queue, job)
|
|
14
|
+
@driver = driver
|
|
15
|
+
@queue = queue
|
|
16
|
+
@job = job
|
|
17
|
+
@controller = new AbortController()
|
|
18
|
+
@timer = null
|
|
19
|
+
@pending = null
|
|
20
|
+
@closed = false
|
|
21
|
+
|
|
22
|
+
ClaimWatch:signal?
|
|
23
|
+
return @controller.signal
|
|
24
|
+
|
|
25
|
+
ClaimWatch:start()
|
|
26
|
+
if @driver == null or @closed
|
|
27
|
+
return null
|
|
28
|
+
fire = ()
|
|
29
|
+
@timer = null
|
|
30
|
+
@pending = check()
|
|
31
|
+
@pending = chain @pending
|
|
32
|
+
catch(error)
|
|
33
|
+
context log
|
|
34
|
+
log.err("[jobId=${@job.id}] queue=${@queue} Claim check failed: ${error}")
|
|
35
|
+
finally
|
|
36
|
+
start()
|
|
37
|
+
@timer = setTimeout(fire, 1000)
|
|
38
|
+
@timer.unref()
|
|
39
|
+
return null
|
|
40
|
+
|
|
41
|
+
ClaimWatch:check()
|
|
42
|
+
return chain Promise/resolve(null)
|
|
43
|
+
then(ignored)
|
|
44
|
+
return @driver.touch(@queue, [@job])
|
|
45
|
+
then(result)
|
|
46
|
+
if result.settled != null and !result.settled.includes(@job.id)
|
|
47
|
+
context log
|
|
48
|
+
log.debug("[jobId=${@job.id}] queue=${@queue} Claim ended; aborting local execution")
|
|
49
|
+
@closed = true
|
|
50
|
+
@controller.abort()
|
|
51
|
+
return null
|
|
52
|
+
|
|
53
|
+
ClaimWatch:close()
|
|
54
|
+
@closed = true
|
|
55
|
+
if @timer != null
|
|
56
|
+
clearTimeout(@timer)
|
|
57
|
+
@timer = null
|
|
58
|
+
return Promise/resolve(@pending)
|
package/src/cli.lango
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import "./configuration"
|
|
2
|
+
loadConfiguration(directory)
|
|
3
|
+
import "./worker"
|
|
4
|
+
class Worker
|
|
5
|
+
import js global process
|
|
6
|
+
import js global Number
|
|
7
|
+
|
|
8
|
+
export main(args)
|
|
9
|
+
|
|
10
|
+
main(args)
|
|
11
|
+
if args.length >= 2 and args[0] == "worker"
|
|
12
|
+
return startWorker(args)
|
|
13
|
+
if args.length != 2 or args[0] != "check"
|
|
14
|
+
usage()
|
|
15
|
+
process.exitCode = 1
|
|
16
|
+
return null
|
|
17
|
+
try
|
|
18
|
+
config = loadConfiguration(args[1])
|
|
19
|
+
println("${config.application}: ${config.targets.length} job routes, ${config.schedules.length} schedules; configuration valid")
|
|
20
|
+
catch error
|
|
21
|
+
process.stderr.write("Lango Boss configuration check failed: ${error}\n")
|
|
22
|
+
process.exitCode = 1
|
|
23
|
+
return null
|
|
24
|
+
|
|
25
|
+
usage()
|
|
26
|
+
process.stderr.write("Usage: lango-boss check <application-directory>\n lango-boss worker <application-directory> --identity-module <module> --identity-handler <export> [--workers <count>] [--verbose]\n")
|
|
27
|
+
return null
|
|
28
|
+
|
|
29
|
+
startWorker(args)
|
|
30
|
+
worker = null
|
|
31
|
+
try
|
|
32
|
+
settings = parseWorker(args)
|
|
33
|
+
worker = new Worker(args[1], settings)
|
|
34
|
+
catch error
|
|
35
|
+
process.stderr.write("Lango Boss worker configuration failed: ${error}\n")
|
|
36
|
+
process.exitCode = 1
|
|
37
|
+
return null
|
|
38
|
+
state = {
|
|
39
|
+
stopping: false
|
|
40
|
+
stop: null
|
|
41
|
+
}
|
|
42
|
+
stop = ()
|
|
43
|
+
capture worker
|
|
44
|
+
capture state
|
|
45
|
+
if state.stopping
|
|
46
|
+
return null
|
|
47
|
+
state.stopping = true
|
|
48
|
+
return chain worker.close()
|
|
49
|
+
catch(error)
|
|
50
|
+
process.stderr.write("Lango Boss shutdown failed: ${error}\n")
|
|
51
|
+
process.exitCode = 1
|
|
52
|
+
finally
|
|
53
|
+
capture state
|
|
54
|
+
removeSignals(state.stop)
|
|
55
|
+
state.stop = stop
|
|
56
|
+
process.once("SIGINT", stop)
|
|
57
|
+
process.once("SIGTERM", stop)
|
|
58
|
+
return chain worker.start()
|
|
59
|
+
then(ignored)
|
|
60
|
+
println("Lango Boss worker started")
|
|
61
|
+
return null
|
|
62
|
+
catch(error)
|
|
63
|
+
capture state
|
|
64
|
+
process.stderr.write("Lango Boss worker startup failed: ${error}\n")
|
|
65
|
+
process.exitCode = 1
|
|
66
|
+
removeSignals(state.stop)
|
|
67
|
+
return null
|
|
68
|
+
|
|
69
|
+
removeSignals(handler)
|
|
70
|
+
process.removeListener("SIGINT", handler)
|
|
71
|
+
process.removeListener("SIGTERM", handler)
|
|
72
|
+
return null
|
|
73
|
+
|
|
74
|
+
parseWorker(args)
|
|
75
|
+
settings = {
|
|
76
|
+
identity: {}
|
|
77
|
+
}
|
|
78
|
+
index = 2
|
|
79
|
+
seen = []
|
|
80
|
+
loop while index < args.length
|
|
81
|
+
flag = args[index]
|
|
82
|
+
if seen.includes(flag)
|
|
83
|
+
throw "Duplicate worker option: ${flag}"
|
|
84
|
+
seen.add(flag)
|
|
85
|
+
if flag == "--verbose"
|
|
86
|
+
process.env.LANGO_VERBOSE = "1"
|
|
87
|
+
index = index + 1
|
|
88
|
+
continue
|
|
89
|
+
index = index + 1
|
|
90
|
+
if index >= args.length
|
|
91
|
+
throw "Missing value for ${flag}"
|
|
92
|
+
value = args[index]
|
|
93
|
+
switch flag
|
|
94
|
+
case "--identity-module"
|
|
95
|
+
settings.identity.module = value
|
|
96
|
+
case "--identity-handler"
|
|
97
|
+
settings.identity.handler = value
|
|
98
|
+
case "--workers"
|
|
99
|
+
settings.workers = Number(value)
|
|
100
|
+
else
|
|
101
|
+
throw "Unknown worker option: ${flag}"
|
|
102
|
+
index = index + 1
|
|
103
|
+
return settings
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import js "@lango-project/lango-server" as server
|
|
2
|
+
import js "node:fs" as fs
|
|
3
|
+
import js "node:path" as path
|
|
4
|
+
import "./schedules"
|
|
5
|
+
scheduleConfiguration(xml, targets)
|
|
6
|
+
import "./validation"
|
|
7
|
+
name(value, label)
|
|
8
|
+
|
|
9
|
+
export loadConfiguration(directory)
|
|
10
|
+
|
|
11
|
+
loadConfiguration(directory)
|
|
12
|
+
if !is_string(directory) or directory.trim() == ""
|
|
13
|
+
throw "Application directory is required"
|
|
14
|
+
if server.readDatabaseConfiguration == null or server.readJobRoutes == null
|
|
15
|
+
throw "Lango Server must provide readDatabaseConfiguration and readJobRoutes; update or link the current Server package"
|
|
16
|
+
root = path/resolve(directory)
|
|
17
|
+
application = from_json(fs/readFileSync(path/join(root, "package.json"), "utf8"))
|
|
18
|
+
appName = name(application.name, "Application name")
|
|
19
|
+
database = server/readDatabaseConfiguration(root)
|
|
20
|
+
if database == null or database.type != "postgresql"
|
|
21
|
+
throw "Lango Boss requires the application's PostgreSQL database configuration"
|
|
22
|
+
targets = server/readJobRoutes(root)
|
|
23
|
+
for targets as target
|
|
24
|
+
name(target.queue, "Queue name")
|
|
25
|
+
schedules = []
|
|
26
|
+
revision = 0
|
|
27
|
+
file = path/join(root, "jobs.xml")
|
|
28
|
+
try
|
|
29
|
+
xml = fs/readFileSync(file, "utf8")
|
|
30
|
+
parsed = scheduleConfiguration(xml, targets)
|
|
31
|
+
schedules = parsed.items
|
|
32
|
+
revision = parsed.revision
|
|
33
|
+
catch error
|
|
34
|
+
if error.code != "ENOENT"
|
|
35
|
+
throw error
|
|
36
|
+
return {
|
|
37
|
+
application: appName
|
|
38
|
+
root: root
|
|
39
|
+
database: database
|
|
40
|
+
targets: targets
|
|
41
|
+
schedules: schedules
|
|
42
|
+
scheduleRevision: revision
|
|
43
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import "./envelope"
|
|
2
|
+
executionTask(config, queue, job)
|
|
3
|
+
import js global Promise
|
|
4
|
+
import js global AbortSignal
|
|
5
|
+
import "./claim_watch"
|
|
6
|
+
class ClaimWatch
|
|
7
|
+
|
|
8
|
+
export class Dispatcher(config, executor)
|
|
9
|
+
methods
|
|
10
|
+
run(queue, jobs, driver = null)
|
|
11
|
+
|
|
12
|
+
Dispatcher(config, executor)
|
|
13
|
+
@config = config
|
|
14
|
+
@executor = executor
|
|
15
|
+
|
|
16
|
+
Dispatcher:run(queue, jobs, driver = null)
|
|
17
|
+
context log
|
|
18
|
+
if !is_array(jobs) or jobs.length != 1
|
|
19
|
+
return Promise/reject("Dispatcher requires a single claimed job")
|
|
20
|
+
job = jobs[0]
|
|
21
|
+
task = null
|
|
22
|
+
try
|
|
23
|
+
task = executionTask(@config, queue, job)
|
|
24
|
+
catch error
|
|
25
|
+
log.err("[jobId=${job.id}] queue=${queue} Invalid job envelope: ${error}")
|
|
26
|
+
return Promise/resolve([outcome(job.id, "deadletter", {
|
|
27
|
+
version: 1
|
|
28
|
+
error: {
|
|
29
|
+
code: "InvalidJobEnvelope"
|
|
30
|
+
permanent: true
|
|
31
|
+
}
|
|
32
|
+
})])
|
|
33
|
+
log.debug("[jobId=${job.id}] queue=${queue} path=${task.path} dispatching attempt=${task.attempt}")
|
|
34
|
+
watch = new ClaimWatch(driver, queue, job)
|
|
35
|
+
watch.start()
|
|
36
|
+
signal = watch.signal?
|
|
37
|
+
if job.signal != null
|
|
38
|
+
signal = AbortSignal/any([signal, job.signal])
|
|
39
|
+
return chain @executor.execute(task, {
|
|
40
|
+
signal: signal
|
|
41
|
+
timeoutMs: job.expireInSeconds * 1000
|
|
42
|
+
})
|
|
43
|
+
then(value)
|
|
44
|
+
capture job
|
|
45
|
+
log.debug("[jobId=${job.id}] execution completed; settling claimed attempt")
|
|
46
|
+
return [outcome(job.id, "completed", {
|
|
47
|
+
version: 1
|
|
48
|
+
value: value
|
|
49
|
+
})]
|
|
50
|
+
catch(error)
|
|
51
|
+
capture job
|
|
52
|
+
permanent = error.permanent == true
|
|
53
|
+
state = "failed"
|
|
54
|
+
if permanent
|
|
55
|
+
state = "deadletter"
|
|
56
|
+
code = error.code
|
|
57
|
+
if !is_string(code) or code == ""
|
|
58
|
+
code = "ExecutionFailed"
|
|
59
|
+
return [outcome(job.id, state, {
|
|
60
|
+
version: 1
|
|
61
|
+
error: {
|
|
62
|
+
code: code
|
|
63
|
+
permanent: permanent
|
|
64
|
+
}
|
|
65
|
+
})]
|
|
66
|
+
finally
|
|
67
|
+
capture watch
|
|
68
|
+
return watch.close()
|
|
69
|
+
|
|
70
|
+
outcome(id, state, output)
|
|
71
|
+
return {
|
|
72
|
+
id: id
|
|
73
|
+
status: state
|
|
74
|
+
output: output
|
|
75
|
+
}
|
package/src/driver.lango
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import "./namespace"
|
|
2
|
+
schemaName(application)
|
|
3
|
+
import js "pg-boss" as pgBoss
|
|
4
|
+
import js global Math
|
|
5
|
+
import js global Promise
|
|
6
|
+
|
|
7
|
+
export createDriver(config, consumer = false, scheduling = false, cronMonitorIntervalSeconds = 30)
|
|
8
|
+
export prepareQueues(driver, targets)
|
|
9
|
+
export queueProvider(config)
|
|
10
|
+
|
|
11
|
+
createDriver(config, consumer = false, scheduling = false, cronMonitorIntervalSeconds = 30)
|
|
12
|
+
connections = 2
|
|
13
|
+
if consumer
|
|
14
|
+
connections = 4
|
|
15
|
+
driver = new pgBoss/PgBoss({
|
|
16
|
+
connectionString: config.database.connection
|
|
17
|
+
connectionTimeoutMillis: Math/min(2147483647, config.database.connectTimeoutSeconds * 1000)
|
|
18
|
+
statement_timeout: config.database.queryTimeoutMs
|
|
19
|
+
schema: schemaName(config.application)
|
|
20
|
+
max: connections
|
|
21
|
+
schedule: scheduling
|
|
22
|
+
cronMonitorIntervalSeconds: cronMonitorIntervalSeconds
|
|
23
|
+
supervise: consumer
|
|
24
|
+
registerInstance: consumer
|
|
25
|
+
application_name: "lango-boss"
|
|
26
|
+
})
|
|
27
|
+
driver.on("error", (error)
|
|
28
|
+
context log
|
|
29
|
+
log.err("Boss PostgreSQL error: ${error.message}")
|
|
30
|
+
)
|
|
31
|
+
driver.on("warning", (warning)
|
|
32
|
+
context log
|
|
33
|
+
log.warn("Boss PostgreSQL warning: ${warning.message}")
|
|
34
|
+
)
|
|
35
|
+
return driver
|
|
36
|
+
|
|
37
|
+
prepareQueues(driver, targets)
|
|
38
|
+
pending = Promise/resolve(null)
|
|
39
|
+
queues = []
|
|
40
|
+
for targets as target
|
|
41
|
+
if !queues.includes(target.queue)
|
|
42
|
+
queues.add(target.queue)
|
|
43
|
+
pending = prepareQueue(pending, driver, target.queue)
|
|
44
|
+
return pending
|
|
45
|
+
|
|
46
|
+
prepareQueue(pending, driver, name)
|
|
47
|
+
return chain pending
|
|
48
|
+
then(ignored)
|
|
49
|
+
capture driver
|
|
50
|
+
capture name
|
|
51
|
+
return driver.createQueue(name, {
|
|
52
|
+
policy: "standard"
|
|
53
|
+
})
|
|
54
|
+
then(ignored)
|
|
55
|
+
capture driver
|
|
56
|
+
capture name
|
|
57
|
+
return driver.getQueue(name)
|
|
58
|
+
then(queue)
|
|
59
|
+
if queue.policy != "standard"
|
|
60
|
+
throw "Lango Boss requires standard queues"
|
|
61
|
+
return null
|
|
62
|
+
|
|
63
|
+
queueProvider(config)
|
|
64
|
+
return {
|
|
65
|
+
module: "@lango-project/lango-boss/queue"
|
|
66
|
+
handler: "createQueue"
|
|
67
|
+
options: {
|
|
68
|
+
application: config.application
|
|
69
|
+
schema: schemaName(config.application)
|
|
70
|
+
targets: config.targets
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import js global Number
|
|
2
|
+
|
|
3
|
+
export executionTask(config, queue, job)
|
|
4
|
+
|
|
5
|
+
map(value, label)
|
|
6
|
+
if value == null or !is_object(value) or is_array(value)
|
|
7
|
+
throw "${label} must be a map"
|
|
8
|
+
return null
|
|
9
|
+
|
|
10
|
+
fields(value, allowed)
|
|
11
|
+
for value as key: item
|
|
12
|
+
if !allowed.includes(key)
|
|
13
|
+
throw "Unknown envelope field: ${key}"
|
|
14
|
+
return null
|
|
15
|
+
|
|
16
|
+
executionTask(config, queue, job)
|
|
17
|
+
payload = job.data
|
|
18
|
+
map(payload, "Job envelope")
|
|
19
|
+
fields(payload, ["version", "application", "path", "input", "source", "failedRetentionSeconds"])
|
|
20
|
+
if payload.version != 1 or payload.application != config.application
|
|
21
|
+
throw "Unsupported or foreign job envelope"
|
|
22
|
+
target = null
|
|
23
|
+
for config.targets as item
|
|
24
|
+
if item.path == payload.path and item.queue == queue
|
|
25
|
+
target = item
|
|
26
|
+
if target == null
|
|
27
|
+
throw "Job path is not registered for this queue"
|
|
28
|
+
map(payload.input, "Job input")
|
|
29
|
+
source = payload.source
|
|
30
|
+
map(source, "Job source")
|
|
31
|
+
fields(source, ["kind", "clientId", "userId", "grantId", "identity"])
|
|
32
|
+
for source as key: value
|
|
33
|
+
if !is_string(value) or value.trim() == ""
|
|
34
|
+
throw "Job source references must be nonempty strings"
|
|
35
|
+
switch source.kind
|
|
36
|
+
case "user"
|
|
37
|
+
if source.clientId == null or source.identity != null
|
|
38
|
+
throw "Invalid user job source"
|
|
39
|
+
case "system"
|
|
40
|
+
if source.identity == null or source.clientId != null or source.userId != null or source.grantId != null
|
|
41
|
+
throw "Invalid system job source"
|
|
42
|
+
else
|
|
43
|
+
throw "Unsupported job source"
|
|
44
|
+
if !Number/isSafeInteger(payload.failedRetentionSeconds) or payload.failedRetentionSeconds < 1 or payload.failedRetentionSeconds > 2147483647
|
|
45
|
+
throw "Invalid failed retention metadata"
|
|
46
|
+
if !Number/isSafeInteger(job.retryCount) or job.retryCount < 0
|
|
47
|
+
throw "Invalid job attempt metadata"
|
|
48
|
+
if !Number/isSafeInteger(job.expireInSeconds) or job.expireInSeconds < 1 or job.expireInSeconds > 1209600
|
|
49
|
+
throw "Invalid job expiration metadata"
|
|
50
|
+
return {
|
|
51
|
+
id: job.id
|
|
52
|
+
path: target.path
|
|
53
|
+
input: payload.input
|
|
54
|
+
source: source
|
|
55
|
+
attempt: job.retryCount + 1
|
|
56
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import "./configuration"
|
|
2
|
+
alias configuration = loadConfiguration(directory)
|
|
3
|
+
import "./boss"
|
|
4
|
+
alias BossClass = class Boss
|
|
5
|
+
import "./worker"
|
|
6
|
+
alias WorkerClass = class Worker
|
|
7
|
+
|
|
8
|
+
export loadConfiguration(directory)
|
|
9
|
+
export global Boss
|
|
10
|
+
export global Worker
|
|
11
|
+
export jobs(options = null)
|
|
12
|
+
export job(id)
|
|
13
|
+
export retry(id)
|
|
14
|
+
export cancel(id)
|
|
15
|
+
export queues(options = null)
|
|
16
|
+
export schedules(options = null)
|
|
17
|
+
export pauseQueue(name)
|
|
18
|
+
export resumeQueue(name)
|
|
19
|
+
export pauseSchedule(name)
|
|
20
|
+
export resumeSchedule(name)
|
|
21
|
+
|
|
22
|
+
global Boss = BossClass
|
|
23
|
+
global Worker = WorkerClass
|
|
24
|
+
|
|
25
|
+
loadConfiguration(directory)
|
|
26
|
+
return configuration(directory)
|
|
27
|
+
|
|
28
|
+
jobs(options = null)
|
|
29
|
+
context queue
|
|
30
|
+
return queue.jobs(options)
|
|
31
|
+
|
|
32
|
+
job(id)
|
|
33
|
+
context queue
|
|
34
|
+
return queue.job(id)
|
|
35
|
+
|
|
36
|
+
retry(id)
|
|
37
|
+
context queue
|
|
38
|
+
return queue.manage("retry", id)
|
|
39
|
+
|
|
40
|
+
cancel(id)
|
|
41
|
+
context queue
|
|
42
|
+
return queue.manage("cancel", id)
|
|
43
|
+
|
|
44
|
+
queues(options = null)
|
|
45
|
+
context queue
|
|
46
|
+
return queue.manage("queues", null, options)
|
|
47
|
+
|
|
48
|
+
schedules(options = null)
|
|
49
|
+
context queue
|
|
50
|
+
return queue.manage("schedules", null, options)
|
|
51
|
+
|
|
52
|
+
pauseQueue(name)
|
|
53
|
+
context queue
|
|
54
|
+
return queue.manage("pauseQueue", name)
|
|
55
|
+
|
|
56
|
+
resumeQueue(name)
|
|
57
|
+
context queue
|
|
58
|
+
return queue.manage("resumeQueue", name)
|
|
59
|
+
|
|
60
|
+
pauseSchedule(name)
|
|
61
|
+
context queue
|
|
62
|
+
return queue.manage("pauseSchedule", name)
|
|
63
|
+
|
|
64
|
+
resumeSchedule(name)
|
|
65
|
+
context queue
|
|
66
|
+
return queue.manage("resumeSchedule", name)
|