This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (45 loc) · 1.5 KB
/
app.js
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
import {mongoConnect} from './setup/database';
import {brokerConnect} from './setup/broker'
import * as os from 'os';
import {consume} from './setup/consume';
import {checkState} from './setup/populate_messages';
const cluster = require('cluster');
async function connect() {
try{
let database = await mongoConnect();
let connection = await brokerConnect();
exports.database = database;
exports.connection = connection;
/** listen incoming message : **/
try {
let listen = await consume();
listen.channel.consume(listen.queue, async msg => {
console.log(msg.content.toString());
// process data here
let response = await checkState();
listen.channel.sendToQueue(msg.properties.replyTo, new Buffer(JSON.stringify(response)), {
correlationId: msg.properties.correlationId,
type: msg.properties.type
});
}, {noAck: true});
}catch (err){
console.log(err);
//TODO: reconnect or whateva
}
}catch (err){
console.log(err);
}
}
if (cluster.isMaster) {
let cpuCount = os.cpus().length;
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
cluster.on('exit', worker => {
console.log('\nWorker %d died :(', worker.id);
cluster.fork();
});
}else {
connect();
console.log('\nWorker %d running!', cluster.worker.id);
}