From: Michael Tremer Date: Fri, 4 Oct 2024 17:41:24 +0000 (+0000) Subject: jobs: Automatically launch jobs once it has connected to the build service X-Git-Tag: 0.9.30~1169 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9eb696a3b2087cbc140ac5c2fa5507fff3aba66d;p=pakfire.git jobs: Automatically launch jobs once it has connected to the build service Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index 0537de61a..b546231ef 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -382,14 +382,6 @@ static int pakfire_daemon_job(struct pakfire_daemon* daemon, json_object* m) { } } - // Launch the job - r = pakfire_job_launch(job); - if (r < 0) { - CTX_ERROR(daemon->ctx, "Could not launch job: %s\n", strerror(-r)); - - goto ERROR; - } - // Increment the number of running jobs daemon->running_jobs++; diff --git a/src/libpakfire/include/pakfire/job.h b/src/libpakfire/include/pakfire/job.h index 6478311ba..0b9a5477f 100644 --- a/src/libpakfire/include/pakfire/job.h +++ b/src/libpakfire/include/pakfire/job.h @@ -35,7 +35,6 @@ struct pakfire_job* pakfire_job_ref(struct pakfire_job* worker); struct pakfire_job* pakfire_job_unref(struct pakfire_job* worker); // Launch -int pakfire_job_launch(struct pakfire_job* worker); int pakfire_job_terminate(struct pakfire_job* worker, int signal); int pakfire_job_exited(sd_event_source* s, const siginfo_t* si, void* data); diff --git a/src/libpakfire/job.c b/src/libpakfire/job.c index 982ffabe9..9197ecf80 100644 --- a/src/libpakfire/job.c +++ b/src/libpakfire/job.c @@ -77,6 +77,12 @@ struct pakfire_job { // Control Connection struct pakfire_xfer* control; + + // Track the current state + enum { + PAKFIRE_JOB_STATE_INIT = 0, + PAKFIRE_JOB_STATE_LAUNCHED, + } state; }; static int pakfire_parse_job(struct pakfire_job* job, json_object* data) { @@ -215,6 +221,69 @@ static void pakfire_job_free(struct pakfire_job* job) { free(job); } +/* + +*/ +static int pakfire_job_parent(struct pakfire_job* job) { + int r; + + // Tell the daemon process that the job has been launched + r = pakfire_daemon_job_launched(job->daemon, job, job->pidfd); + if (r < 0) + return r; + + return 0; +} + +static int pakfire_job_child(struct pakfire_job* job) { + // Fetch our PID + pid_t pid = getpid(); + + CTX_DEBUG(job->ctx, "Launched job child as PID %d\n", pid); + + // XXX TODO + return 1; +} + +/* + Launches the job +*/ +static int pakfire_job_launch(struct pakfire_job* job) { + char job_id[UUID_STR_LEN]; + int pid; + int r; + + // Format the job ID as string + uuid_unparse(job->job_id, job_id); + + CTX_DEBUG(job->ctx, "Launching job %s\n", job_id); + + // Update state + job->state = PAKFIRE_JOB_STATE_LAUNCHED; + + // Configure child process + struct clone_args args = { + .flags = CLONE_PIDFD, + .exit_signal = SIGCHLD, + .pidfd = (long long unsigned int)&job->pidfd, + }; + + // Fork this process + pid = clone3(&args, sizeof(args)); + if (pid < 0) { + CTX_ERROR(job->ctx, "Could not clone: %m\n"); + + return -errno; + + // Child process + } else if (pid == 0) { + r = pakfire_job_child(job); + _exit(r); + } + + return pakfire_job_parent(job); +} + static int pakfire_job_recv(struct pakfire_xfer* xfer, const char* message, const size_t size, void* data) { // XXX TODO @@ -266,6 +335,16 @@ static int pakfire_job_connected(struct pakfire_xfer* xfer, void* data) { // Store a reference to the control connection job->control = pakfire_xfer_ref(xfer); + switch (job->state) { + // If the job has connected for the first time, we launch it + case PAKFIRE_JOB_STATE_INIT: + return pakfire_job_launch(job); + + // Otherwise there is nothing to do + default: + break; + } + return 0; } @@ -389,66 +468,6 @@ struct pakfire_job* pakfire_job_unref(struct pakfire_job* job) { return NULL; } -/* - -*/ -static int pakfire_job_parent(struct pakfire_job* job) { - int r; - - // Tell the daemon process that the job has been launched - r = pakfire_daemon_job_launched(job->daemon, job, job->pidfd); - if (r < 0) - return r; - - return 0; -} - -static int pakfire_job_child(struct pakfire_job* job) { - // Fetch our PID - pid_t pid = getpid(); - - CTX_DEBUG(job->ctx, "Launched job child as PID %d\n", pid); - - // XXX TODO - return 1; -} - -/* - Launches the job and returns the pidfd -*/ -int pakfire_job_launch(struct pakfire_job* job) { - char job_id[UUID_STR_LEN]; - int pid; - int r; - - // Format the job ID as string - uuid_unparse(job->job_id, job_id); - - CTX_DEBUG(job->ctx, "Launching job %s\n", job_id); - - // Configure child process - struct clone_args args = { - .flags = CLONE_PIDFD, - .exit_signal = SIGCHLD, - .pidfd = (long long unsigned int)&job->pidfd, - }; - - // Fork this process - pid = clone3(&args, sizeof(args)); - if (pid < 0) { - CTX_ERROR(job->ctx, "Could not clone: %m\n"); - - return -errno; - - // Child process - } else if (pid == 0) { - r = pakfire_job_child(job); - _exit(r); - } - - return pakfire_job_parent(job); -} - /* Terminates the job (if it is still running) */