forked from cmd430/tv-movie-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (33 loc) · 942 Bytes
/
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
"use strict";
const app = require("express")(),
cluster = require("cluster"),
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("Starting up");
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(() => {
util.initCron(config.scrapeTime, scraper.scrape(), util.setStatus("Idle"));
});
scope.on("error", (err) => {
util.onError(err);
});
}
} else {
app.listen(config.port);
}