feat: listener to restart
gitea/mylogger/pipeline/pr-master This commit looks good Details

This commit is contained in:
Javier Segarra 2024-05-06 14:12:33 +02:00
parent 206b279ced
commit bd48afa684
2 changed files with 31 additions and 19 deletions

View File

@ -1,16 +1,16 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const SALIX_VERSION_HEADER = "Salix-version"; const SALIX_VERSION_HEADER = "Salix-version";
const CONFIG_FILENAME = '/config/salix.local.yml'; const CONFIG_FILENAME = "/config/salix.local.yml";
const i18n = { const i18n = {
versionChanged: "La versión ha cambiado", versionChanged: "La versión ha cambiado",
deprecatedVersion: "La configuracion guardada está desactualizada", deprecatedVersion: "La configuracion guardada está desactualizada",
noDeprecatedVersion: "La configuración guardada no requiere actualización", noDeprecatedVersion: "La configuración guardada no requiere actualización",
erroFs:{ erroFs: {
noExists: 'El archivo no existe', noExists: "El archivo no existe",
noWrite: 'Error al escribir el archivo YAML:', noWrite: "Error al escribir el archivo YAML:",
noRead: 'Error al leer el archivo YAML:' noRead: "Error al leer el archivo YAML:",
} },
}; };
module.exports = class Salix { module.exports = class Salix {
constructor() { constructor() {
@ -19,6 +19,10 @@ module.exports = class Salix {
this._salixVersion = null; this._salixVersion = null;
this._salixConfig = null; this._salixConfig = null;
} }
fooListener(val) {}
registerNewListener(externalListenerFunction) {
this.fooListener = externalListenerFunction;
}
async init(logger) { async init(logger) {
this.conf = logger.conf; this.conf = logger.conf;
const salixFileConfig = await this.loadConfig(); const salixFileConfig = await this.loadConfig();
@ -29,11 +33,10 @@ module.exports = class Salix {
this._salixConfig = salixFileConfig; this._salixConfig = salixFileConfig;
//Obtengo la version //Obtengo la version
await this.getSalixVersion(false); await this.getSalixVersion(false);
this._salixVersion && this.handleVersion(salixFileConfig); this.salixVersion && this.handleVersion(salixFileConfig);
this._logInfo = salixFileConfig.log; this._logInfo = salixFileConfig.log;
} }
} catch (error) { } catch (error) {}
}
setInterval( setInterval(
() => this.getSalixVersion(), () => this.getSalixVersion(),
@ -63,7 +66,7 @@ module.exports = class Salix {
async saveConfig(configValue) { async saveConfig(configValue) {
const defaultConfig = { const defaultConfig = {
salixVersion: this._salixVersion, salixVersion: this.salixVersion,
}; };
try { try {
const contenidoYAML = fs.writeFileSync( const contenidoYAML = fs.writeFileSync(
@ -90,7 +93,7 @@ module.exports = class Salix {
async handleVersion(salixFileConfig) { async handleVersion(salixFileConfig) {
const isDeprecated = this.compareVersion( const isDeprecated = this.compareVersion(
this._salixVersion, this.salixVersion,
salixFileConfig.salixVersion salixFileConfig.salixVersion
); );
isDeprecated && (await this.salixLogInfo()); isDeprecated && (await this.salixLogInfo());
@ -106,6 +109,7 @@ module.exports = class Salix {
i18n.deprecatedVersion i18n.deprecatedVersion
); );
deprecatedConfig = true; deprecatedConfig = true;
break;
} else if (version1[i] < version2[i]) { } else if (version1[i] < version2[i]) {
console.log( console.log(
// `La versión ${v1} es menor que la versión ${v2}` // `La versión ${v1} es menor que la versión ${v2}`
@ -128,17 +132,18 @@ module.exports = class Salix {
throw new Error(response.error.message); throw new Error(response.error.message);
} }
const newVersion = salixCall.headers.get(SALIX_VERSION_HEADER); const newVersion = salixCall.headers.get(SALIX_VERSION_HEADER);
if (this._salixVersion) { if(!this.salixVersion) this.salixVersion = newVersion;
if (this.salixVersion!==newVersion) {
const isDeprecated = this.compareVersion( const isDeprecated = this.compareVersion(
this._salixVersion, newVersion,
newVersion this.salixVersion
); );
if (isDeprecated) { if (isDeprecated) {
this._salixVersion = newVersion; this.salixVersion = newVersion;
console.info(i18n.versionChanged); console.info(i18n.versionChanged);
await this.salixLogInfo(); await this.salixLogInfo();
} }
} else this._salixVersion = newVersion; }
return response; return response;
} catch ({ message }) { } catch ({ message }) {
console.error(message); console.error(message);
@ -174,4 +179,9 @@ module.exports = class Salix {
get salixVersion() { get salixVersion() {
return this._salixVersion; return this._salixVersion;
} }
set salixVersion(newVersion) {
if(this._salixVersion) this.fooListener(newVersion);
this._salixVersion = newVersion;
}
}; };

View File

@ -22,12 +22,14 @@ module.exports = class MyLogger {
async start() { async start() {
const conf = (this.conf = loadConfig(__dirname, 'config')); const conf = (this.conf = loadConfig(__dirname, 'config'));
const salix = new Salix(); const salix = new Salix();
salix.registerNewListener(() => {
this.stop();
this.start();
});
await salix.init(this); await salix.init(this);
// await salix.salixLogInfo(); // await salix.salixLogInfo();
// salix.registerNewListener((val) => console.log(`New Value: ${val}`));
this.modelLoader.init(this, salix.logInfo); this.modelLoader.init(this, salix.logInfo);
this.showDb.init(this); this.showDb.init(this);
const includeSchema = {}; const includeSchema = {};
for (const [schemaName, tableMap] of this.schemaMap.map) for (const [schemaName, tableMap] of this.schemaMap.map)
includeSchema[schemaName] = Array.from(tableMap.keys()); includeSchema[schemaName] = Array.from(tableMap.keys());