From: Michael Tremer Date: Tue, 13 Aug 2024 19:29:55 +0000 (+0000) Subject: daemon: Split stuff into a new worker thing X-Git-Tag: 0.9.30~1204 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fc68c6fda176f8a9eb8d286196f0a33da7e20ee3;p=pakfire.git daemon: Split stuff into a new worker thing Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 6f659e3fb..050dbc6c3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 = \ diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index ce348e95d..3887a042e 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -24,36 +24,11 @@ #include #include -#include - #include -#include #include #include -#include #include - -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 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 index 000000000..e6bd14674 --- /dev/null +++ b/src/libpakfire/include/pakfire/worker.h @@ -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 . # +# # +#############################################################################*/ + +#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 index 000000000..b553769e9 --- /dev/null +++ b/src/libpakfire/worker.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#include + +#include + +#include +#include +#include +#include + +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; +}