include/pq_super.h

filter: Statementstmt 88.4% 223 / 252 Branchbran 63.8% 120 / 188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 343 / 440 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/queue.xs ↑ Summary include/pq_cron.h »
1 #ifndef PQ_SUPER_H
2 #define PQ_SUPER_H
3  
4 /* pq_super.h - the supervisor: fork -j children, keep them alive, take
5 * them down cleanly.
6 *
7 * The parent's database traffic is confined to startup (register the
8 * supervisor row, migrate) and the 10s pass (its own heartbeat, and the
9 * cron scheduler when enabled) - children own their handles via the
10 * pool's pid check. What the parent owns is process lifecycle: respawn
11 * with backoff, the supervision pipe each child reports claims on (the
12 * hard-kill timeout built on it lands in phase 4), and shutdown.
13 *
14 * -j N means N child processes, each with its own worker row. A supervisor
15 * row with jobs=4 cannot tell you which child is stuck; four child rows
16 * can.
17 *
18 * Include after pq_worker.h. */
19  
20 #ifdef WIN32
21  
22 /* No fork, so no pool: a Windows host runs one worker per process. */
23 static int pqs_run(pTHX_ SV *queue, SV *worker_opts, int nworkers,
24 double graceful_timeout, int fail_fast,
25 int scheduler) {
26 PERL_UNUSED_ARG(queue);
27 PERL_UNUSED_ARG(worker_opts);
28 PERL_UNUSED_ARG(nworkers);
29 PERL_UNUSED_ARG(graceful_timeout);
30 PERL_UNUSED_ARG(fail_fast);
31 PERL_UNUSED_ARG(scheduler);
32 croak("Punk::Queue::Supervisor needs fork, which Windows does not have:"
33 " run $queue->worker->run in each worker process instead");
34 return 1;
35 }
36  
37 #else
38  
39 #include <sys/wait.h>
40 #include <fcntl.h>
41  
42 /* Signal flags. Handlers set these and nothing else; the 1s poll timeout
43 * (and EINTR, since SA_RESTART is off) turns them into action. */
44 static volatile sig_atomic_t PQS_TERM = 0; /* graceful */
45 static volatile sig_atomic_t PQS_QUIT = 0; /* immediate */
46 static volatile sig_atomic_t PQS_HUP = 0; /* recycle children */
47 static volatile sig_atomic_t PQS_CHLD = 0; /* reap */
48 static volatile sig_atomic_t PQS_USR2 = 0; /* status to stderr */
49  
50 43 static void pqs_sig(int sig) {
51 43 switch (sig) {
52 14 case SIGTERM: PQS_TERM = 1; break;
53 0 case SIGINT: PQS_TERM = 1; break;
54 0 case SIGQUIT: PQS_QUIT = 1; break;
55 1 case SIGHUP: PQS_HUP = 1; break;
56 28 case SIGCHLD: PQS_CHLD = 1; break;
57 0 case SIGUSR2: PQS_USR2 = 1; break;
58 }
59 43 }
60  
61 typedef struct pqs_slot {
62 pid_t pid; /* 0 = empty */
63 int rfd; /* our end of the supervision pipe, or -1 */
64 double spawned; /* pq_now_local at fork */
65 double backoff_until; /* do not respawn before this */
66 int backoff_step; /* index into the schedule */
67 int claimed_any; /* this incarnation claimed at least once */
68 long current_job; /* 0 = idle */
69 double job_started;
70 double job_timeout; /* 0 = none; from the claim line */
71 char rbuf[256]; /* partial-line carry for the pipe */
72 size_t rlen;
73 } pqs_slot;
74  
75 static const double PQS_BACKOFF[] = { 0.0, 0.5, 1.0, 2.0, 4.0, 8.0, 30.0 };
76 #define PQS_BACKOFF_MAX 6
77 #define PQS_STABLE_SECS 60.0
78  
79 typedef struct pq_super {
80 SV *queue;
81 SV *worker_opts; /* hashref passed to each child's worker */
82 pqs_slot *slots;
83 int n;
84 IV supervisor_id;
85 double graceful_timeout;
86 int fail_fast;
87 int noclaim_exits; /* consecutive, across the whole pool */
88 int shutting_down;
89 int exit_code;
90 /* the cron scheduler (phase 9) */
91 int sched_on;
92 int leader; /* holding the pq.cron.leader lease */
93 double next_sched; /* aligned 10s boundary */
94 } pq_super;
95  
96 /* ---- the supervision pipe (parent side) ------------------------------------
97 *
98 * The child writes "C <jobid> <epoch>\n" on claim and "D\n" on completion.
99 * Phase 3 tracks it (claimed_any feeds the fail-fast escalation; the
100 * current-job fields feed SIGUSR2's status line); phase 4's hard timeout
101 * kill reads job_started off the same state. */
102 4544 static void pqs_pipe_drain(pqs_slot *s) {
103 549 for (;;) {
104 5093 ssize_t n = read(s->rfd, s->rbuf + s->rlen,
105 5093 sizeof(s->rbuf) - s->rlen - 1);
106 char *nl;
107 5093 ▶ if (n <= 0) break; /* EAGAIN or EOF */
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; /* EAGAIN or EOF */ 4544 549 5093
108 549 s->rlen += (size_t)n;
109 549 s->rbuf[s->rlen] = 0;
110 1177 ▶ while ((nl = strchr(s->rbuf, '\n'))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((nl = strchr(s->rbuf, '\n'))) { 628 549 1177
111 628 *nl = 0;
112 628 ▶ if (s->rbuf[0] == 'C') {
Branch: 1 branch
BranchTrueFalseTotal
if (s->rbuf[0] == 'C') { 318 310 628
113 318 long id = 0; double ts = 0, to = 0;
114 318 ▶ if (sscanf(s->rbuf + 1, " %ld %lf %lf", &id, &ts, &to) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(s->rbuf + 1, " %ld %lf %lf", &id, &ts, &to) >= 2) { 318 0 318
115 318 s->current_job = id;
116 318 s->job_started = ts;
117 318 s->job_timeout = to;
118 318 s->claimed_any = 1;
119 }
120 }
121 310 ▶ else if (s->rbuf[0] == 'D') {
Branch: 1 branch
BranchTrueFalseTotal
else if (s->rbuf[0] == 'D') { 310 0 310
122 310 s->current_job = 0;
123 310 s->job_started = 0;
124 310 s->job_timeout = 0;
125 }
126 628 s->rlen -= (size_t)(nl + 1 - s->rbuf);
127 628 memmove(s->rbuf, nl + 1, s->rlen + 1);
128 }
129 }
130 4544 }
131  
132 /* ---- children --------------------------------------------------------------
133 *
134 * The child runs the worker in-process on the queue object built before the
135 * fork - the task registry came along with it, and the connection pool's
136 * pid check hands the child a fresh handle on first use. After the run it
137 * leaves through _exit: the parent's stdio buffers and destructors were
138 * duplicated by fork, and flushing or running them twice is how a forked
139 * perl corrupts its parent's output. */
140 28 static void pqs_spawn(pTHX_ pq_super *sup, int i) {
141 28 pqs_slot *s = &sup->slots[i];
142 int fds[2];
143 pid_t pid;
144  
145 28 ▶ if (pipe(fds) != 0) { s->pid = 0; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(fds) != 0) { s->pid = 0; return; } 0 28 28
146 /* parent end non-blocking; child end inherited as-is */
147 28 fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK);
148  
149 28 pid = fork();
150 35 ▶ if (pid < 0) { close(fds[0]); close(fds[1]); s->pid = 0; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) { close(fds[0]); close(fds[1]); s->pid = 0; return; } 0 35 35
151  
152 35 ▶ if (pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { 7 28 35
153 /* child */
154 7 int died = 0;
155 SV *worker, *argv[1], *r;
156 7 close(fds[0]);
157  
158 /* default signal handling; pqw_run installs its own TERM/INT */
159 7 signal(SIGHUP, SIG_DFL);
160 7 signal(SIGQUIT, SIG_DFL);
161 7 signal(SIGCHLD, SIG_DFL);
162 7 signal(SIGUSR2, SIG_IGN);
163  
164 {
165 7 HV *wh = newHV();
166 7 HV *opts = newHV();
167 /* copy the shared worker opts, then add this child's pipe fd */
168 7 ▶ if (sup->worker_opts && SvROK(sup->worker_opts)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sup->worker_opts && SvROK(sup->worker_opts)) { 7 0 7
if (sup->worker_opts && SvROK(sup->worker_opts)) { 7 0 7
169 7 HV *in = (HV *)SvRV(sup->worker_opts);
170 HE *he;
171 7 hv_iterinit(in);
172 7 ▶ while ((he = hv_iternext(in)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(in))) 0 7 7
173 0 ▶ (void)hv_store_ent(opts, HeSVKEY_force(he),
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(opts, HeSVKEY_force(he), 0 0 0
(void)hv_store_ent(opts, HeSVKEY_force(he), 0 0 0
174 newSVsv(HeVAL(he)), 0);
175 }
176 7 (void)hv_stores(opts, "sup_fd", newSViv(fds[1]));
177 7 (void)hv_stores(wh, "queue", newSVsv(sup->queue));
178 7 (void)hv_stores(wh, "opts", newRV_noinc((SV *)opts));
179 7 worker = sv_2mortal(newRV_noinc((SV *)wh));
180 7 sv_bless(worker, gv_stashpvs("Punk::Queue::Worker", GV_ADD));
181 }
182  
183 7 argv[0] = worker;
184 7 r = pq_call_meth_ev(aTHX_ worker, "run", NULL, 0, 1, &died);
185 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
186 0 ▶ if (died)
Branch: 1 branch
BranchTrueFalseTotal
if (died) 0 0 0
187 0 fprintf(stderr, "punk-queue worker child: %s",
188 0 ▶ SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SvPV_nolen(ERRSV)); 0 0 0
189 0 close(fds[1]);
190 0 _exit(died ? 1 : 0);
191 }
192  
193 /* parent */
194 28 close(fds[1]);
195 28 s->pid = pid;
196 28 s->rfd = fds[0];
197 28 s->spawned = pq_now_local(aTHX);
198 28 s->claimed_any = 0;
199 28 s->current_job = 0;
200 28 s->rlen = 0;
201 }
202  
203 47 static void pqs_slot_close(pqs_slot *s) {
204 47 ▶ if (s->rfd >= 0) { close(s->rfd); s->rfd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->rfd >= 0) { close(s->rfd); s->rfd = -1; } 28 19 47
205 47 s->pid = 0;
206 47 }
207  
208 /* Reap everything reapable; schedule respawns with backoff. */
209 44 static void pqs_reap(pTHX_ pq_super *sup) {
210 28 for (;;) {
211 72 int status = 0;
212 72 pid_t pid = waitpid(-1, &status, WNOHANG);
213 int i;
214 72 ▶ if (pid <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (pid <= 0) break; 44 28 72
215 36 ▶ for (i = 0; i < sup->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup->n; i++) { 36 0 36
216 36 pqs_slot *s = &sup->slots[i];
217 double lived;
218 36 ▶ if (s->pid != pid) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (s->pid != pid) continue; 8 28 36
219 28 pqs_pipe_drain(s); /* final lines */
220 28 lived = pq_now_local(aTHX) - s->spawned;
221 28 pqs_slot_close(s);
222  
223 28 ▶ if (!s->claimed_any) sup->noclaim_exits++;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->claimed_any) sup->noclaim_exits++; 5 23 28
224 23 else sup->noclaim_exits = 0;
225  
226 28 ▶ if (lived >= PQS_STABLE_SECS) s->backoff_step = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lived >= PQS_STABLE_SECS) s->backoff_step = 0; 0 28 28
227 28 ▶ else if (s->backoff_step < PQS_BACKOFF_MAX) s->backoff_step++;
Branch: 1 branch
BranchTrueFalseTotal
else if (s->backoff_step < PQS_BACKOFF_MAX) s->backoff_step++; 28 0 28
228 28 s->backoff_until = pq_now_local(aTHX)
229 28 + PQS_BACKOFF[s->backoff_step];
230  
231 /* Three consecutive exits with no claim across the pool is
232 * "the app does not work in the child" - a compile error, a
233 * bad dsn - and respawning forever just hides it. */
234 28 ▶ if (sup->noclaim_exits >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (sup->noclaim_exits >= 3) { 0 28 28
235 0 ▶ warn("punk-queue: %d consecutive worker exits without a "
Branch: 1 branch
BranchTrueFalseTotal
warn("punk-queue: %d consecutive worker exits without a " 0 0 0
236 "single claim - the worker is failing at startup "
237 "(bad app class or dsn?)%s",
238 sup->noclaim_exits,
239 sup->fail_fast ? "; --fail-fast set, stopping" : "");
240 0 ▶ if (sup->fail_fast) {
Branch: 1 branch
BranchTrueFalseTotal
if (sup->fail_fast) { 0 0 0
241 0 sup->shutting_down = 1;
242 0 sup->exit_code = 1;
243 }
244 }
245 28 break;
246 }
247 }
248 44 }
249  
250 /* ---- the hard timeout ------------------------------------------------------
251 *
252 * The only timeout layer that is a guarantee. alarm cannot interrupt a
253 * blocking C call and a loop timer cannot stop work already running, so
254 * past timeout * 1.5 + 5 the parent SIGKILLs the child and settles the job
255 * as failed itself. The 1.5 gives the soft layers room to do it politely
256 * first; the +5 keeps a 1-second timeout from becoming a hair trigger.
257 *
258 * This is the one place the parent touches the database after startup: it
259 * must, because the child that would have settled the row is the thing
260 * being shot. Every call is G_EVAL - a kill must not die. */
261 1 static void pqs_hard_kill(pTHX_ pq_super *sup, int i) {
262 1 pqs_slot *s = &sup->slots[i];
263 1 long jid = s->current_job;
264 1 int died = 0;
265 1 IV retries = 0;
266 SV *info, *r;
267 SV *argv[3];
268  
269 /* A float in a perl format reads an NV, which is not a double
270 * everywhere: the quadmath perls take 16 bytes off the varargs where a
271 * double put 8. The cast alone is not enough - the FORMAT has to name
272 * the NV width too (NVff is "Qf" there, "Lf" on longdouble, "f"
273 * otherwise) or the quadmath formatter rejects a bare %f outright. So
274 * every float handed to a perl formatter is cast AND formatted with
275 * NVff. */
276 1 warn("punk-queue: job %ld exceeded its timeout (%.1" NVff "s) - killing "
277 "worker pid %ld", jid, (NV)s->job_timeout, (long)s->pid);
278 1 kill(s->pid, SIGKILL);
279 1 s->current_job = 0; /* one kill per job */
280 1 s->job_timeout = 0;
281  
282 1 argv[0] = sv_2mortal(newSViv((IV)jid));
283 1 info = pq_call_meth_ev(aTHX_ sup->queue, "job_info", argv, 1, 1, &died);
284 1 ▶ if (!died && info && SvROK(info)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!died && info && SvROK(info)) { 1 0 1
if (!died && info && SvROK(info)) { 1 0 1
if (!died && info && SvROK(info)) { 1 0 1
285 1 SV *rv = pq_get(aTHX_ (HV *)SvRV(info), "retries");
286 1 ▶ if (rv && SvOK(rv)) retries = SvIV(rv);
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvOK(rv)) retries = SvIV(rv); 1 0 1
if (rv && SvOK(rv)) retries = SvIV(rv); 1 0 1
287 }
288 1 ▶ if (info) SvREFCNT_dec(info);
Branch: 1 branch
BranchTrueFalseTotal
if (info) SvREFCNT_dec(info); 1 0 1
289 1 ▶ if (died) return;
Branch: 1 branch
BranchTrueFalseTotal
if (died) return; 0 1 1
290  
291 1 argv[0] = sv_2mortal(newSViv((IV)jid));
292 1 argv[1] = sv_2mortal(newSViv(retries));
293 1 argv[2] = sv_2mortal(newSVpvs(
294 "Punk::Queue: job exceeded its timeout and its worker was killed"));
295 1 r = pq_call_meth_ev(aTHX_ sup->queue, "fail_job", argv, 3, 1, &died);
296 1 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 1 0 1
297 }
298  
299 3200 static void pqs_check_timeouts(pTHX_ pq_super *sup) {
300 3200 double now = pq_now_local(aTHX);
301 int i;
302 7727 ▶ for (i = 0; i < sup->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup->n; i++) { 4527 3200 7727
303 4527 pqs_slot *s = &sup->slots[i];
304 4527 ▶ if (!s->pid || !s->current_job || s->job_timeout <= 0) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!s->pid || !s->current_job || s->job_timeout <= 0) continue; 4516 11 4527
if (!s->pid || !s->current_job || s->job_timeout <= 0) continue; 1451 3065 4516
if (!s->pid || !s->current_job || s->job_timeout <= 0) continue; 1444 7 1451
305 7 ▶ if (now - s->job_started > s->job_timeout * 1.5 + 5.0)
Branch: 1 branch
BranchTrueFalseTotal
if (now - s->job_started > s->job_timeout * 1.5 + 5.0) 1 6 7
306 1 pqs_hard_kill(aTHX_ sup, i);
307 }
308 3200 }
309  
310 /* ---- the 10s pass: heartbeat + the cron scheduler --------------------------
311 *
312 * The parent's only recurring database traffic, on a 10-second cadence
313 * aligned to the wall clock.
314 *
315 * First its own heartbeat: the supervisor registered a worker row at
316 * boot, and a row that never beats reads as stale everywhere that
317 * checks proof of life (the admin UI badges anything quiet for 60s;
318 * repair's stale-worker pass would eventually reap it). The children
319 * beat from their own timers; the parent beats here, scheduler or not.
320 *
321 * Then the scheduler, when enabled: every supervisor competes for the
322 * pq.cron.leader lease, so occurrences fire at :00/:10/... rather than
323 * at an arbitrary offset from process start. The holder renews; a loser
324 * tries a fresh acquire (the phase-6 lease: one INSERT wins on the
325 * unique index). Losing the lease is not an error - a leader that
326 * paused past its expiry discovers it here and steps down. The tick's
327 * optimistic guard and occurrence dedupe make even a brief double
328 * leadership harmless.
329 *
330 * Everything is a G_EVAL method call: a hiccup (connection trouble, a
331 * bad stored expression - which the tick disables itself) must never
332 * take the process pool down with it. */
333 #define PQS_LEADER_LOCK "pq.cron.leader"
334 #define PQS_LEASE_SECS 30.0
335  
336 3200 static void pqs_sched_pass(pTHX_ pq_super *sup) {
337 3200 double now = pq_now_local(aTHX);
338 SV *backend;
339 3200 int died = 0;
340  
341 3203 ▶ if (now < sup->next_sched) return;
Branch: 1 branch
BranchTrueFalseTotal
if (now < sup->next_sched) return; 3180 20 3200
342 /* in floating point throughout: an IV multiply here is a 2038 overflow
343 * on the 32-bit-IV perls, where the aligned boundary would come back
344 * negative and the pass would run flat out */
345 20 sup->next_sched = Perl_floor(now / 10.0) * 10.0 + 10.0;
346  
347 20 backend = pq_backend_of(aTHX_ sup->queue);
348  
349 {
350 SV *argv[1], *r;
351 20 argv[0] = sv_2mortal(newSViv(sup->supervisor_id));
352 20 r = pq_call_meth_ev(aTHX_ backend, "worker_heartbeat", argv, 1, 1,
353 &died);
354 20 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 20 0 20
355 20 died = 0;
356 }
357  
358 20 ▶ if (!sup->sched_on) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sup->sched_on) return; 3 17 20
359  
360 17 ▶ if (sup->leader) {
Branch: 1 branch
BranchTrueFalseTotal
if (sup->leader) { 3 14 17
361 SV *argv[3], *r;
362 3 argv[0] = sv_2mortal(newSVpvs(PQS_LEADER_LOCK));
363 3 argv[1] = sv_2mortal(newSViv(sup->supervisor_id));
364 3 argv[2] = sv_2mortal(newSVnv(PQS_LEASE_SECS));
365 3 r = pq_call_meth_ev(aTHX_ backend, "renew_lock", argv, 3, 1,
366 &died);
367 3 ▶ sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 3 0 3
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 3 0 3
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 3 0 3
368 3 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 3 0 3
369 }
370 17 ▶ if (!sup->leader) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sup->leader) { 14 3 17
371 14 HV *o = (HV *)sv_2mortal((SV *)newHV());
372 SV *argv[3], *r;
373 14 (void)hv_stores(o, "owner", newSViv(sup->supervisor_id));
374 14 argv[0] = sv_2mortal(newSVpvs(PQS_LEADER_LOCK));
375 14 argv[1] = sv_2mortal(newSVnv(PQS_LEASE_SECS));
376 14 argv[2] = sv_2mortal(newRV_inc((SV *)o));
377 14 r = pq_call_meth_ev(aTHX_ backend, "lock", argv, 3, 1, &died);
378 14 ▶ sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 14 0 14
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 14 0 14
sup->leader = (!died && r && SvTRUE(r)) ? 1 : 0; 13 1 14
379 14 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 14 0 14
380 }
381 17 ▶ if (sup->leader) {
Branch: 1 branch
BranchTrueFalseTotal
if (sup->leader) { 16 1 17
382 SV *argv[1], *r;
383 16 argv[0] = sv_2mortal(newSViv(100));
384 16 r = pq_call_meth_ev(aTHX_ backend, "_cron_tick", argv, 1, 1,
385 &died);
386 16 ▶ if (died)
Branch: 1 branch
BranchTrueFalseTotal
if (died) 0 16 16
387 0 ▶ warn("punk-queue: cron tick failed: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("punk-queue: cron tick failed: %s", SvPV_nolen(ERRSV)); 0 0 0
388 16 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 16 0 16
389 }
390 }
391  
392 0 static void pqs_status(pq_super *sup) {
393 int i;
394 0 fprintf(stderr, "punk-queue supervisor: %d slot(s)\n", sup->n);
395 0 ▶ for (i = 0; i < sup->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup->n; i++) { 0 0 0
396 0 pqs_slot *s = &sup->slots[i];
397 0 ▶ if (s->pid)
Branch: 1 branch
BranchTrueFalseTotal
if (s->pid) 0 0 0
398 0 fprintf(stderr, " [%d] pid %ld %s\n", i, (long)s->pid,
399 0 ▶ s->current_job
Branch: 1 branch
BranchTrueFalseTotal
s->current_job 0 0 0
400 ? "running a job" : "idle");
401 else
402 0 fprintf(stderr, " [%d] respawning (backoff step %d)\n",
403 i, s->backoff_step);
404 }
405 0 }
406  
407 15 static void pqs_kill_all(pq_super *sup, int sig) {
408 int i;
409 35 ▶ for (i = 0; i < sup->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup->n; i++) 20 15 35
410 20 ▶ if (sup->slots[i].pid) kill(sup->slots[i].pid, sig);
Branch: 1 branch
BranchTrueFalseTotal
if (sup->slots[i].pid) kill(sup->slots[i].pid, sig); 20 0 20
411 15 }
412  
413 63 static int pqs_alive(pq_super *sup) {
414 63 int i, n = 0;
415 157 ▶ for (i = 0; i < sup->n; i++) if (sup->slots[i].pid) n++;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < sup->n; i++) if (sup->slots[i].pid) n++; 48 46 94
for (i = 0; i < sup->n; i++) if (sup->slots[i].pid) n++; 94 63 157
416 63 return n;
417 }
418  
419 /* ---- entry -----------------------------------------------------------------
420 *
421 * Runs until told to stop. Returns the process exit code. */
422 14 static int pqs_run(pTHX_ SV *queue, SV *worker_opts, int nworkers,
423 double graceful_timeout, int fail_fast,
424 int scheduler) {
425 pq_super sup;
426 struct sigaction sa;
427 struct sigaction old[6];
428 static const int sigs[6] =
429 { SIGTERM, SIGINT, SIGQUIT, SIGHUP, SIGCHLD, SIGUSR2 };
430 int i;
431 14 SV *backend = pq_backend_of(aTHX_ queue);
432  
433 14 memset(&sup, 0, sizeof sup);
434 14 sup.queue = queue;
435 14 sup.worker_opts = worker_opts;
436 14 sup.n = nworkers > 0 ? nworkers : 1;
437 14 ▶ sup.graceful_timeout = graceful_timeout > 0 ? graceful_timeout : 60.0;
Branch: 1 branch
BranchTrueFalseTotal
sup.graceful_timeout = graceful_timeout > 0 ? graceful_timeout : 60.0; 14 0 14
438 14 sup.fail_fast = fail_fast;
439 14 sup.sched_on = scheduler;
440 14 sup.slots = (pqs_slot *)pq_xcalloc(aTHX_ (size_t)sup.n, sizeof(pqs_slot));
441 33 ▶ for (i = 0; i < sup.n; i++) sup.slots[i].rfd = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup.n; i++) sup.slots[i].rfd = -1; 19 14 33
442  
443 /* the pool row the admin UI sees; jobs=0 says "does not claim" */
444 {
445 14 HV *ro = (HV *)sv_2mortal((SV *)newHV());
446 14 (void)hv_stores(ro, "role", newSVpvs("supervisor"));
447 14 (void)hv_stores(ro, "jobs", newSViv(0));
448 14 sup.supervisor_id = pq_register_worker(aTHX_ backend, 0, ro);
449 }
450  
451 14 PQS_TERM = PQS_QUIT = PQS_HUP = PQS_CHLD = PQS_USR2 = 0;
452 14 memset(&sa, 0, sizeof sa);
453 14 sa.sa_handler = pqs_sig;
454 14 sigemptyset(&sa.sa_mask);
455 14 sa.sa_flags = 0; /* no SA_RESTART: EINTR wakes poll */
456 98 ▶ for (i = 0; i < 6; i++) sigaction(sigs[i], &sa, &old[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) sigaction(sigs[i], &sa, &old[i]); 84 14 98
457  
458 3214 ▶ while (!sup.shutting_down) {
Branch: 1 branch
BranchTrueFalseTotal
while (!sup.shutting_down) { 3214 0 3214
459 3214 ▶ if (PQS_QUIT) { sup.shutting_down = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_QUIT) { sup.shutting_down = 1; break; } 0 3214 3214
460 3214 ▶ if (PQS_TERM) sup.shutting_down = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_TERM) sup.shutting_down = 1; 14 3200 3214
461 3214 ▶ if (sup.shutting_down) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sup.shutting_down) break; 14 3200 3214
462  
463 3200 ▶ if (PQS_HUP) {
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_HUP) { 1 3199 3200
464 1 PQS_HUP = 0;
465 /* children finish their current job and exit 0; the loop
466 * respawns them fresh - the zero-downtime recycle */
467 1 pqs_kill_all(&sup, SIGTERM);
468 }
469 3200 ▶ if (PQS_CHLD) { PQS_CHLD = 0; pqs_reap(aTHX_ &sup); }
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_CHLD) { PQS_CHLD = 0; pqs_reap(aTHX_ &sup); } 9 3191 3200
470 3200 ▶ if (PQS_USR2) { PQS_USR2 = 0; pqs_status(&sup); }
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_USR2) { PQS_USR2 = 0; pqs_status(&sup); } 0 3200 3200
471  
472 7727 ▶ for (i = 0; i < sup.n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup.n; i++) { 4527 3200 7727
473 4527 pqs_slot *s = &sup.slots[i];
474 4527 ▶ if (!s->pid && pq_now_local(aTHX) >= s->backoff_until)
Branch: 2 branches
BranchTrueFalseTotal
if (!s->pid && pq_now_local(aTHX) >= s->backoff_until) 39 4488 4527
if (!s->pid && pq_now_local(aTHX) >= s->backoff_until) 28 11 39
475 28 pqs_spawn(aTHX_ &sup, i);
476 }
477  
478 {
479 struct pollfd pfds[64];
480 3200 int npfd = 0;
481 7727 ▶ for (i = 0; i < sup.n && npfd < 64; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < sup.n && npfd < 64; i++) { 4527 3200 7727
for (i = 0; i < sup.n && npfd < 64; i++) { 4527 0 4527
482 4527 ▶ if (sup.slots[i].pid && sup.slots[i].rfd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (sup.slots[i].pid && sup.slots[i].rfd >= 0) { 4516 11 4527
if (sup.slots[i].pid && sup.slots[i].rfd >= 0) { 4516 0 4516
483 4516 pfds[npfd].fd = sup.slots[i].rfd;
484 4516 pfds[npfd].events = POLLIN;
485 4516 pfds[npfd].revents = 0;
486 4516 npfd++;
487 }
488 }
489 3200 (void)poll(pfds, (nfds_t)npfd, 1000);
490 7727 ▶ for (i = 0; i < sup.n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup.n; i++) 4527 3200 7727
491 4527 ▶ if (sup.slots[i].pid && sup.slots[i].rfd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (sup.slots[i].pid && sup.slots[i].rfd >= 0) 4516 11 4527
if (sup.slots[i].pid && sup.slots[i].rfd >= 0) 4516 0 4516
492 4516 pqs_pipe_drain(&sup.slots[i]);
493 }
494  
495 3200 pqs_check_timeouts(aTHX_ &sup);
496 3200 pqs_sched_pass(aTHX_ &sup);
497 }
498  
499 /* shutdown: graceful unless QUIT asked for immediate */
500 14 ▶ if (PQS_QUIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (PQS_QUIT) { 0 14 14
501 0 pqs_kill_all(&sup, SIGKILL);
502 0 ▶ while (pqs_alive(&sup)) { pqs_reap(aTHX_ &sup); poll(NULL, 0, 50); }
Branch: 1 branch
BranchTrueFalseTotal
while (pqs_alive(&sup)) { pqs_reap(aTHX_ &sup); poll(NULL, 0, 50); } 0 0 0
503 }
504 else {
505 14 double deadline = pq_now_local(aTHX) + sup.graceful_timeout;
506 14 pqs_kill_all(&sup, SIGTERM);
507 49 ▶ while (pqs_alive(&sup) && pq_now_local(aTHX) < deadline) {
Branch: 2 branches
BranchTrueFalseTotal
while (pqs_alive(&sup) && pq_now_local(aTHX) < deadline) { 35 14 49
while (pqs_alive(&sup) && pq_now_local(aTHX) < deadline) { 35 0 35
508 35 pqs_reap(aTHX_ &sup);
509 35 poll(NULL, 0, 100);
510 }
511 14 ▶ if (pqs_alive(&sup)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pqs_alive(&sup)) { 0 14 14
512 0 warn("punk-queue: %d worker(s) outlived the graceful window, "
513 "killing", pqs_alive(&sup));
514 0 pqs_kill_all(&sup, SIGKILL);
515 0 ▶ while (pqs_alive(&sup)) { pqs_reap(aTHX_ &sup); poll(NULL, 0, 50); }
Branch: 1 branch
BranchTrueFalseTotal
while (pqs_alive(&sup)) { pqs_reap(aTHX_ &sup); poll(NULL, 0, 50); } 0 0 0
516 }
517 }
518  
519 98 ▶ for (i = 0; i < 6; i++) sigaction(sigs[i], &old[i], NULL);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) sigaction(sigs[i], &old[i], NULL); 84 14 98
520 /* hand the cron lease back so a standby takes over now, not in 30s */
521 14 ▶ if (sup.leader) {
Branch: 1 branch
BranchTrueFalseTotal
if (sup.leader) { 13 1 14
522 13 int died = 0;
523 SV *argv[2], *r;
524 13 argv[0] = sv_2mortal(newSVpvs(PQS_LEADER_LOCK));
525 13 argv[1] = sv_2mortal(newSViv(sup.supervisor_id));
526 13 r = pq_call_meth_ev(aTHX_ backend, "unlock", argv, 2, 1, &died);
527 13 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 13 0 13
528 }
529 {
530 14 int died = 0;
531 SV *argv[1], *r;
532 14 argv[0] = sv_2mortal(newSViv(sup.supervisor_id));
533 14 r = pq_call_meth_ev(aTHX_ backend, "unregister_worker", argv, 1, 1,
534 &died);
535 14 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 14 0 14
536 }
537 33 ▶ for (i = 0; i < sup.n; i++) pqs_slot_close(&sup.slots[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sup.n; i++) pqs_slot_close(&sup.slots[i]); 19 14 33
538 14 safefree(sup.slots);
539 14 return sup.exit_code;
540 }
541  
542 #endif /* WIN32 */
543  
544 #endif /* PQ_SUPER_H */
« xs/queue.xs ↑ Summary include/pq_cron.h »