@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.
Files changed (121) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +2 -0
  3. package/README.md +118 -2
  4. package/dist/.lango/bignumber.mjs +3119 -0
  5. package/dist/.lango/entries/cli.mjs +139 -0
  6. package/dist/.lango/entries/cli.mjs.map +1 -0
  7. package/dist/.lango/localization.mjs +108 -0
  8. package/dist/.lango/runtime.mjs +725 -0
  9. package/dist/.lango/vendor/i18next.LICENSE +21 -0
  10. package/dist/.lango/vendor/i18next.mjs +2434 -0
  11. package/dist/.lango/xmldom/conventions.js +429 -0
  12. package/dist/.lango/xmldom/dom-parser.js +591 -0
  13. package/dist/.lango/xmldom/dom.js +3759 -0
  14. package/dist/.lango/xmldom/entities.js +2171 -0
  15. package/dist/.lango/xmldom/errors.js +205 -0
  16. package/dist/.lango/xmldom/grammar.js +559 -0
  17. package/dist/.lango/xmldom/index.js +41 -0
  18. package/dist/.lango/xmldom/package.json +1 -0
  19. package/dist/.lango/xmldom/sax.js +983 -0
  20. package/dist/.lango/xmldom.LICENSE.md +8 -0
  21. package/dist/boss.mjs +136 -0
  22. package/dist/boss.mjs.map +1 -0
  23. package/dist/claim_watch.mjs +99 -0
  24. package/dist/claim_watch.mjs.map +1 -0
  25. package/dist/cli.mjs +7 -0
  26. package/dist/cli.mjs.map +1 -0
  27. package/dist/configuration.mjs +58 -0
  28. package/dist/configuration.mjs.map +1 -0
  29. package/dist/dispatcher.mjs +108 -0
  30. package/dist/dispatcher.mjs.map +1 -0
  31. package/dist/driver.mjs +98 -0
  32. package/dist/driver.mjs.map +1 -0
  33. package/dist/envelope.mjs +92 -0
  34. package/dist/envelope.mjs.map +1 -0
  35. package/dist/library.mjs +139 -0
  36. package/dist/library.mjs.map +1 -0
  37. package/dist/management.mjs +360 -0
  38. package/dist/management.mjs.map +1 -0
  39. package/dist/management_transaction.mjs +26 -0
  40. package/dist/management_transaction.mjs.map +1 -0
  41. package/dist/namespace.mjs +27 -0
  42. package/dist/namespace.mjs.map +1 -0
  43. package/dist/queue.mjs +161 -0
  44. package/dist/queue.mjs.map +1 -0
  45. package/dist/reads.mjs +359 -0
  46. package/dist/reads.mjs.map +1 -0
  47. package/dist/retention.mjs +133 -0
  48. package/dist/retention.mjs.map +1 -0
  49. package/dist/schedule_sync.mjs +222 -0
  50. package/dist/schedule_sync.mjs.map +1 -0
  51. package/dist/schedule_transaction.mjs +24 -0
  52. package/dist/schedule_transaction.mjs.map +1 -0
  53. package/dist/schedules.mjs +201 -0
  54. package/dist/schedules.mjs.map +1 -0
  55. package/dist/transaction.mjs +16 -0
  56. package/dist/transaction.mjs.map +1 -0
  57. package/dist/validation.mjs +90 -0
  58. package/dist/validation.mjs.map +1 -0
  59. package/dist/worker.mjs +316 -0
  60. package/dist/worker.mjs.map +1 -0
  61. package/docs/guide.md +88 -0
  62. package/docs/management.md +93 -0
  63. package/docs/producer.md +115 -0
  64. package/docs/reads.md +107 -0
  65. package/docs/reference.md +77 -0
  66. package/docs/release.md +103 -0
  67. package/docs/retention.md +47 -0
  68. package/docs/scheduling.md +79 -0
  69. package/docs/worker.md +90 -0
  70. package/examples/reports/README.md +118 -0
  71. package/examples/reports/app.lango +133 -0
  72. package/examples/reports/deploy.xml +6 -0
  73. package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
  74. package/examples/reports/dist/.lango/entries/app.mjs +133 -0
  75. package/examples/reports/dist/.lango/entries/app.mjs.map +1 -0
  76. package/examples/reports/dist/.lango/localization.mjs +108 -0
  77. package/examples/reports/dist/.lango/runtime.mjs +725 -0
  78. package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
  79. package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
  80. package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
  81. package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
  82. package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
  83. package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
  84. package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
  85. package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
  86. package/examples/reports/dist/.lango/xmldom/index.js +41 -0
  87. package/examples/reports/dist/.lango/xmldom/package.json +1 -0
  88. package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
  89. package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
  90. package/examples/reports/dist/app.mjs +7 -0
  91. package/examples/reports/dist/app.mjs.map +1 -0
  92. package/examples/reports/dist/handlers.mjs +202 -0
  93. package/examples/reports/dist/handlers.mjs.map +1 -0
  94. package/examples/reports/dist/setup.mjs +14 -0
  95. package/examples/reports/dist/setup.mjs.map +1 -0
  96. package/examples/reports/handlers.lango +153 -0
  97. package/examples/reports/jobs.xml +5 -0
  98. package/examples/reports/package.json +23 -0
  99. package/examples/reports/routes.xml +4 -0
  100. package/examples/reports/setup.lango +17 -0
  101. package/package.json +62 -4
  102. package/src/boss.lango +92 -0
  103. package/src/claim_watch.lango +58 -0
  104. package/src/cli.lango +103 -0
  105. package/src/configuration.lango +43 -0
  106. package/src/dispatcher.lango +75 -0
  107. package/src/driver.lango +72 -0
  108. package/src/envelope.lango +56 -0
  109. package/src/library.lango +66 -0
  110. package/src/management.lango +302 -0
  111. package/src/management_transaction.lango +28 -0
  112. package/src/namespace.lango +15 -0
  113. package/src/queue.lango +115 -0
  114. package/src/reads.lango +277 -0
  115. package/src/retention.lango +107 -0
  116. package/src/schedule_sync.lango +190 -0
  117. package/src/schedule_transaction.lango +22 -0
  118. package/src/schedules.lango +165 -0
  119. package/src/transaction.lango +23 -0
  120. package/src/validation.lango +45 -0
  121. package/src/worker.lango +230 -0
@@ -0,0 +1,302 @@
1
+ import "./namespace"
2
+ schemaName(application)
3
+ import "./reads"
4
+ usable(db)
5
+ manager(token)
6
+ uuid(value)
7
+ import "./validation"
8
+ name(value, label)
9
+ import "./schedule_sync"
10
+ initializeSchedules(schema)
11
+ alias scheduleOperation = operation(schema, action, queue, key, item = null)
12
+ import js "@lango-project/lango-server" as server
13
+ import js global Object
14
+ import js global Number
15
+
16
+ export prepareManagement(config)
17
+ export manage(settings, scope, token, action, target = null, options = null)
18
+
19
+ prepareManagement(config)
20
+ action = ()
21
+ capture config
22
+ return initializeManagement(config)
23
+ return server/withDatabase(config.root, action)
24
+
25
+ initializeManagement(config)
26
+ schema = schemaName(config.application)
27
+ start_trans()
28
+ try
29
+ scheduleLock(config.application)
30
+ initializeSchedules(schema)
31
+ queues = SQL
32
+ create table if not exists ${schema}.lango_managed_queues (
33
+ name text primary key, created_at timestamptz not null default now()
34
+ )
35
+ queues.execute()
36
+ audit = SQL
37
+ create table if not exists ${schema}.lango_management_audit (
38
+ id bigserial primary key, occurred_at timestamptz not null default now(),
39
+ application text not null, action text not null, target text not null,
40
+ actor jsonb not null, outcome jsonb not null
41
+ )
42
+ audit.execute()
43
+ for config.targets as item
44
+ store = SQL
45
+ insert into ${schema}.lango_managed_queues (name) values (:name) on conflict do nothing
46
+ store.name = item.queue
47
+ store.execute()
48
+ commit_trans()
49
+ catch error
50
+ rollback_trans()
51
+ throw error
52
+ return null
53
+
54
+ scheduleLock(application)
55
+ query = SQL
56
+ select pg_advisory_xact_lock(hashtextextended(:name, 0))
57
+ query.name = "lango-boss:schedules:${application}"
58
+ query.execute()
59
+ return null
60
+
61
+ actor(token)
62
+ result = {}
63
+ for ["clientId", "userId", "grantId", "identity"] as field
64
+ value = token.get(field)
65
+ if is_string(value)
66
+ result[field] = value
67
+ return result
68
+
69
+ manage(settings, scope, token, action, target = null, options = null)
70
+ usable(scope)
71
+ context db = scope
72
+ context log
73
+ try
74
+ manager(token)
75
+ catch error
76
+ log.warn("Boss management denied action=${to_json(action)} actor=${to_json(actor(token))}")
77
+ throw error
78
+ schema = schemaName(settings.application)
79
+ if action == "queues" or action == "schedules"
80
+ return inventory(settings.application, schema, action, options)
81
+ operations = ["retry", "cancel", "pauseQueue", "resumeQueue", "pauseSchedule", "resumeSchedule"]
82
+ if !operations.includes(action)
83
+ throw "Unknown Boss management operation"
84
+ if action == "retry" or action == "cancel"
85
+ if !uuid(target)
86
+ return null
87
+ else
88
+ name(target, "Management target")
89
+ own = db.transactionState() == null
90
+ if own
91
+ start_trans()
92
+ try
93
+ result = null
94
+ switch action
95
+ case "retry"
96
+ case "cancel"
97
+ result = changeJob(settings, schema, action, target)
98
+ case "pauseQueue"
99
+ case "resumeQueue"
100
+ result = changeQueue(schema, action, target)
101
+ case "pauseSchedule"
102
+ case "resumeSchedule"
103
+ scheduleLock(settings.application)
104
+ result = changeSchedule(settings, schema, action, target)
105
+ appendAudit(settings.application, schema, token, action, target, result)
106
+ if own
107
+ commit_trans()
108
+ log.debug("Boss management staged action=${action} target=${to_json(target)} actor=${to_json(actor(token))}")
109
+ return result
110
+ catch error
111
+ if own
112
+ rollback_trans()
113
+ log.err("Boss management failed action=${action} target=${to_json(target)} actor=${to_json(actor(token))}: ${error}")
114
+ throw error
115
+
116
+ native(schema, action, queue, id = null)
117
+ context db
118
+ return db.invoke({
119
+ module: "@lango-project/lango-boss/management-transaction"
120
+ handler: "apply"
121
+ }, {
122
+ schema: schema
123
+ action: action
124
+ queue: queue
125
+ id: id
126
+ })
127
+
128
+ changeJob(settings, schema, action, id)
129
+ query = SQL
130
+ select name, state::text, data->>'path' as path, retry_limit
131
+ from ${schema}.job where id = :id::uuid
132
+ and data->>'application' = :application and data->'version' = '1'::jsonb
133
+ order by name limit 2 for update
134
+ query.id = id
135
+ query.application = settings.application
136
+ query.execute()
137
+ if !query.next()
138
+ return null
139
+ queue = query.name
140
+ before = query.state
141
+ path = query.path
142
+ limit = query.retry_limit
143
+ if query.next()
144
+ return null
145
+ changed = false
146
+ reason = null
147
+ state = before
148
+ if action == "retry" and before == "failed"
149
+ available = false
150
+ for settings.targets as item
151
+ if item.path == path and item.queue == queue
152
+ available = true
153
+ if !available
154
+ reason = "TargetUnavailable"
155
+ else
156
+ if limit == 2147483647
157
+ reason = "RetryLimitExceeded"
158
+ else
159
+ native(schema, action, queue, id)
160
+ state = "retry"
161
+ changed = true
162
+ if action == "cancel" and (before == "created" or before == "retry" or before == "active")
163
+ native(schema, action, queue, id)
164
+ state = "cancelled"
165
+ changed = true
166
+ return {
167
+ id: id
168
+ queue: queue
169
+ previousState: before
170
+ state: state
171
+ changed: changed
172
+ cancellationRequested: changed and before == "active"
173
+ reason: reason
174
+ }
175
+
176
+ changeQueue(schema, action, target)
177
+ query = SQL
178
+ select q.paused_on is not null as paused from ${schema}.queue q
179
+ join ${schema}.lango_managed_queues m on q.name = m.name
180
+ where q.name = :name for update of q
181
+ query.name = target
182
+ query.execute()
183
+ if !query.next()
184
+ return null
185
+ paused = action == "pauseQueue"
186
+ changed = query.paused != paused
187
+ if changed
188
+ native(schema, action, target)
189
+ return {
190
+ name: target
191
+ paused: paused
192
+ changed: changed
193
+ }
194
+
195
+ changeSchedule(settings, schema, action, target)
196
+ query = SQL
197
+ select queue, key, paused, definition from ${schema}.lango_schedule_definitions
198
+ where name = :name and present = true for update
199
+ query.name = target
200
+ query.execute()
201
+ if !query.next()
202
+ return null
203
+ paused = action == "pauseSchedule"
204
+ changed = query.paused != paused
205
+ item = Object/assign({}, query.definition, {
206
+ application: settings.application
207
+ })
208
+ if changed
209
+ if !paused and item.enabled
210
+ scheduleOperation(schema, "upsert", query.queue, query.key, item)
211
+ else
212
+ scheduleOperation(schema, "remove", query.queue, query.key)
213
+ store = SQL
214
+ update ${schema}.lango_schedule_definitions set paused = :paused, updated_at = now() where name = :name
215
+ store.name = target
216
+ store.paused = paused
217
+ store.execute()
218
+ return {
219
+ name: target
220
+ paused: paused
221
+ enabled: item.enabled
222
+ effectiveEnabled: item.enabled and !paused
223
+ changed: changed
224
+ }
225
+
226
+ appendAudit(application, schema, token, action, target, result)
227
+ query = SQL
228
+ insert into ${schema}.lango_management_audit (application, action, target, actor, outcome)
229
+ values (:application, :action, :target, :actor::jsonb, :outcome::jsonb)
230
+ query.application = application
231
+ query.action = action
232
+ query.target = target
233
+ query.actor = to_json(actor(token))
234
+ query.outcome = to_json(result)
235
+ query.execute()
236
+ return null
237
+
238
+ inventory(application, schema, action, options)
239
+ if options == null
240
+ options = {}
241
+ if !is_object(options) or is_array(options)
242
+ throw "Inventory options must be a map"
243
+ for options as field: value
244
+ if field != "limit" and field != "cursor"
245
+ throw "Unknown inventory option: ${field}"
246
+ limit = options.limit
247
+ if limit == null
248
+ limit = 50
249
+ if !Number/isSafeInteger(limit) or limit < 1 or limit > 200
250
+ throw "Inventory limit must be between 1 and 200"
251
+ after = options.cursor
252
+ if after != null
253
+ name(after, "Inventory cursor")
254
+ rows = []
255
+ if action == "queues"
256
+ queues = SQL
257
+ select q.name, q.paused_on is not null as paused,
258
+ (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,
259
+ (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,
260
+ (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
261
+ from ${schema}.queue q join ${schema}.lango_managed_queues m on m.name = q.name
262
+ where (:after::text is null or q.name > :after) order by q.name limit :limit
263
+ queues.after = after
264
+ queues.application = application
265
+ queues.limit = limit + 1
266
+ queues.execute()
267
+ loop while queues.next()
268
+ rows.add({
269
+ name: queues.name
270
+ paused: queues.paused
271
+ queued: queues.queued
272
+ active: queues.active
273
+ failed: queues.failed
274
+ })
275
+ else
276
+ schedules = SQL
277
+ select name, queue, paused, definition from ${schema}.lango_schedule_definitions
278
+ where present = true and (:after::text is null or name > :after) order by name limit :limit
279
+ schedules.after = after
280
+ schedules.limit = limit + 1
281
+ schedules.execute()
282
+ loop while schedules.next()
283
+ item = schedules.definition
284
+ rows.add({
285
+ name: schedules.name
286
+ queue: schedules.queue
287
+ path: item.path
288
+ cron: item.cron
289
+ timeZone: item.timeZone
290
+ enabled: item.enabled
291
+ paused: schedules.paused
292
+ effectiveEnabled: item.enabled and !schedules.paused
293
+ })
294
+ cursor = null
295
+ if rows.length > limit
296
+ rows.pop()
297
+ last = rows[rows.length - 1]
298
+ cursor = last.name
299
+ return {
300
+ items: rows
301
+ nextCursor: cursor
302
+ }
@@ -0,0 +1,28 @@
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 "retry"
16
+ return boss.retry(input.queue, input.id, {
17
+ db: db
18
+ })
19
+ case "cancel"
20
+ return boss.cancel(input.queue, input.id, {
21
+ db: db
22
+ })
23
+ case "pauseQueue"
24
+ return boss.pauseQueue(input.queue)
25
+ case "resumeQueue"
26
+ return boss.resumeQueue(input.queue)
27
+ else
28
+ throw "Unknown management operation"
@@ -0,0 +1,15 @@
1
+ import js "node:crypto" as crypto
2
+
3
+ export schemaName(application)
4
+ export scheduleKey(application, name)
5
+
6
+ schemaName(application)
7
+ hash = crypto/createHash("sha256")
8
+ hash.update(application)
9
+ suffix = hash.digest("hex").slice(0, 32)
10
+ return "lango_${suffix}"
11
+
12
+ scheduleKey(application, name)
13
+ hash = crypto/createHash("sha256")
14
+ hash.update(name)
15
+ return "${schemaName(application)}/${hash.digest("hex")}"
@@ -0,0 +1,115 @@
1
+ import js global Date
2
+ import js global Number
3
+ import "./reads"
4
+ readJob(settings, db, token, id, management = false)
5
+ readJobs(settings, db, token, options)
6
+ import "./management"
7
+ alias management = manage(settings, scope, token, action, target = null, options = null)
8
+
9
+ export createQueue(settings)
10
+
11
+ class QueueSender(settings, db, token)
12
+ methods
13
+ send(path, input)
14
+ get(id)
15
+ job(id)
16
+ jobs(options = null)
17
+ manage(action, target = null, options = null)
18
+
19
+ createQueue(settings)
20
+ context db
21
+ context token
22
+ if db == null or db.parameterStyle != "postgresql"
23
+ throw "Lango Boss queue requires an active PostgreSQL scope"
24
+ return new QueueSender(settings, db, token)
25
+
26
+ QueueSender(settings, db, token)
27
+ @settings = from_json(to_json(settings))
28
+ @db = db
29
+ @token = token
30
+
31
+ QueueSender:get(id)
32
+ return readJob(@settings, @db, @token, id)
33
+
34
+ QueueSender:job(id)
35
+ return readJob(@settings, @db, @token, id, true)
36
+
37
+ QueueSender:jobs(options = null)
38
+ return readJobs(@settings, @db, @token, options)
39
+
40
+ QueueSender:manage(action, target = null, options = null)
41
+ return management(@settings, @db, @token, action, target, options)
42
+
43
+ QueueSender:send(path, input)
44
+ target = null
45
+ for @settings.targets as candidate
46
+ if candidate.path == path
47
+ target = candidate
48
+ if target == null
49
+ throw "Unknown job path: ${path}"
50
+ if input == null or !is_object(input) or is_array(input)
51
+ throw "Queue input must be a JSON map"
52
+ if target.policy != "anonymous" and !@token.hasPolicy(target.policy)
53
+ throw "Queue target policy rejected: ${target.policy}"
54
+ if target.reauthSeconds > 0
55
+ stamp = @token.get("authenticatedAt")
56
+ now = Date/now()
57
+ if !Number/isSafeInteger(stamp) or stamp < 0 or stamp > now or now - stamp >= target.reauthSeconds * 1000
58
+ throw "Queue target requires fresh authentication"
59
+ payload = {
60
+ version: 1
61
+ application: @settings.application
62
+ path: target.path
63
+ input: from_json(to_json(input))
64
+ source: source(@token)
65
+ failedRetentionSeconds: 604800
66
+ }
67
+ state = @db.transactionState()
68
+ if state != null and state != "active"
69
+ throw "Queue submission requires a usable transaction"
70
+ own = state == null
71
+ if own
72
+ @db.begin()
73
+ try
74
+ id = @db.invoke({
75
+ module: "@lango-project/lango-boss/transaction"
76
+ handler: "send"
77
+ }, {
78
+ schema: @settings.schema
79
+ queue: target.queue
80
+ payload: payload
81
+ })
82
+ if !is_string(id) or id == ""
83
+ throw "Queue submission did not return a job id"
84
+ if own
85
+ @db.commit()
86
+ return id
87
+ catch error
88
+ if own
89
+ try
90
+ @db.rollback()
91
+ catch rollbackError
92
+ context log
93
+ log.err("Queue transaction cleanup failed")
94
+ throw error
95
+
96
+ source(token)
97
+ profile = token.get("identity")
98
+ if token.clientId? == null
99
+ if !is_string(profile) or profile == ""
100
+ throw "Queue submission requires a trusted source identity"
101
+ return {
102
+ kind: "system"
103
+ identity: profile
104
+ }
105
+ result = {
106
+ kind: "user"
107
+ clientId: token.clientId?
108
+ }
109
+ for ["userId", "grantId"] as field
110
+ value = token.get(field)
111
+ if value != null
112
+ if !is_string(value) or value == ""
113
+ throw "Queue identity references must be nonempty strings"
114
+ result[field] = value
115
+ return result