include/pq_log.h

filter: Statementstmt 94.1% 48 / 51 Branchbran 75.0% 24 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.7% 72 / 83 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_backend.h ↑ Summary include/pq_claim.h »
1 #ifndef PQ_LOG_H
2 #define PQ_LOG_H
3  
4 /* pq_log.h - per-job log lines (schema step 3, pq_job_logs).
5 *
6 * Two writers: the transitions log their own lifecycle - claimed,
7 * finished, attempt failed with the retry it scheduled, terminal
8 * failure, operator retry - and the task body writes whatever it wants
9 * through $job->log. The lifecycle rows are gated on the `logging`
10 * backend option (default on) so a throughput-sensitive queue can shed
11 * them; an explicit $job->log always writes, because a call the task
12 * author typed is not noise.
13 *
14 * Rows live and die with their job: remove_job and repair's ancient
15 * sweep cascade here, repair also sweeps orphans, and reset empties the
16 * table. Retention is therefore the job's retention - inspecting a
17 * finished job shows its whole story across every attempt, until
18 * remove_after reaps the job itself.
19 *
20 * Include after pq_job.h (uses its hv helpers), before pq_backend.h
21 * (whose transitions call these). */
22  
23 283 static const char *pq_log_level(pTHX_ const char *l) {
24 283 ▶ if (!l || !*l) return "info";
Branch: 2 branches
BranchTrueFalseTotal
if (!l || !*l) return "info"; 280 3 283
if (!l || !*l) return "info"; 0 280 280
25 280 ▶ if (strEQ(l, "debug") || strEQ(l, "info")
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(l, "debug") || strEQ(l, "info") 279 1 280
if (strEQ(l, "debug") || strEQ(l, "info") 29 250 279
26 29 ▶ || strEQ(l, "warn") || strEQ(l, "error"))
Branch: 2 branches
BranchTrueFalseTotal
|| strEQ(l, "warn") || strEQ(l, "error")) 25 4 29
|| strEQ(l, "warn") || strEQ(l, "error")) 24 1 25
27 279 return l;
28 1 croak("Punk::Queue: log level must be debug, info, warn or error, "
29 "not '%s'", l);
30 return NULL; /* not reached */
31 }
32  
33 279 static int pq_logging_on(pTHX_ SV *self) {
34 279 return pq_opt_num(aTHX_ self, "logging", 1.0) != 0.0;
35 }
36  
37 /* the one insert every writer funnels through */
38 283 static void pq_log_add(pTHX_ SV *self, IV id, const char *level, SV *msg) {
39 283 AV *b = pq_binds(aTHX);
40 283 pq_bind_iv(aTHX_ b, id);
41 283 pq_bind_nv(aTHX_ b, pq_now(aTHX_ self));
42 283 pq_bind_sv(aTHX_ b, sv_2mortal(newSVpv(pq_log_level(aTHX_ level), 0)));
43 282 pq_bind_sv(aTHX_ b, msg);
44 282 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
45 "INSERT INTO pq_job_logs (job_id, created, level, message)"
46 " VALUES (?, ?, ?, ?)")), b);
47 282 }
48  
49 /* lifecycle rows: printf-formatted, dropped when logging is off */
50 279 static void pq_log_addf(pTHX_ SV *self, IV id, const char *level,
51 const char *pat, ...) {
52 SV *msg;
53 va_list args;
54 279 ▶ if (!pq_logging_on(aTHX_ self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pq_logging_on(aTHX_ self)) return; 2 277 279
55 277 va_start(args, pat);
56 277 msg = sv_2mortal(Perl_vnewSVpvf(aTHX_ pat, &args));
57 277 va_end(args);
58 277 pq_log_add(aTHX_ self, id, level, msg);
59 }
60  
61 /* an error SV as a bounded, newline-stripped C string for the lifecycle
62 * messages; refs go through their JSON so a structured die is readable */
63 #define PQ_LOG_ERRMAX 500
64 27 static const char *pq_log_errpv(pTHX_ SV *err, char *buf, size_t max) {
65 STRLEN len;
66 const char *p;
67 27 SV *src = err;
68 27 ▶ if (!src || !SvOK(src)) return "unknown error";
Branch: 2 branches
BranchTrueFalseTotal
if (!src || !SvOK(src)) return "unknown error"; 27 0 27
if (!src || !SvOK(src)) return "unknown error"; 0 27 27
69 27 ▶ if (SvROK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(src)) 0 27 27
70 0 src = sv_2mortal(pq_json_encode(aTHX_ src, "null"));
71 27 p = SvPV(src, len);
72 48 ▶ while (len && (p[len - 1] == '\n' || p[len - 1] == '\r')) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (p[len - 1] == '\n' || p[len - 1] == '\r')) len--; 48 0 48
while (len && (p[len - 1] == '\n' || p[len - 1] == '\r')) len--; 21 27 48
while (len && (p[len - 1] == '\n' || p[len - 1] == '\r')) len--; 0 27 27
73 27 ▶ if (len > max - 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > max - 4) { 0 27 27
74 0 memcpy(buf, p, max - 4);
75 0 memcpy(buf + max - 4, "...", 4);
76 }
77 else {
78 27 memcpy(buf, p, len);
79 27 buf[len] = 0;
80 }
81 27 return buf;
82 }
83  
84 /* the whole log for one job, oldest first. +1, caller owns. */
85 9 static AV *pq_job_log_rows(pTHX_ SV *self, IV id) {
86 9 AV *out = newAV();
87 9 AV *b = pq_binds(aTHX), *row;
88 SV *sth;
89 9 pq_bind_iv(aTHX_ b, id);
90 9 sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
91 "SELECT created, level, message FROM pq_job_logs"
92 " WHERE job_id = ? ORDER BY id")));
93 9 (void)pq_execute(aTHX_ sth, b);
94 32 ▶ while ((row = pq_fetchrow(aTHX_ sth))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) { 23 9 32
95 23 HV *h = newHV();
96 23 pq_hv_set_nv_or_undef(aTHX_ h, "created", pq_col(aTHX_ row, 0));
97 23 pq_hv_set_sv(aTHX_ h, "level", pq_col(aTHX_ row, 1));
98 23 pq_hv_set_sv(aTHX_ h, "message", pq_col(aTHX_ row, 2));
99 23 av_push(out, newRV_noinc((SV *)h));
100 23 SvREFCNT_dec((SV *)row);
101 }
102 9 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
103 9 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 9 0 9
104 9 return out;
105 }
106  
107 #endif /* PQ_LOG_H */
« include/pq_backend.h ↑ Summary include/pq_claim.h »