From 78208556b281bd6ee8a95d16a517152728010475 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 24 Apr 2026 16:00:21 +0200 Subject: [PATCH] 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 --- src/core/job.c | 14 +++++++++----- src/core/job.h | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/job.c b/src/core/job.c index 920d246b88..b4578a946c 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 ff1d25ff50..a8eca99505 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;