-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.ts
94 lines (78 loc) · 2.59 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cron from 'node-cron';
import { callParseNewMonth, callParseNOldMonths } from '@/modules/parser/calls';
import { getScheduledTasksObject, validateCronString } from '@/modules/scheduler/utils';
import { getAppNow, isWeekendAndStartOfMonth } from '@/libs/datetime';
import logger from '@/libs/winston';
import { logPrettyPrintObject } from '@/utils/pretty-print';
import { SCRIPTS } from '@/constants/scripts';
import { SERVER_CONFIG } from '@/config/server';
import type { ParserResponse } from '@/types/api';
const { appTimeZone } = SERVER_CONFIG;
const fiveMinutes = 5 * 60 * 1000;
export const debuggingScheduler = () => {
const debuggingTask = cron.schedule(
validateCronString('* * * * *'),
() => {
logger.info('running a task every minute');
},
{
name: 'debugging',
}
);
setTimeout(() => {
debuggingTask.stop();
logger.info('debuggingTask stopped after 5 minutes');
}, fiveMinutes);
};
/** Every 2-15 in month at 9:00am Europe/Belgrade if 2nd isn't weekend day. */
export const newMonthScheduler = () => {
cron.schedule(
validateCronString('0 9 2-15 * *'),
async () => {
const now = new Date();
if (isWeekendAndStartOfMonth(now)) {
logger.info(`Skipping parsing new month, month starts with weekend, now: ${getAppNow()}`);
return;
}
try {
const parserResponse: ParserResponse = await callParseNewMonth();
logger.info('newMonthScheduler parserResponse:', parserResponse);
} catch (error) {
logger.error('Parsing new month failed.', error);
}
},
{
name: SCRIPTS.parseNew,
timezone: appTimeZone,
}
);
};
/** Parse N old months to seed database. Run every 10 minutes. */
export const seedOldMonthsScheduler = () => {
let numberOfCalls = 0;
const seedOldMonthsTask = cron.schedule(
validateCronString('*/10 * * * *'),
async () => {
try {
const parserResponse: ParserResponse = await callParseNOldMonths();
logger.info('seedOldMonthsScheduler parserResponse:', parserResponse);
numberOfCalls++;
} catch (error) {
logger.error('Parsing old months failed.', error);
}
},
{
scheduled: false,
name: SCRIPTS.parseOldMany,
timezone: appTimeZone,
}
);
// 5 x 5 calls
if (numberOfCalls > 4) {
// todo: disable task if seeded, check first month in db
seedOldMonthsTask.stop();
logger.info(`seedOldMonthsTask stopped after numberOfCalls: ${numberOfCalls}`);
}
};
export const logScheduledTasks = () =>
logPrettyPrintObject(getScheduledTasksObject(), 'Scheduled tasks');