@lango-project/lango-boss 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +111 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +725 -0
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/boss.mjs +136 -0
- package/dist/boss.mjs.map +1 -0
- package/dist/claim_watch.mjs +99 -0
- package/dist/claim_watch.mjs.map +1 -0
- package/dist/cli.mjs +143 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/configuration.mjs +58 -0
- package/dist/configuration.mjs.map +1 -0
- package/dist/dispatcher.mjs +108 -0
- package/dist/dispatcher.mjs.map +1 -0
- package/dist/driver.mjs +98 -0
- package/dist/driver.mjs.map +1 -0
- package/dist/envelope.mjs +92 -0
- package/dist/envelope.mjs.map +1 -0
- package/dist/library.mjs +139 -0
- package/dist/library.mjs.map +1 -0
- package/dist/management.mjs +360 -0
- package/dist/management.mjs.map +1 -0
- package/dist/management_transaction.mjs +26 -0
- package/dist/management_transaction.mjs.map +1 -0
- package/dist/namespace.mjs +27 -0
- package/dist/namespace.mjs.map +1 -0
- package/dist/queue.mjs +161 -0
- package/dist/queue.mjs.map +1 -0
- package/dist/reads.mjs +359 -0
- package/dist/reads.mjs.map +1 -0
- package/dist/retention.mjs +133 -0
- package/dist/retention.mjs.map +1 -0
- package/dist/schedule_sync.mjs +222 -0
- package/dist/schedule_sync.mjs.map +1 -0
- package/dist/schedule_transaction.mjs +24 -0
- package/dist/schedule_transaction.mjs.map +1 -0
- package/dist/schedules.mjs +201 -0
- package/dist/schedules.mjs.map +1 -0
- package/dist/transaction.mjs +16 -0
- package/dist/transaction.mjs.map +1 -0
- package/dist/validation.mjs +90 -0
- package/dist/validation.mjs.map +1 -0
- package/dist/worker.mjs +316 -0
- package/dist/worker.mjs.map +1 -0
- package/docs/guide.md +88 -0
- package/docs/management.md +93 -0
- package/docs/producer.md +115 -0
- package/docs/reads.md +107 -0
- package/docs/reference.md +77 -0
- package/docs/release.md +103 -0
- package/docs/retention.md +47 -0
- package/docs/scheduling.md +79 -0
- package/docs/worker.md +90 -0
- package/examples/reports/README.md +118 -0
- package/examples/reports/app.lango +133 -0
- package/examples/reports/deploy.xml +6 -0
- package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
- package/examples/reports/dist/.lango/localization.mjs +108 -0
- package/examples/reports/dist/.lango/runtime.mjs +725 -0
- package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
- package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
- package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
- package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
- package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
- package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
- package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
- package/examples/reports/dist/.lango/xmldom/index.js +41 -0
- package/examples/reports/dist/.lango/xmldom/package.json +1 -0
- package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
- package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
- package/examples/reports/dist/app.mjs +137 -0
- package/examples/reports/dist/app.mjs.map +1 -0
- package/examples/reports/dist/handlers.mjs +202 -0
- package/examples/reports/dist/handlers.mjs.map +1 -0
- package/examples/reports/dist/setup.mjs +14 -0
- package/examples/reports/dist/setup.mjs.map +1 -0
- package/examples/reports/handlers.lango +153 -0
- package/examples/reports/jobs.xml +5 -0
- package/examples/reports/package.json +23 -0
- package/examples/reports/routes.xml +4 -0
- package/examples/reports/setup.lango +17 -0
- package/package.json +60 -4
- package/src/boss.lango +92 -0
- package/src/claim_watch.lango +58 -0
- package/src/cli.lango +103 -0
- package/src/configuration.lango +43 -0
- package/src/dispatcher.lango +75 -0
- package/src/driver.lango +72 -0
- package/src/envelope.lango +56 -0
- package/src/library.lango +66 -0
- package/src/management.lango +302 -0
- package/src/management_transaction.lango +28 -0
- package/src/namespace.lango +15 -0
- package/src/queue.lango +115 -0
- package/src/reads.lango +277 -0
- package/src/retention.lango +107 -0
- package/src/schedule_sync.lango +190 -0
- package/src/schedule_transaction.lango +22 -0
- package/src/schedules.lango +165 -0
- package/src/transaction.lango +23 -0
- package/src/validation.lango +45 -0
- package/src/worker.lango +230 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import "./validation"
|
|
2
|
+
attributes(tag, allowed)
|
|
3
|
+
empty(tag)
|
|
4
|
+
integer(value, fallback, minimum, maximum, name)
|
|
5
|
+
boolean(value, fallback, name)
|
|
6
|
+
name(value, label)
|
|
7
|
+
import js "cron-parser" as cron
|
|
8
|
+
import js global Intl
|
|
9
|
+
import js global Object
|
|
10
|
+
|
|
11
|
+
export parseSchedules(xml, targets)
|
|
12
|
+
export scheduleConfiguration(xml, targets)
|
|
13
|
+
|
|
14
|
+
defaults()
|
|
15
|
+
return {
|
|
16
|
+
retryLimit: 3
|
|
17
|
+
retryDelay: 10
|
|
18
|
+
retryBackoff: true
|
|
19
|
+
retryDelayMax: 300
|
|
20
|
+
expireInSeconds: 900
|
|
21
|
+
failedRetentionSeconds: 604800
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
retryFields()
|
|
25
|
+
return ["retryLimit", "retryDelay", "retryBackoff", "retryDelayMax", "expireInSeconds", "failedRetentionSeconds"]
|
|
26
|
+
|
|
27
|
+
retryOptions(spec, base)
|
|
28
|
+
result = {
|
|
29
|
+
retryLimit: integer(spec.retryLimit, base.retryLimit, 0, 2147483647, "retryLimit")
|
|
30
|
+
retryDelay: integer(spec.retryDelay, base.retryDelay, 1, 2147483647, "retryDelay")
|
|
31
|
+
retryBackoff: boolean(spec.retryBackoff, base.retryBackoff, "retryBackoff")
|
|
32
|
+
retryDelayMax: integer(spec.retryDelayMax, base.retryDelayMax, 1, 2147483647, "retryDelayMax")
|
|
33
|
+
expireInSeconds: integer(spec.expireInSeconds, base.expireInSeconds, 1, 1209600, "expireInSeconds")
|
|
34
|
+
failedRetentionSeconds: integer(spec.failedRetentionSeconds, base.failedRetentionSeconds, 1, 2147483647, "failedRetentionSeconds")
|
|
35
|
+
}
|
|
36
|
+
if result.retryDelay > result.retryDelayMax
|
|
37
|
+
throw "retryDelay cannot exceed retryDelayMax"
|
|
38
|
+
return result
|
|
39
|
+
|
|
40
|
+
timeZone(value)
|
|
41
|
+
if !is_string(value) or value.trim() != value or value == ""
|
|
42
|
+
throw "A valid time zone is required"
|
|
43
|
+
try
|
|
44
|
+
formatter = new Intl/DateTimeFormat("en", {
|
|
45
|
+
timeZone: value
|
|
46
|
+
})
|
|
47
|
+
catch error
|
|
48
|
+
throw "Invalid time zone: ${value}"
|
|
49
|
+
return value
|
|
50
|
+
|
|
51
|
+
data(tag)
|
|
52
|
+
children = tag.children()
|
|
53
|
+
if children.length == 0
|
|
54
|
+
return {}
|
|
55
|
+
if children.length != 1 or children[0].tag() != "data"
|
|
56
|
+
throw "Job may contain only one data element"
|
|
57
|
+
child = children[0]
|
|
58
|
+
attributes(child, [])
|
|
59
|
+
if child.children().length != 0
|
|
60
|
+
throw "Job data must contain JSON text"
|
|
61
|
+
value = null
|
|
62
|
+
try
|
|
63
|
+
value = from_json(child.innerText())
|
|
64
|
+
catch error
|
|
65
|
+
throw "Job data must be valid JSON"
|
|
66
|
+
if value == null or !is_object(value) or is_array(value)
|
|
67
|
+
throw "Job data must be a JSON map"
|
|
68
|
+
return value
|
|
69
|
+
|
|
70
|
+
targetFor(path, targets)
|
|
71
|
+
for targets as target
|
|
72
|
+
if target.path == path
|
|
73
|
+
return target
|
|
74
|
+
throw "Unknown job path: ${path}"
|
|
75
|
+
|
|
76
|
+
parseSchedules(xml, targets)
|
|
77
|
+
parsed = scheduleConfiguration(xml, targets)
|
|
78
|
+
return parsed.items
|
|
79
|
+
|
|
80
|
+
scheduleConfiguration(xml, targets)
|
|
81
|
+
root = xml2obj(xml)
|
|
82
|
+
if root.tag() != "jobs"
|
|
83
|
+
throw "Expected jobs root"
|
|
84
|
+
attributes(root, ["timeZone", "identity", "revision"])
|
|
85
|
+
if root.revision == null
|
|
86
|
+
throw "Jobs revision is required"
|
|
87
|
+
revision = integer(root.revision, null, 1, 2147483647, "revision")
|
|
88
|
+
if root.innerText().trim() != ""
|
|
89
|
+
throw "Jobs XML cannot contain text outside data elements"
|
|
90
|
+
zone = root.timeZone
|
|
91
|
+
if zone == null
|
|
92
|
+
zone = "UTC"
|
|
93
|
+
timeZone(zone)
|
|
94
|
+
profile = name(root.identity, "Jobs identity")
|
|
95
|
+
base = defaults()
|
|
96
|
+
seenDefaults = false
|
|
97
|
+
for root.children() as tag
|
|
98
|
+
if tag.tag() == "defaults"
|
|
99
|
+
if seenDefaults
|
|
100
|
+
throw "Duplicate jobs defaults"
|
|
101
|
+
seenDefaults = true
|
|
102
|
+
attributes(tag, retryFields())
|
|
103
|
+
empty(tag)
|
|
104
|
+
base = retryOptions(tag.attributes(), base)
|
|
105
|
+
else
|
|
106
|
+
if tag.tag() != "job"
|
|
107
|
+
throw "Unknown jobs element: ${tag.tag()}"
|
|
108
|
+
seen = Object/create(null)
|
|
109
|
+
items = []
|
|
110
|
+
for root.children() as tag
|
|
111
|
+
if tag.tag() == "defaults"
|
|
112
|
+
continue
|
|
113
|
+
allowed = ["name", "path", "cron", "timeZone", "missed", "precision", "enabled"]
|
|
114
|
+
for retryFields() as field
|
|
115
|
+
allowed.add(field)
|
|
116
|
+
attributes(tag, allowed)
|
|
117
|
+
if tag.innerText().trim() != ""
|
|
118
|
+
throw "Job XML cannot contain text outside data"
|
|
119
|
+
spec = tag.attributes()
|
|
120
|
+
id = name(spec.name, "Job name")
|
|
121
|
+
if Object/hasOwn(seen, id)
|
|
122
|
+
throw "Duplicate job name: ${id}"
|
|
123
|
+
seen[id] = true
|
|
124
|
+
target = targetFor(spec.path, targets)
|
|
125
|
+
selectedZone = zone
|
|
126
|
+
if spec.timeZone != null
|
|
127
|
+
selectedZone = timeZone(spec.timeZone)
|
|
128
|
+
if !is_string(spec.cron) or spec.cron.trim() == ""
|
|
129
|
+
throw "Job cron expression is required: ${id}"
|
|
130
|
+
try
|
|
131
|
+
cron/CronExpressionParser/parse(spec.cron, {
|
|
132
|
+
tz: selectedZone
|
|
133
|
+
})
|
|
134
|
+
catch error
|
|
135
|
+
throw "Invalid job cron expression: ${id}"
|
|
136
|
+
missed = spec.missed
|
|
137
|
+
if missed == null
|
|
138
|
+
missed = "skip"
|
|
139
|
+
missedValues = ["skip", "once"]
|
|
140
|
+
if !missedValues.includes(missed)
|
|
141
|
+
throw "Job missed must be skip or once"
|
|
142
|
+
precision = spec.precision
|
|
143
|
+
if precision == null
|
|
144
|
+
precision = "minute"
|
|
145
|
+
precisions = ["minute", "second"]
|
|
146
|
+
if !precisions.includes(precision)
|
|
147
|
+
throw "Job precision must be minute or second"
|
|
148
|
+
retry = retryOptions(spec, base)
|
|
149
|
+
items.add({
|
|
150
|
+
name: id
|
|
151
|
+
path: target.path
|
|
152
|
+
queue: target.queue
|
|
153
|
+
identity: profile
|
|
154
|
+
cron: spec.cron
|
|
155
|
+
timeZone: selectedZone
|
|
156
|
+
missed: missed
|
|
157
|
+
precision: precision
|
|
158
|
+
enabled: boolean(spec.enabled, true, "enabled")
|
|
159
|
+
data: data(tag)
|
|
160
|
+
retry: retry
|
|
161
|
+
})
|
|
162
|
+
return {
|
|
163
|
+
revision: revision
|
|
164
|
+
items: items
|
|
165
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import js "pg-boss" as pgBoss
|
|
2
|
+
|
|
3
|
+
export send(db, input)
|
|
4
|
+
|
|
5
|
+
send(db, input)
|
|
6
|
+
boss = new pgBoss/PgBoss({
|
|
7
|
+
db: db
|
|
8
|
+
schema: input.schema
|
|
9
|
+
migrate: false
|
|
10
|
+
supervise: false
|
|
11
|
+
schedule: false
|
|
12
|
+
registerInstance: false
|
|
13
|
+
})
|
|
14
|
+
return boss.send(input.queue, input.payload, {
|
|
15
|
+
db: db
|
|
16
|
+
retryLimit: 3
|
|
17
|
+
retryDelay: 10
|
|
18
|
+
retryBackoff: true
|
|
19
|
+
retryDelayMax: 300
|
|
20
|
+
expireInSeconds: 900
|
|
21
|
+
heartbeatSeconds: 30
|
|
22
|
+
deleteAfterSeconds: 0
|
|
23
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import js global Number
|
|
2
|
+
import js global RegExp
|
|
3
|
+
|
|
4
|
+
export attributes(tag, allowed)
|
|
5
|
+
export empty(tag)
|
|
6
|
+
export integer(value, fallback, minimum, maximum, name)
|
|
7
|
+
export boolean(value, fallback, name)
|
|
8
|
+
export name(value, label)
|
|
9
|
+
|
|
10
|
+
attributes(tag, allowed)
|
|
11
|
+
for tag.attributes() as key: value
|
|
12
|
+
if !allowed.includes(key)
|
|
13
|
+
throw "Unknown ${tag.tag()} attribute: ${key}"
|
|
14
|
+
return null
|
|
15
|
+
|
|
16
|
+
empty(tag)
|
|
17
|
+
if tag.children().length != 0 or tag.innerText().trim() != ""
|
|
18
|
+
throw "Expected an empty ${tag.tag()} element"
|
|
19
|
+
return null
|
|
20
|
+
|
|
21
|
+
integer(value, fallback, minimum, maximum, name)
|
|
22
|
+
if value == null
|
|
23
|
+
return fallback
|
|
24
|
+
pattern = new RegExp("^[0-9]+$")
|
|
25
|
+
if !is_string(value) or !pattern.test(value)
|
|
26
|
+
throw "${name} must be an integer"
|
|
27
|
+
result = Number(value)
|
|
28
|
+
if !Number/isSafeInteger(result) or result < minimum or result > maximum
|
|
29
|
+
throw "${name} must be between ${minimum} and ${maximum}"
|
|
30
|
+
return result
|
|
31
|
+
|
|
32
|
+
boolean(value, fallback, name)
|
|
33
|
+
if value == null
|
|
34
|
+
return fallback
|
|
35
|
+
if value != "true" and value != "false"
|
|
36
|
+
throw "${name} must be true or false"
|
|
37
|
+
return value == "true"
|
|
38
|
+
|
|
39
|
+
name(value, label)
|
|
40
|
+
if !is_string(value) or value.trim() != value or value == "" or value.length > 200
|
|
41
|
+
throw "${label} must be a nonempty name of at most 200 characters"
|
|
42
|
+
pattern = new RegExp("[\\x00-\\x1f\\x7f]")
|
|
43
|
+
if pattern.test(value)
|
|
44
|
+
throw "${label} cannot contain control characters"
|
|
45
|
+
return value
|
package/src/worker.lango
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
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 "./dispatcher"
|
|
8
|
+
class Dispatcher
|
|
9
|
+
import "./schedule_sync"
|
|
10
|
+
synchronizeSchedules(config)
|
|
11
|
+
import "./retention"
|
|
12
|
+
class Retention
|
|
13
|
+
import "./management"
|
|
14
|
+
prepareManagement(config)
|
|
15
|
+
import js "node:crypto" as crypto
|
|
16
|
+
import js "@lango-project/lango-server" as server
|
|
17
|
+
import js global Number
|
|
18
|
+
import js global Promise
|
|
19
|
+
import js global Object
|
|
20
|
+
|
|
21
|
+
export class Worker(directory, settings)
|
|
22
|
+
methods
|
|
23
|
+
start()
|
|
24
|
+
close()
|
|
25
|
+
|
|
26
|
+
Worker(directory, settings)
|
|
27
|
+
@config = loadConfiguration(directory)
|
|
28
|
+
@settings = workerSettings(settings)
|
|
29
|
+
@queues = []
|
|
30
|
+
for @config.targets as target
|
|
31
|
+
if !@queues.includes(target.queue)
|
|
32
|
+
@queues.add(target.queue)
|
|
33
|
+
@executor = new server/Executor(@config.root, {
|
|
34
|
+
identity: @settings.identity
|
|
35
|
+
workers: @settings.workers
|
|
36
|
+
maxQueue: @queues.length * @settings.workers
|
|
37
|
+
startupTimeoutMs: @settings.startupTimeoutMs
|
|
38
|
+
shutdownTimeoutMs: @settings.shutdownTimeoutMs
|
|
39
|
+
queueProvider: queueProvider(@config)
|
|
40
|
+
})
|
|
41
|
+
@dispatcher = new Dispatcher(@config, @executor)
|
|
42
|
+
@state = "new"
|
|
43
|
+
@driver = null
|
|
44
|
+
@starting = null
|
|
45
|
+
@closing = null
|
|
46
|
+
@releasing = null
|
|
47
|
+
@closeError = null
|
|
48
|
+
@retention = new Retention(@config, @settings.cleanupIntervalSeconds, @settings.cleanupBatchSize)
|
|
49
|
+
|
|
50
|
+
workerSettings(settings)
|
|
51
|
+
if settings == null or !is_object(settings) or is_array(settings)
|
|
52
|
+
throw "Worker settings must include an identity provider"
|
|
53
|
+
allowed = ["identity", "workers", "pollingIntervalSeconds", "heartbeatRefreshSeconds", "startupTimeoutMs", "shutdownTimeoutMs", "cronMonitorIntervalSeconds", "cleanupIntervalSeconds", "cleanupBatchSize"]
|
|
54
|
+
for settings as key: value
|
|
55
|
+
if !allowed.includes(key)
|
|
56
|
+
throw "Unknown worker option: ${key}"
|
|
57
|
+
return {
|
|
58
|
+
identity: settings.identity
|
|
59
|
+
workers: integer(settings.workers, 2, 1, 128, "workers")
|
|
60
|
+
pollingIntervalSeconds: interval(settings.pollingIntervalSeconds, 2, 0.5, 3600, "pollingIntervalSeconds")
|
|
61
|
+
heartbeatRefreshSeconds: interval(settings.heartbeatRefreshSeconds, null, 0.5, 29, "heartbeatRefreshSeconds")
|
|
62
|
+
cronMonitorIntervalSeconds: integer(settings.cronMonitorIntervalSeconds, 30, 1, 45, "cronMonitorIntervalSeconds")
|
|
63
|
+
cleanupIntervalSeconds: integer(settings.cleanupIntervalSeconds, 60, 1, 86400, "cleanupIntervalSeconds")
|
|
64
|
+
cleanupBatchSize: integer(settings.cleanupBatchSize, 1000, 1, 10000, "cleanupBatchSize")
|
|
65
|
+
startupTimeoutMs: integer(settings.startupTimeoutMs, 30000, 1, 2147483647, "startupTimeoutMs")
|
|
66
|
+
shutdownTimeoutMs: integer(settings.shutdownTimeoutMs, 30000, 1, 2147483647, "shutdownTimeoutMs")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
integer(value, fallback, minimum, maximum, name)
|
|
70
|
+
if value == null
|
|
71
|
+
return fallback
|
|
72
|
+
if !Number/isSafeInteger(value) or value < minimum or value > maximum
|
|
73
|
+
throw "Invalid worker option: ${name}"
|
|
74
|
+
return value
|
|
75
|
+
|
|
76
|
+
interval(value, fallback, minimum, maximum, name)
|
|
77
|
+
if value == null
|
|
78
|
+
return fallback
|
|
79
|
+
if !is_number(value) or !Number/isFinite(value) or value < minimum or value > maximum
|
|
80
|
+
throw "Invalid worker option: ${name}"
|
|
81
|
+
return value
|
|
82
|
+
|
|
83
|
+
Worker:start()
|
|
84
|
+
if @state != "new"
|
|
85
|
+
return Promise/reject("Worker can only be started once")
|
|
86
|
+
@state = "starting"
|
|
87
|
+
@driver = createDriver(@config)
|
|
88
|
+
@starting = launch()
|
|
89
|
+
return @starting
|
|
90
|
+
|
|
91
|
+
Worker:launch()
|
|
92
|
+
return chain @driver.start()
|
|
93
|
+
then(ignored)
|
|
94
|
+
return prepareQueues(@driver, @config.targets)
|
|
95
|
+
then(ignored)
|
|
96
|
+
return prepareManagement(@config)
|
|
97
|
+
then(ignored)
|
|
98
|
+
return @executor.start()
|
|
99
|
+
then(ignored)
|
|
100
|
+
return validateSchedules()
|
|
101
|
+
then(ignored)
|
|
102
|
+
return synchronizeSchedules(@config)
|
|
103
|
+
then(ignored)
|
|
104
|
+
return @retention.start()
|
|
105
|
+
then(ignored)
|
|
106
|
+
return activateConsumer()
|
|
107
|
+
then(ignored)
|
|
108
|
+
pending = Promise/resolve(null)
|
|
109
|
+
for @queues as queue
|
|
110
|
+
pending = subscribe(pending, queue)
|
|
111
|
+
return pending
|
|
112
|
+
then(ignored)
|
|
113
|
+
if @state != "starting"
|
|
114
|
+
throw "Worker closed during startup"
|
|
115
|
+
@state = "running"
|
|
116
|
+
return null
|
|
117
|
+
catch(error)
|
|
118
|
+
@state = "closing"
|
|
119
|
+
return chain release()
|
|
120
|
+
finally
|
|
121
|
+
capture error
|
|
122
|
+
throw error
|
|
123
|
+
|
|
124
|
+
Worker:validateSchedules()
|
|
125
|
+
pending = Promise/resolve(null)
|
|
126
|
+
checked = Object/create(null)
|
|
127
|
+
for @config.schedules as item
|
|
128
|
+
key = to_json([item.identity, item.path])
|
|
129
|
+
if !Object/hasOwn(checked, key)
|
|
130
|
+
checked[key] = true
|
|
131
|
+
pending = validateSchedule(pending, item)
|
|
132
|
+
return pending
|
|
133
|
+
|
|
134
|
+
Worker:validateSchedule(pending, item)
|
|
135
|
+
return chain pending
|
|
136
|
+
then(ignored)
|
|
137
|
+
capture item
|
|
138
|
+
capture crypto
|
|
139
|
+
return @executor.authorize({
|
|
140
|
+
id: "schedule-check:${crypto/randomUUID()}"
|
|
141
|
+
path: item.path
|
|
142
|
+
input: {}
|
|
143
|
+
source: {
|
|
144
|
+
kind: "system"
|
|
145
|
+
identity: item.identity
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
Worker:activateConsumer()
|
|
150
|
+
return chain @driver.stop()
|
|
151
|
+
then(ignored)
|
|
152
|
+
if @state != "starting"
|
|
153
|
+
throw "Worker closed before consumer activation"
|
|
154
|
+
@driver = createDriver(@config, true, @config.scheduleRevision > 0, @settings.cronMonitorIntervalSeconds)
|
|
155
|
+
return @driver.start()
|
|
156
|
+
|
|
157
|
+
Worker:subscribe(pending, queue)
|
|
158
|
+
return chain pending
|
|
159
|
+
then(ignored)
|
|
160
|
+
capture queue
|
|
161
|
+
if @state != "starting"
|
|
162
|
+
throw "Worker closed during startup"
|
|
163
|
+
handler = (jobs)
|
|
164
|
+
capture queue
|
|
165
|
+
return @dispatcher.run(queue, jobs, @driver)
|
|
166
|
+
return @driver.work(queue, {
|
|
167
|
+
includeMetadata: true
|
|
168
|
+
perJobResults: true
|
|
169
|
+
batchSize: 1
|
|
170
|
+
localConcurrency: @settings.workers
|
|
171
|
+
pollingIntervalSeconds: @settings.pollingIntervalSeconds
|
|
172
|
+
heartbeatRefreshSeconds: @settings.heartbeatRefreshSeconds
|
|
173
|
+
}, handler)
|
|
174
|
+
|
|
175
|
+
Worker:close()
|
|
176
|
+
if @closing != null
|
|
177
|
+
return @closing
|
|
178
|
+
if @state == "new" or @state == "closed"
|
|
179
|
+
@state = "closed"
|
|
180
|
+
return Promise/resolve(null)
|
|
181
|
+
@state = "closing"
|
|
182
|
+
pending = Promise/resolve(null)
|
|
183
|
+
if @starting != null
|
|
184
|
+
pending = @starting
|
|
185
|
+
@closing = closeAfter(pending)
|
|
186
|
+
return @closing
|
|
187
|
+
|
|
188
|
+
Worker:closeAfter(pending)
|
|
189
|
+
return chain pending
|
|
190
|
+
catch(error)
|
|
191
|
+
return null
|
|
192
|
+
then(ignored)
|
|
193
|
+
return release()
|
|
194
|
+
|
|
195
|
+
Worker:release()
|
|
196
|
+
if @releasing == null
|
|
197
|
+
@releasing = shutdown()
|
|
198
|
+
return @releasing
|
|
199
|
+
|
|
200
|
+
Worker:shutdown()
|
|
201
|
+
stops = []
|
|
202
|
+
if @driver != null
|
|
203
|
+
for @queues as queue
|
|
204
|
+
stops.add(@driver.offWork(queue, {
|
|
205
|
+
wait: false
|
|
206
|
+
}))
|
|
207
|
+
return chain Promise/all(stops)
|
|
208
|
+
finally
|
|
209
|
+
return @retention.close()
|
|
210
|
+
catch(error)
|
|
211
|
+
@closeError = error
|
|
212
|
+
return null
|
|
213
|
+
then(ignored)
|
|
214
|
+
return @executor.close()
|
|
215
|
+
catch(error)
|
|
216
|
+
@closeError = error
|
|
217
|
+
return null
|
|
218
|
+
then(ignored)
|
|
219
|
+
if @driver != null
|
|
220
|
+
return @driver.stop({
|
|
221
|
+
graceful: true
|
|
222
|
+
timeout: @settings.shutdownTimeoutMs
|
|
223
|
+
})
|
|
224
|
+
return null
|
|
225
|
+
finally
|
|
226
|
+
@state = "closed"
|
|
227
|
+
then(ignored)
|
|
228
|
+
if @closeError != null
|
|
229
|
+
throw @closeError
|
|
230
|
+
return null
|