self->results.num = 0;
}
+static int td_source_arm_timer(td_source* self, uint64_t usec) {
+ uint64_t next_heartbeat;
+ int r;
+
+ // Determine when to call again
+ switch (self->state) {
+ // If we are in error state we might skip this source for a while
+ case STATE_ERROR:
+ next_heartbeat = usec + SEC_TO_USEC(3600); // 1 hr
+ break;
+
+ // Don't call again if we have been disabled
+ case STATE_DISABLED:
+ next_heartbeat = 0;
+ break;
+
+ // In all other cases, we keep beating
+ default:
+ next_heartbeat = usec + HEARTBEAT;
+ }
+
+ // Arm the timer again
+ if (next_heartbeat) {
+ r = sd_event_source_set_time(self->events.heartbeat, next_heartbeat);
+ if (r < 0)
+ return r;
+
+ // Call this continuously
+ r = sd_event_source_set_enabled(self->events.heartbeat, SD_EVENT_ON);
+ if (r < 0)
+ return r;
+
+ // Or turn it off entirely
+ } else {
+ r = sd_event_source_set_enabled(self->events.heartbeat, SD_EVENT_OFF);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
static int td_source_change_state(td_source* self, enum td_source_state state) {
+ int r;
+
// If the state has not changed, we are doing nothing
if (self->state == state)
return 0;
switch (state) {
case STATE_RUNNING:
DEBUG(self->ctx, "%s is now running\n", td_source_name(self));
+
+ // Arm the timer again
+ r = td_source_arm_timer(self, 0);
+ if (r < 0)
+ return r;
+
break;
case STATE_FLAPPING:
static int td_source_heartbeat(sd_event_source* source, uint64_t usec, void* data) {
td_source* self = data;
- uint64_t next_heartbeat;
uint64_t t_delay;
uint64_t t_start;
uint64_t t_end;
if (r < 0)
return r;
- // Determine when to call again
- switch (self->state) {
- // If we are in error state we might skip this source for a while
- case STATE_ERROR:
- next_heartbeat = usec + SEC_TO_USEC(3600); // 1 hr
- break;
-
- // Don't call again if we have been disabled
- case STATE_DISABLED:
- next_heartbeat = 0;
- break;
-
- // In all other cases, we keep beating
- default:
- next_heartbeat = usec + HEARTBEAT;
- }
-
- // Arm the timer again
- if (next_heartbeat) {
- r = sd_event_source_set_time(self->events.heartbeat, next_heartbeat);
- if (r < 0)
- return r;
-
- // Call this continuously
- r = sd_event_source_set_enabled(self->events.heartbeat, SD_EVENT_ON);
- if (r < 0)
- return r;
-
- // Or turn it off entirely
- } else {
- r = sd_event_source_set_enabled(self->events.heartbeat, SD_EVENT_OFF);
- if (r < 0)
- return r;
- }
-
- return 0;
+ // Arm the timer to be called again
+ return td_source_arm_timer(self, usec);
}
static int td_source_register_heartbeat(td_source* self) {