From: Michael Tremer Date: Sat, 27 Sep 2025 15:46:59 +0000 (+0000) Subject: daemon: Flush the queue on SIGHUP X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ac3c80e37141666974f6f4ea99157b1cf2f2707;p=telemetry.git daemon: Flush the queue on SIGHUP Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 95be5ed..3dff76b 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -61,6 +61,7 @@ struct collecty_daemon { struct { sd_event_source* sigterm; sd_event_source* sigint; + sd_event_source* sighup; sd_event_source* modules_init; sd_event_source* flush; } events; @@ -69,6 +70,14 @@ struct collecty_daemon { STAILQ_HEAD(queue, collecty_queue_object) queue; }; +static int collecty_daemon_flush(collecty_daemon* self) { + DEBUG(self->ctx, "Flushing the queue...\n"); + + // XXX TODO + + return 0; +} + static int collecty_daemon_modules_init(sd_event_source* source, void* data) { collecty_daemon* daemon = data; @@ -85,6 +94,14 @@ static int collecty_daemon_terminate(sd_event_source* source, return sd_event_exit(sd_event_source_get_event(source), 0); } +static int collecty_daemon_SIGHUP(sd_event_source* source, + const struct signalfd_siginfo* si, void* data) { + collecty_daemon* self = data; + + // Flush the queue + return collecty_daemon_flush(self); +} + static int collecty_daemon_setup_loop(collecty_daemon* self) { int r; @@ -116,6 +133,14 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) { return r; } + // Listen for SIGHUP + r = sd_event_add_signal(self->loop, &self->events.sighup, + SIGHUP|SD_EVENT_SIGNAL_PROCMASK, collecty_daemon_SIGHUP, self); + if (r < 0) { + ERROR(self->ctx, "Could not register handling SIGHUP: %s\n", strerror(-r)); + return r; + } + // Initialize all modules when the loop starts r = sd_event_add_defer(self->loop, &self->events.modules_init, collecty_daemon_modules_init, self); @@ -137,14 +162,10 @@ static void collecty_daemon_free_queue_object(struct collecty_queue_object* o) { free(o); } -static int collecty_daemon_flush(sd_event_source* source, uint64_t usec, void* data) { +static int collecty_daemon_flush_timer(sd_event_source* source, uint64_t usec, void* data) { collecty_daemon* self = data; int r; - DEBUG(self->ctx, "Flushing the queue...\n"); - - // TODO - // Call again soon... r = sd_event_source_set_time(source, usec + FLUSH_QUEUE_INTERVAL); if (r < 0) @@ -155,7 +176,8 @@ static int collecty_daemon_flush(sd_event_source* source, uint64_t usec, void* d if (r < 0) return r; - return 0; + // Flush the queue + return collecty_daemon_flush(self); } static int collecty_daemon_setup_queue(collecty_daemon* self) { @@ -166,7 +188,7 @@ static int collecty_daemon_setup_queue(collecty_daemon* self) { // Create a timer which regularly flushes the queue r = sd_event_add_time_relative(self->loop, &self->events.flush, - CLOCK_MONOTONIC, FLUSH_QUEUE_INTERVAL, 0, collecty_daemon_flush, self); + CLOCK_MONOTONIC, FLUSH_QUEUE_INTERVAL, 0, collecty_daemon_flush_timer, self); if (r < 0) { ERROR(self->ctx, "Failed to set up the flush timer: %s\n", strerror(-r)); return r; @@ -199,6 +221,8 @@ static void collecty_daemon_free(collecty_daemon* self) { sd_event_source_unref(self->events.sigterm); if (self->events.sigint) sd_event_source_unref(self->events.sigint); + if (self->events.sighup) + sd_event_source_unref(self->events.sighup); if (self->ctx) collecty_ctx_unref(self->ctx); if (self->loop)