-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratorMode.js
More file actions
56 lines (50 loc) · 1.79 KB
/
generatorMode.js
File metadata and controls
56 lines (50 loc) · 1.79 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
/**
* Created by vd on 22.11.2017.
*/
const config = require('./config'),
ENV = process.env.NODE_ENV || "dev";
const lorem = require("lorem-ipsum"),
loremDefaults = {count: 3, units: "words"};
const redis = require("redis"),
client = redis.createClient(config.redisPort[ENV]);
const errorHandler = require("./errorHandler");
let onQuitGenCb = function () {
};
/**
* Start Generator Mode,
* @param {number} clientId
* @param {function} onQuitGeneratorMode - function If Generator already exists call
*/
function start(clientId, onQuitGeneratorMode) {
if (onQuitGeneratorMode) onQuitGenCb = onQuitGeneratorMode;
// Immediate set message generator to prevent duplicate generators
client.multi()
.get("message-generator")
.set("message-generator", clientId, "EX", Math.round(config.keepAliveTimeout[ENV] / 1000))
.publish(config.channelName[ENV], lorem(loremDefaults))
.exec((err, reply) => {
// if we already have generator, do nothing
if (reply[0] && reply[0] !== clientId) {
client.discard();
clearTimeout(timerId);
quit();
}
if (err) return errorHandler(err);
});
// Send message's with verified time
let timerId = setTimeout(function tick() {
// Save latest message generated
client.multi()
.set("message-generator", clientId, "EX", Math.round(config.keepAliveTimeout[ENV] / 1000))
.publish(config.channelName[ENV], lorem(loremDefaults))
.exec((err) => {
if(err) return errorHandler(err)
});
timerId = setTimeout(tick, config.messagePerMs[ENV]);
}, config.messagePerMs[ENV]);
}
function quit() {
client.quit();
onQuitGenCb()
}
exports.start = start;