@@ -107,27 +107,45 @@ int find_pid(int pid)
107107
108108time_t get_heartbeat_time (int i )
109109{
110- time_t t = time (NULL );
111- return t - apps [i ].last_heartbeat ;
110+ time_t now = time (NULL );
111+ return now - apps [i ].last_heartbeat ;
112112}
113113
114114bool is_timeup (int i )
115115{
116- bool ret = false;
117- time_t t = time (NULL );
116+ const Application_t * app = & apps [i ];
118117
119- if (t < apps [ i ]. last_heartbeat )
118+ if (! app -> started )
120119 {
120+ return false; // App not running yet
121+ }
122+
123+ if (app -> heartbeat_interval <= 0 )
124+ {
125+ return false; // Heartbeat not expected for this app
126+ }
127+
128+ const time_t now = time (NULL );
129+
130+ if (now < app -> last_heartbeat )
131+ {
132+ LOGW ("Time anomaly detected for %s (system clock changed?)" , app -> name );
121133 update_heartbeat_time (i );
134+ return false; // Reset and give another interval
122135 }
123136
124- if (t - apps [i ].last_heartbeat >= (apps [i ].first_heartbeat ? apps [i ].heartbeat_interval : apps [i ].heartbeat_delay ))
137+ const time_t first_heartbeat_threshold = (time_t )MAX (app -> heartbeat_interval , app -> heartbeat_delay ); // delay is designed to be larger than interval
138+ const time_t regular_threshold = (time_t )app -> heartbeat_interval ;
139+ const time_t threshold = app -> first_heartbeat ? regular_threshold : first_heartbeat_threshold ;
140+ const time_t elapsed = now - app -> last_heartbeat ;
141+
142+ if (elapsed >= threshold )
125143 {
126- ret = true ;
127- LOGD ( "Heartbeat time up for %s" , apps [ i ]. name ) ;
144+ LOGD ( "Heartbeat time up for %s" , app -> name ) ;
145+ return true ;
128146 }
129147
130- return ret ;
148+ return false ;
131149}
132150
133151void set_first_heartbeat (int i )
0 commit comments