@lango-project/lango-server 0.2.3 → 0.2.4
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/README.md +14 -3
- package/dist/builder.mjs +26 -2
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/database_scope.mjs +11 -0
- package/dist/cli/database_scope.mjs.map +1 -1
- package/dist/cli/middleware.mjs +1 -1
- package/dist/cli/middleware.mjs.map +1 -1
- package/dist/cli/options.mjs +12 -1
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/postgres_adapter.mjs +12 -1
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +36 -1
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +4 -1
- package/dist/cli/server.mjs.map +1 -1
- package/dist/database_action.mjs +71 -0
- package/dist/database_action.mjs.map +1 -0
- package/dist/database_scope.mjs +11 -0
- package/dist/database_scope.mjs.map +1 -1
- package/dist/execution_failure.mjs +30 -0
- package/dist/execution_failure.mjs.map +1 -0
- package/dist/execution_identity.mjs +103 -0
- package/dist/execution_identity.mjs.map +1 -0
- package/dist/execution_log.mjs +47 -0
- package/dist/execution_log.mjs.map +1 -0
- package/dist/execution_runtime.mjs +201 -0
- package/dist/execution_runtime.mjs.map +1 -0
- package/dist/execution_worker.mjs +41 -0
- package/dist/execution_worker.mjs.map +1 -0
- package/dist/executor.mjs +384 -0
- package/dist/executor.mjs.map +1 -0
- package/dist/group.mjs +15 -0
- package/dist/group.mjs.map +1 -1
- package/dist/job_configuration.mjs +37 -0
- package/dist/job_configuration.mjs.map +1 -0
- package/dist/job_context.mjs +61 -0
- package/dist/job_context.mjs.map +1 -0
- package/dist/library.mjs +31 -0
- package/dist/library.mjs.map +1 -1
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs +44 -0
- package/dist/module_loader.mjs.map +1 -0
- package/dist/options.mjs +12 -1
- package/dist/options.mjs.map +1 -1
- package/dist/postgres_adapter.mjs +12 -1
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +67 -5
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs +52 -0
- package/dist/queue_provider.mjs.map +1 -0
- package/dist/routes.mjs +36 -1
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +65 -25
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +4 -1
- package/dist/server.mjs.map +1 -1
- package/docs/jobs.md +123 -0
- package/package.json +6 -4
- package/src/builder.lango +22 -0
- package/src/database_action.lango +30 -0
- package/src/database_scope.lango +7 -0
- package/src/execution_failure.lango +14 -0
- package/src/execution_identity.lango +71 -0
- package/src/execution_log.lango +28 -0
- package/src/execution_runtime.lango +135 -0
- package/src/execution_worker.lango +32 -0
- package/src/executor.lango +284 -0
- package/src/group.lango +11 -0
- package/src/job_configuration.lango +29 -0
- package/src/job_context.lango +44 -0
- package/src/library.lango +21 -0
- package/src/middleware.lango +1 -1
- package/src/module_loader.lango +25 -0
- package/src/options.lango +7 -1
- package/src/postgres_adapter.lango +12 -0
- package/src/postgres_worker.lango +54 -3
- package/src/queue_provider.lango +30 -0
- package/src/routes.lango +44 -0
- package/src/runtime.lango +45 -17
- package/src/server.lango +3 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "./execution_failure"
|
|
2
|
+
class ExecutionFailure
|
|
3
|
+
import js global Object
|
|
4
|
+
|
|
5
|
+
export class JobContext(task, route)
|
|
6
|
+
properties
|
|
7
|
+
id?
|
|
8
|
+
path?
|
|
9
|
+
queue?
|
|
10
|
+
attempt?
|
|
11
|
+
source?
|
|
12
|
+
checking?
|
|
13
|
+
methods
|
|
14
|
+
reject(code)
|
|
15
|
+
|
|
16
|
+
JobContext(task, route)
|
|
17
|
+
@id = task.id
|
|
18
|
+
@path = route.path
|
|
19
|
+
@queue = route.queue
|
|
20
|
+
@attempt = task.attempt
|
|
21
|
+
@source = from_json(to_json(task.source))
|
|
22
|
+
@checking = task.authorize == true
|
|
23
|
+
Object/freeze(@this)
|
|
24
|
+
|
|
25
|
+
JobContext:id?
|
|
26
|
+
return @id
|
|
27
|
+
|
|
28
|
+
JobContext:path?
|
|
29
|
+
return @path
|
|
30
|
+
|
|
31
|
+
JobContext:queue?
|
|
32
|
+
return @queue
|
|
33
|
+
|
|
34
|
+
JobContext:attempt?
|
|
35
|
+
return @attempt
|
|
36
|
+
|
|
37
|
+
JobContext:source?
|
|
38
|
+
return from_json(to_json(@source))
|
|
39
|
+
|
|
40
|
+
JobContext:checking?
|
|
41
|
+
return @checking
|
|
42
|
+
|
|
43
|
+
JobContext:reject(code)
|
|
44
|
+
throw new ExecutionFailure(code)
|
package/src/library.lango
CHANGED
|
@@ -2,9 +2,30 @@ import "./server"
|
|
|
2
2
|
alias ServerClass = class Server
|
|
3
3
|
import "./builder"
|
|
4
4
|
alias BuilderClass = class Builder
|
|
5
|
+
import "./executor"
|
|
6
|
+
alias ExecutorClass = class Executor
|
|
7
|
+
import "./database_action"
|
|
8
|
+
alias databaseAction = withDatabase(directory, action)
|
|
9
|
+
import "./job_configuration"
|
|
10
|
+
alias databaseConfig = readDatabaseConfiguration(directory)
|
|
11
|
+
alias jobRoutes = readJobRoutes(directory)
|
|
5
12
|
|
|
6
13
|
export global Server
|
|
7
14
|
export global Builder
|
|
15
|
+
export global Executor
|
|
16
|
+
export readDatabaseConfiguration(directory)
|
|
17
|
+
export readJobRoutes(directory)
|
|
18
|
+
export withDatabase(directory, action)
|
|
8
19
|
|
|
9
20
|
global Server = ServerClass
|
|
10
21
|
global Builder = BuilderClass
|
|
22
|
+
global Executor = ExecutorClass
|
|
23
|
+
|
|
24
|
+
readDatabaseConfiguration(directory)
|
|
25
|
+
return databaseConfig(directory)
|
|
26
|
+
|
|
27
|
+
readJobRoutes(directory)
|
|
28
|
+
return jobRoutes(directory)
|
|
29
|
+
|
|
30
|
+
withDatabase(directory, action)
|
|
31
|
+
return databaseAction(directory, action)
|
package/src/middleware.lango
CHANGED
|
@@ -3,7 +3,7 @@ import js global RegExp
|
|
|
3
3
|
export middlewareName(name)
|
|
4
4
|
|
|
5
5
|
middlewareName(name)
|
|
6
|
-
reserved = ["routes", "endpoint", "longpoll", "group", "include", "event", "middleware"]
|
|
6
|
+
reserved = ["routes", "endpoint", "longpoll", "job", "group", "include", "event", "middleware"]
|
|
7
7
|
pattern = new RegExp("^[A-Za-z_][A-Za-z0-9_-]*$")
|
|
8
8
|
if !is_string(name) or !pattern.test(name) or reserved.includes(name)
|
|
9
9
|
throw "Invalid or reserved middleware name: ${name}"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import js global Object
|
|
2
|
+
import js global Promise
|
|
3
|
+
|
|
4
|
+
export class ModuleLoader()
|
|
5
|
+
methods
|
|
6
|
+
target(spec)
|
|
7
|
+
|
|
8
|
+
ModuleLoader()
|
|
9
|
+
@modules = Object/create(null)
|
|
10
|
+
|
|
11
|
+
ModuleLoader:target(spec)
|
|
12
|
+
file = spec.module
|
|
13
|
+
if Object/hasOwn(@modules, file)
|
|
14
|
+
return Promise/resolve(exported(@modules[file], spec.handler))
|
|
15
|
+
return chain import_module(file)
|
|
16
|
+
then(mod)
|
|
17
|
+
capture file
|
|
18
|
+
capture spec
|
|
19
|
+
@modules[file] = mod
|
|
20
|
+
return exported(mod, spec.handler)
|
|
21
|
+
|
|
22
|
+
exported(mod, name)
|
|
23
|
+
if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)
|
|
24
|
+
throw "Handler is not exported: ${name}"
|
|
25
|
+
return mod[name]
|
package/src/options.lango
CHANGED
|
@@ -20,6 +20,7 @@ export contains(items, value)
|
|
|
20
20
|
export options(root, supplied = null)
|
|
21
21
|
export deployment(directory)
|
|
22
22
|
export attributes(tag, allowed)
|
|
23
|
+
export readDeployment(directory, credentials = true)
|
|
23
24
|
|
|
24
25
|
attributes(tag, allowed)
|
|
25
26
|
for tag.attributes() as key: value
|
|
@@ -97,6 +98,9 @@ options(root, supplied = null)
|
|
|
97
98
|
return result
|
|
98
99
|
|
|
99
100
|
deployment(directory)
|
|
101
|
+
return options(directory, readDeployment(directory))
|
|
102
|
+
|
|
103
|
+
readDeployment(directory, credentials = true)
|
|
100
104
|
file = path/join(directory, "deploy.xml")
|
|
101
105
|
root = xml2obj(fs/readFileSync(file, "utf8"))
|
|
102
106
|
if root.tag() != "deploy"
|
|
@@ -151,6 +155,8 @@ deployment(directory)
|
|
|
151
155
|
continue
|
|
152
156
|
case "jwt"
|
|
153
157
|
attributes(tag, ["issuer", "audience", "ttlSeconds", "keyEnv", "keysFile", "activeKeyId"])
|
|
158
|
+
if !credentials
|
|
159
|
+
continue
|
|
154
160
|
else
|
|
155
161
|
throw "Unknown deployment section: ${name}"
|
|
156
162
|
for tag.attributes() as key: value
|
|
@@ -171,7 +177,7 @@ deployment(directory)
|
|
|
171
177
|
settings[key] = value == "true"
|
|
172
178
|
else
|
|
173
179
|
settings[key] = Number(value)
|
|
174
|
-
return
|
|
180
|
+
return settings
|
|
175
181
|
|
|
176
182
|
contains(items, value)
|
|
177
183
|
return items.includes(value)
|
|
@@ -13,6 +13,7 @@ export class PostgresAdapter(mod, settings, root)
|
|
|
13
13
|
parameterStyle
|
|
14
14
|
methods
|
|
15
15
|
execute(sql, parameters)
|
|
16
|
+
invoke(spec, input)
|
|
16
17
|
begin()
|
|
17
18
|
commit()
|
|
18
19
|
rollback()
|
|
@@ -34,6 +35,7 @@ PostgresAdapter(mod, settings, root)
|
|
|
34
35
|
entry = resolver.resolve("@lango-project/lango-server")
|
|
35
36
|
@worker = Reflect/construct(mod.Worker, [path/join(path/dirname(entry), "postgres_worker.mjs"), {
|
|
36
37
|
workerData: {
|
|
38
|
+
root: root
|
|
37
39
|
settings: settings
|
|
38
40
|
port: channel.port2
|
|
39
41
|
signal: @signal
|
|
@@ -73,6 +75,8 @@ PostgresAdapter:exchange(task, timeout)
|
|
|
73
75
|
if result.failed
|
|
74
76
|
@broken = result.dead
|
|
75
77
|
error = new Error("PostgreSQL query failed")
|
|
78
|
+
if result.detail != null
|
|
79
|
+
error = new Error("PostgreSQL extension failed: ${result.detail}")
|
|
76
80
|
error.code = result.code
|
|
77
81
|
error.position = result.position
|
|
78
82
|
error.transactionUnusable = @state == "failed"
|
|
@@ -86,6 +90,14 @@ PostgresAdapter:execute(sql, parameters)
|
|
|
86
90
|
parameters: parameters
|
|
87
91
|
}, @timeout)
|
|
88
92
|
|
|
93
|
+
PostgresAdapter:invoke(spec, input)
|
|
94
|
+
result = exchange({
|
|
95
|
+
action: "invoke"
|
|
96
|
+
spec: spec
|
|
97
|
+
input: input
|
|
98
|
+
}, @timeout)
|
|
99
|
+
return result.value
|
|
100
|
+
|
|
89
101
|
PostgresAdapter:begin()
|
|
90
102
|
if @depth == 0 and @state == null
|
|
91
103
|
execute("BEGIN", [])
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import js "pg" as pg
|
|
2
2
|
import js "node:worker_threads" as threads
|
|
3
|
+
import js "node:module" as modules
|
|
4
|
+
import js "node:path" as path
|
|
5
|
+
import "./module_loader"
|
|
6
|
+
class ModuleLoader
|
|
7
|
+
import "./error_details"
|
|
8
|
+
errorDetails(error)
|
|
3
9
|
import js global Atomics
|
|
4
10
|
import js global Math
|
|
5
11
|
import js global Number
|
|
@@ -7,6 +13,7 @@ import js global Promise
|
|
|
7
13
|
|
|
8
14
|
|
|
9
15
|
global state = initialState()
|
|
16
|
+
global loader = new ModuleLoader()
|
|
10
17
|
global installed = install()
|
|
11
18
|
|
|
12
19
|
install()
|
|
@@ -20,12 +27,19 @@ install()
|
|
|
20
27
|
reply(result)
|
|
21
28
|
return null
|
|
22
29
|
catch(error)
|
|
30
|
+
capture task
|
|
23
31
|
global state
|
|
24
|
-
|
|
32
|
+
sqlError = is_string(error.code) and error.code.length == 5
|
|
33
|
+
if state.transaction != null and (task.action != "invoke" or sqlError or state.dead)
|
|
25
34
|
state.transaction = "failed"
|
|
26
|
-
if
|
|
35
|
+
if task.action != "invoke" and !sqlError
|
|
36
|
+
state.dead = true
|
|
37
|
+
if sqlError and error.code.startsWith("08")
|
|
27
38
|
state.dead = true
|
|
28
39
|
position = null
|
|
40
|
+
detail = null
|
|
41
|
+
if task.action == "invoke" and !sqlError
|
|
42
|
+
detail = errorDetails(error)
|
|
29
43
|
if error.position != null
|
|
30
44
|
position = Number/parseInt(error.position, 10)
|
|
31
45
|
reply({
|
|
@@ -33,6 +47,7 @@ install()
|
|
|
33
47
|
dead: state.dead
|
|
34
48
|
code: error.code
|
|
35
49
|
position: position
|
|
50
|
+
detail: detail
|
|
36
51
|
state: state.transaction
|
|
37
52
|
})
|
|
38
53
|
return null
|
|
@@ -80,6 +95,8 @@ execute(task)
|
|
|
80
95
|
case "MERGE"
|
|
81
96
|
changes = result.rowCount
|
|
82
97
|
return success(result.rows, changes)
|
|
98
|
+
case "invoke"
|
|
99
|
+
return invoke(task)
|
|
83
100
|
case "close"
|
|
84
101
|
return chain state.client.end()
|
|
85
102
|
then(ignored)
|
|
@@ -87,7 +104,40 @@ execute(task)
|
|
|
87
104
|
else
|
|
88
105
|
throw "Unknown PostgreSQL operation"
|
|
89
106
|
|
|
90
|
-
|
|
107
|
+
invoke(task)
|
|
108
|
+
global state
|
|
109
|
+
global loader
|
|
110
|
+
spec = task.spec
|
|
111
|
+
if spec == null or !is_string(spec.module) or spec.module.startsWith("node:") or !is_string(spec.handler)
|
|
112
|
+
throw "Database extension requires an application module and exported handler"
|
|
113
|
+
resolver = modules/createRequire(path/join(threads.workerData.root, "package.json"))
|
|
114
|
+
file = resolver.resolve(spec.module)
|
|
115
|
+
if !path/isAbsolute(file)
|
|
116
|
+
throw "Database extension must resolve to an application file"
|
|
117
|
+
return chain loader.target({
|
|
118
|
+
module: file
|
|
119
|
+
handler: spec.handler
|
|
120
|
+
})
|
|
121
|
+
then(handler)
|
|
122
|
+
capture task
|
|
123
|
+
global state
|
|
124
|
+
client = state.client
|
|
125
|
+
executeSql = (sql, parameters)
|
|
126
|
+
capture client
|
|
127
|
+
return chain client.query(sql, parameters)
|
|
128
|
+
catch(error)
|
|
129
|
+
global state
|
|
130
|
+
if state.transaction != null
|
|
131
|
+
state.transaction = "failed"
|
|
132
|
+
throw error
|
|
133
|
+
adapter = {
|
|
134
|
+
executeSql: executeSql
|
|
135
|
+
}
|
|
136
|
+
return handler(adapter, task.input)
|
|
137
|
+
then(value)
|
|
138
|
+
return success([], null, value)
|
|
139
|
+
|
|
140
|
+
success(rows, changes, value = null)
|
|
91
141
|
global state
|
|
92
142
|
return {
|
|
93
143
|
failed: false
|
|
@@ -96,6 +146,7 @@ success(rows, changes)
|
|
|
96
146
|
rows: rows
|
|
97
147
|
changes: changes
|
|
98
148
|
lastInsertRowid: null
|
|
149
|
+
value: value
|
|
99
150
|
}
|
|
100
151
|
|
|
101
152
|
reply(result)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import js "node:module" as modules
|
|
2
|
+
import js "node:path" as path
|
|
3
|
+
|
|
4
|
+
export queueProvider(root, spec)
|
|
5
|
+
|
|
6
|
+
queueProvider(root, spec)
|
|
7
|
+
if spec == null
|
|
8
|
+
return null
|
|
9
|
+
if !is_object(spec) or is_array(spec)
|
|
10
|
+
throw "Queue provider must be a map"
|
|
11
|
+
allowed = ["module", "handler", "options"]
|
|
12
|
+
for spec as key: value
|
|
13
|
+
if !allowed.includes(key)
|
|
14
|
+
throw "Unknown queue provider option: ${key}"
|
|
15
|
+
if !is_string(spec.module) or spec.module == "" or spec.module.startsWith("node:") or !is_string(spec.handler) or spec.handler == ""
|
|
16
|
+
throw "Queue provider requires a module and exported handler"
|
|
17
|
+
settings = spec.options
|
|
18
|
+
if settings == null
|
|
19
|
+
settings = {}
|
|
20
|
+
if !is_object(settings) or is_array(settings)
|
|
21
|
+
throw "Queue provider options must be a map"
|
|
22
|
+
resolver = modules/createRequire(path/join(root, "package.json"))
|
|
23
|
+
file = resolver.resolve(spec.module)
|
|
24
|
+
if !path/isAbsolute(file)
|
|
25
|
+
throw "Queue provider must resolve to an application file"
|
|
26
|
+
return {
|
|
27
|
+
module: file
|
|
28
|
+
handler: spec.handler
|
|
29
|
+
options: from_json(to_json(settings))
|
|
30
|
+
}
|
package/src/routes.lango
CHANGED
|
@@ -19,6 +19,8 @@ import "./longpoll_options"
|
|
|
19
19
|
export class Routes(root)
|
|
20
20
|
methods
|
|
21
21
|
endpoint(spec)
|
|
22
|
+
job(spec)
|
|
23
|
+
jobs()
|
|
22
24
|
addMiddleware(name, spec)
|
|
23
25
|
middlewares()
|
|
24
26
|
longpoll(spec)
|
|
@@ -31,6 +33,7 @@ export class Routes(root)
|
|
|
31
33
|
Routes(root)
|
|
32
34
|
@root = path/resolve(root)
|
|
33
35
|
@items = []
|
|
36
|
+
@jobs = []
|
|
34
37
|
@events = {}
|
|
35
38
|
@middlewares = Object/create(null)
|
|
36
39
|
|
|
@@ -60,6 +63,35 @@ Routes:checkPath(path)
|
|
|
60
63
|
throw "Conflicting endpoint pattern: ${path}"
|
|
61
64
|
return null
|
|
62
65
|
|
|
66
|
+
Routes:job(spec)
|
|
67
|
+
allowed = ["path", "module", "handler", "queue", "policy", "auth", "reauthSeconds"]
|
|
68
|
+
for spec as key: value
|
|
69
|
+
if !allowed.includes(key)
|
|
70
|
+
throw "Unknown job route option: ${key}"
|
|
71
|
+
item = new Endpoint(spec)
|
|
72
|
+
if item.path.includes("*") or item.path.includes("{") or item.path.includes("}")
|
|
73
|
+
throw "Job routes require an exact path"
|
|
74
|
+
if !is_string(spec.queue) or spec.queue.trim() != spec.queue or spec.queue == ""
|
|
75
|
+
throw "Job route requires a queue name"
|
|
76
|
+
for @jobs as other
|
|
77
|
+
if other.path == item.path
|
|
78
|
+
throw "Duplicate job route: ${item.path}"
|
|
79
|
+
@jobs.add({
|
|
80
|
+
path: item.path
|
|
81
|
+
module: resolveModule(item.module, @root)
|
|
82
|
+
handler: item.handler
|
|
83
|
+
queue: spec.queue
|
|
84
|
+
policy: item.policy
|
|
85
|
+
auth: item.auth
|
|
86
|
+
reauthSeconds: item.reauthSeconds
|
|
87
|
+
reauthExplicit: item.reauthExplicit
|
|
88
|
+
legacyAuth: item.legacyAuth
|
|
89
|
+
})
|
|
90
|
+
return @this
|
|
91
|
+
|
|
92
|
+
Routes:jobs()
|
|
93
|
+
return from_json(to_json(@jobs))
|
|
94
|
+
|
|
63
95
|
Routes:addMiddleware(name, spec)
|
|
64
96
|
middlewareName(name)
|
|
65
97
|
if Object/hasOwn(@middlewares, name)
|
|
@@ -183,6 +215,18 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
183
215
|
if reauthSeconds == null
|
|
184
216
|
reauthSeconds = inheritedReauthSeconds
|
|
185
217
|
switch tag.tag()
|
|
218
|
+
case "job"
|
|
219
|
+
attributes(tag, ["path", "module", "handler", "queue", "auth", "policy", "reauthSeconds"])
|
|
220
|
+
leaf(tag)
|
|
221
|
+
job({
|
|
222
|
+
path: joinPath(prefix, spec.path)
|
|
223
|
+
module: resolveModule(mod, directory)
|
|
224
|
+
handler: spec.handler
|
|
225
|
+
queue: spec.queue
|
|
226
|
+
auth: auth
|
|
227
|
+
policy: policy
|
|
228
|
+
reauthSeconds: reauthSeconds
|
|
229
|
+
})
|
|
186
230
|
case "endpoint"
|
|
187
231
|
attributes(tag, ["path", "module", "handler", "auth", "policy", "reauthSeconds"])
|
|
188
232
|
leaf(tag)
|
package/src/runtime.lango
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import js "node:crypto" as crypto
|
|
2
|
+
import "./module_loader"
|
|
3
|
+
class ModuleLoader
|
|
2
4
|
import "./route_info"
|
|
3
5
|
routeInfo(definitions)
|
|
4
6
|
import js global Number
|
|
@@ -53,7 +55,8 @@ Runtime(definition)
|
|
|
53
55
|
@routes = []
|
|
54
56
|
@events = {}
|
|
55
57
|
@tokens = new Tokens(definition.settings)
|
|
56
|
-
@
|
|
58
|
+
@loader = new ModuleLoader()
|
|
59
|
+
@queueFactory = null
|
|
57
60
|
@middlewares = Object/create(null)
|
|
58
61
|
@database = new Database(definition.settings.database, definition.settings.root)
|
|
59
62
|
@clients = null
|
|
@@ -64,6 +67,8 @@ Runtime:preload()
|
|
|
64
67
|
trace("Worker ${threads.threadId}: preparing database, middleware, routes and events")
|
|
65
68
|
definitions = @definition.routes
|
|
66
69
|
pending = @database.open()
|
|
70
|
+
if @definition.queueProvider != null
|
|
71
|
+
pending = prepareQueue(pending)
|
|
67
72
|
if @definition.middlewares != null
|
|
68
73
|
for @definition.middlewares as name: spec
|
|
69
74
|
pending = prepareMiddleware(pending, name, spec)
|
|
@@ -81,6 +86,20 @@ Runtime:close()
|
|
|
81
86
|
@database.close()
|
|
82
87
|
return null
|
|
83
88
|
|
|
89
|
+
Runtime:prepareQueue(pending)
|
|
90
|
+
return chain pending
|
|
91
|
+
then(ignored)
|
|
92
|
+
return @loader.target(@definition.queueProvider)
|
|
93
|
+
then(factory)
|
|
94
|
+
@queueFactory = factory
|
|
95
|
+
return null
|
|
96
|
+
|
|
97
|
+
Runtime:queueSender()
|
|
98
|
+
if @queueFactory == null
|
|
99
|
+
return null
|
|
100
|
+
factory = @queueFactory
|
|
101
|
+
return factory(@definition.queueProvider.options)
|
|
102
|
+
|
|
84
103
|
Runtime:prepareMiddleware(pending, name, spec)
|
|
85
104
|
return chain pending
|
|
86
105
|
then(ignored)
|
|
@@ -144,19 +163,7 @@ Runtime:prepareEvent(pending, name, spec)
|
|
|
144
163
|
Runtime:target(spec)
|
|
145
164
|
file = spec.module
|
|
146
165
|
trace("Worker ${threads.threadId}: resolving ${file} export ${spec.handler}")
|
|
147
|
-
|
|
148
|
-
return Promise/resolve(exported(@modules[file], spec.handler))
|
|
149
|
-
return chain import_module(file)
|
|
150
|
-
then(mod)
|
|
151
|
-
capture file
|
|
152
|
-
capture spec
|
|
153
|
-
@modules[file] = mod
|
|
154
|
-
return exported(mod, spec.handler)
|
|
155
|
-
|
|
156
|
-
exported(mod, name)
|
|
157
|
-
if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)
|
|
158
|
-
throw "Handler is not exported: ${name}"
|
|
159
|
-
return mod[name]
|
|
166
|
+
return @loader.target(spec)
|
|
160
167
|
|
|
161
168
|
Runtime:match(path)
|
|
162
169
|
best = null
|
|
@@ -254,6 +261,15 @@ Runtime:process(task, route, input, claims, fresh, restored)
|
|
|
254
261
|
context log
|
|
255
262
|
context db
|
|
256
263
|
context token = new Token(claims)
|
|
264
|
+
sender = null
|
|
265
|
+
try
|
|
266
|
+
sender = queueSender()
|
|
267
|
+
catch error
|
|
268
|
+
closeScope(db, false)
|
|
269
|
+
closeClientScope()
|
|
270
|
+
log.err("${requestIdentity(request.id, token)} Queue preparation failed: ${errorDetails(error)}")
|
|
271
|
+
return failure(500, "InternalServerError")
|
|
272
|
+
context queue = sender
|
|
257
273
|
trace("${requestIdentity(request.id, token)} Matched route ${route.path}; fresh=${fresh}")
|
|
258
274
|
return chain Promise/resolve(null)
|
|
259
275
|
then(ignored)
|
|
@@ -348,9 +364,7 @@ Runtime:process(task, route, input, claims, fresh, restored)
|
|
|
348
364
|
log.err("${requestIdentity(request.id, token)} Request failed: ${errorDetails(error)}")
|
|
349
365
|
return failure(500, "InternalServerError")
|
|
350
366
|
finally
|
|
351
|
-
|
|
352
|
-
if clientIdentity != null
|
|
353
|
-
clientIdentity.closed = true
|
|
367
|
+
closeClientScope()
|
|
354
368
|
closeScope(db, false)
|
|
355
369
|
|
|
356
370
|
Runtime:tokenClaims(token, claims, allowRenewal)
|
|
@@ -475,6 +489,14 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
475
489
|
context log
|
|
476
490
|
context token = new Token(claims, true)
|
|
477
491
|
context response = Object/freeze({})
|
|
492
|
+
sender = null
|
|
493
|
+
try
|
|
494
|
+
sender = queueSender()
|
|
495
|
+
catch error
|
|
496
|
+
closeScope(db, false)
|
|
497
|
+
log.err("${requestIdentity(id, token)} Request event queue preparation failed: ${errorDetails(error)}")
|
|
498
|
+
return result
|
|
499
|
+
context queue = sender
|
|
478
500
|
metadata = {
|
|
479
501
|
input: input
|
|
480
502
|
output: output
|
|
@@ -529,6 +551,12 @@ closeScope(db, strict)
|
|
|
529
551
|
throw "UnclosedTransaction"
|
|
530
552
|
return null
|
|
531
553
|
|
|
554
|
+
closeClientScope()
|
|
555
|
+
context clientIdentity
|
|
556
|
+
if clientIdentity != null
|
|
557
|
+
clientIdentity.closed = true
|
|
558
|
+
return null
|
|
559
|
+
|
|
532
560
|
needLogin(route, status, reason)
|
|
533
561
|
context request
|
|
534
562
|
context token
|
package/src/server.lango
CHANGED
|
@@ -51,6 +51,7 @@ Server(path, definition = null)
|
|
|
51
51
|
definition = {
|
|
52
52
|
settings: deployment(directory)
|
|
53
53
|
routes: routes.definitions()
|
|
54
|
+
jobs: routes.jobs()
|
|
54
55
|
events: routes.events()
|
|
55
56
|
middlewares: routes.middlewares()
|
|
56
57
|
}
|
|
@@ -59,6 +60,8 @@ Server(path, definition = null)
|
|
|
59
60
|
@definition = from_json(to_json(definition))
|
|
60
61
|
settings = @definition.settings
|
|
61
62
|
resolvePolicies(@definition.routes, settings.policies)
|
|
63
|
+
if @definition.jobs != null
|
|
64
|
+
resolvePolicies(@definition.jobs, settings.policies)
|
|
62
65
|
@broker = new LongpollBroker(@definition.settings, @definition.routes)
|
|
63
66
|
@polls = Object/create(null)
|
|
64
67
|
@state = "new"
|