From: Michael Tremer Date: Fri, 4 Oct 2024 17:50:29 +0000 (+0000) Subject: jobs: Have them monitor themselves and update their state X-Git-Tag: 0.9.30~1168 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a5ddaaa7431714b3855407ccfd5752123adc83ee;p=pakfire.git jobs: Have them monitor themselves and update their state Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index b546231ef..ea9a05ac4 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -744,32 +744,11 @@ ERROR: } /* - Called after a new job has been launched to register with the daemon's event loop. + Called after a job has exited */ -int pakfire_daemon_job_launched(struct pakfire_daemon* daemon, - struct pakfire_job* job, int pidfd) { - int r; - - CTX_DEBUG(daemon->ctx, "Registering job %p with PIDFD %d\n", job, pidfd); - - r = sd_event_add_child_pidfd(daemon->loop, NULL, pidfd, - WEXITED, pakfire_job_exited, job); - if (r < 0) { - CTX_DEBUG(daemon->ctx, "Could not register the job with the event loop: %s\n", strerror(-r)); - - return r; - } - - return 0; -} - -/* - Called after a job has exited. -*/ -int pakfire_daemon_job_exited(struct pakfire_daemon* daemon, struct pakfire_job* job) { - CTX_DEBUG(daemon->ctx, "Deregistering job %p\n", job); +int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_job* job) { + CTX_DEBUG(daemon->ctx, "Removing job %p\n", job); -#if 0 for (unsigned int i = 0; i < MAX_JOBS; i++) { if (daemon->jobs[i] != job) continue; @@ -780,7 +759,6 @@ int pakfire_daemon_job_exited(struct pakfire_daemon* daemon, struct pakfire_job* break; } -#endif return 0; } diff --git a/src/libpakfire/include/pakfire/daemon.h b/src/libpakfire/include/pakfire/daemon.h index 59e94e4d3..dd9ca597e 100644 --- a/src/libpakfire/include/pakfire/daemon.h +++ b/src/libpakfire/include/pakfire/daemon.h @@ -38,10 +38,7 @@ struct pakfire_httpclient* pakfire_daemon_httpclient(struct pakfire_daemon* daem int pakfire_daemon_main(struct pakfire_daemon* daemon); -int pakfire_daemon_job_launched( - struct pakfire_daemon* daemon, struct pakfire_job* job, int pidfd); -int pakfire_daemon_job_exited( - struct pakfire_daemon* daemon, struct pakfire_job* job); +int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_job* job); #endif /* PAKFIRE_PRIVATE */ #endif /* PAKFIRE_DAEMON_H */ diff --git a/src/libpakfire/job.c b/src/libpakfire/job.c index 9197ecf80..7fdac057b 100644 --- a/src/libpakfire/job.c +++ b/src/libpakfire/job.c @@ -82,6 +82,8 @@ struct pakfire_job { enum { PAKFIRE_JOB_STATE_INIT = 0, PAKFIRE_JOB_STATE_LAUNCHED, + PAKFIRE_JOB_STATE_EXITED, + PAKFIRE_JOB_STATE_KILLED, } state; }; @@ -222,15 +224,52 @@ static void pakfire_job_free(struct pakfire_job* job) { } /* + This method is triggered by SIGCHLD whenever the job exits +*/ +int pakfire_job_exited(sd_event_source* s, const siginfo_t* si, void* data) { + struct pakfire_job* job = data; + char job_id[UUID_STR_LEN]; + int r; + + // Format the job ID as string + uuid_unparse(job->job_id, job_id); + + switch (si->si_code) { + case CLD_EXITED: + CTX_DEBUG(job->ctx, "Job %s has exited with status code %d\n", + job_id, si->si_status); + + // Update state + job->state = PAKFIRE_JOB_STATE_EXITED; + + break; + + case CLD_KILLED: + CTX_ERROR(job->ctx, "Job %s has been killed by signal %d\n", + job_id, si->si_signo); + + // Update state + job->state = PAKFIRE_JOB_STATE_KILLED; + break; + } + + return 0; +} + +/* + Called to initialize the parent process. */ 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) + // Register the PID file descriptor + r = sd_event_add_child_pidfd(job->loop, NULL, job->pidfd, WEXITED, pakfire_job_exited, job); + if (r < 0) { + CTX_DEBUG(job->ctx, "Could not register the job with the event loop: %s\n", strerror(-r)); + return r; + } return 0; } @@ -487,34 +526,3 @@ int pakfire_job_terminate(struct pakfire_job* job, int signal) { return 0; } - -/* - This method is triggered by SIGCHLD whenever the job exits. -*/ -int pakfire_job_exited(sd_event_source* s, const siginfo_t* si, void* data) { - struct pakfire_job* job = data; - char job_id[UUID_STR_LEN]; - int r; - - // Format the job ID as string - uuid_unparse(job->job_id, job_id); - - switch (si->si_code) { - case CLD_EXITED: - CTX_DEBUG(job->ctx, "Job %s has exited with status code %d\n", - job_id, si->si_status); - break; - - case CLD_KILLED: - CTX_ERROR(job->ctx, "Job %s has been killed by signal %d\n", - job_id, si->si_signo); - break; - } - - // Let the daemon know this job has exited - r = pakfire_daemon_job_exited(job->daemon, job); - if (r) - return r; - - return 0; -}