-
Notifications
You must be signed in to change notification settings - Fork 127
/
index.js
56 lines (50 loc) · 1.32 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
"use strict";
const app = require("express")(),
cluster = require("cluster"),
CronJob = require("cron").CronJob,
cpuCount = require("os").cpus().length,
domain = require("domain"),
fs = require("fs"),
config = require("./config"),
scraper = require("./scraper"),
util = require("./util");
require("./setup.js")(config, app);
require("./routes.js")(app);
if (cluster.isMaster) {
for (let i = 0; i < Math.min(cpuCount, config.workers); i++) {
cluster.fork();
}
util.makeTemp();
util.setStatus("Idle");
cluster.on("exit", (worker, code, signal) => {
util.onError("Worker '" + worker.process.pid + "' died, spinning up another!");
cluster.fork();
});
if (config.master) {
const scope = domain.create();
scope.run(() => {
try {
const job = new CronJob({
cronTime: config.scrapeTime,
onTick: () => {
scraper.scrape();
},
onComplete: () => {
util.setStatus("Idle");
},
start: true,
timeZone: "America/Los_Angeles"
});
util.log("Cron job started");
} catch (ex) {
util.onError("Cron pattern not valid");
}
scraper.scrape();
});
scope.on("error", (err) => {
util.onError(err);
});
}
} else {
app.listen(config.port, config.ip);
}