@rpcbase/server 0.76.0 → 0.79.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/run_native.js +2 -0
- package/express/index.js +1 -0
- package/index.js +2 -0
- package/mailer/index.js +1 -1
- package/package.json +6 -4
- package/queue.js +65 -0
package/cli/run_native.js
CHANGED
|
@@ -40,6 +40,8 @@ const init_processes = (args) => {
|
|
|
40
40
|
const worker_queue_dir = path.join(process.cwd(), "../infrastructure/data/worker-queue/data")
|
|
41
41
|
const worker_queue_logs_path = path.join(process.cwd(), "../infrastructure/data/worker-queue/log/logs.txt")
|
|
42
42
|
|
|
43
|
+
console.log("WORKER QUEUE ON PORT", env.WORKER_QUEUE_PORT)
|
|
44
|
+
|
|
43
45
|
// TODO: add a redis cache
|
|
44
46
|
const processes = [
|
|
45
47
|
// web server
|
package/express/index.js
CHANGED
|
@@ -43,6 +43,7 @@ module.exports = () => {
|
|
|
43
43
|
// local dev origins
|
|
44
44
|
[
|
|
45
45
|
`http://localhost:${CLIENT_PORT}`,
|
|
46
|
+
`http://admin.localhost:${CLIENT_PORT}`,
|
|
46
47
|
// WARNING: hardcoded port
|
|
47
48
|
"http://localhost:8090", // TMP: used by inspected app from admin
|
|
48
49
|
"http://localhost:9292", // TMP
|
package/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
const database = require("./database")
|
|
3
|
+
const queue = require("./queue")
|
|
3
4
|
const express = require("./express")
|
|
4
5
|
const client_router = require("./src/client/client_router")
|
|
5
6
|
const rpc_router = require("./src/rpc/rpc_router")
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
9
|
+
queue,
|
|
8
10
|
database,
|
|
9
11
|
express,
|
|
10
12
|
client_router,
|
package/mailer/index.js
CHANGED
|
@@ -8,7 +8,7 @@ console.warn("email sender forcing is production")
|
|
|
8
8
|
|
|
9
9
|
let client
|
|
10
10
|
|
|
11
|
-
if (is_production) {
|
|
11
|
+
if (is_production && typeof POSTMARK_API_KEY === "string" && POSTMARK_API_KEY.trim() !== "") {
|
|
12
12
|
client = new postmark.ServerClient(POSTMARK_API_KEY)
|
|
13
13
|
} else {
|
|
14
14
|
client = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.79.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"@rpcbase/agent": "0.9.0",
|
|
14
14
|
"@rpcbase/std": "0.3.0",
|
|
15
15
|
"body-parser": "1.20.0",
|
|
16
|
+
"bull": "4.8.4",
|
|
16
17
|
"connect-redis": "6.1.3",
|
|
17
18
|
"cors": "2.8.5",
|
|
18
19
|
"debug": "4.3.4",
|
|
@@ -20,10 +21,11 @@
|
|
|
20
21
|
"express": "4.18.1",
|
|
21
22
|
"express-session": "1.17.3",
|
|
22
23
|
"glob": "8.0.3",
|
|
23
|
-
"
|
|
24
|
+
"lodash": "4.17.21",
|
|
25
|
+
"mongoose": "6.4.3",
|
|
24
26
|
"picocolors": "1.0.0",
|
|
25
|
-
"postmark": "3.0.
|
|
26
|
-
"redis": "4.
|
|
27
|
+
"postmark": "3.0.12",
|
|
28
|
+
"redis": "4.2.0",
|
|
27
29
|
"validator": "13.7.0",
|
|
28
30
|
"yargs": "17.5.1"
|
|
29
31
|
}
|
package/queue.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const Queue = require("bull")
|
|
3
|
+
|
|
4
|
+
const {WORKER_QUEUE_PORT, CONTAINER_MODE} = process.env
|
|
5
|
+
const hostname = CONTAINER_MODE === "native" ? "localhost" : "worker-queue"
|
|
6
|
+
const worker_queue_url = `redis://${hostname}:${WORKER_QUEUE_PORT}`
|
|
7
|
+
|
|
8
|
+
console.log("using worker-queue url", worker_queue_url)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const tasks_list = Object.create(null)
|
|
12
|
+
|
|
13
|
+
let worker_queue
|
|
14
|
+
|
|
15
|
+
const worker_add = async(task_name, payload, options) => {
|
|
16
|
+
const res = await worker_queue.add({task_name, payload}, options)
|
|
17
|
+
// paymentsQueue.add(paymentsData, { repeat: { cron: '15 3 * * *' } });
|
|
18
|
+
console.log("created task:", task_name, res.id)
|
|
19
|
+
return res
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const register_task = (name, fn) => {
|
|
24
|
+
// append module to tasks list
|
|
25
|
+
tasks_list[name] = fn
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const start_worker = async() => {
|
|
29
|
+
worker_queue = new Queue("worker-queue", worker_queue_url)
|
|
30
|
+
|
|
31
|
+
// error handler
|
|
32
|
+
worker_queue.on("error", (err) => {
|
|
33
|
+
console.log(`A queue error happened: ${err.message}`)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// on stall
|
|
37
|
+
worker_queue.on("stalled", (id) => {
|
|
38
|
+
console.log(`Job ${id} stalled and will be reprocessed`)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
worker_queue.on("job failed", (id, err) => {
|
|
42
|
+
console.log(`Job ${id} failed with error ${err.message}`)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// processor function
|
|
46
|
+
worker_queue.process(async(job) => {
|
|
47
|
+
try {
|
|
48
|
+
const {task_name, payload} = job.data
|
|
49
|
+
console.log(`start job ${job.id} ${task_name}`)
|
|
50
|
+
// await Pomise.delay(2000)
|
|
51
|
+
const task_fn = tasks_list[task_name]
|
|
52
|
+
const res = await task_fn(payload)
|
|
53
|
+
return res
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.log("ERRRR", err)
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
start: start_worker,
|
|
63
|
+
register_task,
|
|
64
|
+
add: worker_add,
|
|
65
|
+
}
|