-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronJobs.js
More file actions
80 lines (70 loc) · 2.29 KB
/
cronJobs.js
File metadata and controls
80 lines (70 loc) · 2.29 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
const cron = require('node-cron');
const { fetchAndStoreNews, deleteAllNews, pool } = require('./newsFetcher');
// Flags to prevent overlapping jobs
let isDailyResetRunning = false;
let isThreeHourlyRunning = false;
// Retry helper with exponential backoff
async function retry(fn, retries = 3, delay = 2000) {
for (let i = 0; i < retries; i++) {
try {
return await fn();
} catch (err) {
console.error(`[${new Date().toISOString()}] Attempt ${i + 1} failed:`, err.message);
if (i < retries - 1) {
await new Promise(r => setTimeout(r, delay * (i + 1)));
} else {
throw err;
}
}
}
}
// Safe wrapper with logging
async function safeRun(jobName, fn) {
try {
console.log(`[${new Date().toISOString()}] Starting job: ${jobName}`);
await fn();
console.log(`[${new Date().toISOString()}] Completed job: ${jobName}`);
} catch (err) {
console.error(`[${new Date().toISOString()}] Error in job: ${jobName}`, err);
}
}
// Daily reset at 2 AM
cron.schedule('0 2 * * *', async () => {
if (isDailyResetRunning) {
console.warn(`[${new Date().toISOString()}] Daily reset already running, skipping`);
return;
}
isDailyResetRunning = true;
await safeRun('Daily Reset + Fetch', async () => {
const client = await pool.connect();
try {
// Begin transaction
await client.query('BEGIN');
await deleteAllNews(client);
await fetchAndStoreNews(client);
// Commit transaction if everything succeeds
await client.query('COMMIT');
} catch (err) {
// RollBack if transaction fails
await client.query('ROLLBACK');
console.error(`[${new Date().toISOString()}] Transaction rolled back due to error`);
throw err;
} finally {
client.release();
}
});
isDailyResetRunning = false;
});
// 3-hourly incremental fetch
cron.schedule('0 */3 * * *', async () => {
if (isThreeHourlyRunning || isDailyResetRunning) {
console.warn(`[${new Date().toISOString()}] 3-hourly fetch skipped due to overlap`);
return;
}
isThreeHourlyRunning = true;
await safeRun('3-Hourly Fetch', async () => {
await retry(fetchAndStoreNews, 3);
});
isThreeHourlyRunning = false;
});
console.log(`[${new Date().toISOString()}] Cron jobs scheduled...`);