From: Michael Tremer Date: Sat, 27 Sep 2025 16:11:00 +0000 (+0000) Subject: queue: Flush on exit X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=45d3e738f0798bbe3068bc81568e5d2bad8b832b;p=collecty.git queue: Flush on exit Signed-off-by: Michael Tremer --- diff --git a/src/daemon/queue.c b/src/daemon/queue.c index c340f1a..e284881 100644 --- a/src/daemon/queue.c +++ b/src/daemon/queue.c @@ -56,6 +56,7 @@ struct collecty_queue { // Events struct { sd_event_source* flush; + sd_event_source* exit; } events; // Queue @@ -80,6 +81,13 @@ static int collecty_queue_heartbeat(sd_event_source* source, uint64_t usec, void return collecty_queue_flush(self); } +static int collecty_queue_exit(sd_event_source* source, void* data) { + collecty_queue* self = data; + + // Flush the queue + return collecty_queue_flush(self); +} + static void collecty_queue_free_object(struct collecty_queue_object* o) { if (o->module) collecty_module_unref(o->module); @@ -106,6 +114,8 @@ static void collecty_queue_free(collecty_queue* self) { collecty_queue_free_object(o); } + if (self->events.exit) + sd_event_source_unref(self->events.exit); if (self->events.flush) sd_event_source_unref(self->events.flush); if (self->ctx) @@ -145,6 +155,13 @@ int collecty_queue_create(collecty_queue** queue, goto ERROR; } + // Create an event that is called when the loop exits + r = sd_event_add_exit(self->loop, &self->events.exit, collecty_queue_exit, self); + if (r < 0) { + ERROR(self->ctx, "Failed to setup exit handler: %s\n", strerror(-r)); + goto ERROR; + } + // Return the pointer *queue = self; return 0;