]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Split stuff into a new worker thing
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 13 Aug 2024 19:29:55 +0000 (19:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 13 Aug 2024 19:29:55 +0000 (19:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/daemon.c
src/libpakfire/include/pakfire/worker.h [new file with mode: 0644]
src/libpakfire/worker.c [new file with mode: 0644]

index 6f659e3fb0bc3e2b96872dd38e207ce376d17e97..050dbc6c318ef1e3c423d6593cb8581b580c87e4 100644 (file)
@@ -256,6 +256,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/string.c \
        src/libpakfire/transaction.c \
        src/libpakfire/util.c \
+       src/libpakfire/worker.c \
        src/libpakfire/xfer.c
 
 pkginclude_HEADERS += \
@@ -305,6 +306,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/string.h \
        src/libpakfire/include/pakfire/transaction.h \
        src/libpakfire/include/pakfire/util.h \
+       src/libpakfire/include/pakfire/worker.h \
        src/libpakfire/include/pakfire/xfer.h
 
 libpakfire_la_CFLAGS = \
index ce348e95d7b69f6787b38fc11f14b17ac16e626e..3887a042ebe7dde70f15d9443f31a4c39703a09a 100644 (file)
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-event.h>
 
-#include <uuid/uuid.h>
-
 #include <pakfire/buildservice.h>
-#include <pakfire/constants.h>
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
-#include <pakfire/string.h>
 #include <pakfire/util.h>
-
-struct pakfire_worker {
-       uuid_t job_id;
-
-       char name[NAME_MAX];
-       char arch[ARCH_MAX];
-
-       // Flags
-       enum pakfire_worker_flags {
-               PAKFIRE_WORKER_TEST   = (1 << 0),
-               PAKFIRE_WORKER_CCACHE = (1 << 1),
-       } flags;
-
-       // Package URL
-       char pkg[PATH_MAX];
-
-       // ccache path
-       char ccache_path[PATH_MAX];
-
-       // Configuration
-       char conf[4096];
-};
+#include <pakfire/worker.h>
 
 struct pakfire_daemon {
        struct pakfire_ctx* ctx;
@@ -79,128 +54,12 @@ static int pakfire_daemon_terminate(sd_event_source* source,
        return sd_event_exit(sd_event_source_get_event(source), 0);
 }
 
-static int pakfire_parse_job(struct pakfire_daemon* daemon,
-               struct pakfire_worker* worker, json_object* data) {
-       json_object* ccache = NULL;
-       json_object* o = NULL;
-       const char* s = NULL;
-       int r;
-
-       // Fetch the Job ID
-       if (!json_object_object_get_ex(data, "id", &o)) {
-               CTX_ERROR(daemon->ctx, "Job does not have an ID\n");
-
-               return -EINVAL;
-       }
-
-       s = json_object_get_string(o);
-
-       // Parse the Job ID
-       r = uuid_parse(s, worker->job_id);
-       if (r) {
-               CTX_ERROR(daemon->ctx, "Could not parse the Job ID (%s)\n", s);
-
-               return -EINVAL;
-       }
-
-       // Fetch the name
-       if (!json_object_object_get_ex(data, "name", &o)) {
-               CTX_ERROR(daemon->ctx, "Job does not have a name\n");
-
-               return -EINVAL;
-       }
-
-       // Store the name
-       r = pakfire_string_set(worker->name, json_object_get_string(o));
-       if (r) {
-               CTX_ERROR(daemon->ctx, "Could not store name: %m\n");
-
-               return r;
-       }
-
-       // Fetch the arch
-       if (!json_object_object_get_ex(data, "arch", &o)) {
-               CTX_ERROR(daemon->ctx, "Job does not have an architecture\n");
-
-               return -EINVAL;
-       }
-
-       // Store the arch
-       r = pakfire_string_set(worker->arch, json_object_get_string(o));
-       if (r) {
-               CTX_ERROR(daemon->ctx, "Could not store arch: %m\n");
-
-               return r;
-       }
-
-       // Fetch ccache settings
-       if (json_object_object_get_ex(data, "ccache", &ccache)) {
-               if (json_object_object_get_ex(ccache, "enabled", &o)) {
-                       if (json_object_get_boolean(o)) {
-                               worker->flags |= PAKFIRE_WORKER_CCACHE;
-                       }
-               }
-
-               // Path
-               if (json_object_object_get_ex(ccache, "path", &o)) {
-                       r = pakfire_string_set(worker->ccache_path, json_object_get_string(o));
-                       if (r) {
-                               CTX_ERROR(daemon->ctx, "Could not store the ccache path: %m\n");
-
-                               return r;
-                       }
-               }
-       }
-
-       // Check if this is a test job
-       if (json_object_object_get_ex(data, "test", &o)) {
-               if (json_object_get_boolean(o)) {
-                       worker->flags |= PAKFIRE_WORKER_TEST;
-               }
-       }
-
-       // Fetch the configuration
-       if (!json_object_object_get_ex(data, "conf", &o)) {
-               CTX_ERROR(daemon->ctx, "Job does not have a configuration\n");
-
-               return -EINVAL;
-       }
-
-       // Store the configuration
-       r = pakfire_string_set(worker->conf, json_object_get_string(o));
-       if (r) {
-               CTX_ERROR(daemon->ctx, "Could not store the configuration: %m\n");
-
-               return r;
-       }
-
-       // Fetch the package URL
-       if (!json_object_object_get_ex(data, "pkg", &o)) {
-               CTX_ERROR(daemon->ctx, "Job does not have a package URL\n");
-
-               return -EINVAL;
-       }
-
-       // Store the package URL
-       r = pakfire_string_set(worker->pkg, json_object_get_string(o));
-       if (r) {
-               CTX_ERROR(daemon->ctx, "Could not store the package URL: %m\n");
-
-               return r;
-       }
-
-       CTX_DEBUG(daemon->ctx, "Job parsing completed\n");
-
-       return 0;
-}
-
 /*
        Called when a new job has been received
 */
 static int pakfire_daemon_job(struct pakfire_daemon* daemon, json_object* m) {
-       struct pakfire_worker worker = {};
+       struct pakfire_worker* worker = NULL;
        struct json_object* data = NULL;
-       char job_id[UUID_STR_LEN];
        int r;
 
        // Fetch the data from the message
@@ -210,18 +69,15 @@ static int pakfire_daemon_job(struct pakfire_daemon* daemon, json_object* m) {
                return -EINVAL;
        }
 
-       // Parse the job
-       r = pakfire_parse_job(daemon, &worker, data);
-       if (r)
-               return r;
+       // Create a new worker
+       r = pakfire_worker_create(&worker, daemon->ctx, data);
+       if (r) {
+               CTX_ERROR(daemon->ctx, "Could not create a new worker: %s\n", strerror(-r));
 
-       // Format the Job ID as string
-       uuid_unparse_lower(worker.job_id, job_id);
+               return r;
+       }
 
-       CTX_INFO(daemon->ctx, "Received a new job:\n");
-       CTX_INFO(daemon->ctx, "  ID   : %s\n", job_id);
-       CTX_INFO(daemon->ctx, "  Name : %s\n", worker.name);
-       CTX_INFO(daemon->ctx, "  Arch : %s\n", worker.arch);
+       // XXX TODO We need to do something with the new worker
 
        return 0;
 }
diff --git a/src/libpakfire/include/pakfire/worker.h b/src/libpakfire/include/pakfire/worker.h
new file mode 100644 (file)
index 0000000..e6bd146
--- /dev/null
@@ -0,0 +1,36 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2024 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_WORKER_H
+#define PAKFIRE_WORKER_H
+
+#ifdef PAKFIRE_PRIVATE
+
+struct pakfire_worker;
+
+int pakfire_worker_create(struct pakfire_worker** worker,
+       struct pakfire_ctx* ctx, json_object* data);
+
+struct pakfire_worker* pakfire_worker_ref(struct pakfire_worker* worker);
+struct pakfire_worker* pakfire_worker_unref(struct pakfire_worker* worker);
+
+#endif /* PAKFIRE_PRIVATE */
+
+#endif /* PAKFIRE_WORKER_H */
diff --git a/src/libpakfire/worker.c b/src/libpakfire/worker.c
new file mode 100644 (file)
index 0000000..b553769
--- /dev/null
@@ -0,0 +1,231 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2024 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <limits.h>
+
+#include <json.h>
+
+#include <uuid/uuid.h>
+
+#include <pakfire/constants.h>
+#include <pakfire/ctx.h>
+#include <pakfire/string.h>
+#include <pakfire/worker.h>
+
+struct pakfire_worker {
+       struct pakfire_ctx* ctx;
+       int nrefs;
+
+       uuid_t job_id;
+
+       char name[NAME_MAX];
+       char arch[ARCH_MAX];
+
+       // Flags
+       enum pakfire_worker_flags {
+               PAKFIRE_WORKER_TEST   = (1 << 0),
+               PAKFIRE_WORKER_CCACHE = (1 << 1),
+       } flags;
+
+       // Package URL
+       char pkg[PATH_MAX];
+
+       // ccache path
+       char ccache_path[PATH_MAX];
+
+       // Configuration
+       char conf[4096];
+};
+
+static int pakfire_parse_job(struct pakfire_worker* worker, json_object* data) {
+       json_object* ccache = NULL;
+       json_object* o = NULL;
+       const char* s = NULL;
+       int r;
+
+       char job_id[UUID_STR_LEN];
+
+       // Fetch the Job ID
+       if (!json_object_object_get_ex(data, "id", &o)) {
+               CTX_ERROR(worker->ctx, "Job does not have an ID\n");
+
+               return -EINVAL;
+       }
+
+       s = json_object_get_string(o);
+
+       // Parse the Job ID
+       r = uuid_parse(s, worker->job_id);
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not parse the Job ID (%s)\n", s);
+
+               return -EINVAL;
+       }
+
+       // Fetch the name
+       if (!json_object_object_get_ex(data, "name", &o)) {
+               CTX_ERROR(worker->ctx, "Job does not have a name\n");
+
+               return -EINVAL;
+       }
+
+       // Store the name
+       r = pakfire_string_set(worker->name, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not store name: %m\n");
+
+               return r;
+       }
+
+       // Fetch the arch
+       if (!json_object_object_get_ex(data, "arch", &o)) {
+               CTX_ERROR(worker->ctx, "Job does not have an architecture\n");
+
+               return -EINVAL;
+       }
+
+       // Store the arch
+       r = pakfire_string_set(worker->arch, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not store arch: %m\n");
+
+               return r;
+       }
+
+       // Fetch ccache settings
+       if (json_object_object_get_ex(data, "ccache", &ccache)) {
+               if (json_object_object_get_ex(ccache, "enabled", &o)) {
+                       if (json_object_get_boolean(o)) {
+                               worker->flags |= PAKFIRE_WORKER_CCACHE;
+                       }
+               }
+
+               // Path
+               if (json_object_object_get_ex(ccache, "path", &o)) {
+                       r = pakfire_string_set(worker->ccache_path, json_object_get_string(o));
+                       if (r) {
+                               CTX_ERROR(worker->ctx, "Could not store the ccache path: %m\n");
+
+                               return r;
+                       }
+               }
+       }
+
+       // Check if this is a test job
+       if (json_object_object_get_ex(data, "test", &o)) {
+               if (json_object_get_boolean(o)) {
+                       worker->flags |= PAKFIRE_WORKER_TEST;
+               }
+       }
+
+       // Fetch the configuration
+       if (!json_object_object_get_ex(data, "conf", &o)) {
+               CTX_ERROR(worker->ctx, "Job does not have a configuration\n");
+
+               return -EINVAL;
+       }
+
+       // Store the configuration
+       r = pakfire_string_set(worker->conf, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not store the configuration: %m\n");
+
+               return r;
+       }
+
+       // Fetch the package URL
+       if (!json_object_object_get_ex(data, "pkg", &o)) {
+               CTX_ERROR(worker->ctx, "Job does not have a package URL\n");
+
+               return -EINVAL;
+       }
+
+       // Store the package URL
+       r = pakfire_string_set(worker->pkg, json_object_get_string(o));
+       if (r) {
+               CTX_ERROR(worker->ctx, "Could not store the package URL: %m\n");
+
+               return r;
+       }
+
+       CTX_DEBUG(worker->ctx, "Job parsing completed\n");
+
+       // Format the Job ID as string
+       uuid_unparse_lower(worker->job_id, job_id);
+
+       CTX_INFO(worker->ctx, "Received a new job:\n");
+       CTX_INFO(worker->ctx, "  ID   : %s\n", job_id);
+       CTX_INFO(worker->ctx, "  Name : %s\n", worker->name);
+       CTX_INFO(worker->ctx, "  Arch : %s\n", worker->arch);
+
+       return 0;
+}
+
+static void pakfire_worker_free(struct pakfire_worker* worker) {
+       if (worker->ctx)
+               pakfire_ctx_unref(worker->ctx);
+       free(worker);
+}
+
+int pakfire_worker_create(struct pakfire_worker** worker, struct pakfire_ctx* ctx, json_object* data) {
+       struct pakfire_worker* w = NULL;
+       int r;
+
+       // Allocate a new object
+       w = calloc(1, sizeof(*w));
+       if (!w)
+               return -errno;
+
+       // Reference the context
+       w->ctx = pakfire_ctx_ref(ctx);
+
+       // Initialize the reference counter
+       w->nrefs = 1;
+
+       // Parse the job
+       r = pakfire_parse_job(w, data);
+       if (r)
+               goto ERROR;
+
+       // Return the reference
+       *worker = w;
+
+       return 0;
+
+ERROR:
+       pakfire_worker_free(w);
+
+       return r;
+}
+
+struct pakfire_worker* pakfire_worker_ref(struct pakfire_worker* worker) {
+       ++worker->nrefs;
+
+       return worker;
+}
+
+struct pakfire_worker* pakfire_worker_unref(struct pakfire_worker* worker) {
+       if (--worker->nrefs > 0)
+               return worker;
+
+       pakfire_worker_free(worker);
+       return NULL;
+}