@rpcbase/server 0.328.0 → 0.329.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/boot/worker.js +4 -0
- package/package.json +1 -1
- package/queue/register_queue_listener.js +42 -23
package/boot/worker.js
CHANGED
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* @flow */
|
|
2
|
+
const assert = require("assert")
|
|
2
3
|
const debug = require("debug")
|
|
3
4
|
const _get = require("lodash/get")
|
|
4
5
|
const _set = require("lodash/set")
|
|
@@ -11,10 +12,14 @@ const dispatch_queue = require("./dispatch_queue")
|
|
|
11
12
|
|
|
12
13
|
const log = debug("rb:queue:listener")
|
|
13
14
|
|
|
15
|
+
const {RB_APP_NAME} = process.env
|
|
16
|
+
|
|
17
|
+
const RETRY_MAXIMUM_DELAY = 2000
|
|
14
18
|
const RETRY_MINIMUM_DELAY = 10
|
|
15
|
-
const MAX_RETRIES =
|
|
19
|
+
const MAX_RETRIES = 64
|
|
20
|
+
|
|
16
21
|
// TODO: reset retry counters when connection becomes healthy again?
|
|
17
|
-
|
|
22
|
+
let retry_counter = 0
|
|
18
23
|
|
|
19
24
|
|
|
20
25
|
// Listens for mongodb change events,
|
|
@@ -31,58 +36,75 @@ const mongoose_delete_plugin = (schema) => {
|
|
|
31
36
|
schema.post("findOneAndDelete", function(doc) {
|
|
32
37
|
log("queue:findOneAndDelete", "dispatch_doc_change NYI")
|
|
33
38
|
log("DEL PLUGIN", doc)
|
|
34
|
-
const
|
|
39
|
+
const coll_name = change.ns.coll
|
|
40
|
+
|
|
41
|
+
const model_name = Object.keys(mongoose.models).find((k) => {
|
|
42
|
+
return mongoose.models[k].collection.collectionName === coll_name
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
assert(model_name, `unable to resolve model name for coll_name:${coll_name}`)
|
|
46
|
+
|
|
35
47
|
dispatch_queue(queue, model_name, "delete", doc)
|
|
36
48
|
})
|
|
37
49
|
}
|
|
38
50
|
|
|
39
51
|
|
|
40
|
-
const
|
|
52
|
+
const dispatch_change_handler = (change) => {
|
|
53
|
+
// skip if change is on another db
|
|
54
|
+
if (change?.ns?.db !== RB_APP_NAME) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
41
58
|
const op = change.operationType
|
|
42
59
|
if (["insert", "update"].includes(op)) {
|
|
60
|
+
const coll_name = change.ns.coll
|
|
61
|
+
|
|
62
|
+
const model_name = Object.keys(mongoose.models).find((k) => {
|
|
63
|
+
return mongoose.models[k].collection.collectionName === coll_name
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
assert(model_name, `unable to resolve model name for coll_name:${coll_name}`)
|
|
67
|
+
|
|
43
68
|
dispatch_queue(queue, model_name, op, change.fullDocument, change.updateDescription)
|
|
44
69
|
}
|
|
45
70
|
}
|
|
46
71
|
|
|
47
72
|
|
|
48
|
-
const
|
|
49
|
-
log("registering emitter
|
|
73
|
+
const register_db_emitter = () => {
|
|
74
|
+
log("registering db emitter")
|
|
50
75
|
|
|
51
|
-
const model = mongoose.model(model_name)
|
|
52
76
|
// TODO: implement delete operation fullDocument retrieve,
|
|
53
77
|
// when this is released https://jira.mongodb.org/browse/SERVER-36941
|
|
54
78
|
// this is done via a plugin right now
|
|
55
79
|
// https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ChangeStreamOptions.html
|
|
56
|
-
const emitter =
|
|
80
|
+
const emitter = mongoose.connection.watch({fullDocument: "updateLookup"})
|
|
57
81
|
|
|
58
|
-
emitter.on("change",
|
|
82
|
+
emitter.on("change", dispatch_change_handler)
|
|
59
83
|
|
|
60
84
|
emitter.on("error", (err) => {
|
|
61
|
-
log("server:queue:register_queue_listener: change listener emitter got error", err)
|
|
85
|
+
console.log("server:queue:register_queue_listener: change listener emitter got error", err)
|
|
62
86
|
})
|
|
63
87
|
|
|
64
88
|
emitter.on("close", (arg, arg2) => {
|
|
65
|
-
// console.log("close", arg, arg2)
|
|
66
89
|
// TODO: investigate why we disconnect here and can't reconnect easily
|
|
67
|
-
|
|
68
|
-
_set(retry_counters, model_name, current_retry + 1)
|
|
90
|
+
retry_counter++
|
|
69
91
|
|
|
70
|
-
if (
|
|
71
|
-
console.log("queue listener reached max retries
|
|
92
|
+
if (retry_counter > MAX_RETRIES) {
|
|
93
|
+
console.log("queue listener reached max retries, exiting with failure")
|
|
72
94
|
process.exit(1)
|
|
73
95
|
return
|
|
74
96
|
}
|
|
75
97
|
|
|
76
|
-
const timeout_amount = RETRY_MINIMUM_DELAY + 10 * Math.pow(2,
|
|
77
|
-
console.log("queue_listener: emitter closed,
|
|
98
|
+
const timeout_amount = Math.min(RETRY_MAXIMUM_DELAY, RETRY_MINIMUM_DELAY + 10 * Math.pow(2, retry_counter))
|
|
99
|
+
console.log("queue_listener: emitter closed, retrying in", timeout_amount)
|
|
78
100
|
|
|
79
101
|
setTimeout(() => {
|
|
80
|
-
|
|
102
|
+
register_db_emitter()
|
|
81
103
|
}, timeout_amount)
|
|
82
104
|
})
|
|
83
105
|
|
|
84
106
|
emitter.on("end", (arg) => {
|
|
85
|
-
console.log("
|
|
107
|
+
console.log("EMITTER ENDED", arg)
|
|
86
108
|
})
|
|
87
109
|
}
|
|
88
110
|
|
|
@@ -93,10 +115,7 @@ const register_queue_listener = () => {
|
|
|
93
115
|
mongoose.plugin(mongoose_delete_plugin)
|
|
94
116
|
|
|
95
117
|
mongoose.connection.once("open", () => {
|
|
96
|
-
|
|
97
|
-
const models = mongoose.modelNames()
|
|
98
|
-
models.forEach((model_name) => register_model_emitter(model_name))
|
|
99
|
-
})
|
|
118
|
+
register_db_emitter()
|
|
100
119
|
})
|
|
101
120
|
}
|
|
102
121
|
|