]> git.ipfire.org Git - telemetry.git/commitdiff
source: Arm the timer again when we get re-enabled
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 12 Jul 2026 11:23:48 +0000 (11:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 12 Jul 2026 11:23:48 +0000 (11:23 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/source.c

index ffa8f0407a2e26b4362960591392e1a2a33a18d4..a0c5b7e810c68a521640f1a9befc3782c17624cf 100644 (file)
@@ -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) {