From: Lennart Poettering Date: Fri, 24 Apr 2026 14:00:21 +0000 (+0200) Subject: job: add a "finished" state for jobs X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78208556b281bd6ee8a95d16a517152728010475;p=thirdparty%2Fsystemd.git job: add a "finished" state for jobs So far when a job completed we'd never transition into any new state, we'd just do some final processing work (such as notifying clients) and destroy it. Let's change that, and briefly enter a final state: "finished". This is useful so that code that notifies clients can generically send the quadruplet of id, type, state, result for any change notification and naturally can communicate job completion that way: by setting the state field to "finished". Prompted by the discussions in: #41583 --- diff --git a/src/core/job.c b/src/core/job.c index 920d246b884..b4578a946c6 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -140,17 +140,18 @@ static void job_set_state(Job *j, JobState state) { if (j->state == state) return; + JobState old_state = j->state; j->state = state; if (!j->installed) return; if (j->state == JOB_RUNNING) + /* This job changed into running, count up */ j->manager->n_running_jobs++; - else { - assert(j->state == JOB_WAITING); + else if (old_state == JOB_RUNNING) { + /* This job changed away from running into another state, count down. */ assert(j->manager->n_running_jobs > 0); - j->manager->n_running_jobs--; if (j->manager->n_running_jobs <= 0) @@ -1018,6 +1019,8 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool alr j->result = result; + job_set_state(j, JOB_FINISHED); + log_unit_debug(u, "Job %" PRIu32 " %s/%s finished, result=%s", j->id, u->id, job_type_to_string(t), job_result_to_string(result)); @@ -1646,8 +1649,9 @@ int job_get_after(Job *j, Job*** ret) { } static const char* const job_state_table[_JOB_STATE_MAX] = { - [JOB_WAITING] = "waiting", - [JOB_RUNNING] = "running", + [JOB_WAITING] = "waiting", + [JOB_RUNNING] = "running", + [JOB_FINISHED] = "finished", }; DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); diff --git a/src/core/job.h b/src/core/job.h index ff1d25ff502..a8eca99505d 100644 --- a/src/core/job.h +++ b/src/core/job.h @@ -52,6 +52,7 @@ enum JobType { typedef enum JobState { JOB_WAITING, JOB_RUNNING, + JOB_FINISHED, _JOB_STATE_MAX, _JOB_STATE_INVALID = -EINVAL, } JobState;