forked from webrecorder/browsertrix-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·64 lines (47 loc) · 1.25 KB
/
main.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
#!/usr/bin/env node
var crawler = null;
var lastSigInt = 0;
let forceTerm = false;
async function handleTerminate() {
if (!crawler || !crawler.crawlState) {
process.exit(0);
}
try {
if (!crawler.crawlState.drainMax) {
console.log("SIGNAL: gracefully finishing current pages...");
crawler.crawlState.setDrain(crawler.finalExit);
} else if ((Date.now() - lastSigInt) > 200) {
console.log("SIGNAL: stopping crawl now...");
await crawler.serializeConfig();
process.exit(0);
}
lastSigInt = Date.now();
} catch (e) {
console.log(e);
}
}
process.on("SIGINT", async () => {
console.log("SIGINT received...");
await handleTerminate();
});
process.on("SIGUSR1", () => {
if (crawler) {
crawler.finalExit = true;
}
});
process.on("SIGTERM", async () => {
if (forceTerm || crawler.done) {
console.log("SIGTERM received, exit immediately");
process.exit(crawler.done ? 0 : 1);
}
console.log("SIGTERM received...");
await handleTerminate();
});
process.on("SIGABRT", async () => {
console.log("SIGABRT received, will force immediate exit on SIGTERM");
forceTerm = true;
crawler.exitCode = 1;
});
const { Crawler } = require("./crawler");
crawler = new Crawler();
crawler.run();