diff --git a/src/services/maintenance.ts b/src/services/maintenance.ts index 50e5a5c..a0691d2 100644 --- a/src/services/maintenance.ts +++ b/src/services/maintenance.ts @@ -9,6 +9,7 @@ export class MaintenanceService { private static _lastRestartAttempt: number = 0; private static _lastEvolutionState: string | null = null; private static _lastStateChangeTs: number = 0; + private static readonly _processStartSec: number = Math.floor(Date.now() / 1000); private static get retentionDays(): number { const v = Number(process.env.GROUP_MEMBERS_INACTIVE_RETENTION_DAYS); @@ -160,8 +161,8 @@ export class MaintenanceService { // Logging solo en primer muestreo o cuando cambie if (!prev) { console.log(`[HealthCheck] Estado inicial de la instancia '${instance}': ${newState}`); - this._lastStateChangeTs = nowSec; - try { Metrics.set('evolution_instance_last_state_change_ts', nowSec, { instance: String(instance || '') }); } catch {} + this._lastStateChangeTs = this._processStartSec; + try { Metrics.set('evolution_instance_last_state_change_ts', this._processStartSec, { instance: String(instance || '') }); } catch {} } else if (prev !== newState) { console.log(`[HealthCheck] Cambio de estado en instancia '${instance}': ${prev} → ${newState}`); this._lastStateChangeTs = nowSec; @@ -172,6 +173,9 @@ export class MaintenanceService { } this._lastEvolutionState = newState; + try { + Metrics.set('evolution_instance_state_age_seconds', Math.max(0, nowSec - this._lastStateChangeTs), { instance: String(instance || '') }); + } catch {} }; try {