@rpcbase/server 0.78.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 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
@@ -29,7 +29,7 @@ module.exports = () => {
29
29
  res.set("Cache-Control", "no-store")
30
30
  next()
31
31
  })
32
- console.log("\nwowowowow\nwowowoowo\nlfdkfdskfjlkfjdskfjds\n\n")
32
+
33
33
  // CORS
34
34
  const cors_origins = is_production ?
35
35
  // https://stackoverflow.com/questions/14003332/access-control-allow-origin-wildcard-subdomains-ports-and-protocols
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.78.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",
@@ -21,10 +22,10 @@
21
22
  "express-session": "1.17.3",
22
23
  "glob": "8.0.3",
23
24
  "lodash": "4.17.21",
24
- "mongoose": "6.4.0",
25
+ "mongoose": "6.4.3",
25
26
  "picocolors": "1.0.0",
26
27
  "postmark": "3.0.12",
27
- "redis": "4.1.0",
28
+ "redis": "4.2.0",
28
29
  "validator": "13.7.0",
29
30
  "yargs": "17.5.1"
30
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
+ }