@@ -3,6 +3,7 @@ import { storePulse, eventEmitter } from "./clickhouse";
33import { config } from "./config" ;
44import { Logger } from "./logger" ;
55import { NotificationManager } from "./notifications" ;
6+ import { GRACE_PERIOD , isInGracePeriod , STARTUP_TIME } from "./times" ;
67import type { MissingPulseDetectorOptions , Monitor , NotificationsConfig } from "./types" ;
78
89export class MissingPulseDetector {
@@ -13,23 +14,12 @@ export class MissingPulseDetector {
1314 private consecutiveDownCounts : Map < string , number > = new Map ( ) ;
1415 private lastNotificationDownCount : Map < string , number > = new Map ( ) ;
1516 private notificationManager : NotificationManager ;
16- private startupTime : number ;
17- private gracePeriod : number ;
1817
1918 constructor ( options : MissingPulseDetectorOptions = { } ) {
2019 this . checkInterval = options . checkInterval || 30000 ; // Check every 30 seconds globally
21- this . gracePeriod = 300000 ; // 5 minutes default
22- this . startupTime = Date . now ( ) ;
2320 this . notificationManager = new NotificationManager ( config . notifications || { channels : { } } ) ;
2421 }
2522
26- /**
27- * Check if we're still in the startup grace period
28- */
29- private isInGracePeriod ( ) : boolean {
30- return Date . now ( ) - this . startupTime < this . gracePeriod ;
31- }
32-
3323 /**
3424 * Start the missing pulse detection
3525 */
@@ -88,17 +78,17 @@ export class MissingPulseDetector {
8878 // No status data yet - monitor hasn't sent its first pulse
8979 if ( ! status ) {
9080 // Check if enough time has passed since startup to consider this a problem
91- const timeSinceStartup = now - this . startupTime ;
81+ const timeSinceStartup = now - STARTUP_TIME ;
9282 const expectedInterval = monitor . interval * 1000 ;
9383 const maxAllowedInterval = expectedInterval * monitor . toleranceFactor ;
9484
9585 // Only start checking after grace period + one full interval
96- if ( timeSinceStartup > this . gracePeriod + maxAllowedInterval ) {
86+ if ( timeSinceStartup > GRACE_PERIOD + maxAllowedInterval ) {
9787 Logger . warn ( "Monitor has never sent a pulse" , {
9888 monitorId : monitor . id ,
9989 monitorName : monitor . name ,
10090 timeSinceStartup : Math . round ( timeSinceStartup / 1000 ) + "s" ,
101- gracePeriod : this . gracePeriod / 1000 + "s" ,
91+ gracePeriod : GRACE_PERIOD / 1000 + "s" ,
10292 } ) ;
10393
10494 await this . handleMissingPulse ( monitor , timeSinceStartup , expectedInterval ) ;
@@ -150,11 +140,11 @@ export class MissingPulseDetector {
150140 missedIntervals,
151141 consecutiveMisses : missedCount ,
152142 maxRetries : monitor . maxRetries ,
153- inGracePeriod : this . isInGracePeriod ( ) ,
143+ inGracePeriod : isInGracePeriod ( ) ,
154144 } ) ;
155145
156146 // Don't mark monitors as down during grace period
157- if ( this . isInGracePeriod ( ) ) {
147+ if ( isInGracePeriod ( ) ) {
158148 Logger . info ( "Skipping status change during grace period" , {
159149 monitorId : monitor . id ,
160150 monitorName : monitor . name ,
0 commit comments