include/pq_sched.h

filter: Statementstmt 98.5% 267 / 271 Branchbran 70.9% 105 / 148 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.7% 372 / 419 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_claim.h ↑ Summary include/pq_sql.h »
1 #ifndef PQ_SCHED_H
2 #define PQ_SCHED_H
3  
4 /* pq_sched.h - cron storage and the scheduler tick.
5 *
6 * Declarations reconcile into pq_crons keyed on `name`: upsert updates
7 * the definition and recomputes next_run when the schedule itself (expr
8 * or tz) changed, and NEVER touches `enabled` - an operator's disable
9 * must survive every deploy, or pausing a misbehaving cron lasts exactly
10 * until the next boot. disable_missing_crons retires declarations that
11 * disappeared; nothing is ever deleted, so history and identity survive.
12 *
13 * The tick is leader-only (the phase-6 lease arbitrates) and per due
14 * cron ADVANCES FIRST, THEN ENQUEUES: a crash between the two loses one
15 * occurrence, which is strictly better than duplicating one. The
16 * optimistic `WHERE next_run = ?` guard makes a double-elected leader
17 * harmless, and the occurrence dedupe key is the belt to those braces.
18 * The guarantee, exactly: at-most-once per occurrence, at-least-once per
19 * enqueued job.
20 *
21 * Include after pq_cron.h, pq_lock.h and pq_backend.h. */
22  
23 #define PQ_CRON_COLS \
24 "id, name, expr, task, args, queue, priority, attempts, tz, catchup," \
25 " enabled, last_run, next_run, last_job"
26  
27 enum {
28 PQ_CR_ID = 0, PQ_CR_NAME, PQ_CR_EXPR, PQ_CR_TASK, PQ_CR_ARGS,
29 PQ_CR_QUEUE, PQ_CR_PRIORITY, PQ_CR_ATTEMPTS, PQ_CR_TZ, PQ_CR_CATCHUP,
30 PQ_CR_ENABLED, PQ_CR_LAST_RUN, PQ_CR_NEXT_RUN, PQ_CR_LAST_JOB
31 };
32  
33 37 static HV *pq_cron_row_hv(pTHX_ AV *row) {
34 37 HV *h = newHV();
35 37 ▶ if (!row) return h;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return h; 0 37 37
36 37 pq_hv_set_iv(aTHX_ h, "id", pq_col_iv(aTHX_ row, PQ_CR_ID));
37 37 pq_hv_set_sv(aTHX_ h, "name", pq_col(aTHX_ row, PQ_CR_NAME));
38 37 pq_hv_set_sv(aTHX_ h, "expr", pq_col(aTHX_ row, PQ_CR_EXPR));
39 37 pq_hv_set_sv(aTHX_ h, "task", pq_col(aTHX_ row, PQ_CR_TASK));
40 37 (void)hv_stores(h, "args",
41 pq_json_decode(aTHX_ pq_col(aTHX_ row, PQ_CR_ARGS)));
42 37 pq_hv_set_sv(aTHX_ h, "queue", pq_col(aTHX_ row, PQ_CR_QUEUE));
43 37 pq_hv_set_iv(aTHX_ h, "priority", pq_col_iv(aTHX_ row, PQ_CR_PRIORITY));
44 37 pq_hv_set_iv(aTHX_ h, "attempts", pq_col_iv(aTHX_ row, PQ_CR_ATTEMPTS));
45 37 pq_hv_set_sv(aTHX_ h, "tz", pq_col(aTHX_ row, PQ_CR_TZ));
46 37 pq_hv_set_sv(aTHX_ h, "catchup", pq_col(aTHX_ row, PQ_CR_CATCHUP));
47 37 pq_hv_set_iv(aTHX_ h, "enabled", pq_col_iv(aTHX_ row, PQ_CR_ENABLED));
48 37 pq_hv_set_nv_or_undef(aTHX_ h, "last_run",
49 pq_col(aTHX_ row, PQ_CR_LAST_RUN));
50 37 pq_hv_set_nv_or_undef(aTHX_ h, "next_run",
51 pq_col(aTHX_ row, PQ_CR_NEXT_RUN));
52 {
53 37 SV *j = pq_col(aTHX_ row, PQ_CR_LAST_JOB);
54 37 ▶ (void)hv_stores(h, "last_job",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "last_job", 5 32 37
55 SvOK(j) ? newSViv(SvIV(j)) : newSV(0));
56 }
57 37 return h;
58 }
59  
60 /* next occurrence for a stored expr/tz pair, strictly after `from` */
61 28 static double pq_sched_next(pTHX_ SV *expr, SV *tz, double from) {
62 pq_cron c;
63 pq_tz z;
64 28 STRLEN el, tl = 0;
65 28 const char *es = SvPV_const(expr, el);
66 56 ▶ const char *ts = (tz && SvOK(tz)) ? SvPV_const(tz, tl) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *ts = (tz && SvOK(tz)) ? SvPV_const(tz, tl) : NULL; 28 0 28
const char *ts = (tz && SvOK(tz)) ? SvPV_const(tz, tl) : NULL; 28 0 28
67 28 pq_cron_parse(aTHX_ es, el, &c);
68 28 pq_tz_parse(aTHX_ ts, tl, &z);
69 28 return pq_cron_next(aTHX_ &c, from, &z);
70 }
71  
72 /* ---- upsert / reconcile ---------------------------------------------------- */
73  
74 29 static IV pq_upsert_cron(pTHX_ SV *self, HV *def) {
75 29 SV *name = pq_get(aTHX_ def, "name");
76 29 SV *expr = pq_get(aTHX_ def, "expr");
77 29 SV *task = pq_get(aTHX_ def, "task");
78 29 SV *queue = pq_get(aTHX_ def, "queue");
79 29 SV *tz = pq_get(aTHX_ def, "tz");
80 29 SV *catchup = pq_get(aTHX_ def, "catchup");
81 SV *args_json;
82 29 double now = pq_now(aTHX_ self);
83 29 IV id = 0;
84  
85 29 pq_name_check(aTHX_ name, "cron");
86 29 pq_name_check(aTHX_ task, "task");
87 29 ▶ if (queue && SvOK(queue)) pq_name_check(aTHX_ queue, "queue");
Branch: 2 branches
BranchTrueFalseTotal
if (queue && SvOK(queue)) pq_name_check(aTHX_ queue, "queue"); 11 18 29
if (queue && SvOK(queue)) pq_name_check(aTHX_ queue, "queue"); 11 0 11
88 18 else queue = sv_2mortal(newSVpvs("default"));
89 29 ▶ if (!(expr && SvOK(expr)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(expr && SvOK(expr))) 29 0 29
if (!(expr && SvOK(expr))) 0 29 29
90 0 croak("Punk::Queue: a cron needs an expression");
91 29 ▶ if (!(tz && SvOK(tz))) tz = sv_2mortal(newSVpvs("UTC"));
Branch: 2 branches
BranchTrueFalseTotal
if (!(tz && SvOK(tz))) tz = sv_2mortal(newSVpvs("UTC")); 9 20 29
if (!(tz && SvOK(tz))) tz = sv_2mortal(newSVpvs("UTC")); 0 9 9
92 38 ▶ if (catchup && SvOK(catchup)) {
Branch: 2 branches
BranchTrueFalseTotal
if (catchup && SvOK(catchup)) { 9 20 29
if (catchup && SvOK(catchup)) { 9 0 9
93 9 const char *cp = SvPV_nolen(catchup);
94 9 ▶ if (strNE(cp, "once") && strNE(cp, "all") && strNE(cp, "skip"))
Branch: 3 branches
BranchTrueFalseTotal
if (strNE(cp, "once") && strNE(cp, "all") && strNE(cp, "skip")) 2 7 9
if (strNE(cp, "once") && strNE(cp, "all") && strNE(cp, "skip")) 1 1 2
if (strNE(cp, "once") && strNE(cp, "all") && strNE(cp, "skip")) 0 1 1
95 0 croak("Punk::Queue: cron catchup must be once, all or skip, "
96 "not '%s'", cp);
97 }
98 20 else catchup = sv_2mortal(newSVpvs("once"));
99  
100 /* the boot croak: syntax AND can-it-ever-fire */
101 {
102 pq_cron c;
103 pq_tz z;
104 STRLEN el, tl;
105 29 ▶ const char *es = SvPV_const(expr, el);
Branch: 1 branch
BranchTrueFalseTotal
const char *es = SvPV_const(expr, el); 29 0 29
106 29 const char *ts = SvPV_const(tz, tl);
107 29 pq_cron_compile(aTHX_ es, el, ts, tl, &c, &z);
108 }
109  
110 26 args_json = sv_2mortal(pq_json_encode(aTHX_
111 pq_get(aTHX_ def, "args"), "[]"));
112  
113 26 pq_txn_begin(aTHX_ self);
114 {
115 26 AV *b = pq_binds(aTHX), *row;
116 26 pq_bind_sv(aTHX_ b, name);
117 26 row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
118 "SELECT id, expr, tz FROM pq_crons WHERE name = ?")), b);
119  
120 26 ▶ if (row) {
Branch: 1 branch
BranchTrueFalseTotal
if (row) { 6 20 26
121 6 SV *oexpr = pq_col(aTHX_ row, 1);
122 6 SV *otz = pq_col(aTHX_ row, 2);
123 6 int sched_changed =
124 6 ▶ !(SvOK(oexpr) && sv_eq(oexpr, expr))
Branch: 1 branch
BranchTrueFalseTotal
!(SvOK(oexpr) && sv_eq(oexpr, expr)) 4 2 6
125 12 ▶ || !(SvOK(otz) && sv_eq(otz, tz));
Branch: 3 branches
BranchTrueFalseTotal
|| !(SvOK(otz) && sv_eq(otz, tz)); 6 0 6
|| !(SvOK(otz) && sv_eq(otz, tz)); 4 0 4
|| !(SvOK(otz) && sv_eq(otz, tz)); 0 4 4
126 6 id = pq_col_iv(aTHX_ row, 0);
127 6 SvREFCNT_dec((SV *)row);
128  
129 6 b = pq_binds(aTHX);
130 {
131 6 SV *sql = pq_sql_new(aTHX_
132 "UPDATE pq_crons SET expr = ?, task = ?, args = ?,"
133 " queue = ?, priority = ?, attempts = ?, tz = ?,"
134 " catchup = ?, updated = ?");
135 6 pq_bind_sv(aTHX_ b, expr);
136 6 pq_bind_sv(aTHX_ b, task);
137 6 pq_bind_sv(aTHX_ b, args_json);
138 6 pq_bind_sv(aTHX_ b, queue);
139 {
140 6 SV *prio = pq_get(aTHX_ def, "priority");
141 6 ▶ pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0);
Branch: 2 branches
BranchTrueFalseTotal
pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0); 4 2 6
pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0); 4 0 4
142 }
143 {
144 6 SV *at = pq_get(aTHX_ def, "attempts");
145 6 ▶ pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1);
Branch: 2 branches
BranchTrueFalseTotal
pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1); 3 3 6
pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1); 3 0 3
146 }
147 6 pq_bind_sv(aTHX_ b, tz);
148 6 pq_bind_sv(aTHX_ b, catchup);
149 6 pq_bind_nv(aTHX_ b, now);
150 6 ▶ if (sched_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if (sched_changed) { 2 4 6
151 /* a new schedule starts from now - and deliberately
152 * NOT a new enabled state */
153 2 pq_sql_cat(aTHX_ sql, ", next_run = ?");
154 2 pq_bind_nv(aTHX_ b,
155 pq_sched_next(aTHX_ expr, tz, now));
156 }
157 6 pq_sql_cat(aTHX_ sql, " WHERE id = ?");
158 6 pq_bind_iv(aTHX_ b, id);
159 6 (void)pq_do(aTHX_ self, sql, b);
160 }
161 }
162 else {
163 20 SV *prio = pq_get(aTHX_ def, "priority");
164 20 SV *at = pq_get(aTHX_ def, "attempts");
165 20 b = pq_binds(aTHX);
166 20 pq_bind_sv(aTHX_ b, name);
167 20 pq_bind_sv(aTHX_ b, expr);
168 20 pq_bind_sv(aTHX_ b, task);
169 20 pq_bind_sv(aTHX_ b, args_json);
170 20 pq_bind_sv(aTHX_ b, queue);
171 20 ▶ pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0);
Branch: 2 branches
BranchTrueFalseTotal
pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0); 7 13 20
pq_bind_iv(aTHX_ b, (prio && SvOK(prio)) ? SvIV(prio) : 0); 7 0 7
172 20 ▶ pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1);
Branch: 2 branches
BranchTrueFalseTotal
pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1); 6 14 20
pq_bind_iv(aTHX_ b, (at && SvOK(at)) ? SvIV(at) : 1); 6 0 6
173 20 pq_bind_sv(aTHX_ b, tz);
174 20 pq_bind_sv(aTHX_ b, catchup);
175 20 pq_bind_nv(aTHX_ b, pq_sched_next(aTHX_ expr, tz, now));
176 20 pq_bind_nv(aTHX_ b, now);
177 20 pq_bind_nv(aTHX_ b, now);
178 {
179 20 SV *sql = pq_sql_new(aTHX_
180 "INSERT INTO pq_crons (name, expr, task, args, queue,"
181 " priority, attempts, tz, catchup, enabled, next_run,"
182 " created, updated)"
183 " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?)");
184 20 ▶ if (pq_has_returning(aTHX_ self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pq_has_returning(aTHX_ self)) { 20 0 20
185 AV *r2;
186 20 pq_sql_cat(aTHX_ sql, " RETURNING id");
187 20 r2 = pq_selectrow(aTHX_ self, sql, b);
188 20 ▶ if (r2) { id = pq_col_iv(aTHX_ r2, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (r2) { id = pq_col_iv(aTHX_ r2, 0); 20 0 20
189 20 SvREFCNT_dec((SV *)r2); }
190 }
191 else {
192 0 (void)pq_do(aTHX_ self, sql, b);
193 0 id = pq_last_insert_id(aTHX_ self);
194 }
195 }
196 }
197 }
198 26 pq_txn_commit(aTHX_ self);
199 26 return id;
200 }
201  
202 /* Retire declarations the app no longer makes: enabled = 0, never
203 * delete, so an accidentally dropped declaration keeps its identity and
204 * history when it comes back. */
205 20 static IV pq_disable_missing_crons(pTHX_ SV *self, AV *names) {
206 20 ▶ SSize_t n = names ? av_len(names) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = names ? av_len(names) + 1 : 0, i; 20 0 20
207 20 AV *bind = pq_binds(aTHX);
208 20 SV *sql = pq_sql_new(aTHX_
209 "UPDATE pq_crons SET enabled = 0, updated = ?");
210 20 pq_bind_nv(aTHX_ bind, pq_now(aTHX_ self));
211 20 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 8 12 20
212 8 pq_sql_cat(aTHX_ sql, " WHERE enabled = 1 AND name NOT IN (");
213 8 pq_sql_placeholders(aTHX_ sql, n);
214 8 pq_sql_cat(aTHX_ sql, ")");
215 16 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 8 8 16
216 8 pq_bind_sv(aTHX_ bind, *av_fetch(names, i, 0));
217 }
218 12 else pq_sql_cat(aTHX_ sql, " WHERE enabled = 1");
219 20 return pq_do(aTHX_ self, sql, bind);
220 }
221  
222 /* Enable / disable, the operational pause. Re-enabling recomputes
223 * next_run from NOW regardless of the cron's own catchup policy - the
224 * disabled window behaves as `skip`, so flipping a cron back on cannot
225 * fire a catch-up storm for the time it was off. */
226 9 static IV pq_enable_cron(pTHX_ SV *self, SV *name, int on) {
227 9 IV took = 0;
228 9 pq_name_check(aTHX_ name, "cron");
229 9 pq_txn_begin(aTHX_ self);
230 9 ▶ if (on) {
Branch: 1 branch
BranchTrueFalseTotal
if (on) { 4 5 9
231 4 AV *b = pq_binds(aTHX), *row;
232 4 pq_bind_sv(aTHX_ b, name);
233 4 row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
234 "SELECT id, expr, tz FROM pq_crons WHERE name = ?"
235 " AND enabled = 0")), b);
236 4 ▶ if (row) {
Branch: 1 branch
BranchTrueFalseTotal
if (row) { 3 1 4
237 3 IV id = pq_col_iv(aTHX_ row, 0);
238 3 double nx = pq_sched_next(aTHX_ pq_col(aTHX_ row, 1),
239 pq_col(aTHX_ row, 2),
240 pq_now(aTHX_ self));
241 3 SvREFCNT_dec((SV *)row);
242 3 b = pq_binds(aTHX);
243 3 pq_bind_nv(aTHX_ b, nx);
244 3 pq_bind_nv(aTHX_ b, pq_now(aTHX_ self));
245 3 pq_bind_iv(aTHX_ b, id);
246 3 took = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
247 "UPDATE pq_crons SET enabled = 1, next_run = ?,"
248 " updated = ? WHERE id = ?")), b);
249 }
250 }
251 else {
252 5 AV *b = pq_binds(aTHX);
253 5 pq_bind_nv(aTHX_ b, pq_now(aTHX_ self));
254 5 pq_bind_sv(aTHX_ b, name);
255 5 took = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
256 "UPDATE pq_crons SET enabled = 0, updated = ?"
257 " WHERE name = ? AND enabled = 1")), b);
258 }
259 9 pq_txn_commit(aTHX_ self);
260 9 return took > 0 ? 1 : 0;
261 }
262  
263 33 static SV *pq_cron_info(pTHX_ SV *self, SV *name) {
264 33 AV *b = pq_binds(aTHX), *row;
265 HV *h;
266 33 pq_bind_sv(aTHX_ b, name);
267 33 row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
268 "SELECT " PQ_CRON_COLS " FROM pq_crons WHERE name = ?")), b);
269 33 ▶ if (!row) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return NULL; 3 30 33
270 30 h = pq_cron_row_hv(aTHX_ row);
271 30 SvREFCNT_dec((SV *)row);
272 30 return newRV_noinc((SV *)h);
273 }
274  
275 6 static SV *pq_list_crons(pTHX_ SV *self, IV offset, IV limit) {
276 6 HV *out = newHV();
277 6 AV *crons = newAV();
278 {
279 6 AV *row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
280 "SELECT count(*) FROM pq_crons")), NULL);
281 6 ▶ (void)hv_stores(out, "total",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "total", 6 0 6
282 newSViv(row ? pq_col_iv(aTHX_ row, 0) : 0));
283 6 ▶ if (row) SvREFCNT_dec((SV *)row);
Branch: 1 branch
BranchTrueFalseTotal
if (row) SvREFCNT_dec((SV *)row); 6 0 6
284 }
285 {
286 SV *sth;
287 6 AV *bind = pq_binds(aTHX), *row;
288 6 ▶ pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 50);
Branch: 1 branch
BranchTrueFalseTotal
pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 50); 0 6 6
289 6 pq_bind_iv(aTHX_ bind, offset > 0 ? offset : 0);
290 6 sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
291 "SELECT " PQ_CRON_COLS " FROM pq_crons ORDER BY name"
292 " LIMIT ? OFFSET ?")));
293 6 (void)pq_execute(aTHX_ sth, bind);
294 13 ▶ while ((row = pq_fetchrow(aTHX_ sth))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) { 7 6 13
295 7 av_push(crons, newRV_noinc((SV *)pq_cron_row_hv(aTHX_ row)));
296 7 SvREFCNT_dec((SV *)row);
297 }
298 6 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
299 6 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 6 0 6
300 }
301 6 (void)hv_stores(out, "crons", newRV_noinc((SV *)crons));
302 6 return newRV_noinc((SV *)out);
303 }
304  
305 /* ---- the tick --------------------------------------------------------------
306 *
307 * Leader-only; the caller holds the lease. Per due cron:
308 *
309 * advance next_run under the optimistic `WHERE next_run = ?` guard -
310 * a second leader loses cleanly - THEN enqueue the occurrence with the
311 * dedupe key cron:<name-hash>:<occurrence>, which makes even a race
312 * that beats the guard at-most-once per occurrence.
313 *
314 * An occurrence is "missed" when it is more than a minute stale (one
315 * minute is cron's own resolution). `once` fires the oldest missed
316 * occurrence then jumps to the future; `all` fires each missed one up
317 * to catchup_max then jumps; `skip` fires only fresh occurrences.
318 * Returns the number of jobs enqueued. */
319  
320 26 static IV pq_cron_tick(pTHX_ SV *self, IV limit) {
321 26 double now = pq_now(aTHX_ self);
322 26 IV catchup_max = (IV)pq_opt_num(aTHX_ self, "catchup_max", 10.0);
323 26 IV fired_total = 0;
324 SV *sth;
325 26 AV *bind = pq_binds(aTHX), *row;
326 26 AV *due = (AV *)sv_2mortal((SV *)newAV());
327 SSize_t i, ndue;
328  
329 26 pq_bind_nv(aTHX_ bind, now);
330 26 ▶ pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 100);
Branch: 1 branch
BranchTrueFalseTotal
pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 100); 26 0 26
331 26 sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
332 "SELECT " PQ_CRON_COLS " FROM pq_crons"
333 " WHERE enabled = 1 AND next_run <= ?"
334 " ORDER BY next_run LIMIT ?")));
335 26 (void)pq_execute(aTHX_ sth, bind);
336 36 ▶ while ((row = pq_fetchrow(aTHX_ sth)))
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) 10 26 36
337 10 av_push(due, newRV_noinc((SV *)row));
338 26 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
339 26 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 26 0 26
340  
341 26 ndue = av_len(due) + 1;
342 36 ▶ for (i = 0; i < ndue; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ndue; i++) { 10 26 36
343 10 AV *r2 = (AV *)SvRV(*av_fetch(due, i, 0));
344 10 IV id = pq_col_iv(aTHX_ r2, PQ_CR_ID);
345 10 SV *name = pq_col(aTHX_ r2, PQ_CR_NAME);
346 10 SV *expr = pq_col(aTHX_ r2, PQ_CR_EXPR);
347 10 SV *task = pq_col(aTHX_ r2, PQ_CR_TASK);
348 10 SV *tz = pq_col(aTHX_ r2, PQ_CR_TZ);
349 10 const char *policy = SvOK(pq_col(aTHX_ r2, PQ_CR_CATCHUP))
350 20 ? SvPV_nolen(pq_col(aTHX_ r2, PQ_CR_CATCHUP))
351 10 ▶ : "once";
Branch: 1 branch
BranchTrueFalseTotal
: "once"; 10 0 10
352 10 SV *occ_sv = pq_col(aTHX_ r2, PQ_CR_NEXT_RUN);
353 10 ▶ double occ = SvOK(occ_sv) ? SvNV(occ_sv) : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
double occ = SvOK(occ_sv) ? SvNV(occ_sv) : 0.0; 10 0 10
354 10 IV count = 0;
355 10 int died = 0;
356  
357 2 for (;;) {
358 double nx;
359 12 int missed = (now - occ) > 60.0;
360 12 ▶ int fire = !(strEQ(policy, "skip") && missed);
Branch: 2 branches
BranchTrueFalseTotal
int fire = !(strEQ(policy, "skip") && missed); 2 10 12
int fire = !(strEQ(policy, "skip") && missed); 1 1 2
361 IV took;
362 12 nx = -1;
363 {
364 12 SV *r3 = NULL;
365 12 int d2 = 0;
366 SV *argv[3];
367 12 argv[0] = expr;
368 12 argv[1] = sv_2mortal(newSVnv(occ));
369 12 argv[2] = tz;
370 12 r3 = pq_call_meth_ev(aTHX_
371 sv_2mortal(newSVpvs("Punk::Queue::Cron")),
372 "next_after", argv, 3, 1, &d2);
373 12 ▶ if (!d2 && r3 && SvOK(r3)) nx = SvNV(r3);
Branch: 3 branches
BranchTrueFalseTotal
if (!d2 && r3 && SvOK(r3)) nx = SvNV(r3); 11 1 12
if (!d2 && r3 && SvOK(r3)) nx = SvNV(r3); 11 0 11
if (!d2 && r3 && SvOK(r3)) nx = SvNV(r3); 11 0 11
374 12 ▶ if (r3) SvREFCNT_dec(r3);
Branch: 1 branch
BranchTrueFalseTotal
if (r3) SvREFCNT_dec(r3); 11 1 12
375 12 ▶ if (d2 || nx < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (d2 || nx < 0) { 11 1 12
if (d2 || nx < 0) { 0 11 11
376 2 ▶ warn("Punk::Queue: cron '%s' failed to compute its "
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::Queue: cron '%s' failed to compute its " 1 0 1
377 "next occurrence - disabling it",
378 SvOK(name) ? SvPV_nolen(name) : "?");
379 1 (void)pq_enable_cron(aTHX_ self, name, 0);
380 1 break;
381 }
382 }
383  
384 /* a stale occurrence under `skip` never fires - jump straight
385 * to the future instead of walking the gap a minute at a
386 * time. last_run stays put: nothing ran. */
387 11 ▶ if (missed && !fire) {
Branch: 2 branches
BranchTrueFalseTotal
if (missed && !fire) { 5 6 11
if (missed && !fire) { 1 4 5
388 1 double jump = nx > now ? nx
389 1 ▶ : pq_sched_next(aTHX_ expr, tz, now);
Branch: 1 branch
BranchTrueFalseTotal
: pq_sched_next(aTHX_ expr, tz, now); 0 1 1
390 1 AV *b = pq_binds(aTHX);
391 1 pq_bind_nv(aTHX_ b, jump);
392 1 pq_bind_nv(aTHX_ b, now);
393 1 pq_bind_iv(aTHX_ b, id);
394 1 pq_bind_nv(aTHX_ b, occ);
395 1 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
396 "UPDATE pq_crons SET next_run = ?, updated = ?"
397 " WHERE id = ? AND next_run = ?")), b);
398 1 break;
399 }
400  
401 /* advance FIRST (the optimistic guard) ... */
402 {
403 10 AV *b = pq_binds(aTHX);
404 10 pq_bind_nv(aTHX_ b, occ);
405 10 pq_bind_nv(aTHX_ b, nx);
406 10 pq_bind_nv(aTHX_ b, now);
407 10 pq_bind_iv(aTHX_ b, id);
408 10 pq_bind_nv(aTHX_ b, occ);
409 10 took = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
410 "UPDATE pq_crons SET last_run = ?, next_run = ?,"
411 " updated = ? WHERE id = ? AND next_run = ?")), b);
412 }
413 10 ▶ if (took <= 0) break; /* another leader owns this cron */
Branch: 1 branch
BranchTrueFalseTotal
if (took <= 0) break; /* another leader owns this cron */ 0 10 10
414  
415 /* ... THEN enqueue, deduped per occurrence */
416 10 ▶ if (fire) {
Branch: 1 branch
BranchTrueFalseTotal
if (fire) { 10 0 10
417 10 HV *opts = (HV *)sv_2mortal((SV *)newHV());
418 20 ▶ SV *ukey = sv_2mortal(Perl_newSVpvf(aTHX_
Branch: 2 branches
BranchTrueFalseTotal
SV *ukey = sv_2mortal(Perl_newSVpvf(aTHX_ 10 0 10
SV *ukey = sv_2mortal(Perl_newSVpvf(aTHX_ 10 0 10
419 "cron:%lx:%ld",
420 (unsigned long)pq_lock_hash(
421 SvOK(name) ? SvPV_nolen(name) : "?",
422 SvOK(name) ? SvCUR(name) : 1),
423 (long)occ));
424 SV *jid;
425 10 (void)hv_stores(opts, "queue",
426 newSVsv(pq_col(aTHX_ r2, PQ_CR_QUEUE)));
427 10 (void)hv_stores(opts, "priority",
428 newSViv(pq_col_iv(aTHX_ r2, PQ_CR_PRIORITY)));
429 10 (void)hv_stores(opts, "attempts",
430 newSViv(pq_col_iv(aTHX_ r2, PQ_CR_ATTEMPTS)));
431 10 (void)hv_stores(opts, "unique", newSVsv(ukey));
432  
433 {
434 SV *argv[4];
435 10 argv[0] = task;
436 10 argv[1] = pq_col(aTHX_ r2, PQ_CR_ARGS);
437 /* args column is JSON text; decode for enqueue */
438 10 argv[1] = sv_2mortal(pq_json_decode(aTHX_ argv[1]));
439 10 argv[2] = sv_2mortal(newRV_inc((SV *)opts));
440 10 jid = pq_call_meth_ev(aTHX_ self, "enqueue",
441 argv, 3, 1, &died);
442 }
443 10 ▶ if (!died && jid && SvOK(jid)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!died && jid && SvOK(jid)) { 10 0 10
if (!died && jid && SvOK(jid)) { 10 0 10
if (!died && jid && SvOK(jid)) { 10 0 10
444 10 AV *b = pq_binds(aTHX);
445 10 pq_bind_sv(aTHX_ b, jid);
446 10 pq_bind_iv(aTHX_ b, id);
447 10 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
448 "UPDATE pq_crons SET last_job = ? WHERE id = ?")),
449 b);
450 10 fired_total++;
451 10 count++;
452 }
453 10 ▶ if (jid) SvREFCNT_dec(jid);
Branch: 1 branch
BranchTrueFalseTotal
if (jid) SvREFCNT_dec(jid); 10 0 10
454 }
455  
456 10 occ = nx;
457 10 ▶ if (occ > now) break;
Branch: 1 branch
BranchTrueFalseTotal
if (occ > now) break; 6 4 10
458  
459 /* still behind: policy decides how far the catch-up goes */
460 4 ▶ if (strEQ(policy, "once") && count >= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(policy, "once") && count >= 1) { 1 3 4
if (strEQ(policy, "once") && count >= 1) { 1 0 1
461 1 double jump = pq_sched_next(aTHX_ expr, tz, now);
462 1 AV *b = pq_binds(aTHX);
463 1 pq_bind_nv(aTHX_ b, jump);
464 1 pq_bind_iv(aTHX_ b, id);
465 1 pq_bind_nv(aTHX_ b, occ);
466 1 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
467 "UPDATE pq_crons SET next_run = ? WHERE id = ?"
468 " AND next_run = ?")), b);
469 1 break;
470 }
471 3 ▶ if (strEQ(policy, "all") && count >= catchup_max) {
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(policy, "all") && count >= catchup_max) { 3 0 3
if (strEQ(policy, "all") && count >= catchup_max) { 1 2 3
472 1 double jump = pq_sched_next(aTHX_ expr, tz, now);
473 1 AV *b = pq_binds(aTHX);
474 1 pq_bind_nv(aTHX_ b, jump);
475 1 pq_bind_iv(aTHX_ b, id);
476 1 pq_bind_nv(aTHX_ b, occ);
477 1 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
478 "UPDATE pq_crons SET next_run = ? WHERE id = ?"
479 " AND next_run = ?")), b);
480 1 break;
481 }
482 }
483 }
484 26 return fired_total;
485 }
486  
487 #endif /* PQ_SCHED_H */
« include/pq_claim.h ↑ Summary include/pq_sql.h »