-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBulkWebMailCluster.js
269 lines (233 loc) · 10.5 KB
/
BulkWebMailCluster.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
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
const cluster = require('cluster')
const logger = require('./logger')
var mailCfg = require('./mailcfg.json')
const BulkWebMailMana = require('./BulkWebMailMana')
const BackendServer = require('./BackendServer')
const fs = require('fs')
class BulkWebMailCluster {
static messageCmd = {
messageCmdOutSuccReq: 1,
messageCmdOutSuccRes: 2,
messageCmdSenderReq: 3,
messageCmdSenderRes: 4,
messageCmdLeftSendReq:5,
messageCmdLeftSendRes:6
}
constructor(maxWorker = 0) {
this.workers = []
this.waitList = []
this.mailWorker = null
this.maxWorker = maxWorker
}
sleep(time = 0) {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, time)
})
}
setupBulkWebMailWorkerProcess() {
let numCores = require('os').cpus().length
let that = this;
that.maxWorker = mailCfg.nodeCluster.maxCore
if (that.maxWorker > numCores || that.maxWorker == 0) {
that.maxWorker = numCores;
}
logger.info('Master cluster setting up ' + that.maxWorker + ' workers')
let eachSendCount = parseInt(mailCfg.mailSenderCount /that.maxWorker)
let eachRecvCount = parseInt(mailCfg.mailReceiverCount/that.maxWorker)
for (let ii = 0; ii < that.maxWorker; ii++) {
let worker = {}
let mailCfgAss = {}
Object.assign(mailCfgAss, mailCfg)
worker.worker = cluster.fork()
mailCfgAss.mailSenderStart = eachSendCount * ii + mailCfg.mailSenderStart
mailCfgAss.mailSenderCount = eachSendCount
mailCfgAss.mailReceiverStart = eachRecvCount * ii + mailCfg.mailReceiverStart
mailCfgAss.mailReceiverCount = eachRecvCount
if (ii == that.maxWorker -1 ) {
mailCfgAss.mailReceiverCount = mailCfg.mailReceiverCount - eachRecvCount * (ii)
}
worker.mailCfg = mailCfgAss
this.workers.push(worker)
}
let backServ = new BackendServer(mailCfg.backServerPort);
backServ.setGetSuccessFun(async ()=> {
that.waitList = []
let waitCount = 0;
const maxWait = 20;
for (let ii = 0; ii < that.workers.length; ii++) {
that.workers[ii].worker.send({cmd:BulkWebMailCluster.messageCmd.messageCmdOutSuccReq})
}
while (that.waitList.length < that.maxWorker && waitCount < maxWait) {
await that.sleep(500)
waitCount++
}
let totalCount = 0;
let outputHtml = ''
for (let ii = 0; ii < that.waitList.length; ii++) {
for (let jj = 0; jj < that.waitList[ii].content.length; jj++) {
// list of {sender:xxx, receiver:xxx}
outputHtml += `<br> ${that.waitList[ii].content[jj].sender} to: ${that.waitList[ii].content[jj].receiver}</br>`
totalCount++;
}
}
outputHtml = `<h1> 总共发出 ${totalCount} 封</h1>` + outputHtml
return outputHtml
});
backServ.setGetSenderStatFun(async ()=>{
that.waitList = []
let waitCount = 0;
const maxWait = 20;
for (let ii = 0; ii < that.workers.length; ii++) {
that.workers[ii].worker.send({cmd:BulkWebMailCluster.messageCmd.messageCmdSenderReq})
}
while (that.waitList.length < that.maxWorker && waitCount < maxWait) {
await that.sleep(500)
waitCount++
}
let outputHtml = ''
let index = 0;
let totalSuccess =0 , totalFail = 0;
for (let ii = 0; ii < that.waitList.length; ii++) {
for (let jj = 0; jj < that.waitList[ii].content.length; jj++) {
index++
let succRate = 0;
if (that.waitList[ii].content[jj].usedCount == 0) {
succRate = -100;
} else {
succRate = that.waitList[ii].content[jj].succDeliver * 100 /that.waitList[ii].content[jj].usedCount;
}
outputHtml += `<br> ${index}: ${that.waitList[ii].content[jj].address} ` +
`${that.waitList[ii].content[jj].usedCount} ${that.waitList[ii].content[jj].succDeliver} ${succRate}</br>`
outputHtml += `<br> success: `
for (let kk = 0; kk < that.waitList[ii].content[jj].succList.length; kk++) {
outputHtml += `${that.waitList[ii].content[jj].succList[kk]} `
totalSuccess++;
}
outputHtml += `</br>`;
outputHtml += `<br> failed: `
for (let kk = 0; kk < that.waitList[ii].content[jj].failList.length; kk++) {
outputHtml += `${that.waitList[ii].content[jj].failList[kk]} `
totalFail++;
}
outputHtml += `</br>`;
}
}
outputHtml = `<h1> totalSend: ${totalSuccess + totalFail} totalSuccess: ${totalSuccess} </h1>` + outputHtml;
return outputHtml
})
backServ.setGetLeftSendFun(async ()=>{
that.waitList = []
let waitCount = 0;
const maxWait = 20;
for (let ii = 0; ii < that.workers.length; ii++) {
that.workers[ii].worker.send({cmd:BulkWebMailCluster.messageCmd.messageCmdLeftSendReq})
}
while (that.waitList.length < that.maxWorker && waitCount < maxWait) {
await that.sleep(500)
waitCount++
}
let outputHtml = ''
let totalLeft = 0;
for (let ii = 0; ii < that.waitList.length; ii++) {
totalLeft+= that.waitList[ii].content.length
for (let jj = 0; jj < that.waitList[ii].content.length; jj++) {
outputHtml += `<br> ${that.waitList[ii].content[jj]} </br>`
}
}
outputHtml = `<h1> totalLeft: ${totalLeft} </h1>` + outputHtml;
return outputHtml
})
backServ.run();
// spread the mail sending work to each worker
cluster.on('online', function(worker) {
logger.info('worker ' + 'id ' + worker.id + ' pid: ' + worker.process.pid + ' online')
if (worker.id > that.maxWorker) {
logger.error(`workid ${worker.id} exceed the max cores ${ that.maxWorker}`)
}
})
cluster.on('message', function(worker, message) {
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdOutSuccRes) {
that.waitList.push(message)
}
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdSenderRes) {
that.waitList.push(message)
}
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdLeftSendRes) {
that.waitList.push(message)
}
})
cluster.on('exit', function(worker, code, signal) {
logger.info('workder ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal)
})
}
setUpWorkExecute(cluster) {
let numCores = require('os').cpus().length
let that = this;
that.maxWorker = mailCfg.nodeCluster.maxCore
if (that.maxWorker > numCores || that.maxWorker == 0) {
that.maxWorker = numCores;
}
let eachSendCount = parseInt(mailCfg.mailSenderCount /that.maxWorker)
let eachRecvCount = parseInt(mailCfg.mailReceiverCount/that.maxWorker)
let mailCfgAss = {}
let index = cluster.worker.id -1
Object.assign(mailCfgAss, mailCfg)
mailCfgAss.mailSenderStart = eachSendCount * index + mailCfg.mailSenderStart
mailCfgAss.mailSenderCount = eachSendCount
mailCfgAss.mailReceiverStart = eachRecvCount * index + mailCfg.mailReceiverStart
mailCfgAss.mailReceiverCount = eachRecvCount
if (cluster.worker.id == that.maxWorker) {
mailCfgAss.mailReceiverCount = mailCfg.mailReceiverCount - eachRecvCount * (that.maxWorker - 1)
}
// setup the BulkWebMailMana, but don't run it until 10 seconds
let mailWorker = new BulkWebMailMana()
that.mailWorker = mailWorker
setTimeout(()=>{
that.mailWorker.runWebMailLoop(mailCfgAss);
}, 10000* index)
process.on('message', (message) => {
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdOutSuccReq) {
logger.info(`worker ${cluster.worker.id } receiver message messageCmdOutSuccReq`)
let outList = that.mailWorker.successMailOut();
process.send({cmd:BulkWebMailCluster.messageCmd.messageCmdOutSuccRes,
content:outList})
}
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdSenderReq) {
logger.info(`worker ${cluster.worker.id } receiver message messageCmdSenderReq`)
let outList = that.mailWorker.mailSenderStatOut();
process.send({cmd:BulkWebMailCluster.messageCmd.messageCmdSenderRes,
content:outList})
}
if (message.cmd == BulkWebMailCluster.messageCmd.messageCmdLeftSendReq) {
logger.info(`worker ${cluster.worker.id } receiver message messageCmdLeftSendReq`)
let outList = that.mailWorker.mailLeftSendOut();
process.send({cmd:BulkWebMailCluster.messageCmd.messageCmdLeftSendRes,
content:outList})
}
})
}
}
function setupWebMailCluster() {
var args = process.argv
// use the specified config file
if (args.length > 2) {
try {
let cfgFile = args[2]
let rawdata = fs.readFileSync(cfgFile);
let mailLoadCfg = JSON.parse(rawdata);
mailCfg = mailLoadCfg
logger.error(`use config file ${cfgFile}`)
}catch(e) {
logger.error(e)
}
}
let mailCluster = new BulkWebMailCluster();
if (cluster.isMaster) {
mailCluster.setupBulkWebMailWorkerProcess()
}else {
mailCluster.setUpWorkExecute(cluster)
}
}
setupWebMailCluster()