-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
51 lines (45 loc) · 1.88 KB
/
index.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
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const Database = require('better-sqlite3');
const db = new Database('bithack.db', {});
if (cluster.isMaster) {
let addresses = 0, hit = 0, dirty = 0, pristine = 0;
for (let i = 0; i < 1; i++) {
cluster.fork();
}
const attachEventHandler = (id) => {
cluster.workers[id].on('message', (json) => {
if (json.type === "stats") {
addresses += json.data.addresses;
hit += json.data.hit;
dirty += json.data.dirty;
pristine += json.data.pristine;
} else if (json.type === "hit") {
let stmt = db.prepare("INSERT INTO result VALUES (@wallet, @wif, @balance)");
stmt.run(json.data);
} else if (json.type === "dirty") {
let stmt = db.prepare("INSERT INTO dirty VALUES (@wallet, @wif, @balance)");
stmt.run(json.data);
} else {
console.log("Received unknown message from worker", JSON.stringify(json));
}
});
}
Object.keys(cluster.workers).forEach((id) => {
// console.log("Worker " + id + " running with ID : " + cluster.workers[id].process.pid);
attachEventHandler(id)
// cluster.workers[id].send({type:"message", "message" : "Start"});
});
cluster.on('exit', function (worker, code, signal) {
// console.log('Worker ' + worker.process.pid + ' has died');
console.log("Processed", "addresses", addresses, "hit", hit, "dirty", dirty);
cluster.fork();
Object.keys(cluster.workers).forEach((id) => {
// console.log("Worker " + id + " running with ID : " + cluster.workers[id].process.pid);
attachEventHandler(id)
});
});
} else {
const worker = require("./worker");
worker.doProcess();
}