@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
package/src/reads.lango
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import js global Date
|
|
2
|
+
import js global Number
|
|
3
|
+
import js global Object
|
|
4
|
+
import js global RegExp
|
|
5
|
+
import js global Buffer
|
|
6
|
+
import "./namespace"
|
|
7
|
+
schemaName(application)
|
|
8
|
+
|
|
9
|
+
export readJob(settings, db, token, id, management = false)
|
|
10
|
+
export readJobs(settings, db, token, options)
|
|
11
|
+
export usable(db)
|
|
12
|
+
export manager(token)
|
|
13
|
+
export uuid(value)
|
|
14
|
+
|
|
15
|
+
usable(db)
|
|
16
|
+
state = db.transactionState()
|
|
17
|
+
if state != null and state != "active"
|
|
18
|
+
throw "Job reads require a usable transaction"
|
|
19
|
+
return null
|
|
20
|
+
|
|
21
|
+
manager(token)
|
|
22
|
+
if !token.hasPolicy("boss.manage")
|
|
23
|
+
throw "Boss management policy required: boss.manage"
|
|
24
|
+
return null
|
|
25
|
+
|
|
26
|
+
uuid(value)
|
|
27
|
+
pattern = new RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
|
28
|
+
return is_string(value) and pattern.test(value)
|
|
29
|
+
|
|
30
|
+
owned(source, token)
|
|
31
|
+
if source == null or !is_object(source) or is_array(source) or source.kind != "user" or source.identity != null
|
|
32
|
+
return false
|
|
33
|
+
if !is_string(source.clientId) or source.clientId == ""
|
|
34
|
+
return false
|
|
35
|
+
if source.clientId != token.clientId?
|
|
36
|
+
return false
|
|
37
|
+
for ["userId", "grantId"] as key
|
|
38
|
+
expected = source[key]
|
|
39
|
+
if expected != null and (!is_string(expected) or expected == "")
|
|
40
|
+
return false
|
|
41
|
+
if expected != token.get(key)
|
|
42
|
+
return false
|
|
43
|
+
return true
|
|
44
|
+
|
|
45
|
+
permitted(settings, path, queue, token)
|
|
46
|
+
for settings.targets as target
|
|
47
|
+
if target.path == path and target.queue == queue
|
|
48
|
+
if target.policy != "anonymous" and !token.hasPolicy(target.policy)
|
|
49
|
+
return false
|
|
50
|
+
if target.reauthSeconds > 0
|
|
51
|
+
stamp = token.get("authenticatedAt")
|
|
52
|
+
now = Date/now()
|
|
53
|
+
if !Number/isSafeInteger(stamp) or stamp < 0 or stamp > now or now - stamp >= target.reauthSeconds * 1000
|
|
54
|
+
return false
|
|
55
|
+
return true
|
|
56
|
+
return false
|
|
57
|
+
|
|
58
|
+
invoke(settings, scope, action, input)
|
|
59
|
+
context db = scope
|
|
60
|
+
schema = schemaName(settings.application)
|
|
61
|
+
if action == "job"
|
|
62
|
+
single = SQL
|
|
63
|
+
select id::text, name as queue, data->>'path' as path, state::text,
|
|
64
|
+
retry_count as "retryCount", retry_limit as "retryLimit",
|
|
65
|
+
to_char(created_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "createdAt",
|
|
66
|
+
to_char(start_after at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "startAfter",
|
|
67
|
+
to_char(started_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "startedAt",
|
|
68
|
+
to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "completedAt",
|
|
69
|
+
data->'input' as input, data->'source' as source, output
|
|
70
|
+
from ${schema}.job
|
|
71
|
+
where id = :id::uuid and data->>'application' = :application
|
|
72
|
+
and data->'version' = '1'::jsonb
|
|
73
|
+
limit 2
|
|
74
|
+
single.id = input.id
|
|
75
|
+
single.application = settings.application
|
|
76
|
+
single.execute()
|
|
77
|
+
if !single.next()
|
|
78
|
+
return null
|
|
79
|
+
result = summary(single)
|
|
80
|
+
result.input = single.input
|
|
81
|
+
result.source = single.source
|
|
82
|
+
result.output = single.output
|
|
83
|
+
if single.next()
|
|
84
|
+
return null
|
|
85
|
+
return result
|
|
86
|
+
listing = SQL
|
|
87
|
+
select id::text, name as queue, data->>'path' as path, state::text,
|
|
88
|
+
retry_count as "retryCount", retry_limit as "retryLimit",
|
|
89
|
+
to_char(created_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "createdAt",
|
|
90
|
+
to_char(start_after at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "startAfter",
|
|
91
|
+
to_char(started_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "startedAt",
|
|
92
|
+
to_char(completed_on at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as "completedAt"
|
|
93
|
+
from ${schema}.job
|
|
94
|
+
where data->>'application' = :application and data->'version' = '1'::jsonb
|
|
95
|
+
and (:queue::text is null or name = :queue)
|
|
96
|
+
and (:path::text is null or data->>'path' = :path)
|
|
97
|
+
and (:state::text is null or state::text = :state)
|
|
98
|
+
and (:cursorTime::timestamptz is null or (created_on, id, name) < (:cursorTime::timestamptz, :cursorId::uuid, :cursorQueue::text))
|
|
99
|
+
order by created_on desc, id desc, name desc
|
|
100
|
+
limit :limit
|
|
101
|
+
listing.application = settings.application
|
|
102
|
+
listing.queue = input.queue
|
|
103
|
+
listing.path = input.path
|
|
104
|
+
listing.state = input.state
|
|
105
|
+
listing.limit = input.limit
|
|
106
|
+
listing.cursorTime = null
|
|
107
|
+
listing.cursorId = null
|
|
108
|
+
listing.cursorQueue = null
|
|
109
|
+
if input.position != null
|
|
110
|
+
listing.cursorTime = input.position.createdAt
|
|
111
|
+
listing.cursorId = input.position.id
|
|
112
|
+
listing.cursorQueue = input.position.name
|
|
113
|
+
listing.execute()
|
|
114
|
+
rows = []
|
|
115
|
+
loop while listing.next()
|
|
116
|
+
rows.add(summary(listing))
|
|
117
|
+
return rows
|
|
118
|
+
|
|
119
|
+
readJob(settings, db, token, id, management = false)
|
|
120
|
+
usable(db)
|
|
121
|
+
if management
|
|
122
|
+
manager(token)
|
|
123
|
+
if !uuid(id)
|
|
124
|
+
return null
|
|
125
|
+
row = invoke(settings, db, "job", {
|
|
126
|
+
id: id
|
|
127
|
+
})
|
|
128
|
+
if row == null
|
|
129
|
+
return null
|
|
130
|
+
if !management and (!owned(row.source, token) or !permitted(settings, row.path, row.queue, token))
|
|
131
|
+
return null
|
|
132
|
+
result = summary(row)
|
|
133
|
+
result.resultAvailable = false
|
|
134
|
+
result.result = null
|
|
135
|
+
result.error = null
|
|
136
|
+
output = row.output
|
|
137
|
+
if output != null and is_object(output) and !is_array(output) and output.version == 1
|
|
138
|
+
if row.state == "completed" and Object/hasOwn(output, "value")
|
|
139
|
+
result.resultAvailable = true
|
|
140
|
+
result.result = output.value
|
|
141
|
+
if row.state == "failed" and output.error != null and is_object(output.error)
|
|
142
|
+
if is_string(output.error.code) and is_bool(output.error.permanent)
|
|
143
|
+
result.error = {
|
|
144
|
+
code: output.error.code
|
|
145
|
+
permanent: output.error.permanent
|
|
146
|
+
}
|
|
147
|
+
if management
|
|
148
|
+
result.input = row.input
|
|
149
|
+
result.source = safeSource(row.source)
|
|
150
|
+
return result
|
|
151
|
+
|
|
152
|
+
safeSource(source)
|
|
153
|
+
result = {}
|
|
154
|
+
if source == null or !is_object(source) or is_array(source)
|
|
155
|
+
return null
|
|
156
|
+
for ["kind", "clientId", "userId", "grantId", "identity"] as field
|
|
157
|
+
if is_string(source[field])
|
|
158
|
+
result[field] = source[field]
|
|
159
|
+
return result
|
|
160
|
+
|
|
161
|
+
summary(row)
|
|
162
|
+
return {
|
|
163
|
+
id: row.id
|
|
164
|
+
queue: row.queue
|
|
165
|
+
path: row.path
|
|
166
|
+
state: row.state
|
|
167
|
+
retryCount: row.retryCount
|
|
168
|
+
retryLimit: row.retryLimit
|
|
169
|
+
createdAt: row.createdAt
|
|
170
|
+
startAfter: row.startAfter
|
|
171
|
+
startedAt: row.startedAt
|
|
172
|
+
completedAt: row.completedAt
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
filters(options)
|
|
176
|
+
if options == null
|
|
177
|
+
options = {}
|
|
178
|
+
if !is_object(options) or is_array(options)
|
|
179
|
+
throw "Job list options must be a map"
|
|
180
|
+
allowed = ["queue", "path", "state", "limit", "cursor"]
|
|
181
|
+
for options as key: value
|
|
182
|
+
if !allowed.includes(key)
|
|
183
|
+
throw "Unknown job list option: ${key}"
|
|
184
|
+
result = {
|
|
185
|
+
queue: null
|
|
186
|
+
path: null
|
|
187
|
+
state: null
|
|
188
|
+
limit: 50
|
|
189
|
+
cursor: null
|
|
190
|
+
}
|
|
191
|
+
for ["queue", "path", "state"] as field
|
|
192
|
+
value = options[field]
|
|
193
|
+
if value != null
|
|
194
|
+
if !is_string(value) or value == "" or value.length > 2048
|
|
195
|
+
throw "Invalid job list filter: ${field}"
|
|
196
|
+
result[field] = value
|
|
197
|
+
states = ["created", "retry", "active", "completed", "cancelled", "failed"]
|
|
198
|
+
if result.state != null and !states.includes(result.state)
|
|
199
|
+
throw "Invalid job state filter"
|
|
200
|
+
if options.limit != null
|
|
201
|
+
if !Number/isSafeInteger(options.limit) or options.limit < 1 or options.limit > 200
|
|
202
|
+
throw "Job list limit must be between 1 and 200"
|
|
203
|
+
result.limit = options.limit
|
|
204
|
+
if options.cursor != null
|
|
205
|
+
if !is_string(options.cursor) or options.cursor.length == 0 or options.cursor.length > 8192
|
|
206
|
+
throw "Invalid job cursor"
|
|
207
|
+
result.cursor = options.cursor
|
|
208
|
+
return result
|
|
209
|
+
|
|
210
|
+
decodeCursor(settings, selected)
|
|
211
|
+
if selected.cursor == null
|
|
212
|
+
return null
|
|
213
|
+
try
|
|
214
|
+
bytes = Buffer/from(selected.cursor, "base64url")
|
|
215
|
+
if bytes.toString("base64url") != selected.cursor
|
|
216
|
+
throw "Invalid encoding"
|
|
217
|
+
value = from_json(bytes.toString("utf8"))
|
|
218
|
+
if value == null or !is_object(value) or is_array(value)
|
|
219
|
+
throw "Invalid cursor"
|
|
220
|
+
if value.version != 1 or value.application != settings.application
|
|
221
|
+
throw "Wrong cursor scope"
|
|
222
|
+
for ["queue", "path", "state"] as field
|
|
223
|
+
if !Object/hasOwn(value, field) or value[field] != selected[field]
|
|
224
|
+
throw "Changed cursor filters"
|
|
225
|
+
pattern = new RegExp("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{6}Z$")
|
|
226
|
+
if !is_string(value.createdAt) or !pattern.test(value.createdAt) or !uuid(value.id) or !is_string(value.name)
|
|
227
|
+
throw "Invalid cursor position"
|
|
228
|
+
stamp = Date/parse(value.createdAt)
|
|
229
|
+
if !Number/isFinite(stamp)
|
|
230
|
+
throw "Invalid cursor time"
|
|
231
|
+
date = new Date(stamp)
|
|
232
|
+
if date.toISOString() != "${value.createdAt.slice(0, 23)}Z"
|
|
233
|
+
throw "Invalid cursor time"
|
|
234
|
+
if value.name.length == 0 or value.name.length > 200
|
|
235
|
+
throw "Invalid cursor queue"
|
|
236
|
+
return value
|
|
237
|
+
catch error
|
|
238
|
+
throw "Invalid job cursor"
|
|
239
|
+
|
|
240
|
+
encodeCursor(settings, selected, row)
|
|
241
|
+
return Buffer/from(to_json({
|
|
242
|
+
version: 1
|
|
243
|
+
application: settings.application
|
|
244
|
+
queue: selected.queue
|
|
245
|
+
path: selected.path
|
|
246
|
+
state: selected.state
|
|
247
|
+
createdAt: row.createdAt
|
|
248
|
+
id: row.id
|
|
249
|
+
name: row.queue
|
|
250
|
+
}), "utf8").toString("base64url")
|
|
251
|
+
|
|
252
|
+
readJobs(settings, db, token, options)
|
|
253
|
+
usable(db)
|
|
254
|
+
manager(token)
|
|
255
|
+
selected = filters(options)
|
|
256
|
+
cursor = decodeCursor(settings, selected)
|
|
257
|
+
rows = invoke(settings, db, "jobs", {
|
|
258
|
+
queue: selected.queue
|
|
259
|
+
path: selected.path
|
|
260
|
+
state: selected.state
|
|
261
|
+
limit: selected.limit + 1
|
|
262
|
+
position: cursor
|
|
263
|
+
})
|
|
264
|
+
items = []
|
|
265
|
+
last = null
|
|
266
|
+
for rows as row
|
|
267
|
+
if items.length == selected.limit
|
|
268
|
+
break
|
|
269
|
+
items.add(summary(row))
|
|
270
|
+
last = row
|
|
271
|
+
next = null
|
|
272
|
+
if rows.length > selected.limit
|
|
273
|
+
next = encodeCursor(settings, selected, last)
|
|
274
|
+
return {
|
|
275
|
+
items: items
|
|
276
|
+
nextCursor: next
|
|
277
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import "./namespace"
|
|
2
|
+
schemaName(application)
|
|
3
|
+
import js "@lango-project/lango-server" as server
|
|
4
|
+
import js global Promise
|
|
5
|
+
import js global setTimeout
|
|
6
|
+
import js global clearTimeout
|
|
7
|
+
|
|
8
|
+
export cleanRecords(config, batchSize)
|
|
9
|
+
export class Retention(config, intervalSeconds, batchSize)
|
|
10
|
+
methods
|
|
11
|
+
start()
|
|
12
|
+
close()
|
|
13
|
+
|
|
14
|
+
Retention(config, intervalSeconds, batchSize)
|
|
15
|
+
@config = config
|
|
16
|
+
@interval = intervalSeconds * 1000
|
|
17
|
+
@batchSize = batchSize
|
|
18
|
+
@timer = null
|
|
19
|
+
@pending = null
|
|
20
|
+
@closed = false
|
|
21
|
+
|
|
22
|
+
Retention:start()
|
|
23
|
+
@pending = sweep()
|
|
24
|
+
return chain @pending
|
|
25
|
+
then(ignored)
|
|
26
|
+
arm()
|
|
27
|
+
|
|
28
|
+
Retention:arm()
|
|
29
|
+
if @closed
|
|
30
|
+
return null
|
|
31
|
+
fire = ()
|
|
32
|
+
@timer = null
|
|
33
|
+
@pending = sweep()
|
|
34
|
+
@pending = chain @pending
|
|
35
|
+
catch(error)
|
|
36
|
+
context log
|
|
37
|
+
log.err("Boss retention cleanup failed: ${error}")
|
|
38
|
+
finally
|
|
39
|
+
arm()
|
|
40
|
+
@timer = setTimeout(fire, @interval)
|
|
41
|
+
@timer.unref()
|
|
42
|
+
return null
|
|
43
|
+
|
|
44
|
+
Retention:sweep()
|
|
45
|
+
return chain Promise/resolve(null)
|
|
46
|
+
then(ignored)
|
|
47
|
+
return cleanRecords(@config, @batchSize)
|
|
48
|
+
then(count)
|
|
49
|
+
context log
|
|
50
|
+
log.debug("Boss retention cleanup removed ${count} terminal jobs")
|
|
51
|
+
return count
|
|
52
|
+
|
|
53
|
+
Retention:close()
|
|
54
|
+
@closed = true
|
|
55
|
+
if @timer != null
|
|
56
|
+
clearTimeout(@timer)
|
|
57
|
+
@timer = null
|
|
58
|
+
return Promise/resolve(@pending)
|
|
59
|
+
|
|
60
|
+
scope(config)
|
|
61
|
+
schema = schemaName(config.application)
|
|
62
|
+
query = SQL
|
|
63
|
+
select set_config('search_path', :schema, false)
|
|
64
|
+
query.schema = schema
|
|
65
|
+
query.execute()
|
|
66
|
+
return null
|
|
67
|
+
|
|
68
|
+
cleanRecords(config, batchSize)
|
|
69
|
+
action = ()
|
|
70
|
+
capture config
|
|
71
|
+
capture batchSize
|
|
72
|
+
scope(config)
|
|
73
|
+
protect = SQL
|
|
74
|
+
update job set deletion_seconds = 0
|
|
75
|
+
where data->>'application' = :application and data->>'version' = '1'
|
|
76
|
+
and deletion_seconds <> 0
|
|
77
|
+
protect.application = config.application
|
|
78
|
+
protect.execute()
|
|
79
|
+
query = SQL
|
|
80
|
+
with candidates as (
|
|
81
|
+
select name, id from job
|
|
82
|
+
where data->>'application' = :application and data->>'version' = '1'
|
|
83
|
+
and state in ('failed', 'completed', 'cancelled')
|
|
84
|
+
and completed_on is not null
|
|
85
|
+
and completed_on + (
|
|
86
|
+
case when state = 'failed' then
|
|
87
|
+
case when jsonb_typeof(data->'failedRetentionSeconds') = 'number' then
|
|
88
|
+
case when (data->>'failedRetentionSeconds')::numeric between 1 and 2147483647
|
|
89
|
+
and trunc((data->>'failedRetentionSeconds')::numeric) = (data->>'failedRetentionSeconds')::numeric
|
|
90
|
+
then (data->>'failedRetentionSeconds')::numeric else 604800 end
|
|
91
|
+
else 604800 end
|
|
92
|
+
else 604800 end
|
|
93
|
+
) * interval '1 second' <= now()
|
|
94
|
+
order by completed_on, name, id
|
|
95
|
+
limit :batchSize for update skip locked
|
|
96
|
+
), removed as (
|
|
97
|
+
delete from job j using candidates c
|
|
98
|
+
where j.name = c.name and j.id = c.id
|
|
99
|
+
returning j.id
|
|
100
|
+
)
|
|
101
|
+
select count(*)::integer as total from removed
|
|
102
|
+
query.application = config.application
|
|
103
|
+
query.batchSize = batchSize
|
|
104
|
+
query.execute()
|
|
105
|
+
query.next()
|
|
106
|
+
return query.total
|
|
107
|
+
return server/withDatabase(config.root, action)
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import "./namespace"
|
|
2
|
+
schemaName(application)
|
|
3
|
+
scheduleKey(application, name)
|
|
4
|
+
import js "@lango-project/lango-server" as server
|
|
5
|
+
import js "node:crypto" as crypto
|
|
6
|
+
import js global Object
|
|
7
|
+
|
|
8
|
+
export synchronizeSchedules(config)
|
|
9
|
+
export initializeSchedules(schema)
|
|
10
|
+
export operation(schema, action, queue, key, item = null)
|
|
11
|
+
|
|
12
|
+
synchronizeSchedules(config)
|
|
13
|
+
action = ()
|
|
14
|
+
capture config
|
|
15
|
+
return synchronize(config)
|
|
16
|
+
return server/withDatabase(config.root, action)
|
|
17
|
+
|
|
18
|
+
fingerprint(items)
|
|
19
|
+
definitions = Object/create(null)
|
|
20
|
+
for items as item
|
|
21
|
+
definitions[item.name] = item
|
|
22
|
+
hash = crypto/createHash("sha256")
|
|
23
|
+
hash.update(to_json(canonical(definitions)))
|
|
24
|
+
return hash.digest("hex")
|
|
25
|
+
|
|
26
|
+
canonical(value)
|
|
27
|
+
if is_array(value)
|
|
28
|
+
array = []
|
|
29
|
+
for value as item
|
|
30
|
+
array.add(canonical(item))
|
|
31
|
+
return array
|
|
32
|
+
if is_object(value) and value != null
|
|
33
|
+
result = Object/create(null)
|
|
34
|
+
keys = Object/keys(value)
|
|
35
|
+
keys.sort()
|
|
36
|
+
for keys as key
|
|
37
|
+
result[key] = canonical(value[key])
|
|
38
|
+
return result
|
|
39
|
+
return value
|
|
40
|
+
|
|
41
|
+
synchronize(config)
|
|
42
|
+
schema = schemaName(config.application)
|
|
43
|
+
search = SQL
|
|
44
|
+
select set_config('search_path', :schema, false)
|
|
45
|
+
search.schema = schema
|
|
46
|
+
search.execute()
|
|
47
|
+
start_trans()
|
|
48
|
+
try
|
|
49
|
+
lock = SQL
|
|
50
|
+
select pg_advisory_xact_lock(hashtextextended(:name, 0))
|
|
51
|
+
lock.name = "lango-boss:schedules:${config.application}"
|
|
52
|
+
lock.execute()
|
|
53
|
+
initializeSchedules(schema)
|
|
54
|
+
stamp = fingerprint(config.schedules)
|
|
55
|
+
current = SQL
|
|
56
|
+
select application, revision, fingerprint from lango_schedule_state where id = 1 for update
|
|
57
|
+
current.execute()
|
|
58
|
+
if current.next()
|
|
59
|
+
if current.application != config.application
|
|
60
|
+
throw "Schedule schema belongs to another application"
|
|
61
|
+
if config.scheduleRevision < current.revision
|
|
62
|
+
throw "Schedule revision is older than the deployed revision"
|
|
63
|
+
if config.scheduleRevision == current.revision and stamp != current.fingerprint
|
|
64
|
+
throw "Schedule content changed without increasing revision"
|
|
65
|
+
applyDefinitions(config, schema)
|
|
66
|
+
store = SQL
|
|
67
|
+
insert into lango_schedule_state (id, application, revision, fingerprint)
|
|
68
|
+
values (1, :application, :revision, :fingerprint)
|
|
69
|
+
on conflict (id) do update set revision = excluded.revision,
|
|
70
|
+
fingerprint = excluded.fingerprint, updated_at = now()
|
|
71
|
+
store.application = config.application
|
|
72
|
+
store.revision = config.scheduleRevision
|
|
73
|
+
store.fingerprint = stamp
|
|
74
|
+
store.execute()
|
|
75
|
+
commit_trans()
|
|
76
|
+
catch error
|
|
77
|
+
rollback_trans()
|
|
78
|
+
throw error
|
|
79
|
+
return null
|
|
80
|
+
|
|
81
|
+
initializeSchedules(schema)
|
|
82
|
+
state = SQL
|
|
83
|
+
create table if not exists ${schema}.lango_schedule_state (
|
|
84
|
+
id integer primary key check (id = 1), application text not null,
|
|
85
|
+
revision integer not null, fingerprint text not null,
|
|
86
|
+
updated_at timestamptz not null default now()
|
|
87
|
+
)
|
|
88
|
+
state.execute()
|
|
89
|
+
entries = SQL
|
|
90
|
+
create table if not exists ${schema}.lango_schedule_definitions (
|
|
91
|
+
name text primary key, queue text not null, key text not null,
|
|
92
|
+
definition jsonb not null, paused boolean not null default false,
|
|
93
|
+
present boolean not null default true,
|
|
94
|
+
updated_at timestamptz not null default now(), unique (queue, key)
|
|
95
|
+
)
|
|
96
|
+
entries.execute()
|
|
97
|
+
return null
|
|
98
|
+
|
|
99
|
+
operation(schema, action, queue, key, item = null)
|
|
100
|
+
context db
|
|
101
|
+
input = {
|
|
102
|
+
schema: schema
|
|
103
|
+
action: action
|
|
104
|
+
queue: queue
|
|
105
|
+
key: key
|
|
106
|
+
}
|
|
107
|
+
if item != null
|
|
108
|
+
input.cron = item.cron
|
|
109
|
+
input.payload = {
|
|
110
|
+
version: 1
|
|
111
|
+
application: item.application
|
|
112
|
+
path: item.path
|
|
113
|
+
input: item.data
|
|
114
|
+
source: {
|
|
115
|
+
kind: "system"
|
|
116
|
+
identity: item.identity
|
|
117
|
+
}
|
|
118
|
+
failedRetentionSeconds: item.retry.failedRetentionSeconds
|
|
119
|
+
}
|
|
120
|
+
input.options = {
|
|
121
|
+
key: key
|
|
122
|
+
tz: item.timeZone
|
|
123
|
+
missed: item.missed
|
|
124
|
+
precision: item.precision
|
|
125
|
+
retryLimit: item.retry.retryLimit
|
|
126
|
+
retryDelay: item.retry.retryDelay
|
|
127
|
+
retryBackoff: item.retry.retryBackoff
|
|
128
|
+
retryDelayMax: item.retry.retryDelayMax
|
|
129
|
+
expireInSeconds: item.retry.expireInSeconds
|
|
130
|
+
heartbeatSeconds: 30
|
|
131
|
+
deleteAfterSeconds: 0
|
|
132
|
+
}
|
|
133
|
+
return db.invoke({
|
|
134
|
+
module: "@lango-project/lango-boss/schedule-transaction"
|
|
135
|
+
handler: "apply"
|
|
136
|
+
}, input)
|
|
137
|
+
|
|
138
|
+
applyDefinitions(config, schema)
|
|
139
|
+
desired = Object/create(null)
|
|
140
|
+
for config.schedules as item
|
|
141
|
+
desired[item.name] = true
|
|
142
|
+
existing = SQL
|
|
143
|
+
select name, queue, key from lango_schedule_definitions where present = true
|
|
144
|
+
existing.execute()
|
|
145
|
+
loop while existing.next()
|
|
146
|
+
if !Object/hasOwn(desired, existing.name)
|
|
147
|
+
operation(schema, "remove", existing.queue, existing.key)
|
|
148
|
+
missing = SQL
|
|
149
|
+
update lango_schedule_definitions set present = false, updated_at = now() where name = :name
|
|
150
|
+
missing.name = existing.name
|
|
151
|
+
missing.execute()
|
|
152
|
+
for config.schedules as item
|
|
153
|
+
applyDefinition(config.application, schema, item)
|
|
154
|
+
return null
|
|
155
|
+
|
|
156
|
+
applyDefinition(application, schema, supplied)
|
|
157
|
+
item = Object/assign({}, supplied, {
|
|
158
|
+
application: application
|
|
159
|
+
})
|
|
160
|
+
key = scheduleKey(application, item.name)
|
|
161
|
+
old = SQL
|
|
162
|
+
select queue, key, paused from lango_schedule_definitions where name = :name
|
|
163
|
+
old.name = item.name
|
|
164
|
+
old.execute()
|
|
165
|
+
found = old.next()
|
|
166
|
+
paused = false
|
|
167
|
+
if found
|
|
168
|
+
paused = old.paused
|
|
169
|
+
moved = found and (old.queue != item.queue or old.key != key)
|
|
170
|
+
if !found or moved
|
|
171
|
+
collision = operation(schema, "get", item.queue, key)
|
|
172
|
+
if collision != null
|
|
173
|
+
throw "Schedule key already exists outside the managed registry: ${item.name}"
|
|
174
|
+
if moved
|
|
175
|
+
operation(schema, "remove", old.queue, old.key)
|
|
176
|
+
if item.enabled and !paused
|
|
177
|
+
operation(schema, "upsert", item.queue, key, item)
|
|
178
|
+
else
|
|
179
|
+
operation(schema, "remove", item.queue, key)
|
|
180
|
+
store = SQL
|
|
181
|
+
insert into lango_schedule_definitions (name, queue, key, definition)
|
|
182
|
+
values (:name, :queue, :key, :definition::jsonb)
|
|
183
|
+
on conflict (name) do update set queue = excluded.queue, key = excluded.key,
|
|
184
|
+
definition = excluded.definition, present = true, updated_at = now()
|
|
185
|
+
store.name = item.name
|
|
186
|
+
store.queue = item.queue
|
|
187
|
+
store.key = key
|
|
188
|
+
store.definition = to_json(supplied)
|
|
189
|
+
store.execute()
|
|
190
|
+
return null
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import js "pg-boss" as pgBoss
|
|
2
|
+
|
|
3
|
+
export apply(db, input)
|
|
4
|
+
|
|
5
|
+
apply(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
|
+
switch input.action
|
|
15
|
+
case "get"
|
|
16
|
+
return boss.getSchedule(input.queue, input.key)
|
|
17
|
+
case "remove"
|
|
18
|
+
return boss.unschedule(input.queue, input.key)
|
|
19
|
+
case "upsert"
|
|
20
|
+
return boss.schedule(input.queue, input.cron, input.payload, input.options)
|
|
21
|
+
else
|
|
22
|
+
throw "Unknown schedule operation"
|