import { checkCon, closeCon } from './models/sequelize.js'; import * as utils from './utils.js'; import chalk from 'chalk'; import yml from 'js-yaml'; import fs from 'fs'; const env = process.env; const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8')); let cycle = 1; class Floriday { async start() { try { await utils.checkConfig(); await utils.checkToken(); utils.separator('Init'); } catch (err) { utils.criticalError(err); } }; async tryConn() { while (true) try { utils.warning(new Error('Awaiting a response from the database...')); await utils.sleep(env.DB_RECON_TIMEOUT); await checkCon(); await this.schedule(); } catch (err) {}; }; async schedule () { try { const intervalTime = JSON.parse(env.IS_PRODUCTION) ? env.MS_PRODUCTION_SCHEDULE : env.MS_TEST_SCHEDULE; while (!this.stopSchedule) { try { await this.trunk(); await new Promise(resolve => setTimeout(resolve, intervalTime)); } catch (err) { await this.tryConn(); await new Promise(resolve => setTimeout(resolve, intervalTime)); } } } catch (err) { throw err; }; }; async trunk() { try{ for (let model of flModels) await utils.syncModel(model); await utils.checkConnections(); utils.separator(`${cycle++} ${(cycle == 2 ? 'Cycle' : 'Cycles')}`); } catch (err) { if (['SequelizeConnectionRefusedError', 'SequelizeConnectionError'].includes(err.name)) throw err; utils.criticalError(err); } }; async stop() { try { this.stopSchedule = false; await closeCon(); console.warn(chalk.dim('\nBye, come back soon 👋')) } catch (err) { utils.criticalError(err); } }; }; export default Floriday;