From 02cd8124baca6799e166ba97a1fa84b4085010d8 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 16 Jun 2023 11:50:40 +0200 Subject: [PATCH] refs #4823 Minor big changes --- floriday.js | 5 +- models/sequelize.js | 20 +++---- utils.js | 134 +++++++++++++++++++++++--------------------- 3 files changed, 82 insertions(+), 77 deletions(-) diff --git a/floriday.js b/floriday.js index 6112dce..01a1e3e 100644 --- a/floriday.js +++ b/floriday.js @@ -6,12 +6,13 @@ import fs from 'fs'; const env = process.env; const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8')); - +let cycle = 1; class Floriday { async start() { try { await utils.checkConfig(); await utils.checkToken(); + utils.separator('Init'); } catch (err) { utils.criticalError(err); } @@ -53,7 +54,7 @@ class Floriday { await utils.syncModel(model); await utils.checkConnections(); - + utils.separator(`${cycle++} ${(cycle == 2 ? 'Cycle' : 'Cycles')}`); } catch (err) { if (['SequelizeConnectionRefusedError', 'SequelizeConnectionError'].includes(err.name)) throw err; diff --git a/models/sequelize.js b/models/sequelize.js index 50fca49..c451496 100644 --- a/models/sequelize.js +++ b/models/sequelize.js @@ -19,12 +19,12 @@ console.log(chalk.hex('#06c581')( )) try { - await utils.startSpin('Creating database connection...', true); + utils.startSpin('Creating database connection...'); sequelize = await createConn(); await checkCon(); - await utils.okSpin(); + utils.okSpin(); } catch (err) { - await utils.criticalSpin(err); + utils.criticalSpin(err); } // Conf Models @@ -167,7 +167,7 @@ try { try { const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true }; const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering'; - await utils.startSpin(`${actionMsg} models...`, true); + utils.startSpin(`${actionMsg} models...`); await sequelize.sync(action); /* @@ -178,10 +178,10 @@ try { sequelize.query(fs.readFileSync('routines/procedures/offerRefresh.sql', 'utf-8')); */ - await utils.okSpin(); + utils.okSpin(); } catch (err) { - await utils.criticalSpin(err) + utils.criticalSpin(err) } /** @@ -219,13 +219,13 @@ async function checkCon() { * Close the connection to the database */ async function closeCon() { - await utils.failSpin(null, true); - await utils.startSpin('Closing database connection...', true); + utils.failSpin(null, true); + utils.startSpin('Closing database connection...'); try { await sequelize.close() - await utils.okSpin(); + utils.okSpin(); } catch (err) { - await utils.criticalSpin(err); + utils.criticalSpin(err); } } diff --git a/utils.js b/utils.js index 05f9ae9..e3475fa 100644 --- a/utils.js +++ b/utils.js @@ -10,6 +10,7 @@ import fs from 'fs'; const env = process.env; const methods = yml.load(fs.readFileSync('./methods.yml', 'utf8')); const flModels = yml.load(fs.readFileSync('./models/models.yml', 'utf8')); +const arrow = '└─────'; let spinner; /** @@ -20,7 +21,7 @@ let spinner; */ export async function checkToken(isForce) { try { - await startSpin(`Checking token...`, true); + startSpin(`Checking token...`); const clientConfigData = await models.config.findOne(); @@ -56,9 +57,9 @@ export async function checkToken(isForce) { }); } else optionalMsg = 'Using stored token...'; - await okSpin(optionalMsg, true); + okSpin(optionalMsg); } catch (err) { - await failSpin(err, true); + failSpin(err); } }; @@ -75,7 +76,7 @@ export async function getCurrentToken() { * Check the floriday data config. */ export async function checkConfig() { - await startSpin(`Checking config...`, true); + startSpin(`Checking config...`); const excludedEnvVars = ['VSCODE_GIT_ASKPASS_EXTRA_ARGS']; const requiredEnvVars = Object.keys(env); @@ -83,13 +84,13 @@ export async function checkConfig() { for (const reqEnvVar of filteredEnvVars) 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(); if (!clientConfigData) 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 */ -export async function sleep(ms) { - await new Promise(resolve => setTimeout(resolve, ms)); +export function sleep(ms) { + new Promise(resolve => setTimeout(resolve, ms)); }; /** @@ -134,7 +135,7 @@ export async function sleep(ms) { * @param {String} model Supported models (./models/methods.yml) */ export async function syncModel(model) { - await startSpin(`Syncing ${model}...`, true); + startSpin(`Syncing ${model}...`); try { const dbSeqNum = await models.sequenceNumber.findOne({ where: { model } }) let curSeqNum = dbSeqNum?.maxSequenceNumber ?? 0, i = 0; @@ -166,9 +167,9 @@ export async function syncModel(model) { txtSpin((i) ? `Syncing ${i} ${model}...` : `Syncing ${model}...`); - await okSpin(null, true); + okSpin(); } catch (err) { - await failSpin(err, true); + failSpin(err); } }; @@ -209,12 +210,12 @@ export async function insertModel(model, data) { */ export async function checkConnections(){ try { - startSpin('Checking connections...', true); + startSpin('Checking connections...'); await createConnections(); await deleteConnections(); - await okSpin(null, true); + if (spinner) okSpin(); } catch (err) { - await failSpin(err, true); + failSpin(err); } }; @@ -663,15 +664,18 @@ export async function createConnections() { }); let connectionsToPut = [], i = 0; - dbConnections.forEach(valor => { - if (!flConnections.includes(valor.organizationId)) - connectionsToPut.push(valor.organizationId); + dbConnections.forEach(value => { + if (!flConnections.includes(value.organizationId)) + connectionsToPut.push(value.organizationId); }); + if (connectionsToPut.length && !spinner) startSpin('Creating connections...'); + for (let connection of connectionsToPut) { await vnRequest('PUT', `${env.API_URL}${methods.connections.base.url}${connection}`); txtSpin(`Creating ${i++} connections, ${connectionsToPut.length - i} missing...`); } + if (spinner && i) okSpin(`Creating ${i++} connections...`); } catch (err) { throw err; } @@ -684,25 +688,25 @@ export async function deleteConnections() { try { const flConnections = (await vnRequest('GET', `${env.API_URL}/connections`)).data; const dbConnections = await models.organization.findAll({ - attributes: ['organizationId'], - where: { isConnected: true } + where: { isConnected: false } }); let ghostConnections = [], i = 0; - flConnections.forEach(valor => { - if (!dbConnections.includes(valor)) - ghostConnections.push(valor); + dbConnections.forEach(value => { + if (flConnections.includes(value.organizationId)) + ghostConnections.push(value.organizationId); }); + if (ghostConnections.length && !spinner) startSpin('Deleting connections...'); + for (let connection of ghostConnections) { await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`); - (spinner) - ? txtSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`) - : startSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`, true); + txtSpin(`Deleting ${i++} connections, ${ghostConnections.length - i} missing...`) } - if (spinner) okSpin(null, true); + + if (spinner && i) okSpin(`Deleting ${i++} connections...`); } catch (err) { - await criticalSpin(err); + criticalSpin(err); } }; @@ -733,9 +737,8 @@ export async function vnRequest(method, url, data, headers) { switch (err.code) { case 'ECONNRESET': // Client network socket TLS case 'EAI_AGAIN': // getaddrinfo - await warnSpin(null, err, false); - await sleep(1000); - await startSpin(null, true); + warnSpin(null, err, false); + sleep(1000); break; case 'ECONNABORTED': case 'ECONNREFUSED': @@ -745,36 +748,32 @@ export async function vnRequest(method, url, data, headers) { return err; case 504: case 502: - await warnSpin(null, err, false); - await sleep(1000); - await startSpin(null, true); + warnSpin(null, err, false); + sleep(1000); break; case 429: // Too Many Requests - await warnSpin(null, err, false); - await sleep(60000); - await startSpin(null, true); + warnSpin(null, err, false); + sleep(60000); break; case 401: // Unauthorized - await warnSpin(null, err, false); + warnSpin(null, err, false); await checkToken(true); headers.Authorization ? headers.Authorization = `Bearer ${await getCurrentToken()}` : criticalError(err); - await startSpin(null, true); break; default: - await warnSpin(null, err, false); - await sleep(env.MS_RETRY_UNHANDLED_ERROR); - await startSpin(null, true); + warnSpin(null, err, false); + sleep(env.MS_RETRY_UNHANDLED_ERROR); break; } break; default: - await warnSpin(null, err, false); - await sleep(env.MS_RETRY_UNHANDLED_ERROR); - await startSpin(null, true); + warnSpin(null, err, false); + sleep(env.MS_RETRY_UNHANDLED_ERROR); break; } + startSpin(); } } }; @@ -785,11 +784,11 @@ export async function vnRequest(method, url, data, headers) { * @param {String} msg Text of spinner * @param {Boolean} isNew Reinstantiate the object **/ -export async function startSpin(msg, isNew) { +export function startSpin(msg, isKeep) { if (JSON.parse(env.TIME_STAMPS) && msg) msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; - (!isNew) + (isKeep) ? spinner.start() : spinner = ora({ text: msg, @@ -804,7 +803,7 @@ export async function startSpin(msg, isNew) { * * @param {String} msg Text of spinner **/ -export async function txtSpin(msg) { +export function txtSpin(msg) { if (JSON.parse(env.TIME_STAMPS) && msg) msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; @@ -815,16 +814,14 @@ export async function txtSpin(msg) { * Sets the spinner to ok. * * @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) msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg ?? ''}`; if (spinner) { spinner.succeed(msg); - if (clear) - spinner = null; + spinner = null; } }; @@ -835,29 +832,26 @@ export async function okSpin(msg, clear) { * @param {Error} err Error object * @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) msg = `${chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`)} ${msg}`; if (spinner) { spinner.warn(msg); - if (clear) - spinner = null; + if (clear) spinner = null; } - if (err) await warning(err); + if (err) warning(err); }; /** * Sets the spinner to fail and throw a error. * * @param {Error} err Error object - * @param {Boolean} clear Clean the instance **/ -export async function failSpin(err, clear) { +export function failSpin(err) { if (spinner) { spinner.fail(); - if (clear) - spinner = null; + spinner = null; } if (err) throw err; }; @@ -867,21 +861,31 @@ export async function failSpin(err, clear) { * * @param {Error} err Error object **/ -export async function criticalSpin(err) { +export function criticalSpin(err) { if (spinner) { spinner.fail(); 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. * * @param {Error} err Error object **/ -export async function criticalError(err) { - const msg = `${chalk.red.bold('└─────')} ${chalk.red.bold('[CRITICAL]')}`; +export function criticalError(err) { + const msg = `${chalk.red.bold(arrow)} ${chalk.red.bold('[CRITICAL]')}`; console.log(`${msg} ${chalk.red(err.message)}`); process.exit(); }; @@ -891,7 +895,7 @@ export async function criticalError(err) { * * @param {Error} err **/ -export async function warning(err) { - const msg = `${chalk.yellow.bold('└─────')} ${chalk.yellow.bold('[WARNING]')}`; +export function warning(err) { + const msg = `${chalk.yellow.bold(arrow)} ${chalk.yellow.bold('[WARNING]')}`; console.log(`${msg} ${chalk.yellow((err.response?.status && err.response?.data?.message) ?? err.message)}`); }; \ No newline at end of file