]> git.ipfire.org Git - pakfire.git/commitdiff
httpclient: Automatically detect whether the event loop is running
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:50:22 +0000 (13:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:50:22 +0000 (13:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/httpclient.c

index 792744dc5296ceb5fe7ce1463c3e27439ccde2c0..9662da42d0f0e2efa0634f526c4ce90dc921292b 100644 (file)
@@ -63,6 +63,7 @@ struct pakfire_httpclient {
 
        // Event Loop
        sd_event* loop;
+       int is_running;
        int still_running;
 
        // Timer
@@ -477,6 +478,32 @@ ERROR:
        return r;
 }
 
+static int pakfire_httpclient_exited(sd_event_source* event, void* data);
+
+static int pakfire_httpclient_started(sd_event_source* event, void* data) {
+       struct pakfire_httpclient* self = data;
+
+       DEBUG(self->ctx, "Event loop started\n");
+
+       // Mark as running
+       self->is_running = 1;
+
+       // Register an event that get's called once we exit
+       return sd_event_add_exit(self->loop, NULL, pakfire_httpclient_exited, self);
+}
+
+static int pakfire_httpclient_exited(sd_event_source* event, void* data) {
+       struct pakfire_httpclient* self = data;
+
+       DEBUG(self->ctx, "Event loop exited\n");
+
+       // Mark as not running
+       self->is_running = 0;
+
+       // Register an event so that we catch when the loop is being restarted
+       return sd_event_add_defer(self->loop, NULL, pakfire_httpclient_started, self);
+}
+
 static int pakfire_httpclient_setup_loop(struct pakfire_httpclient* self, sd_event* loop) {
        int r;
 
@@ -506,6 +533,11 @@ static int pakfire_httpclient_setup_loop(struct pakfire_httpclient* self, sd_eve
                return r;
        }
 
+       // Have something fire when the loop has started
+       r = sd_event_add_defer(self->loop, NULL, pakfire_httpclient_started, self);
+       if (r < 0)
+               return r;
+
        return 0;
 }
 
@@ -706,12 +738,11 @@ int pakfire_httpclient_enqueue(struct pakfire_httpclient* self, struct pakfire_x
        // Keep a reference to the xfer
        TAILQ_INSERT_TAIL(&self->xfers, e, nodes);
 
-       // We are done if we are running in standalone mode
-       if (pakfire_httpclient_has_flag(self, PAKFIRE_HTTPCLIENT_STANDALONE))
-               return 0;
+       // Launch the transfer straight away if we are running already
+       if (self->is_running)
+               return pakfire_httpclient_launch_one(self, e);
 
-       // Otherwise we launch the transfer straight away
-       return pakfire_httpclient_launch_one(self, e);
+       return 0;
 }
 
 int pakfire_httpclient_dequeue(struct pakfire_httpclient* self, struct pakfire_xfer* xfer) {