-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster_with_content.js
50 lines (49 loc) · 1.56 KB
/
cluster_with_content.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
const cluster = require('cluster');
if (cluster.isMaster) {
const files = require('./files');
const workers = [];
const workerLength = 4;
let currentIndex = 0;
const allTimes = {};
[...new Array(workerLength)].forEach(item => {
const worker = cluster.fork();
worker.on('message', d => {
promises[d.file].resolve();
allTimes[d.file] = d.time;
const data = files[currentIndex++];
data && worker.send({file: data.file, content: data.content, index: currentIndex - 1, time: Date.now()});
});
worker.on('error', err => {
console.error('error', err);
});
workers.push(worker);
});
const promises = {};
setTimeout(() => {
files.forEach((item, index) => {
if (index < workerLength) {
workers[index].send({file: item.file, content: item.content, index, time: Date.now()});
currentIndex = index;
}
const obj = {};
obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve;
obj.reject = reject;
});
promises[item.file] = obj;
});
const all = Object.values(promises).map(item => item.promise);
const start = Date.now();
Promise.all(all).then(() => {
console.error(allTimes);
console.error('all times', Date.now() - start);
});
}, 2000);
} else {
const UglifyJS = require('uglify-es');
process.on('message', data => {
const start = Date.now();
const result = UglifyJS.minify(data.content);
process.send({file: data.file, content: result.code, index: data.index, time: Date.now() - start});
});
};