-
Notifications
You must be signed in to change notification settings - Fork 93
/
index.js
57 lines (49 loc) · 1.58 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
52
53
54
55
56
57
var cluster = require('cluster')
, app = require('express')()
, cpuCount = require('os').cpus().length;
// settings
var config = require('./config.js');
// config
require('./setup.js')(config, app);
// routes
require('./routes.js')(app);
if(cluster.isMaster) {
// Fork the child threads
for (var i = 0; i < Math.min(cpuCount, config.workers); i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died, spinning up another!');
cluster.fork();
});
// Is this the master API server? If so scrape
if(config.master) {
var domain = require('domain');
var scope = domain.create();
scope.run(function() {
var helpers = require('./lib/helpers');
// Launch the eztv scraper
try {
var CronJob = require('cron').CronJob;
var job = new CronJob(config.scrapeTime,
function(){
helpers.refreshDatabase();
}, function () {
// This function is executed when the job stops
},
true
);
console.log("Cron job started");
} catch(ex) {
console.log("Cron pattern not valid");
}
// Start extraction right now
helpers.refreshDatabase();
});
scope.on('error', function(err) {
console.log("Error:", err);
})
}
} else {
app.listen(config.port);
}