floriday/main.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as vnUtils from './utils.js';
2023-01-11 12:13:22 +00:00
import { models } from './models/index.js';
2023-04-03 19:02:00 +00:00
import moment from 'moment';
2023-01-11 12:13:22 +00:00
2023-04-04 18:14:20 +00:00
// Añade la hora a todos los console.log
// console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args);
2023-04-03 19:02:00 +00:00
async function main() {
try {
2023-04-04 18:14:20 +00:00
const env = process.env;
let tokenExpirationDate = await vnUtils.getClientToken(models);
if (JSON.parse(env.SYNC_SEQUENCE)) await vnUtils.syncSequence()
if (JSON.parse(env.SYNC_SUPPLIER)) await vnUtils.syncSuppliers();
await vnUtils.syncConnections();
if (JSON.parse(env.SYNC_TRADEITEM)) await vnUtils.syncTradeItems();
console.log('Synced trade items');
try {
while (true) {
try{
console.log('Querying the API to check for new data...');
if (moment().isAfter(tokenExpirationDate)) {
console.log('Token expired, getting a new one...');
tokenExpirationDate = await vnUtils.getClientToken(models);
}
await vnUtils.syncSupplyLines();
} catch (err) {
console.error(err);
2023-04-03 19:02:00 +00:00
}
2023-04-04 18:14:20 +00:00
if (JSON.parse(env.IS_PRODUCTION))
await vnUtils.sleep(300000);
else
await vnUtils.sleep(120000);
2023-01-24 12:51:44 +00:00
}
2023-04-04 18:14:20 +00:00
} catch (err) {
myErr(err);
2023-01-16 13:52:08 +00:00
}
2023-04-04 18:14:20 +00:00
} catch (err) {
myErr(err);
2023-01-16 13:52:08 +00:00
}
2023-04-03 19:02:00 +00:00
}
2023-04-04 18:14:20 +00:00
/**
* Creates the connection to the database.
*
* @param {err}
**/
function myErr(err) {
console.log(`[ERROR]`.red.bold, `${err.name}: ${err.message}`.red);
process.exit();
}
2023-04-03 19:02:00 +00:00
main()