]> git.ipfire.org Git - pakfire.git/commitdiff
worker: Create our own event loop and HTTP client
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Oct 2024 12:59:41 +0000 (12:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Oct 2024 12:59:41 +0000 (12:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/worker.c

index 7bf60b4d8ca39c8d16f3ece56f319a0bdcba315e..fd9bad9cba3b3138370cce3d6421e71f1387bd37 100644 (file)
@@ -65,8 +65,11 @@ struct pakfire_worker {
        // PID File Descriptor
        int pidfd;
 
-       // Process Event
-       sd_event_source* process;
+       // Event Loop
+       sd_event* loop;
+
+       // HTTP Client
+       struct pakfire_httpclient* client;
 };
 
 static int pakfire_parse_job(struct pakfire_worker* worker, json_object* data) {
@@ -194,6 +197,10 @@ static int pakfire_parse_job(struct pakfire_worker* worker, json_object* data) {
 }
 
 static void pakfire_worker_free(struct pakfire_worker* worker) {
+       if (worker->client)
+               pakfire_httpclient_unref(worker->client);
+       if (worker->loop)
+               sd_event_unref(worker->loop);
        if (worker->daemon)
                pakfire_daemon_unref(worker->daemon);
        if (worker->ctx)
@@ -201,6 +208,53 @@ static void pakfire_worker_free(struct pakfire_worker* worker) {
        free(worker);
 }
 
+static int pakfire_worker_setup_loop(struct pakfire_worker* worker) {
+       int r;
+
+       // Create a new event loop
+       r = sd_event_new(&worker->loop);
+       if (r < 0) {
+               CTX_ERROR(worker->ctx, "Could not setup event loop: %s\n", strerror(-r));
+               return r;
+       }
+
+       return 0;
+}
+
+static int pakfire_worker_setup_httpclient(struct pakfire_worker* worker) {
+       struct pakfire_config* config = NULL;
+       int r;
+
+       // Fetch the configuration
+       config = pakfire_ctx_get_config(worker->ctx);
+       if (!config) {
+               CTX_ERROR(worker->ctx, "Could not fetch configuration: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Fetch the URL
+       const char* url = pakfire_config_get(config, "daemon", "url", "https://pakfire.ipfire.org");
+
+       // Create the HTTP client
+       r = pakfire_httpclient_create(&worker->client, worker->ctx, worker->loop);
+       if (r)
+               goto ERROR;
+
+       // Configure the base URL
+       r = pakfire_httpclient_set_baseurl(worker->client, url);
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not configure the URL\n");
+               goto ERROR;
+       }
+
+ERROR:
+       if (config)
+               pakfire_config_unref(config);
+
+       return r;
+}
+
 int pakfire_worker_create(struct pakfire_worker** worker, struct pakfire_ctx* ctx,
                struct pakfire_daemon* daemon, json_object* data) {
        struct pakfire_worker* w = NULL;
@@ -220,6 +274,16 @@ int pakfire_worker_create(struct pakfire_worker** worker, struct pakfire_ctx* ct
        // Store a reference to the daemon
        w->daemon = pakfire_daemon_ref(daemon);
 
+       // Setup a new event loop
+       r = pakfire_worker_setup_loop(w);
+       if (r)
+               goto ERROR;
+
+       // Setup the HTTP client
+       r = pakfire_worker_setup_httpclient(w);
+       if (r)
+               goto ERROR;
+
        // Parse the job
        r = pakfire_parse_job(w, data);
        if (r)
@@ -265,14 +329,22 @@ static int pakfire_worker_parent(struct pakfire_worker* worker) {
 }
 
 static int pakfire_worker_child(struct pakfire_worker* worker) {
+       int r;
+
        // Fetch our PID
        pid_t pid = getpid();
 
        CTX_DEBUG(worker->ctx, "Launched worker child as PID %d\n", pid);
 
-       // XXX TODO
+       // Run the main loop
+       r = sd_event_loop(worker->loop);
+       if (r < 0) {
+               CTX_ERROR(worker->ctx, "Could not run the event loop: %s\n", strerror(-r));
+               goto ERROR;
+       }
 
-       return 0;
+ERROR:
+       return r;
 }
 
 /*