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 path = require("path");
const SALIX_VERSION_HEADER = "Salix-version";
const CONFIG_FILENAME = '/config/salix.local.yml';
const CONFIG_FILENAME = "/config/salix.local.yml";
const i18n = {
versionChanged: "La versión ha cambiado",
deprecatedVersion: "La configuracion guardada está desactualizada",
noDeprecatedVersion: "La configuración guardada no requiere actualización",
erroFs:{
noExists: 'El archivo no existe',
noWrite: 'Error al escribir el archivo YAML:',
noRead: 'Error al leer el archivo YAML:'
}
erroFs: {
noExists: "El archivo no existe",
noWrite: "Error al escribir el archivo YAML:",
noRead: "Error al leer el archivo YAML:",
},
};
module.exports = class Salix {
constructor() {
@ -19,6 +19,10 @@ module.exports = class Salix {
this._salixVersion = null;
this._salixConfig = null;
}
fooListener(val) {}
registerNewListener(externalListenerFunction) {
this.fooListener = externalListenerFunction;
}
async init(logger) {
this.conf = logger.conf;
const salixFileConfig = await this.loadConfig();
@ -29,11 +33,10 @@ module.exports = class Salix {
this._salixConfig = salixFileConfig;
//Obtengo la version
await this.getSalixVersion(false);
this._salixVersion && this.handleVersion(salixFileConfig);
this.salixVersion && this.handleVersion(salixFileConfig);
this._logInfo = salixFileConfig.log;
}
} catch (error) {
}
} catch (error) {}
setInterval(
() => this.getSalixVersion(),
@ -63,7 +66,7 @@ module.exports = class Salix {
async saveConfig(configValue) {
const defaultConfig = {
salixVersion: this._salixVersion,
salixVersion: this.salixVersion,
};
try {
const contenidoYAML = fs.writeFileSync(
@ -90,7 +93,7 @@ module.exports = class Salix {
async handleVersion(salixFileConfig) {
const isDeprecated = this.compareVersion(
this._salixVersion,
this.salixVersion,
salixFileConfig.salixVersion
);
isDeprecated && (await this.salixLogInfo());
@ -106,6 +109,7 @@ module.exports = class Salix {
i18n.deprecatedVersion
);
deprecatedConfig = true;
break;
} else if (version1[i] < version2[i]) {
console.log(
// `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);
}
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(
this._salixVersion,
newVersion
newVersion,
this.salixVersion
);
if (isDeprecated) {
this._salixVersion = newVersion;
this.salixVersion = newVersion;
console.info(i18n.versionChanged);
await this.salixLogInfo();
}
} else this._salixVersion = newVersion;
}
return response;
} catch ({ message }) {
console.error(message);
@ -174,4 +179,9 @@ module.exports = class Salix {
get 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() {
const conf = (this.conf = loadConfig(__dirname, 'config'));
const salix = new Salix();
salix.registerNewListener(() => {
this.stop();
this.start();
});
await salix.init(this);
// await salix.salixLogInfo();
// salix.registerNewListener((val) => console.log(`New Value: ${val}`));
this.modelLoader.init(this, salix.logInfo);
this.showDb.init(this);
const includeSchema = {};
for (const [schemaName, tableMap] of this.schemaMap.map)
includeSchema[schemaName] = Array.from(tableMap.keys());