2023-05-19 11:47:36 +00:00
|
|
|
import { checkCon, closeCon } from './models/sequelize.js';
|
2023-05-11 06:39:02 +00:00
|
|
|
import * as utils from './utils.js';
|
2023-04-06 08:56:52 +00:00
|
|
|
import chalk from 'chalk';
|
2023-06-15 09:29:19 +00:00
|
|
|
import yml from 'js-yaml';
|
|
|
|
import fs from 'fs';
|
2023-04-05 12:46:25 +00:00
|
|
|
|
|
|
|
const env = process.env;
|
2023-06-15 09:29:19 +00:00
|
|
|
const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8'));
|
2023-06-16 11:43:24 +00:00
|
|
|
let cycle = 1, stopSchedule = false;
|
2023-06-14 10:22:34 +00:00
|
|
|
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-06-15 10:50:58 +00:00
|
|
|
await utils.checkToken();
|
2023-06-16 11:43:24 +00:00
|
|
|
await utils.separator('Init');
|
2023-04-24 10:46:06 +00:00
|
|
|
} catch (err) {
|
2023-06-16 11:43:24 +00:00
|
|
|
utils.handler('critical', err);
|
2023-04-24 10:46:06 +00:00
|
|
|
}
|
2023-06-13 10:51:59 +00:00
|
|
|
};
|
2023-04-05 12:46:25 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async tryConn() {
|
2023-05-17 05:36:58 +00:00
|
|
|
while (true)
|
|
|
|
try {
|
2023-06-16 11:43:24 +00:00
|
|
|
await utils.handler('warning', new Error('Awaiting a response from the database...'));
|
2023-05-19 11:47:36 +00:00
|
|
|
await utils.sleep(env.DB_RECON_TIMEOUT);
|
|
|
|
await checkCon();
|
2023-05-17 05:36:58 +00:00
|
|
|
await this.schedule();
|
|
|
|
}
|
2023-06-13 10:51:59 +00:00
|
|
|
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-06-16 11:43:24 +00:00
|
|
|
while (!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) {
|
2023-06-13 10:51:59 +00:00
|
|
|
throw err;
|
|
|
|
};
|
|
|
|
};
|
2023-04-06 08:56:52 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async trunk() {
|
|
|
|
try{
|
2023-06-16 11:43:24 +00:00
|
|
|
await utils.checkConnections();
|
|
|
|
|
|
|
|
for (let model of flModels) {
|
|
|
|
if (stopSchedule) return;
|
2023-06-12 12:42:33 +00:00
|
|
|
await utils.syncModel(model);
|
2023-06-16 11:43:24 +00:00
|
|
|
}
|
2023-05-22 05:54:45 +00:00
|
|
|
|
2023-06-16 11:43:24 +00:00
|
|
|
await utils.separator(`${cycle++} ${(cycle == 2 ? 'Cycle' : 'Cycles')}`);
|
2023-04-24 10:46:06 +00:00
|
|
|
} catch (err) {
|
2023-06-16 11:43:24 +00:00
|
|
|
if (err?.original?.code === 'ER_SOCKET_UNEXPECTED_CLOSE') throw err;
|
|
|
|
utils.handler('critical', err);
|
2023-04-24 10:46:06 +00:00
|
|
|
}
|
2023-06-13 10:51:59 +00:00
|
|
|
};
|
2023-04-05 12:46:25 +00:00
|
|
|
|
2023-04-24 10:46:06 +00:00
|
|
|
async stop() {
|
2023-06-13 10:51:59 +00:00
|
|
|
try {
|
2023-06-16 11:43:24 +00:00
|
|
|
stopSchedule = true;
|
2023-06-13 10:51:59 +00:00
|
|
|
await closeCon();
|
2023-06-14 10:22:34 +00:00
|
|
|
console.warn(chalk.dim('\nBye, come back soon 👋'))
|
2023-06-13 10:51:59 +00:00
|
|
|
} catch (err) {
|
2023-06-16 11:43:24 +00:00
|
|
|
utils.handler('critical', err);
|
2023-06-13 10:51:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2023-04-05 12:46:25 +00:00
|
|
|
|
|
|
|
export default Floriday;
|