From: Michael Tremer Date: Sat, 27 Sep 2025 14:15:41 +0000 (+0000) Subject: modules: Call a heartbeat function every 60s X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c87364cfed6ce45199804abfe39ec7c631cf3da6;p=telemetry.git modules: Call a heartbeat function every 60s Signed-off-by: Michael Tremer --- diff --git a/src/daemon/module.c b/src/daemon/module.c index b6dcb40..f5d0e71 100644 --- a/src/daemon/module.c +++ b/src/daemon/module.c @@ -28,6 +28,9 @@ #include "daemon.h" #include "module.h" +// Interval after which the heartbeat function is being called again +#define HEARTBEAT 60000000 // 60s + struct collecty_module { collecty_ctx* ctx; int nrefs; @@ -43,11 +46,47 @@ struct collecty_module { // Events struct { + sd_event_source* heartbeat; sd_event_source* shutdown; } events; }; -static int collecty_daemon_module_shutdown(sd_event_source* source, void* data) { +static int collecty_module_heartbeat(sd_event_source* source, uint64_t usec, void* data) { + collecty_module* self = data; + int r; + + DEBUG(self->ctx, "Heartbeat called for %s\n", collecty_module_name(self)); + + // XXX TODO + + // Arm the timer again + r = sd_event_source_set_time(self->events.heartbeat, usec + 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; + + return 0; +} + +static int collecty_module_register_heartbeat(collecty_module* self) { + int r; + + // Call the heartbeat function immediately + r = sd_event_add_time_relative(self->loop, &self->events.heartbeat, + CLOCK_MONOTONIC, 0, 0, collecty_module_heartbeat, self); + if (r < 0) { + ERROR(self->ctx, "Failed to register the heartbeat timer: %s\n", strerror(-r)); + return r; + } + + return 0; +} + +static int collecty_module_shutdown(sd_event_source* source, void* data) { collecty_module* self = data; DEBUG(self->ctx, "Shutting down module '%s'\n", collecty_module_name(self)); @@ -63,7 +102,7 @@ static int collecty_module_register_shutdown(collecty_module* self) { // Call the shutdown function when the loop exits r = sd_event_add_exit(self->loop, &self->events.shutdown, - collecty_daemon_module_shutdown, collecty_module_ref(self)); + collecty_module_shutdown, collecty_module_ref(self)); if (r < 0) { ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r)); } @@ -91,6 +130,8 @@ static int collecty_module_init(collecty_module* self) { static void collecty_module_free(collecty_module* self) { if (self->methods->free) self->methods->free(self->ctx); + if (self->events.heartbeat) + sd_event_source_unref(self->events.heartbeat); if (self->events.shutdown) sd_event_source_unref(self->events.shutdown); if (self->loop) @@ -126,6 +167,11 @@ int collecty_module_create(collecty_module** module, // Store the methods self->methods = methods; + // Register heartbeat + r = collecty_module_register_heartbeat(self); + if (r < 0) + goto ERROR; + // Register shutdown r = collecty_module_register_shutdown(self); if (r < 0)