@rpcbase/server 0.171.0 → 0.173.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/package.json +8 -8
- package/queue/register_queue_listener.js +37 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.173.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
"test": "echo \"Error: no test specified\" && exit 0"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@rpcbase/agent": "0.
|
|
14
|
-
"@rpcbase/std": "0.
|
|
15
|
-
"body-parser": "1.20.
|
|
16
|
-
"bull": "4.10.
|
|
13
|
+
"@rpcbase/agent": "0.30.0",
|
|
14
|
+
"@rpcbase/std": "0.6.0",
|
|
15
|
+
"body-parser": "1.20.2",
|
|
16
|
+
"bull": "4.10.4",
|
|
17
17
|
"connect-redis": "6.1.3",
|
|
18
18
|
"cors": "2.8.5",
|
|
19
19
|
"debug": "4.3.4",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"express-session": "1.17.3",
|
|
23
23
|
"glob": "8.1.0",
|
|
24
24
|
"lodash": "4.17.21",
|
|
25
|
-
"mkdirp": "1.
|
|
26
|
-
"mongoose": "6.
|
|
25
|
+
"mkdirp": "2.1.3",
|
|
26
|
+
"mongoose": "6.10.0",
|
|
27
27
|
"picocolors": "1.0.0",
|
|
28
28
|
"postmark": "3.0.15",
|
|
29
29
|
"redis": "4.6.4",
|
|
30
30
|
"request-ip": "3.3.0",
|
|
31
31
|
"validator": "13.9.0",
|
|
32
|
-
"yargs": "17.
|
|
32
|
+
"yargs": "17.7.1"
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
const debug = require("debug")
|
|
3
|
+
const _get = require("lodash/get")
|
|
4
|
+
const _set = require("lodash/set")
|
|
3
5
|
|
|
4
6
|
const mongoose = require("../mongoose")
|
|
5
7
|
const queue = require("./index")
|
|
@@ -9,19 +11,25 @@ const dispatch_queue = require("./dispatch_queue")
|
|
|
9
11
|
|
|
10
12
|
const log = debug("rb:queue:listener")
|
|
11
13
|
|
|
14
|
+
const RETRY_MINIMUM_DELAY = 10
|
|
15
|
+
const MAX_RETRIES = 16
|
|
16
|
+
const retry_counters = {}
|
|
17
|
+
// TODO: reset retry counters when connection becomes healthy again
|
|
18
|
+
|
|
19
|
+
|
|
12
20
|
// Listens for mongodb change events,
|
|
13
21
|
// when a document is created, updated or deleted, dispatch job message
|
|
14
22
|
|
|
15
23
|
// mongoose middleware used for delete events
|
|
16
24
|
const mongoose_delete_plugin = (schema) => {
|
|
17
25
|
schema.pre("deleteOne", function(next) {
|
|
18
|
-
throw new Error("rts
|
|
26
|
+
throw new Error("rts:deleteOne is deprecated, use findOneAndDelete")
|
|
19
27
|
})
|
|
20
28
|
|
|
21
29
|
// TODO: add other delete operations
|
|
22
30
|
// https://mongoosejs.com/docs/queries.html
|
|
23
31
|
schema.post("findOneAndDelete", function(doc) {
|
|
24
|
-
log("queue
|
|
32
|
+
log("queue:findOneAndDelete", "dispatch_doc_change NYI")
|
|
25
33
|
log("DEL PLUGIN", doc)
|
|
26
34
|
const model_name = this.model.modelName
|
|
27
35
|
dispatch_queue(queue, model_name, "delete", doc)
|
|
@@ -50,24 +58,42 @@ const get_dispatch_change_handler = (model_name, filter_fn) => (change) => {
|
|
|
50
58
|
|
|
51
59
|
const register_model_emitter = (model_name, filter_fn) => {
|
|
52
60
|
log("registering emitter for", model_name)
|
|
61
|
+
|
|
53
62
|
const model = mongoose.model(model_name)
|
|
54
63
|
// TODO: implement delete operation fullDocument retrieve,
|
|
55
64
|
// when this is released https://jira.mongodb.org/browse/SERVER-36941
|
|
56
65
|
// this is done via a plugin right now
|
|
66
|
+
// https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ChangeStreamOptions.html
|
|
57
67
|
const emitter = model.watch({fullDocument: "updateLookup"})
|
|
58
68
|
|
|
59
69
|
emitter.on("change", get_dispatch_change_handler(model_name, filter_fn))
|
|
60
70
|
|
|
61
71
|
emitter.on("error", (err) => {
|
|
62
|
-
log("server
|
|
72
|
+
log("server:queue:register_queue_listener: change listener emitter got error", err)
|
|
63
73
|
})
|
|
64
74
|
|
|
65
75
|
emitter.on("close", (arg, arg2) => {
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
// console.log("close", arg, arg2)
|
|
77
|
+
// TODO: investigate why we disconnect here and can't reconnect easily
|
|
78
|
+
const current_retry = _get(retry_counters, model_name, 0)
|
|
79
|
+
_set(retry_counters, model_name, current_retry + 1)
|
|
80
|
+
|
|
81
|
+
if (current_retry > MAX_RETRIES) {
|
|
82
|
+
console.log("queue listener reached max retries for:", model_name, "exiting with failure")
|
|
83
|
+
process.exit(1)
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const timeout = RETRY_MINIMUM_DELAY + 10 * Math.pow(2, current_retry)
|
|
88
|
+
console.log("queue_listener: emitter closed, model:", model_name, "retrying in", timeout)
|
|
89
|
+
|
|
90
|
+
setTimeout(() => {
|
|
69
91
|
register_model_emitter(model_name, filter_fn)
|
|
70
|
-
})
|
|
92
|
+
}, timeout)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
emitter.on("end", (arg) => {
|
|
96
|
+
console.log("emitter ENDED", arg)
|
|
71
97
|
})
|
|
72
98
|
}
|
|
73
99
|
|
|
@@ -78,9 +104,10 @@ const register_queue_listener = (filter_fn) => {
|
|
|
78
104
|
mongoose.plugin(mongoose_delete_plugin)
|
|
79
105
|
|
|
80
106
|
mongoose.connection.once("open", () => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
107
|
+
process.nextTick(() => {
|
|
108
|
+
const models = mongoose.modelNames()
|
|
109
|
+
models.forEach((model_name) => register_model_emitter(model_name, filter_fn))
|
|
110
|
+
})
|
|
84
111
|
})
|
|
85
112
|
}
|
|
86
113
|
|