82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
import { checkCon, closeCon } from './models/sequelize.js';
|
|
import * as utils from './utils.js';
|
|
import moment from 'moment';
|
|
import chalk from 'chalk';
|
|
|
|
const env = process.env;
|
|
|
|
if (JSON.parse(env.TIME_STAMPS)) // Add time to all console.log
|
|
console.log = (...args) => console.info(chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`), ...args);
|
|
|
|
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('Bye, come back soon 👋'))
|
|
} catch (err) {
|
|
utils.criticalError(err);
|
|
}
|
|
};
|
|
};
|
|
|
|
export default Floriday; |