2024-08-02 15:40:44 +03:00
|
|
|
const schedule = require('node-schedule');
|
2024-08-14 17:14:59 +03:00
|
|
|
const config = require('../shared/config/src/main.js')();
|
2024-08-02 15:40:44 +03:00
|
|
|
const cron = require('cron-validator');
|
|
|
|
|
|
|
|
const save_fiat = require('./save_fiat');
|
2024-10-21 21:32:23 +03:00
|
|
|
const logger = require('../shared/logger/src/main.js');
|
|
|
|
|
2024-10-21 20:57:34 +03:00
|
|
|
async function validateSchedule(schedule) {
|
|
|
|
if (!schedule) throw new Error('The crontab schedule is not set');
|
|
|
|
if (!cron.isValidCron(schedule, { alias: true }))
|
|
|
|
throw new Error('The crontab is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function initialize() {
|
2024-10-08 20:15:20 +03:00
|
|
|
await require('../shared/database/src/create_table')();
|
2024-10-21 20:57:34 +03:00
|
|
|
}
|
2024-10-08 20:15:20 +03:00
|
|
|
|
2024-10-21 20:57:34 +03:00
|
|
|
async function runTasks() {
|
2024-11-07 22:31:18 +03:00
|
|
|
await Promise.all([save_fiat()]);
|
2024-10-21 20:57:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
await initialize();
|
|
|
|
await validateSchedule(config['currency']['collecting']['schedule']);
|
2024-08-02 15:40:44 +03:00
|
|
|
|
2024-10-21 20:57:34 +03:00
|
|
|
await runTasks();
|
2024-08-03 00:02:28 +03:00
|
|
|
|
2024-10-21 20:57:34 +03:00
|
|
|
schedule.scheduleJob(
|
|
|
|
config['currency']['collecting']['schedule'],
|
|
|
|
runTasks,
|
|
|
|
);
|
2024-08-02 15:40:44 +03:00
|
|
|
}
|
|
|
|
|
2024-10-21 20:57:34 +03:00
|
|
|
main().catch((err) => {
|
|
|
|
logger.error('Error in main execution:', err);
|
|
|
|
});
|
2024-08-02 21:48:47 +03:00
|
|
|
|
2024-08-17 16:40:20 +03:00
|
|
|
module.exports = { main };
|