]> git.ipfire.org Git - collecty.git/commitdiff
modules: Call a heartbeat function every 60s
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 14:15:41 +0000 (14:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 14:15:41 +0000 (14:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/module.c

index b6dcb40f66769d4bb44edcc4602a52215ad59a10..f5d0e715f3e29cd505d0437cecf7352a9aebbd32 100644 (file)
@@ -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)