kukuy 1.6.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,9 @@ const https = require('https');
3
3
  const url = require('url');
4
4
 
5
5
  class HealthChecker {
6
- constructor() {
6
+ constructor(balancer = null) {
7
7
  this.timeout = 5000; // 5 segundos de timeout
8
+ this.balancer = balancer; // Referencia al balanceador para acceder a los hooks
8
9
  }
9
10
 
10
11
  async checkServerHealth(server) {
@@ -22,6 +23,9 @@ class HealthChecker {
22
23
  }
23
24
  };
24
25
 
26
+ // Emitir hook antes de realizar el check
27
+ await this.executeHook('onHealthCheckStart', { server, checkTime: new Date() });
28
+
25
29
  return new Promise((resolve) => {
26
30
  const request = server.protocol === 'https:'
27
31
  ? https.request(options)
@@ -33,25 +37,76 @@ class HealthChecker {
33
37
 
34
38
  // Considerar saludable si obtenemos una respuesta exitosa
35
39
  const isHealthy = res.statusCode >= 200 && res.statusCode < 400;
36
- resolve(isHealthy);
40
+
41
+ // Emitir hook después de completar el check
42
+ this.executeHook('onHealthCheckComplete', {
43
+ server,
44
+ isHealthy,
45
+ statusCode: res.statusCode,
46
+ checkTime: new Date()
47
+ }).then(() => {
48
+ // Resolver la promesa después de emitir el hook
49
+ resolve(isHealthy);
50
+ }).catch(() => {
51
+ // En caso de error al emitir el hook, resolver igualmente
52
+ resolve(isHealthy);
53
+ });
37
54
  });
38
55
 
39
56
  request.on('error', (err) => {
40
- // No imprimir errores de health check en consola para evitar spam
41
- resolve(false);
57
+ // Emitir hook cuando ocurre un error en el check
58
+ this.executeHook('onHealthCheckError', {
59
+ server,
60
+ error: err,
61
+ checkTime: new Date()
62
+ }).then(() => {
63
+ // No imprimir errores de health check en consola para evitar spam
64
+ resolve(false);
65
+ }).catch(() => {
66
+ resolve(false);
67
+ });
42
68
  });
43
69
 
44
70
  request.on('timeout', () => {
45
- // No imprimir errores de timeout de health check en consola
46
- resolve(false);
71
+ // Emitir hook cuando ocurre un timeout en el check
72
+ this.executeHook('onHealthCheckTimeout', {
73
+ server,
74
+ checkTime: new Date()
75
+ }).then(() => {
76
+ // No imprimir errores de timeout de health check en consola
77
+ resolve(false);
78
+ }).catch(() => {
79
+ resolve(false);
80
+ });
47
81
  });
48
82
 
49
83
  request.end();
50
84
  });
51
85
  } catch (error) {
86
+ // Emitir hook cuando ocurre una excepción
87
+ await this.executeHook('onHealthCheckException', {
88
+ server,
89
+ error,
90
+ checkTime: new Date()
91
+ });
52
92
  return false;
53
93
  }
54
94
  }
95
+
96
+ /**
97
+ * Execute a hook if the balancer reference is available
98
+ * @param {string} hookName - Name of the hook to execute
99
+ * @param {Object} data - Data to pass to the hook
100
+ */
101
+ async executeHook(hookName, data) {
102
+ if (this.balancer && this.balancer.hookManager) {
103
+ try {
104
+ await this.balancer.hookManager.executeHooks(hookName, data);
105
+ } catch (error) {
106
+ console.error(`Error executing ${hookName} hook:`, error);
107
+ }
108
+ }
109
+ }
55
110
  }
56
111
 
57
112
  module.exports = { HealthChecker };