@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,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
+ }
@@ -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)