Add more info to the protocol doc.
[beanstalkd.git] / reserve.c
blob1de030489d79f4e49e6ff8854ed45d1af8e9fb9e
1 /* reserve.c - job reservations */
3 #include "job.h"
4 #include "prot.h"
5 #include "reserve.h"
7 static unsigned int cur_reserved_ct = 0;
9 /* Doubly-linked list of connections with at least one reserved job. */
10 static struct conn running = { &running, &running, 0 };
12 void
13 reserve_job(conn c, job j)
15 j->deadline = time(NULL) + RESERVATION_TIMEOUT;
16 cur_reserved_ct++; /* stats */
17 conn_insert(&running, c);
18 j->state = JOB_STATE_RESERVED;
19 job_insert(&c->reserved_jobs, j);
20 return reply_job(c, j, MSG_RESERVED);
23 int
24 has_reserved_job(conn c)
26 return job_list_any_p(&c->reserved_jobs);
29 /* return the reserved job with the earliest deadline,
30 * or NULL if there's no reserved job */
31 job
32 soonest_job(conn c)
34 job j, soonest = NULL;
36 for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
37 if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
39 return soonest;
42 void
43 enqueue_reserved_jobs(conn c)
45 int r;
46 job j;
48 while (job_list_any_p(&c->reserved_jobs)) {
49 j = job_remove(c->reserved_jobs.next);
50 r = enqueue_job(j, 0);
51 if (!r) bury_job(j);
52 cur_reserved_ct--;
53 if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
57 int
58 has_reserved_this_job(conn c, job needle)
60 job j;
62 for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
63 if (needle == j) return 1;
65 return 0;
68 static job
69 find_reserved_job_in_conn(conn c, unsigned long long int id)
71 job j;
73 for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
74 if (j->id == id) return j;
76 return NULL;
79 job
80 remove_reserved_job(conn c, unsigned long long int id)
82 return remove_this_reserved_job(c, find_reserved_job_in_conn(c, id));
85 /* j can be NULL */
86 job
87 remove_this_reserved_job(conn c, job j)
89 j = job_remove(j);
90 if (j) cur_reserved_ct--;
91 if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
92 return j;
95 job
96 find_reserved_job_in_list(conn list, unsigned long long int id)
98 job j;
99 conn c;
101 for (c = list->next; c != list; c = c->next) {
102 j = find_reserved_job_in_conn(c, id);
103 if (j) return j;
105 return NULL;
109 find_reserved_job(unsigned long long int id)
111 return find_reserved_job_in_list(&running, id);
114 unsigned int
115 get_reserved_job_ct()
117 return cur_reserved_ct;