floriday/floriday.js

78 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-05-19 11:47:36 +00:00
import { checkCon, closeCon } from './models/sequelize.js';
import * as utils from './utils.js';
2023-04-06 08:56:52 +00:00
import chalk from 'chalk';
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 10:46:06 +00:00
} catch (err) {
utils.criticalError(err);
}
};
2023-04-24 10:46:06 +00:00
async tryConn() {
2023-05-17 05:36:58 +00:00
while (true)
try {
2023-05-19 11:47:36 +00:00
utils.warning(new Error('Awaiting a response from the database...'));
await utils.sleep(env.DB_RECON_TIMEOUT);
await checkCon();
2023-05-17 05:36:58 +00:00
await this.schedule();
}
catch (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-22 05:54:45 +00:00
while (!this.stopSchedule) {
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 err;
};
};
2023-04-06 08:56:52 +00:00
2023-04-24 10:46:06 +00:00
async trunk() {
try{
2023-06-12 12:42:33 +00:00
const models = [
'organization',
'warehouse',
'tradeItem',
'supplyLine',
'clockPresaleSupply',
];
for (let model of models)
await utils.syncModel(model);
2023-05-22 05:54:45 +00:00
// await utils.syncConnections();
2023-04-06 08:56:52 +00:00
2023-04-24 10:46:06 +00:00
} catch (err) {
2023-05-19 11:47:36 +00:00
if (err.name === 'SequelizeConnectionRefusedError') throw err;
utils.criticalError(err);
2023-04-24 10:46:06 +00:00
}
};
2023-04-24 10:46:06 +00:00
async stop() {
try {
this.stopSchedule = false;
await closeCon();
console.warn(chalk.dim('\nBye, come back soon 👋'))
} catch (err) {
utils.criticalError(err);
}
};
};
export default Floriday;