floriday/floriday.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-04-06 08:56:52 +00:00
import * as utils from './utils.js';
import { models, checkConn, closeConn } from './models/index.js';
import moment from 'moment';
2023-04-06 08:56:52 +00:00
import chalk from 'chalk';
// Añade la hora a todos los console.log
// console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args);
const env = process.env;
class Floriday {
async start() {
try {
2023-04-06 08:56:52 +00:00
this.tokenExpirationDate = await utils.requestToken(models);
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence()
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
} catch (err) {
2023-04-06 08:56:52 +00:00
utils.criticalError(err);
}
2023-04-06 08:56:52 +00:00
await this.troncal()
}
2023-04-06 08:56:52 +00:00
async tryConn() {
try {
utils.sleep(env.DB_TIMEOUT_RECONECT);
await checkConn();
await this.schedule();
}
catch (err) {
throw new Error(err);
}
}
async schedule () {
try {
const intervalTime = JSON.parse(env.IS_PRODUCTION) ? 300000 : 5000;
setInterval(async () => {
try {
await this.troncal();
}
catch (err) {
await this.tryConn();
}
}, intervalTime);
} catch (err) {
throw new Error(err);
}
}
async troncal() {
try{
2023-04-06 08:56:52 +00:00
if (moment().isAfter(await utils.getCurrentTokenExpiration())) {
this.tokenExpirationDate = await utils.requestToken(models);
}
2023-04-06 08:56:52 +00:00
await utils.syncSupplyLines();
// Continuar con todo lo que haga falta realizar en el evento
} catch (err) {
2023-04-06 08:56:52 +00:00
throw new Error(err);
}
}
2023-04-06 08:56:52 +00:00
async stop() {
await closeConn();
console.log(chalk.dim('Bye, come back soon 👋'))
}
}
export default Floriday;