floriday/floriday.js

78 lines
1.6 KiB
JavaScript

import { checkCon, closeCon } from './models/sequelize.js';
import * as utils from './utils.js';
import chalk from 'chalk';
const env = process.env;
class Floriday {
async start() {
try {
await utils.checkConfig();
await utils.requestToken();
} 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{
const models = [
'organization',
'warehouse',
'tradeItem',
'supplyLine',
'clockPresaleSupply',
];
for (let model of models)
await utils.syncModel(model);
// await utils.syncConnections();
} catch (err) {
if (err.name === 'SequelizeConnectionRefusedError') 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;