2023-05-15 12:19:43 +00:00
|
|
|
import { checkConn, closeConn } from './models/sequelize.js';
|
2023-05-11 06:39:02 +00:00
|
|
|
import * as utils from './utils.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
|
2023-05-09 09:59:21 +00:00
|
|
|
console.log = (...args) => console.info(chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`), ...args);
|
2023-04-05 12:46:25 +00:00
|
|
|
const env = process.env;
|
|
|
|
class Floriday {
|
2023-04-24 10:46:06 +00:00
|
|
|
async start() {
|
|
|
|
try {
|
2023-05-08 10:18:14 +00:00
|
|
|
await utils.checkConfig();
|
2023-05-15 12:19:43 +00:00
|
|
|
await utils.requestToken();
|
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();
|
2023-05-16 08:24:04 +00:00
|
|
|
if (JSON.parse(env.SYNC_WAREHOUSE)) await utils.syncWarehouses();
|
2023-05-16 12:12:34 +00:00
|
|
|
if (JSON.parse(env.SYNC_CONN)) await utils.syncConnections();
|
2023-04-24 10:46:06 +00:00
|
|
|
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
|
|
|
|
} catch (err) {
|
|
|
|
utils.criticalError(err);
|
|
|
|
}
|
|
|
|
}
|
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;
|
2023-05-15 12:19:43 +00:00
|
|
|
this.continueSchedule = true;
|
|
|
|
while (this.continueSchedule) {
|
2023-04-24 10:46:06 +00:00
|
|
|
try {
|
|
|
|
await this.trunk();
|
2023-05-09 09:59:21 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, intervalTime));
|
|
|
|
} catch (err) {
|
2023-04-24 10:46:06 +00:00
|
|
|
await this.tryConn();
|
2023-05-09 09:59:21 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, intervalTime));
|
2023-04-24 10:46:06 +00:00
|
|
|
}
|
2023-05-09 09:59:21 +00:00
|
|
|
}
|
2023-04-24 10:46:06 +00:00
|
|
|
} 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{
|
2023-05-16 12:17:45 +00:00
|
|
|
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) {
|
2023-05-09 11:14:54 +00:00
|
|
|
utils.criticalError(err);
|
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 stop() {
|
2023-05-15 12:19:43 +00:00
|
|
|
this.continueSchedule = false;
|
2023-04-24 10:46:06 +00:00
|
|
|
await closeConn();
|
2023-05-11 12:36:55 +00:00
|
|
|
console.warn(chalk.dim('Bye, come back soon 👋'))
|
2023-04-24 10:46:06 +00:00
|
|
|
}
|
2023-04-05 12:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Floriday;
|