diff --git a/collect-currency/main.js b/collect-currency/main.js index 950dc46..f1ff4b8 100644 --- a/collect-currency/main.js +++ b/collect-currency/main.js @@ -5,23 +5,34 @@ const cron = require('cron-validator'); const save_fiat = require('./save_fiat'); const save_crypto = require('./save_crypto'); -async function main() { - await require('../shared/database/src/create_table')(); - - const config_schedule = config['currency']['collecting']['schedule']; - if (!config_schedule) throw new Error('The crontab schedule is not set'); - if (!cron.isValidCron(config_schedule, { alias: true })) +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'); - - await save_fiat(); - await save_crypto(); - - schedule.scheduleJob(config_schedule, async function () { - await save_fiat(); - await save_crypto(); - }); } -main(); +async function initialize() { + await require('../shared/database/src/create_table')(); +} + +async function runTasks() { + await Promise.all([save_fiat(), save_crypto()]); +} + +async function main() { + await initialize(); + await validateSchedule(config['currency']['collecting']['schedule']); + + await runTasks(); + + schedule.scheduleJob( + config['currency']['collecting']['schedule'], + runTasks, + ); +} + +main().catch((err) => { + logger.error('Error in main execution:', err); +}); module.exports = { main };