xs/worker.xs

filter: Statementstmt 85.7% 30 / 35 Branchbran 59.0% 26 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.8% 56 / 79 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_hm.h ↑ Summary include/pq_dbi.h »
1 MODULE = Punk::Queue PACKAGE = Punk::Queue::Worker
2  
3 # The worker child. Constructed via $q->worker(%opts); run() claims and
4 # performs until stopped. One job in flight at a time - the concurrency
5 # knob is the supervisor's -j.
6  
7 IV
8 run(self)
9 SV *self
10 CODE:
11 21 RETVAL = pqw_run(aTHX_ self);
12 OUTPUT:
13 RETVAL
14  
15 SV *
16 id(self)
17 SV *self
18 PREINIT:
19 HV *h;
20 SV *v;
21 CODE:
22 1 h = pq_hv(aTHX_ self, "Punk::Queue::Worker");
23 1 v = pq_get(aTHX_ h, "worker_id");
24 1 ▶ if (!v) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) XSRETURN_UNDEF; 0 1 1
25 1 RETVAL = newSVsv(v);
26 OUTPUT:
27 RETVAL
28  
29 SV *
30 queue(self)
31 SV *self
32 PREINIT:
33 HV *h;
34 SV *v;
35 CODE:
36 0 h = pq_hv(aTHX_ self, "Punk::Queue::Worker");
37 0 v = pq_get(aTHX_ h, "queue");
38 0 ▶ if (!(v && SvROK(v))) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvROK(v))) XSRETURN_UNDEF; 0 0 0
if (!(v && SvROK(v))) XSRETURN_UNDEF; 0 0 0
39 0 RETVAL = newSVsv(v);
40 OUTPUT:
41 RETVAL
42  
43 MODULE = Punk::Queue PACKAGE = Punk::Queue::Supervisor
44  
45 # The process pool: fork n children running $queue->worker(%opts)->run,
46 # respawn with backoff, shut down cleanly on TERM/INT, recycle on HUP,
47 # report on USR2. Returns the intended process exit code.
48  
49 IV
50 run(class, queue, ...)
51 SV *class
52 SV *queue
53 PREINIT:
54 HV *opts, *wopts;
55 SV *wopts_rv;
56 14 int i, nworkers = 1, fail_fast = 0, scheduler = 1;
57 14 ▶ double graceful = 60.0;
Branch: 1 branch
BranchTrueFalseTotal
double graceful = 60.0; 14 0 14
58 CODE:
59 {
60 PERL_UNUSED_ARG(class);
61 14 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 0 14 14
62 0 croak("Punk::Queue::Supervisor->run: odd number of options");
63 14 opts = (HV *)sv_2mortal((SV *)newHV());
64 75 ▶ for (i = 2; i < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i += 2) 61 14 75
65 61 (void)hv_store_ent(opts, ST(i), newSVsv(ST(i + 1)), 0);
66  
67 {
68 SV *v;
69 14 ▶ if ((v = pq_get(aTHX_ opts, "workers")) && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pq_get(aTHX_ opts, "workers")) && SvOK(v)) 14 0 14
if ((v = pq_get(aTHX_ opts, "workers")) && SvOK(v)) 14 0 14
70 14 nworkers = (int)SvIV(v);
71 14 ▶ if ((v = pq_get(aTHX_ opts, "graceful_timeout")) && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pq_get(aTHX_ opts, "graceful_timeout")) && SvOK(v)) 14 0 14
if ((v = pq_get(aTHX_ opts, "graceful_timeout")) && SvOK(v)) 14 0 14
72 14 graceful = SvNV(v);
73 14 ▶ if ((v = pq_get(aTHX_ opts, "fail_fast")) && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pq_get(aTHX_ opts, "fail_fast")) && SvOK(v)) 14 0 14
if ((v = pq_get(aTHX_ opts, "fail_fast")) && SvOK(v)) 14 0 14
74 14 fail_fast = SvTRUE(v) ? 1 : 0;
75 }
76 {
77 SV *v;
78 14 ▶ if ((v = pq_get(aTHX_ opts, "scheduler")) && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pq_get(aTHX_ opts, "scheduler")) && SvOK(v)) 14 0 14
if ((v = pq_get(aTHX_ opts, "scheduler")) && SvOK(v)) 14 0 14
79 14 scheduler = SvTRUE(v) ? 1 : 0;
80 }
81  
82 /* everything else in opts is the children's worker options */
83 14 wopts = newHV();
84 {
85 HE *he;
86 14 hv_iterinit(opts);
87 75 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 61 14 75
88 61 ▶ SV *k = HeSVKEY_force(he);
Branch: 2 branches
BranchTrueFalseTotal
SV *k = HeSVKEY_force(he); 61 0 61
SV *k = HeSVKEY_force(he); 0 61 61
89 61 const char *ks = SvPV_nolen(k);
90 61 ▶ if (strEQ(ks, "workers") || strEQ(ks, "graceful_timeout")
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(ks, "workers") || strEQ(ks, "graceful_timeout") 47 14 61
if (strEQ(ks, "workers") || strEQ(ks, "graceful_timeout") 33 14 47
91 33 ▶ || strEQ(ks, "fail_fast") || strEQ(ks, "scheduler"))
Branch: 2 branches
BranchTrueFalseTotal
|| strEQ(ks, "fail_fast") || strEQ(ks, "scheduler")) 19 14 33
|| strEQ(ks, "fail_fast") || strEQ(ks, "scheduler")) 14 5 19
92 56 continue;
93 5 (void)hv_store_ent(wopts, k, newSVsv(HeVAL(he)), 0);
94 }
95 }
96 14 wopts_rv = sv_2mortal(newRV_noinc((SV *)wopts));
97  
98 14 ▶ RETVAL = pqs_run(aTHX_ queue, wopts_rv, nworkers, graceful,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pqs_run(aTHX_ queue, wopts_rv, nworkers, graceful, 0 14 14
99 fail_fast, scheduler);
100 }
101 OUTPUT:
102 RETVAL
« include/pq_hm.h ↑ Summary include/pq_dbi.h »