Kekkai/collect-currency/main.js

39 lines
992 B
JavaScript
Raw Normal View History

const schedule = require('node-schedule');
2024-08-14 17:14:59 +03:00
const config = require('../shared/config/src/main.js')();
const cron = require('cron-validator');
const save_fiat = require('./save_fiat');
const save_crypto = require('./save_crypto');
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() {
await require('../shared/database/src/create_table')();
2024-10-21 20:57:34 +03:00
}
2024-10-21 20:57:34 +03:00
async function runTasks() {
await Promise.all([save_fiat(), save_crypto()]);
}
async function main() {
await initialize();
await validateSchedule(config['currency']['collecting']['schedule']);
2024-10-21 20:57:34 +03:00
await runTasks();
2024-10-21 20:57:34 +03:00
schedule.scheduleJob(
config['currency']['collecting']['schedule'],
runTasks,
);
}
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 };