@@ -107,27 +107,45 @@ int find_pid(int pid)
107
107
108
108
time_t get_heartbeat_time (int i )
109
109
{
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 ;
112
112
}
113
113
114
114
bool is_timeup (int i )
115
115
{
116
- bool ret = false;
117
- time_t t = time (NULL );
116
+ const Application_t * app = & apps [i ];
118
117
119
- if (t < apps [ i ]. last_heartbeat )
118
+ if (! app -> started )
120
119
{
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 );
121
133
update_heartbeat_time (i );
134
+ return false; // Reset and give another interval
122
135
}
123
136
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 )
125
143
{
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 ;
128
146
}
129
147
130
- return ret ;
148
+ return false ;
131
149
}
132
150
133
151
void set_first_heartbeat (int i )
0 commit comments