refs #4823 Minor big changes

This commit is contained in:
Guillermo Bonet 2023-06-16 11:50:40 +02:00
parent d2f55ef225
commit 02cd8124ba
3 changed files with 82 additions and 77 deletions

View File

@ -6,12 +6,13 @@ import fs from 'fs';
const env = process.env; const env = process.env;
const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8')); const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8'));
let cycle = 1;
class Floriday { class Floriday {
async start() { async start() {
try { try {
await utils.checkConfig(); await utils.checkConfig();
await utils.checkToken(); await utils.checkToken();
utils.separator('Init');
} catch (err) { } catch (err) {
utils.criticalError(err); utils.criticalError(err);
} }
@ -53,7 +54,7 @@ class Floriday {
await utils.syncModel(model); await utils.syncModel(model);
await utils.checkConnections(); await utils.checkConnections();
utils.separator(`${cycle++} ${(cycle == 2 ? 'Cycle' : 'Cycles')}`);
} catch (err) { } catch (err) {
if (['SequelizeConnectionRefusedError', 'SequelizeConnectionError'].includes(err.name)) if (['SequelizeConnectionRefusedError', 'SequelizeConnectionError'].includes(err.name))
throw err; throw err;

View File

@ -19,12 +19,12 @@ console.log(chalk.hex('#06c581')(
)) ))
try { try {
await utils.startSpin('Creating database connection...', true); utils.startSpin('Creating database connection...');
sequelize = await createConn(); sequelize = await createConn();
await checkCon(); await checkCon();
await utils.okSpin(); utils.okSpin();
} catch (err) { } catch (err) {
await utils.criticalSpin(err); utils.criticalSpin(err);
} }
// Conf Models // Conf Models
@ -167,7 +167,7 @@ try {
try { try {
const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true }; const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true };
const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering'; const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering';
await utils.startSpin(`${actionMsg} models...`, true); utils.startSpin(`${actionMsg} models...`);
await sequelize.sync(action); await sequelize.sync(action);
/* /*
@ -178,10 +178,10 @@ try {
sequelize.query(fs.readFileSync('routines/procedures/offerRefresh.sql', 'utf-8')); sequelize.query(fs.readFileSync('routines/procedures/offerRefresh.sql', 'utf-8'));
*/ */
await utils.okSpin(); utils.okSpin();
} }
catch (err) { catch (err) {
await utils.criticalSpin(err) utils.criticalSpin(err)
} }
/** /**
@ -219,13 +219,13 @@ async function checkCon() {
* Close the connection to the database * Close the connection to the database
*/ */
async function closeCon() { async function closeCon() {
await utils.failSpin(null, true); utils.failSpin(null, true);
await utils.startSpin('Closing database connection...', true); utils.startSpin('Closing database connection...');
try { try {
await sequelize.close() await sequelize.close()
await utils.okSpin(); utils.okSpin();
} catch (err) { } catch (err) {
await utils.criticalSpin(err); utils.criticalSpin(err);
} }
} }

130
utils.js
View File

@ -10,6 +10,7 @@ import fs from 'fs';
const env = process.env; const env = process.env;
const methods = yml.load(fs.readFileSync('./methods.yml', 'utf8')); const methods = yml.load(fs.readFileSync('./methods.yml', 'utf8'));
const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8')); const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8'));
const arrow = '└─────';
let spinner; let spinner;
/** /**
@ -20,7 +21,7 @@ let spinner;
*/ */
export async function checkToken(isForce) { export async function checkToken(isForce) {
try { try {
await startSpin(`Checking token...`, true); startSpin(`Checking token...`);
const clientConfigData = await models.config.findOne(); const clientConfigData = await models.config.findOne();
@ -56,9 +57,9 @@ export async function checkToken(isForce) {
}); });
} else } else
optionalMsg = 'Using stored token...'; optionalMsg = 'Using stored token...';
await okSpin(optionalMsg, true); okSpin(optionalMsg);
} catch (err) { } catch (err) {
await failSpin(err, true); failSpin(err);
} }
}; };
@ -75,7 +76,7 @@ export async function getCurrentToken() {
* Check the floriday data config. * Check the floriday data config.
*/ */
export async function checkConfig() { export async function checkConfig() {
await startSpin(`Checking config...`, true); startSpin(`Checking config...`);
const excludedEnvVars = ['VSCODE_GIT_ASKPASS_EXTRA_ARGS']; const excludedEnvVars = ['VSCODE_GIT_ASKPASS_EXTRA_ARGS'];
const requiredEnvVars = Object.keys(env); const requiredEnvVars = Object.keys(env);
@ -83,13 +84,13 @@ export async function checkConfig() {
for (const reqEnvVar of filteredEnvVars) for (const reqEnvVar of filteredEnvVars)
if (!process.env[reqEnvVar]) if (!process.env[reqEnvVar])
await failSpin(new Error(`You haven't provided the ${reqEnvVar} environment variable`), true); failSpin(new Error(`You haven't provided the ${reqEnvVar} environment variable`));
const clientConfigData = await models.config.findOne(); const clientConfigData = await models.config.findOne();
if (!clientConfigData) if (!clientConfigData)
await updateClientConfig(env.CLIENT_ID, env.CLIENT_SECRET); await updateClientConfig(env.CLIENT_ID, env.CLIENT_SECRET);
await okSpin(null, true); okSpin();
}; };
/** /**
@ -124,8 +125,8 @@ export async function updateClientConfig(clientConfig) {
* *
* @param {Integer} ms * @param {Integer} ms
*/ */
export async function sleep(ms) { export function sleep(ms) {
await new Promise(resolve => setTimeout(resolve, ms)); new Promise(resolve => setTimeout(resolve, ms));
}; };
/** /**
@ -134,7 +135,7 @@ export async function sleep(ms) {
* @param {String} model Supported models (./models/methods.yml) * @param {String} model Supported models (./models/methods.yml)
*/ */
export async function syncModel(model) { export async function syncModel(model) {
await startSpin(`Syncing ${model}...`, true); startSpin(`Syncing ${model}...`);
try { try {
const dbSeqNum = await models.sequenceNumber.findOne({ where: { model } }) const dbSeqNum = await models.sequenceNumber.findOne({ where: { model } })
let curSeqNum = dbSeqNum?.maxSequenceNumber ?? 0, i = 0; let curSeqNum = dbSeqNum?.maxSequenceNumber ?? 0, i = 0;
@ -166,9 +167,9 @@ export async function syncModel(model) {
txtSpin((i) txtSpin((i)
? `Syncing ${i} ${model}...` ? `Syncing ${i} ${model}...`
: `Syncing ${model}...`); : `Syncing ${model}...`);
await okSpin(null, true); okSpin();
} catch (err) { } catch (err) {
await failSpin(err, true); failSpin(err);
} }
}; };
@ -209,12 +210,12 @@ export async function insertModel(model, data) {
*/ */
export async function checkConnections(){ export async function checkConnections(){
try { try {
startSpin('Checking connections...', true); startSpin('Checking connections...');
await createConnections(); await createConnections();
await deleteConnections(); await deleteConnections();
await okSpin(null, true); if (spinner) okSpin();
} catch (err) { } catch (err) {
await failSpin(err, true); failSpin(err);
} }
}; };
@ -663,15 +664,18 @@ export async function createConnections() {
}); });
let connectionsToPut = [], i = 0; let connectionsToPut = [], i = 0;
dbConnections.forEach(valor => { dbConnections.forEach(value => {
if (!flConnections.includes(valor.organizationId)) if (!flConnections.includes(value.organizationId))
connectionsToPut.push(valor.organizationId); connectionsToPut.push(value.organizationId);
}); });
if (connectionsToPut.length && !spinner) startSpin('Creating connections...');
for (let connection of connectionsToPut) { for (let connection of connectionsToPut) {
await vnRequest('PUT', `${env.API_URL}${methods.connections.base.url}${connection}`); await vnRequest('PUT', `${env.API_URL}${methods.connections.base.url}${connection}`);
txtSpin(`Creating ${i++} connections, ${connectionsToPut.length - i} missing...`); txtSpin(`Creating ${i++} connections, ${connectionsToPut.length - i} missing...`);
} }
if (spinner && i) okSpin(`Creating ${i++} connections...`);
} catch (err) { } catch (err) {
throw err; throw err;
} }
@ -684,25 +688,25 @@ export async function deleteConnections() {
try { try {
const flConnections = (await vnRequest('GET', `${env.API_URL}/connections`)).data; const flConnections = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
const dbConnections = await models.organization.findAll({ const dbConnections = await models.organization.findAll({
attributes: ['organizationId'], where: { isConnected: false }
where: { isConnected: true }
}); });
let ghostConnections = [], i = 0; let ghostConnections = [], i = 0;
flConnections.forEach(valor => { dbConnections.forEach(value => {
if (!dbConnections.includes(valor)) if (flConnections.includes(value.organizationId))
ghostConnections.push(valor); ghostConnections.push(value.organizationId);
}); });
if (ghostConnections.length && !spinner) startSpin('Deleting connections...');
for (let connection of ghostConnections) { for (let connection of ghostConnections) {
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`); await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`);
(spinner) txtSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`)
? txtSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`)
: startSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`, true);
} }
if (spinner) okSpin(null, true);
if (spinner && i) okSpin(`Deleting ${i++} connections...`);
} catch (err) { } catch (err) {
await criticalSpin(err); criticalSpin(err);
} }
}; };
@ -733,9 +737,8 @@ export async function vnRequest(method, url, data, headers) {
switch (err.code) { switch (err.code) {
case 'ECONNRESET': // Client network socket TLS case 'ECONNRESET': // Client network socket TLS
case 'EAI_AGAIN': // getaddrinfo case 'EAI_AGAIN': // getaddrinfo
await warnSpin(null, err, false); warnSpin(null, err, false);
await sleep(1000); sleep(1000);
await startSpin(null, true);
break; break;
case 'ECONNABORTED': case 'ECONNABORTED':
case 'ECONNREFUSED': case 'ECONNREFUSED':
@ -745,36 +748,32 @@ export async function vnRequest(method, url, data, headers) {
return err; return err;
case 504: case 504:
case 502: case 502:
await warnSpin(null, err, false); warnSpin(null, err, false);
await sleep(1000); sleep(1000);
await startSpin(null, true);
break; break;
case 429: // Too Many Requests case 429: // Too Many Requests
await warnSpin(null, err, false); warnSpin(null, err, false);
await sleep(60000); sleep(60000);
await startSpin(null, true);
break; break;
case 401: // Unauthorized case 401: // Unauthorized
await warnSpin(null, err, false); warnSpin(null, err, false);
await checkToken(true); await checkToken(true);
headers.Authorization headers.Authorization
? headers.Authorization = `Bearer ${await getCurrentToken()}` ? headers.Authorization = `Bearer ${await getCurrentToken()}`
: criticalError(err); : criticalError(err);
await startSpin(null, true);
break; break;
default: default:
await warnSpin(null, err, false); warnSpin(null, err, false);
await sleep(env.MS_RETRY_UNHANDLED_ERROR); sleep(env.MS_RETRY_UNHANDLED_ERROR);
await startSpin(null, true);
break; break;
} }
break; break;
default: default:
await warnSpin(null, err, false); warnSpin(null, err, false);
await sleep(env.MS_RETRY_UNHANDLED_ERROR); sleep(env.MS_RETRY_UNHANDLED_ERROR);
await startSpin(null, true);
break; break;
} }
startSpin();
} }
} }
}; };
@ -785,11 +784,11 @@ export async function vnRequest(method, url, data, headers) {
* @param {String} msg Text of spinner * @param {String} msg Text of spinner
* @param {Boolean} isNew Reinstantiate the object * @param {Boolean} isNew Reinstantiate the object
**/ **/
export async function startSpin(msg, isNew) { export function startSpin(msg, isKeep) {
if (JSON.parse(env.TIME_STAMPS) && msg) if (JSON.parse(env.TIME_STAMPS) && msg)
msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`;
(!isNew) (isKeep)
? spinner.start() ? spinner.start()
: spinner = ora({ : spinner = ora({
text: msg, text: msg,
@ -804,7 +803,7 @@ export async function startSpin(msg, isNew) {
* *
* @param {String} msg Text of spinner * @param {String} msg Text of spinner
**/ **/
export async function txtSpin(msg) { export function txtSpin(msg) {
if (JSON.parse(env.TIME_STAMPS) && msg) if (JSON.parse(env.TIME_STAMPS) && msg)
msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`;
@ -815,15 +814,13 @@ export async function txtSpin(msg) {
* Sets the spinner to ok. * Sets the spinner to ok.
* *
* @param {String} msg Text of spinner * @param {String} msg Text of spinner
* @param {Boolean} clear Clean the instance
**/ **/
export async function okSpin(msg, clear) { export function okSpin(msg) {
if (JSON.parse(env.TIME_STAMPS) && msg) if (JSON.parse(env.TIME_STAMPS) && msg)
msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg ?? ''}`; msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg ?? ''}`;
if (spinner) { if (spinner) {
spinner.succeed(msg); spinner.succeed(msg);
if (clear)
spinner = null; spinner = null;
} }
}; };
@ -835,28 +832,25 @@ export async function okSpin(msg, clear) {
* @param {Error} err Error object * @param {Error} err Error object
* @param {Boolean} clear Clean the instance * @param {Boolean} clear Clean the instance
**/ **/
export async function warnSpin(msg, err, clear) { export function warnSpin(msg, err, clear) {
if (JSON.parse(env.TIME_STAMPS) && msg) if (JSON.parse(env.TIME_STAMPS) && msg)
msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`;
if (spinner) { if (spinner) {
spinner.warn(msg); spinner.warn(msg);
if (clear) if (clear) spinner = null;
spinner = null;
} }
if (err) await warning(err); if (err) warning(err);
}; };
/** /**
* Sets the spinner to fail and throw a error. * Sets the spinner to fail and throw a error.
* *
* @param {Error} err Error object * @param {Error} err Error object
* @param {Boolean} clear Clean the instance
**/ **/
export async function failSpin(err, clear) { export function failSpin(err) {
if (spinner) { if (spinner) {
spinner.fail(); spinner.fail();
if (clear)
spinner = null; spinner = null;
} }
if (err) throw err; if (err) throw err;
@ -867,21 +861,31 @@ export async function failSpin(err, clear) {
* *
* @param {Error} err Error object * @param {Error} err Error object
**/ **/
export async function criticalSpin(err) { export function criticalSpin(err) {
if (spinner) { if (spinner) {
spinner.fail(); spinner.fail();
spinner = null; spinner = null;
} }
await criticalError(err); criticalError(err);
}; };
/**
* Separator.
*
* @param {String} msg String to show
**/
export function separator(msg) {
console.log(chalk.gray(` ──────────────────────── ${msg}`));
};
/** /**
* Critical error. * Critical error.
* *
* @param {Error} err Error object * @param {Error} err Error object
**/ **/
export async function criticalError(err) { export function criticalError(err) {
const msg = `${chalk.red.bold('└─────')} ${chalk.red.bold('[CRITICAL]')}`; const msg = `${chalk.red.bold(arrow)} ${chalk.red.bold('[CRITICAL]')}`;
console.log(`${msg} ${chalk.red(err.message)}`); console.log(`${msg} ${chalk.red(err.message)}`);
process.exit(); process.exit();
}; };
@ -891,7 +895,7 @@ export async function criticalError(err) {
* *
* @param {Error} err * @param {Error} err
**/ **/
export async function warning(err) { export function warning(err) {
const msg = `${chalk.yellow.bold('└─────')} ${chalk.yellow.bold('[WARNING]')}`; const msg = `${chalk.yellow.bold(arrow)} ${chalk.yellow.bold('[WARNING]')}`;
console.log(`${msg} ${chalk.yellow((err.response?.status && err.response?.data?.message) ?? err.message)}`); console.log(`${msg} ${chalk.yellow((err.response?.status && err.response?.data?.message) ?? err.message)}`);
}; };