]> git.ipfire.org Git - pakfire.git/commitdiff
jobs: Automatically launch jobs once it has connected to the build service
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Oct 2024 17:41:24 +0000 (17:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Oct 2024 17:41:24 +0000 (17:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/daemon.c
src/libpakfire/include/pakfire/job.h
src/libpakfire/job.c

index 0537de61a9aa10a14dd5bdb81bb17d850aff13d4..b546231ef3fc6839c985c60d883e55c881a6259b 100644 (file)
@@ -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++;
 
index 6478311ba0bca8d591e0a1fffb62bdeb36ef776e..0b9a5477f5d61fb396dba67b723b69513e65b09d 100644 (file)
@@ -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);
index 982ffabe988e475aa6c883db204b6c80400b02f6..9197ecf80dc6deeff63b4c74055c54d1fdf05b28 100644 (file)
@@ -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)
 */