]> git.ipfire.org Git - collecty.git/commitdiff
source: Add option for sources to disable themselves
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Oct 2025 17:54:20 +0000 (17:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Oct 2025 17:54:20 +0000 (17:54 +0000)
This happens when we don't have support for something and the source
usually asks to be disabled.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/source.c
src/daemon/source.h

index aaac3cff72c30cb6c11853c23ae413b991dd1908..a512f6df9ed1e1ad7008f6b3ccfc1726d0340212 100644 (file)
@@ -120,6 +120,36 @@ struct td_source {
        } results;
 };
 
+static int td_source_change_state(td_source* self, enum td_source_state state) {
+       // If the state has not changed, we are doing nothing
+       if (self->state == state)
+               return 0;
+
+       // Store the new state
+       self->state = state;
+
+       // Log the state change
+       switch (state) {
+               case STATE_HEALTHY:
+                       DEBUG(self->ctx, "%s is now healthy\n", td_source_name(self));
+                       break;
+
+               case STATE_FLAPPING:
+                       ERROR(self->ctx, "%s is now flapping\n", td_source_name(self));
+                       break;
+
+               case STATE_ERROR:
+                       ERROR(self->ctx, "%s is now in error state\n", td_source_name(self));
+                       break;
+
+               case STATE_DISABLED:
+                       DEBUG(self->ctx, "%s has been disabled\n", td_source_name(self));
+                       break;
+       }
+
+       return 0;
+}
+
 // Return the number of recent bad results
 static int td_source_count_bad_results(td_source* self) {
        int counter = 0;
@@ -209,6 +239,10 @@ static int td_source_error_detection(td_source* self, int result, uint64_t runti
                        td_source_name(self), td_source_count_bad_results(self));
        }
 
+       // If the source has disabled itself, we don't need to change the state again
+       if (self->state == STATE_DISABLED)
+               return 0;
+
        // Count all bad results
        int bad_results = td_source_count_bad_results(self);
 
@@ -228,33 +262,8 @@ static int td_source_error_detection(td_source* self, int result, uint64_t runti
        else
                state = STATE_HEALTHY;
 
-       // If the state has not changed, we are don
-       if (self->state == state)
-               return 0;
-
-       // Store the new state
-       self->state = state;
-
-       // Log the state change
-       switch (state) {
-               case STATE_HEALTHY:
-                       DEBUG(self->ctx, "%s is now healthy\n", td_source_name(self));
-                       break;
-
-               case STATE_FLAPPING:
-                       ERROR(self->ctx, "%s is now flapping\n", td_source_name(self));
-                       break;
-
-               case STATE_ERROR:
-                       ERROR(self->ctx, "%s is now in error state\n", td_source_name(self));
-                       break;
-
-               case STATE_DISABLED:
-                       ERROR(self->ctx, "%s has been disabled\n", td_source_name(self));
-                       break;
-       }
-
-       return 0;
+       // Change state
+       return td_source_change_state(self, state);
 }
 
 static uint64_t td_source_elapsed_time(void) {
@@ -483,6 +492,11 @@ const char* td_source_name(td_source* self) {
        return self->impl->name;
 }
 
+// Called to disable the source (e.g. if there is no support for it)
+int td_source_disable(td_source* self) {
+       return td_source_change_state(self, STATE_DISABLED);
+}
+
 struct udev* td_source_get_udev(td_source* self) {
        return td_daemon_get_udev(self->daemon);
 }
index 89341fc7e6630389efc84edc48aacfb5ffad7164..17d0ef0b203f83f2f8685958df826394aa53d4b3 100644 (file)
@@ -81,6 +81,7 @@ td_source* td_source_ref(td_source* self);
 td_source* td_source_unref(td_source* self);
 
 const char* td_source_name(td_source* self);
+int td_source_disable(td_source* self);
 
 struct udev* td_source_get_udev(td_source* self);