From: Michael Tremer Date: Sun, 12 Jul 2026 11:23:48 +0000 (+0000) Subject: source: Arm the timer again when we get re-enabled X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37219b7fcad6d055ec85c4e0bd90d94e8d4a57de;p=telemetry.git source: Arm the timer again when we get re-enabled Signed-off-by: Michael Tremer --- diff --git a/src/daemon/source.c b/src/daemon/source.c index ffa8f04..a0c5b7e 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -126,7 +126,51 @@ static void td_source_reset_results(td_source* self) { 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; @@ -138,6 +182,12 @@ static int td_source_change_state(td_source* self, enum td_source_state state) { 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: @@ -298,7 +348,6 @@ static uint64_t td_source_elapsed_time(void) { 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; @@ -347,42 +396,8 @@ static int td_source_heartbeat(sd_event_source* source, uint64_t usec, void* dat 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) {