-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobiParser.js
76 lines (72 loc) · 3.32 KB
/
mobiParser.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
var parseMobiFile = require('./mobiParserChild');
var child_process = require("child_process");
var os = require("os");
var numCpus = os.cpus().length;
var cpList = [];
function parseMobi(allBookList) {
return new Promise(function(resolve, reject){
var mobiBookList = [];
allBookList.forEach(function(book, index){
var mobiBook = null;
if(book.bookFormat === 'mobi') {
console.log("Parsing mobi book at #"+index);
book.bookIndex = index;
mobiBookList.push(book);
//allBookList[index]["META"] = parseMobiFile(book.bookPath);
}
});
if(mobiBookList.length === 0) {
resolve();
}
var numberOfChildProcessesRequired = numCpus < mobiBookList.length ? numCpus : mobiBookList.length;
console.log("numberOfChildProcessesRequired "+numberOfChildProcessesRequired);
console.log("mobiBookList length = "+mobiBookList.length);
for(var i = 0; i<numberOfChildProcessesRequired; i++) {
var cp = child_process.fork("./mobiParserChild");
cp.coreIndex = i;
cpList.push(cp);
}
for(var j = 0; j<cpList.length; j++) {
cpList[j].on("message", function(m){
var coreIndex = this.coreIndex;
if(m.action == "parseComplete") {
var indexOfNextBookForCurrentProcess = m.bookIndex + numberOfChildProcessesRequired;
//console.log("Received parse complete "+m.bookMeta+" "+m.bookIndex);
//console.log("indexOfNextBookForCurrentProcess "+indexOfNextBookForCurrentProcess);
var bookMeta = m.bookMeta;
mobiBookList[m.bookIndex]["META"] = bookMeta;
if(indexOfNextBookForCurrentProcess < mobiBookList.length) {
this.send({
action: "parseFile",
bookPath: mobiBookList[indexOfNextBookForCurrentProcess].bookPath,
bookIndex: indexOfNextBookForCurrentProcess
});
} else {
// Kill current process and wait for other processes to finish
console.log("Killing process "+this.coreIndex);
this.kill();
for(var k = 0; k<numberOfChildProcessesRequired; k++) {
if(cpList[k].killed === false) {
// Some processes are still running. Wait.
console.log("Process "+k+" is still running. Not exiting");
return;
}
}
console.log("All process exited..");
// All processes have been killed.
for(var l=0; l<mobiBookList.length;l++) {
allBookList[mobiBookList[l].bookIndex]["META"] = mobiBookList[l]["META"];
}
resolve();
}
}
});
cpList[j].send({
action: "parseFile",
bookPath: mobiBookList[j].bookPath,
bookIndex: j
});
}
});
}
module.exports = parseMobi;