-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (53 loc) · 1.5 KB
/
index.ts
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
import cron from 'node-cron';
import Main from '../main';
class ScraperScheduler {
private cronExpression: string;
private task: cron.ScheduledTask | null = null;
constructor(
cronExpression: string
) {
this.cronExpression = cronExpression;
}
async scrape(): Promise<void> {
console.log("Waking up to scrape...");
const main = new Main();
await main.run();
console.log("Scraping done. Going back to sleep.");
}
start(): void {
this.task = cron.schedule(this.cronExpression, () => {
console.log('cron triggered');
this.scrape().catch((error) => {
console.error("Error during scraping:", error);
});
});
console.log(`Scheduler started with cron expression: ${this.cronExpression}`);
}
stop(): void {
if (this.task) {
this.task.stop();
console.log("Scheduler stopped.");
}
}
}
// Configuration and initialization
// const cronExpression = '0 */12 * * *'; // Every 12 hours
const cronExpression = '*/5 * * * *'; // Every 5 minutes
// const cronExpression = '* * * * *'; // Every minute
const scheduler = new ScraperScheduler(
cronExpression
);
// Start the scheduler
scheduler.start();
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log("Received SIGINT. Gracefully shutting down.");
scheduler.stop();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log("Received SIGTERM. Gracefully shutting down.");
scheduler.stop();
process.exit(0);
});
export default ScraperScheduler;