Рефакторинг кода

This commit is contained in:
Данил 2024-10-21 20:57:34 +03:00
parent 490422504e
commit 75a97ff2ca

View file

@ -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 };