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
This commit is contained in:
Lennart Poettering
2026-04-27 09:57:22 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent f0483f308a
commit 78208556b2
2 changed files with 10 additions and 5 deletions
+9 -5
View File
@@ -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);
+1
View File
@@ -52,6 +52,7 @@ enum JobType {
typedef enum JobState {
JOB_WAITING,
JOB_RUNNING,
JOB_FINISHED,
_JOB_STATE_MAX,
_JOB_STATE_INVALID = -EINVAL,
} JobState;