This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-3.js
147 lines (134 loc) · 4.3 KB
/
example-3.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
//
// example-3.js - Proof of concept of a clustered implemented spider
//
var path = require('path'),
extractor = require('extractor'),
db = require('dirty')('spider.db'),
cluster = require('cluster'),
numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
if (process.argv.length <= 2) {
console.error("USAGE: node " + process.argv[1] + " STARTING_URL");
process.exit(1);
}
console.log("PARENT numCPUs: " + numCPUs);
console.log("PARENT pid: " + process.pid);
console.log("PARENT loading db ...");
db.on('load', function() {
var n = [], i, process_page = function(m) {
var row;
if (m.processed_url !== undefined &&
m.found_urls !== undefined) {
if (m.found_urls && m.found_urls.length > 0) {
m.found_urls.forEach(function(url) {
row = db.get(url);
if (row === undefined) {
console.log("Discovered: " + url);
db.set(url, { url: url, processed: false });
}
});
}
if (m.processed_url) {
console.log("Processed: " + m.processed_url);
db.set(m.processed_url, {url: m.processed_url, processed: true});
}
}
},
worker_died = function (worker) {
console.error("ERROR: worker died: " + worker.pid);
},
worker_exited = function (err_no) {
console.log("worker " + n[i].pid + " exited: " + err_no);
},
message_parent = function(ky, val) {
if (val.processed === false) {
console.log("PARENT sending url: " + ky + " to be processed by " + n[i].pid);
n[i].send({url:ky});
return false;
}
};
// Seed the DB
for (i = 2; i < process.argv.length; i++) {
db.set(process.argv[i], { url: process.argv[i], processed: false });
}
// Fork the children
for (i = 0; i < numCPUs; i++ ) {
n.push(cluster.fork().process);
console.log("PARENT Forked child with pid: " + n[i].pid);
n[i].on('message', process_page);
n[i].on('death', worker_died);
n[i].on('exit', worker_exited);
db.forEach(message_parent);
}
// Setup an service for sending message to child
setInterval(function() {
var i = 0, j = 0, k = 0;
db.forEach(function(ky, val) {
if (val.processed) {
k += 1;
} else {
if (i < numCPUs) {
console.log("PARENT requesting spider: " + ky + " by " + n[i].pid);
n[i].send({url:ky});
i += 1;
}
j += 1;
}
});
console.log(j + " records remaining.");
console.log(k + " records processed.");
console.log(numCPUs + " threads.");
}, 10000);
});
db.on('drain', function() {
var processed = 0, unprocessed = 0;
db.forEach(function (ky, val) {
if (val.processed) {
processed += 1;
} else {
unprocessed += 1;
}
});
console.log("Total processed: " + processed + ", unprocessed: " + unprocessed);
});
} else { // End of Parent process
process.on('message', function(m) {
console.log('CHILD ' + process.pid + ' spider:', m.url);
extractor.spider(m.url, function(err, data, env) {
var i, base_path = path.dirname(url), cut_pos = base_path.length, new_url,
urls = [], url = env.pathmame;
if (err) {
console.error("ERROR: " + url + ': ' + err);
} else if (data) {
if (data.anchors !== undefined) {
if (data.anchors.length !== undefined) {
for(i = 0; i < data.anchors.length; i += 1) {
if (data.anchors[i].href !== undefined &&
data.anchors[i].href.substr(0,cut_pos) === base_path) {
new_url = data.anchors[i].href.trim();
if (path.extname(new_url) === '') {
new_url = path.join(new_url,'index.html');
}
// Now add the discovered URL to the list if it is new.
urls.push(new_url);
}
}
} else {
if (data.anchors.href !== undefined &&
data.anchors.href.substr(0,cut_pos) === base_path) {
new_url = data.anchors.href.trim();
if (path.extname(new_url) === '') {
new_url = path.join(new_url,'index.html');
}
// Now add the discovered URL to the list if it is new.
urls.push(new_url);
}
}
}
}
//console.log("DEBUG found_urls: " + util.inspect(urls));
// Send the URLs found to the master process
process.send({processed_url: url, found_urls: urls});
});
}); // End process.on("message", ...);
} // End of child process