This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
forked from qinbingchen/SRFMail_Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (87 loc) · 2.49 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
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
var cluster = require('cluster');
if(cluster.isMaster) {
var i;
var os = require('os');
var ThreadNum = os.cpus.length > 3 ? os.cpus.length : 3;
var Threads = [];
var Log = require('./lib/log')('[APP]');
for(i = 0; i < ThreadNum; i++) {
var worker = cluster.fork();
Threads.push(worker);
}
Threads[0].send('receive');
Threads[1].send('send');
setAutoRestart(Threads[0], 'receive');
setAutoRestart(Threads[1], 'send');
for(i = 2; i < ThreadNum; i++) {
Threads[i].send('web');
Threads[i].on('message', function(msg) {
if(msg == 'Restart Mail') {
restartMail();
}
});
setAutoRestart(Threads[i], 'web');
}
function setAutoRestart(worker, message) {
worker.on('exit', function (code, signal) {
if (signal) {
Log.e("worker was killed by signal: " + signal);
} else if (code !== 0) {
Log.e("worker exited with error code: " + code);
} else {
Log.d("worker success!");
}
var index = Threads.indexOf(worker);
worker = cluster.fork();
Threads[index] = worker;
setAutoRestart(worker);
worker.send(message);
if(message == 'web') {
worker.on('message', function(msg) {
if(msg == 'Restart Mail') {
restartMail();
}
});
}
});
}
function restartMail() {
Threads[0].kill();
Threads[1].kill();
}
} else {
var mongoose = require('mongoose');
var settings = require('./settings');
mongoose.connect(settings.mongodb, {
db: {
native_parser: true
},
server: {
poolSize: 10,
socketOptions: {
keepAlive: 1
}
},
replset: {
socketOptions: {
keepAlive: 1
}
}
});
process.on('message', function(msg) {
switch(msg) {
case 'web':
require('./server/web').start();
break;
case 'receive':
require('./server/receive').start();
break;
case 'send':
require('./server/send').start();
break;
case 'socket':
require('./server/socket').start();
break;
}
})
}