2023-04-06 08:56:52 +00:00
|
|
|
import * as utils from './utils.js';
|
|
|
|
import { models, checkConn, closeConn } from './models/index.js';
|
2023-04-05 12:46:25 +00:00
|
|
|
import moment from 'moment';
|
2023-04-06 08:56:52 +00:00
|
|
|
import chalk from 'chalk';
|
2023-04-05 12:46:25 +00:00
|
|
|
|
|
|
|
// 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 {
|
2023-04-24 10:46:06 +00:00
|
|
|
async start() {
|
|
|
|
try {
|
|
|
|
this.tokenExpirationDate = await utils.requestToken(models);
|
2023-04-24 12:11:25 +00:00
|
|
|
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence();
|
2023-04-24 10:46:06 +00:00
|
|
|
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) {
|
|
|
|
utils.criticalError(err);
|
|
|
|
}
|
2023-04-24 12:11:25 +00:00
|
|
|
await this.trunk();
|
2023-04-24 10:46:06 +00:00
|
|
|
}
|
2023-04-05 12:46:25 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async tryConn() {
|
|
|
|
try {
|
|
|
|
utils.sleep(env.DB_TIMEOUT_RECONECT);
|
|
|
|
await checkConn();
|
|
|
|
await this.schedule();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 08:56:52 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async schedule () {
|
|
|
|
try {
|
|
|
|
const intervalTime = JSON.parse(env.IS_PRODUCTION)
|
|
|
|
? env.MS_PRODUCTION_SCHEDULE
|
|
|
|
: env.MS_TEST_SCHEDULE;
|
|
|
|
setInterval(async () => {
|
|
|
|
try {
|
|
|
|
await this.trunk();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
await this.tryConn();
|
|
|
|
}
|
|
|
|
}, intervalTime);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 08:56:52 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async trunk() {
|
|
|
|
try{
|
|
|
|
if (moment().isAfter(await utils.getCurrentTokenExpiration())) {
|
|
|
|
this.tokenExpirationDate = await utils.requestToken(models);
|
|
|
|
}
|
|
|
|
await utils.syncSupplyLines();
|
2023-04-06 08:56:52 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
// Continuar con todo lo que haga falta realizar en la rutina
|
2023-04-06 08:56:52 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 12:46:25 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async stop() {
|
|
|
|
await closeConn();
|
|
|
|
console.log(chalk.dim('Bye, come back soon 👋'))
|
|
|
|
}
|
2023-04-05 12:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Floriday;
|