From: Michael Tremer Date: Sun, 26 Oct 2025 17:54:20 +0000 (+0000) Subject: source: Add option for sources to disable themselves X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3c0f6ad4d0fd27dfd56667e5e510f6f43f1a6958;p=oddments%2Fcollecty.git source: Add option for sources to disable themselves This happens when we don't have support for something and the source usually asks to be disabled. Signed-off-by: Michael Tremer --- diff --git a/src/daemon/source.c b/src/daemon/source.c index aaac3cf..a512f6d 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -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); } diff --git a/src/daemon/source.h b/src/daemon/source.h index 89341fc..17d0ef0 100644 --- a/src/daemon/source.h +++ b/src/daemon/source.h @@ -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);