From: Michael Tremer Date: Tue, 13 Aug 2024 18:47:55 +0000 (+0000) Subject: daemon: Parse the received job X-Git-Tag: 0.9.30~1208 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=833b9c11bc66ff0dcd790f5b26374b96c6c182d1;p=pakfire.git daemon: Parse the received job Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index 3c37c87b8..ce348e95d 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -24,11 +24,37 @@ #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]; +}; + struct pakfire_daemon { struct pakfire_ctx* ctx; int nrefs; @@ -53,10 +79,150 @@ 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 json_object* data = NULL; + char job_id[UUID_STR_LEN]; + int r; + + // Fetch the data from the message + if (!json_object_object_get_ex(m, "data", &data)) { + CTX_ERROR(daemon->ctx, "Job does not have any data\n"); + + return -EINVAL; + } + + // Parse the job + r = pakfire_parse_job(daemon, &worker, data); + if (r) + return r; + + // Format the Job ID as string + uuid_unparse_lower(worker.job_id, job_id); + + 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); + return 0; }