-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.js
More file actions
284 lines (261 loc) · 8.2 KB
/
manager.js
File metadata and controls
284 lines (261 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict';
let _EventEmitter = require('events').EventEmitter;
let _Setting = require(base_path+'/enum/setting');
class manager extends _EventEmitter
{
/**
*
* @param {*} redis
*/
constructor(redis)
{
super();
this.redis = redis;
// this.initCid();
this.subscribeCid();
this.initIMQueue();
}
/*
* After sending the message to queue
* This manager shall be subscribing to the publish event from the message queue {rsmq.ns}:rt:{qname} eg. rsmq:rt:testQueue 6
* Every cluster will receive this event.
* We pop the message. In theory only 1 cluster will be able to receive the message
* Lets try
*
*
*/
initIMQueue() {
//We need to create queue first before we can send/get messages
var internalMessageQueue = _Setting.INTERNA_MESSAGE_QUEUE;
this.redis.createMessageQueue(_Setting.INTERNA_MESSAGE_QUEUE, -1, null);
this.redis.imSub.subscribe(internalMessageQueue);
this.redis.imSub.on('message', (channel, message) => {
// _.log("Process '" + process.pid + " received " + message);
/**
* convert message with JSON format.
* get Cid Size on redis
* after pop message with correct cid on cluster
* cluster save Counter on redis
*/
message = JSON.parse(message);
var processCounter = message['sendTime'];
// _.log(`processCounter : ${processCounter}`);
this.getCidSize((cidLength)=>{
this.popMessage(cidLength, processCounter, internalMessageQueue, (packet)=>{
this.handleInternalMessage(packet);
});
})
});
}
/**
*
* @param {*} packet
*/
handleInternalMessage(packet) {
// let controllerName = packet.topic;
// let command = packet.type;
// let data = packet.data;
// _.log("handleInternalMessage: " + controllerName + "_" + command);
// let controller = this.getController(controllerName);
// if (!utils.checkUndefined(controller)) {
// controller.handleInternalCommand(command, data);
// }
}
/**
* subscribe channel with Cid and message event handle.
*/
subscribeCid()
{
/**
* subscrible CID_CHANNEL
* received message with SystemSub
* if message is delete
* call updateCid to re-init this cluster 's cid
*/
this.redis.subClient.subscribe(_Setting.CID_CHANNEL); //listening event from internal messages
this.redis.subClient.on('message', (channel, message)=>{
console.log("Message '" + message + "' on channel '" + channel + "' arrived!");
var msgObj = JSON.parse(message);
//the channels that we are going to do work
switch (msgObj.event) {
case 'delete':
this.updateCid();
break;
}
});
}
/**
* update the process with cid
*/
updateCid()
{
/**
* delete cid Number for process environment
* check process is dead.
* if No, re-init Cid Number.
* if Yes, mothing need to do.
*/
delete process.env.cid;
if(process.env.IS_ACTIVE != '0')
{
this.initCid();
}
}
/**
* incr cid with modelRedis
*/
initCid()
{
return new Promise((resolve)=>{
this.redis.incrCid(_Setting.CID_KEY, (reply)=>{
if(!process.env.cid)
{
var cid = reply - 1;
console.log(`cid : ${cid}`);
process.env.cid = cid;
}
resolve();
});
})
}
/**
* server Init
*/
serverInit()
{
this.initCid().then(()=>{
this.emit('settings_loaded');
})
}
/**
* delete cid is registered on redis with Promise
*/
deleteCid()
{
return new Promise((resolve)=>{
this.redis.deleteData(_Setting.CID_KEY, ()=>{
resolve();
});
});
}
/**
* publish event to other cluster is sub on redis with Promise
*/
publishEvent()
{
return new Promise((resolve)=>{
var data = { event : 'delete' };
this.redis.publishEvent(_Setting.CID_CHANNEL, data, ()=>{
resolve();
});
});
}
/**
* exitProcess with delete Cid and publishEvent
*/
exitProcess()
{
/**
* make the process is dead with environment.
* call deleteCid and publishEvent with same time
* after process exit
*/
process.env.IS_ACTIVE = 0;
var promiseExitArray = [];
promiseExitArray.push(this.deleteCid());
promiseExitArray.push(this.publishEvent());
Promise.all(promiseExitArray).then(() => {
process.exit();
});
}
/**
* check the process is Active
* @param {Number} cidLength the length of cluster save on redis
* @param {Number} processCounter Counter with Random Number for which cluster send messages
*/
processInActive(cidLength, processCounter)
{
/**
* get cid Number for process environment
* check cidLength undefined and cid undefined
* if which is undefined, return false
* if cid equals (processCounter Mod cidLength)
* if yes, return true
* if No, return false
*/
var cid = Number(process.env.cid);
if(cidLength && !utils.checkUndefined(cid)){
var pmId = processCounter % cidLength;
return pmId === cid;
}
return false;
}
/**
* PopMessage with modelRedis
* @param {Number} cidLength the length of cluster save on redis
* @param {Number} processCounter Counter with Random Number for which cluster send messages
* @param {String} queue queue of channel name
* @param {Function} callback back to which functions call this functions
*/
popMessage(cidLength, processCounter, queue, callback)
{
/**
* checkProcess is correct with mod cluster Length
* if Yes, Pop messages on RSMQ with channel
* after callback
*/
if(this.processInActive(cidLength, processCounter))
{
this.redis.popMessageFromQueue(queue, (queueName, resp) => {
var packet = undefined;
if (resp.message) {
packet = JSON.parse(resp.message);
}
if(callback){
callback(packet);
}
});
}
}
/**
* send many internal message
* @param {object} messageList message object
*/
sendMultiInternalMessage(messageList) {
_.log("Worker Process: " + process.env.cid + " Receives sendMultiInternalMessage event");
var size = messageList.length;
this.redis.saveData(Setting.INTERNAL_SAVE_QUEUE, size.toString(), null);
// for (var message of messageList)
// {
// var targetMsg = {topic: message.controllerName, type: message.command, data: message.data};
// this.redis.sendInternalMessageToQueue(JSON.stringify(targetMsg), function() { });
// }
}
/**
* @param {Function} callback back to which functions call this functions
* get Cid Size for redis
*/
getCidSize(callback)
{
/**
* get Cid Size for redis
* if Cid Size != undefined
* convert Cid Size to Number
* if Cid Size = undefined
* set Cid Size = 0
* return Cid Size
*/
this.redis.getCidSize(_Setting.CID_KEY, (cidLength)=>{
if(cidLength != undefined)
{
cidLength = Number(cidLength);
}
else
{
cidLength = 0;
}
callback(cidLength);
})
}
}
module.exports = manager;