summaryrefslogtreecommitdiff
path: root/job.c
blob: 2cf8be5330687bb8c18bbd9d03ac30269e3efa6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>

#include "macro.h"
#include "job.h"

Job* job_new(Manager *m, JobType type, Name *name) {
        Job *j;

        assert(m);
        assert(type < _JOB_TYPE_MAX);
        assert(name);

        if (!(j = new0(Job, 1)))
                return NULL;

        j->manager = m;
        j->id = m->current_job_id++;
        j->type = type;
        j->name = name;

        /* We don't link it here, that's what job_link() is for */

        return j;
}

int job_link(Job *j) {
        int r;

        assert(j);
        assert(!j->linked);

        if ((r = hashmap_put(j->manager->jobs, UINT32_TO_PTR(j->id), j)) < 0)
                return r;

        j->name->meta.job = j;

        j->linked = true;

        return 0;
}

void job_free(Job *j) {
        assert(j);

        /* Detach from next 'bigger' objects */

        if (j->linked) {
                assert(j->name);
                assert(j->name->meta.job == j);
                j->name->meta.job = NULL;

                hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
        }

        hashmap_remove(j->manager->jobs_to_add, j->name);
        set_remove(j->manager->jobs_to_remove, j);

        /* Free data and next 'smaller' objects */
        free(j);
}

void job_dump(Job *j, FILE*f) {

        static const char* const job_type_table[_JOB_TYPE_MAX] = {
                [JOB_START] = "start",
                [JOB_STOP] = "stop",
                [JOB_VERIFY_STARTED] = "verify-started",
                [JOB_RELOAD] = "reload",
                [JOB_RESTART] = "restart",
                [JOB_TRY_RESTART] = "try-restart",
                [JOB_RESTART_FINISH] = "restart-finish"
        };

        static const char* const job_state_table[_JOB_STATE_MAX] = {
                [JOB_WAITING] = "waiting",
                [JOB_RUNNING] = "running",
                [JOB_DONE] = "done"
        };

        assert(j);
        assert(f);

        fprintf(f, "Job %u (%s) →%s in state %s\n",
                j->id,
                name_id(j->name),
                job_type_table[j->type],
                job_state_table[j->state]);
}