| 1 |
|
| #ifndef PQ_QUEUE_H |
| 2 |
|
| #define PQ_QUEUE_H |
| 3 |
|
| |
| 4 |
|
| /* pq_queue.h - the Punk::Queue object: backend selection, the task |
| 5 |
|
| * registry, and running one job. |
| 6 |
|
| * |
| 7 |
|
| * Punk::Queue is a thin front for a backend plus a table of task bodies. |
| 8 |
|
| * Keeping the two apart is what lets a queue be inspected from a process |
| 9 |
|
| * that has no task code at all - `punk-queue jobs` (phase 3) needs a |
| 10 |
|
| * backend and nothing else, which is why every CLI subcommand can work |
| 11 |
|
| * from --dsn with no app class. |
| 12 |
|
| * |
| 13 |
|
| * Include after pq_backend.h. */ |
| 14 |
|
| |
| 15 |
|
| /* The backend object behind a queue. Borrowed. */ |
| 16 |
2493 |
| static SV *pq_backend_of(pTHX_ SV *self) { |
| 17 |
2493 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue"); |
| 18 |
2493 |
| SV *b = pq_get(aTHX_ h, "backend"); |
| 19 |
2493 |
▶ | if (!(b && SvROK(b))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(b && SvROK(b))) |
2493 |
0 |
2493 |
| if (!(b && SvROK(b))) |
0 |
2493 |
2493 |
|
| 20 |
0 |
| croak("Punk::Queue: no backend - construct with " |
| 21 |
|
| "Punk::Queue->new(dsn => ...)"); |
| 22 |
2493 |
| return b; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
|
| /* dsn -> backend class. `backend => 'Foo'` overrides, '+Full::Class' |
| 26 |
|
| * escapes to a literal class name, following the same convention Punk's |
| 27 |
|
| * `views` and `database` keywords use. */ |
| 28 |
212 |
| static SV *pq_backend_class(pTHX_ HV *opts) { |
| 29 |
212 |
▶ | SV *b = opts ? pq_get(aTHX_ opts, "backend") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *b = opts ? pq_get(aTHX_ opts, "backend") : NULL; |
212 |
0 |
212 |
|
| 30 |
212 |
▶ | SV *dsn = opts ? pq_get(aTHX_ opts, "dsn") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *dsn = opts ? pq_get(aTHX_ opts, "dsn") : NULL; |
212 |
0 |
212 |
|
| 31 |
|
| STRLEN l; |
| 32 |
|
| const char *s; |
| 33 |
|
| |
| 34 |
212 |
▶ | if (b && SvOK(b)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (b && SvOK(b)) { |
0 |
212 |
212 |
| if (b && SvOK(b)) { |
0 |
0 |
0 |
|
| 35 |
0 |
| s = SvPV_const(b, l); |
| 36 |
0 |
▶ | if (l && s[0] == '+') return sv_2mortal(newSVpvn(s + 1, l - 1)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (l && s[0] == '+') return sv_2mortal(newSVpvn(s + 1, l - 1)); |
0 |
0 |
0 |
| if (l && s[0] == '+') return sv_2mortal(newSVpvn(s + 1, l - 1)); |
0 |
0 |
0 |
|
| 37 |
0 |
| return sv_2mortal(Perl_newSVpvf(aTHX_ "Punk::Queue::Backend::%s", s)); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
212 |
▶ | if (!(dsn && SvOK(dsn))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(dsn && SvOK(dsn))) |
212 |
0 |
212 |
| if (!(dsn && SvOK(dsn))) |
0 |
212 |
212 |
|
| 41 |
0 |
| croak("Punk::Queue: new() needs a dsn (or backend => 'Class')"); |
| 42 |
|
| |
| 43 |
212 |
| s = SvPV_const(dsn, l); |
| 44 |
212 |
▶ | if (l >= 11 && memEQ(s, "dbi:SQLite:", 11)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (l >= 11 && memEQ(s, "dbi:SQLite:", 11)) |
212 |
0 |
212 |
| if (l >= 11 && memEQ(s, "dbi:SQLite:", 11)) |
212 |
0 |
212 |
|
| 45 |
212 |
| return sv_2mortal(newSVpvs("Punk::Queue::Backend::SQLite")); |
| 46 |
0 |
▶ | if (l >= 7 && memEQ(s, "dbi:Pg:", 7)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (l >= 7 && memEQ(s, "dbi:Pg:", 7)) |
0 |
0 |
0 |
| if (l >= 7 && memEQ(s, "dbi:Pg:", 7)) |
0 |
0 |
0 |
|
| 47 |
0 |
| return sv_2mortal(newSVpvs("Punk::Queue::Backend::Pg")); |
| 48 |
|
| |
| 49 |
0 |
| croak("Punk::Queue: no backend for dsn '%s' - shipped backends are " |
| 50 |
|
| "SQLite and Pg; pass backend => 'Class' for anything else", s); |
| 51 |
|
| return NULL; /* not reached */ |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* Load the backend class and instantiate it. |
| 55 |
|
| * |
| 56 |
|
| * A class that defines its own new() gets it called with the option list, |
| 57 |
|
| * which is the seam a third-party backend needs. The shipped backends do |
| 58 |
|
| * not: they are POD-only .pm files over XSUBs, so the default path blesses |
| 59 |
|
| * the option hash directly, and pq_dbi.h reads `opts` off it. |
| 60 |
|
| * |
| 61 |
|
| * Returns +1, caller owns. */ |
| 62 |
212 |
| static SV *pq_backend_construct(pTHX_ SV *bclass, HV *opts) { |
| 63 |
212 |
| SV *loader = sv_2mortal(Perl_newSVpvf(aTHX_ "require %s;", SvPV_nolen(bclass))); |
| 64 |
|
| HV *stash; |
| 65 |
|
| |
| 66 |
212 |
| eval_pv(SvPV_nolen(loader), FALSE); |
| 67 |
212 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
212 |
0 |
212 |
| if (SvTRUE(ERRSV)) |
0 |
212 |
212 |
|
| 68 |
0 |
▶ | croak("Punk::Queue: cannot load backend %s: %s", |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Punk::Queue: cannot load backend %s: %s", |
0 |
0 |
0 |
| croak("Punk::Queue: cannot load backend %s: %s", |
0 |
0 |
0 |
|
| 69 |
|
| SvPV_nolen(bclass), SvPV_nolen(ERRSV)); |
| 70 |
|
| |
| 71 |
212 |
| stash = gv_stashsv(bclass, 0); |
| 72 |
212 |
▶ | if (stash && gv_fetchmethod_autoload(stash, "new", 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (stash && gv_fetchmethod_autoload(stash, "new", 0)) { |
212 |
0 |
212 |
| if (stash && gv_fetchmethod_autoload(stash, "new", 0)) { |
0 |
212 |
212 |
|
| 73 |
|
| SV *argv[1], *obj; |
| 74 |
0 |
| argv[0] = sv_2mortal(newRV_inc((SV *)opts)); |
| 75 |
0 |
| obj = pq_call_meth(aTHX_ bclass, "new", argv, 1, 1); |
| 76 |
0 |
▶ | if (!(obj && SvROK(obj))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(obj && SvROK(obj))) { |
0 |
0 |
0 |
| if (!(obj && SvROK(obj))) { |
0 |
0 |
0 |
|
| 77 |
0 |
▶ | if (obj) SvREFCNT_dec(obj); |
Branch: 1 branch
| Branch | True | False | Total |
| if (obj) SvREFCNT_dec(obj); |
0 |
0 |
0 |
|
| 78 |
0 |
| croak("Punk::Queue: %s->new returned no object", |
| 79 |
|
| SvPV_nolen(bclass)); |
| 80 |
|
| } |
| 81 |
0 |
| return obj; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| { |
| 85 |
212 |
| HV *h = newHV(); |
| 86 |
|
| SV *rv; |
| 87 |
212 |
| (void)hv_stores(h, "opts", newRV_inc((SV *)opts)); |
| 88 |
|
| /* An externally supplied handle bypasses the pool; carry it |
| 89 |
|
| * through so pq_dbh can find it. */ |
| 90 |
|
| { |
| 91 |
212 |
| SV *dbh = pq_get(aTHX_ opts, "dbh"); |
| 92 |
212 |
▶ | if (dbh && SvROK(dbh)) (void)hv_stores(h, "dbh", newSVsv(dbh)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (dbh && SvROK(dbh)) (void)hv_stores(h, "dbh", newSVsv(dbh)); |
0 |
212 |
212 |
| if (dbh && SvROK(dbh)) (void)hv_stores(h, "dbh", newSVsv(dbh)); |
0 |
0 |
0 |
|
| 93 |
|
| } |
| 94 |
212 |
| rv = newRV_noinc((SV *)h); |
| 95 |
212 |
| sv_bless(rv, gv_stashsv(bclass, GV_ADD)); |
| 96 |
212 |
| return rv; |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| /* ---- auto-migration -------------------------------------------------------- |
| 101 |
|
| * |
| 102 |
|
| * On by default (Minion parity), disabled with auto_migrate => 0. |
| 103 |
|
| * |
| 104 |
|
| * It fires on the first job operation, not in new() and not on |
| 105 |
|
| * introspection: `Punk::Queue->new(...)->schema_version` must be able to |
| 106 |
|
| * report 0 for an unmigrated database, which is exactly what a deploy |
| 107 |
|
| * check wants to ask. Enqueueing into a database with no tables, on the |
| 108 |
|
| * other hand, has no useful reading other than "migrate it". |
| 109 |
|
| * |
| 110 |
|
| * The flag flips before the migrate rather than after, so a failing |
| 111 |
|
| * migration surfaces its own error once instead of being retried on every |
| 112 |
|
| * subsequent call. */ |
| 113 |
1905 |
| static void pq_ensure_migrated(pTHX_ SV *self) { |
| 114 |
1905 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue"); |
| 115 |
1905 |
| SV *done = pq_get(aTHX_ h, "migrated"); |
| 116 |
|
| HV *opts; |
| 117 |
|
| SV *want; |
| 118 |
|
| |
| 119 |
1905 |
▶ | if (done && SvTRUE(done)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (done && SvTRUE(done)) return; |
1725 |
180 |
1905 |
| if (done && SvTRUE(done)) return; |
1725 |
0 |
1725 |
|
| 120 |
|
| |
| 121 |
180 |
| opts = pq_get_hv(aTHX_ h, "opts"); |
| 122 |
180 |
▶ | want = opts ? pq_get(aTHX_ opts, "auto_migrate") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| want = opts ? pq_get(aTHX_ opts, "auto_migrate") : NULL; |
180 |
0 |
180 |
|
| 123 |
180 |
| (void)hv_stores(h, "migrated", newSViv(1)); |
| 124 |
180 |
▶ | if (want && !SvTRUE(want)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (want && !SvTRUE(want)) return; |
1 |
179 |
180 |
| if (want && !SvTRUE(want)) return; |
1 |
0 |
1 |
|
| 125 |
|
| |
| 126 |
179 |
| (void)pq_migrate(aTHX_ pq_backend_of(aTHX_ self), 0); |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
|
| /* ---- the task registry ----------------------------------------------------- */ |
| 130 |
|
| |
| 131 |
245 |
| static HV *pq_tasks(pTHX_ SV *self) { |
| 132 |
245 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue"); |
| 133 |
245 |
| HV *t = pq_get_hv(aTHX_ h, "tasks"); |
| 134 |
245 |
▶ | if (!t) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) { |
0 |
245 |
245 |
|
| 135 |
0 |
| t = newHV(); |
| 136 |
0 |
| (void)hv_stores(h, "tasks", newRV_noinc((SV *)t)); |
| 137 |
|
| } |
| 138 |
245 |
| return t; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /* The body registered for a task name, or NULL. Borrowed. */ |
| 142 |
74 |
| static SV *pq_task_body(pTHX_ SV *self, SV *name) { |
| 143 |
74 |
| HV *t = pq_tasks(aTHX_ self); |
| 144 |
|
| HE *he; |
| 145 |
74 |
▶ | if (!(name && SvOK(name))) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(name && SvOK(name))) return NULL; |
74 |
0 |
74 |
| if (!(name && SvOK(name))) return NULL; |
0 |
74 |
74 |
|
| 146 |
74 |
| he = hv_fetch_ent(t, name, 0, 0); |
| 147 |
74 |
▶ | return he ? HeVAL(he) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return he ? HeVAL(he) : NULL; |
70 |
4 |
74 |
|
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| /* ---- defaults -------------------------------------------------------------- |
| 151 |
|
| * |
| 152 |
|
| * Per-task and per-queue defaults, merged at enqueue in a fixed |
| 153 |
|
| * precedence: explicit opts, then the task's defaults, then the queue's, |
| 154 |
|
| * then the built-ins. The queue name is resolved AFTER the task defaults |
| 155 |
|
| * merge, so a task whose defaults say `queue => 'mail'` picks up the mail |
| 156 |
|
| * queue's defaults too - the rule an operator would guess. |
| 157 |
|
| * |
| 158 |
|
| * These live on the front object, not in the database: they are |
| 159 |
|
| * application configuration, and two apps sharing one database may |
| 160 |
|
| * legitimately disagree about them. */ |
| 161 |
|
| |
| 162 |
1984 |
| static HV *pq_defaults_slot(pTHX_ SV *self, const char *slot) { |
| 163 |
1984 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue"); |
| 164 |
1984 |
| HV *d = pq_get_hv(aTHX_ h, slot); |
| 165 |
1984 |
▶ | if (!d) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d) { |
188 |
1796 |
1984 |
|
| 166 |
188 |
| d = newHV(); |
| 167 |
188 |
| (void)hv_store(h, slot, (I32)strlen(slot), |
| 168 |
|
| newRV_noinc((SV *)d), 0); |
| 169 |
|
| } |
| 170 |
1984 |
| return d; |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
1980 |
| static void pq_defaults_apply(pTHX_ HV *defaults, SV *name, HV *opts) { |
| 174 |
|
| HE *slot, *he; |
| 175 |
|
| HV *d; |
| 176 |
1980 |
▶ | if (!(name && SvOK(name))) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(name && SvOK(name))) return; |
1980 |
0 |
1980 |
| if (!(name && SvOK(name))) return; |
0 |
1980 |
1980 |
|
| 177 |
1980 |
| slot = hv_fetch_ent(defaults, name, 0, 0); |
| 178 |
1980 |
▶ | if (!slot) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!slot) return; |
1974 |
6 |
1980 |
|
| 179 |
6 |
▶ | d = (SvROK(HeVAL(slot)) && SvTYPE(SvRV(HeVAL(slot))) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| d = (SvROK(HeVAL(slot)) && SvTYPE(SvRV(HeVAL(slot))) == SVt_PVHV) |
6 |
0 |
6 |
|
| 180 |
12 |
▶ | ? (HV *)SvRV(HeVAL(slot)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(HeVAL(slot)) : NULL; |
6 |
0 |
6 |
|
| 181 |
6 |
▶ | if (!d) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d) return; |
0 |
6 |
6 |
|
| 182 |
6 |
| hv_iterinit(d); |
| 183 |
24 |
▶ | while ((he = hv_iternext(d))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(d))) { |
12 |
6 |
18 |
|
| 184 |
12 |
▶ | SV *k = HeSVKEY_force(he); |
Branch: 2 branches
| Branch | True | False | Total |
| SV *k = HeSVKEY_force(he); |
12 |
0 |
12 |
| SV *k = HeSVKEY_force(he); |
0 |
12 |
12 |
|
| 185 |
12 |
▶ | if (!hv_exists_ent(opts, k, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_exists_ent(opts, k, 0)) |
3 |
9 |
12 |
|
| 186 |
9 |
| (void)hv_store_ent(opts, k, newSVsv(HeVAL(he)), 0); |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
990 |
| static void pq_merge_defaults(pTHX_ SV *self, SV *task, HV *opts) { |
| 191 |
|
| SV *queue; |
| 192 |
990 |
| pq_defaults_apply(aTHX_ pq_defaults_slot(aTHX_ self, "task_defaults"), |
| 193 |
|
| task, opts); |
| 194 |
990 |
| queue = pq_get(aTHX_ opts, "queue"); |
| 195 |
990 |
▶ | if (!(queue && SvOK(queue))) queue = sv_2mortal(newSVpvs("default")); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(queue && SvOK(queue))) queue = sv_2mortal(newSVpvs("default")); |
35 |
955 |
990 |
| if (!(queue && SvOK(queue))) queue = sv_2mortal(newSVpvs("default")); |
0 |
35 |
35 |
|
| 196 |
990 |
| pq_defaults_apply(aTHX_ pq_defaults_slot(aTHX_ self, "queue_defaults"), |
| 197 |
|
| queue, opts); |
| 198 |
990 |
| } |
| 199 |
|
| |
| 200 |
|
| /* ---- the ALRM handler ------------------------------------------------------ |
| 201 |
|
| * |
| 202 |
|
| * The croaking body behind the sync timeout layer. Installed into %SIG |
| 203 |
|
| * around the task call, so Perl's safe signals deliver it between opcodes |
| 204 |
|
| * and the die is caught by perform's G_EVAL like any task failure. */ |
| 205 |
|
| |
| 206 |
1 |
| XS_INTERNAL(pq_alrm_die) { |
| 207 |
1 |
| dXSARGS; |
| 208 |
|
| PERL_UNUSED_VAR(items); |
| 209 |
|
| PERL_UNUSED_VAR(cv); |
| 210 |
1 |
| croak("Punk::Queue: job timed out"); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
2 |
| static SV *pq_alrm_handler(pTHX) { |
| 214 |
|
| static CV *handler = NULL; |
| 215 |
2 |
▶ | if (!handler) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!handler) |
1 |
1 |
2 |
|
| 216 |
1 |
| handler = newXS("Punk::Queue::__alrm_die", pq_alrm_die, __FILE__); |
| 217 |
2 |
| return newRV_inc((SV *)handler); |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| /* The on-loop timeout: fail the future, which surfaces through ->get as |
| 221 |
|
| * the job's failure. Must not croak (hm contract), hence the G_EVAL. */ |
| 222 |
|
| typedef struct pq_perform_to { |
| 223 |
|
| SV *future; |
| 224 |
|
| int fired; |
| 225 |
|
| } pq_perform_to; |
| 226 |
|
| |
| 227 |
1 |
| static void pq_perform_to_cb(pTHX_ void *ud) { |
| 228 |
1 |
| pq_perform_to *ctx = (pq_perform_to *)ud; |
| 229 |
1 |
| int died = 0; |
| 230 |
|
| SV *argv[1], *r; |
| 231 |
1 |
| ctx->fired = 1; |
| 232 |
1 |
| argv[0] = sv_2mortal(newSVpvs("Punk::Queue: job timed out")); |
| 233 |
1 |
| r = pq_call_meth_ev(aTHX_ ctx->future, "fail", argv, 1, 1, &died); |
| 234 |
1 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
1 |
0 |
1 |
|
| 235 |
1 |
| } |
| 236 |
|
| |
| 237 |
|
| /* ---- running one job ------------------------------------------------------- |
| 238 |
|
| * |
| 239 |
|
| * Look the task up, call it with ($job, @args) under G_EVAL, and settle the |
| 240 |
|
| * row. A die becomes a failure with the message as the result; a return |
| 241 |
|
| * value becomes the result. |
| 242 |
|
| * |
| 243 |
|
| * The Perl frame appears exactly here, at call_sv, which is the whole |
| 244 |
|
| * argument for doing the surrounding machinery in C: the claim, the decode, |
| 245 |
|
| * the settle and (from phase 3) the heartbeat and timeout timers cost no |
| 246 |
|
| * Perl at all. |
| 247 |
|
| * |
| 248 |
|
| * Timeouts, layer by layer (the plan's honesty clause applies: only the |
| 249 |
|
| * supervisor's SIGKILL is a hard guarantee): |
| 250 |
|
| * - off-loop, timeout > 0: alarm + a croaking ALRM handler around the |
| 251 |
|
| * call and any blocking future get. Best effort - safe signals cannot |
| 252 |
|
| * interrupt a blocking C call. |
| 253 |
|
| * - on-loop, timeout > 0, the task returned a future: a one-shot hm |
| 254 |
|
| * timer fails the future, and the failure surfaces through ->get. No |
| 255 |
|
| * alarm on this path: a die longjmping out of a loop callback would |
| 256 |
|
| * leave the dispatch loop inconsistent. |
| 257 |
|
| * |
| 258 |
|
| * Returns 1 if the job finished, 0 if it failed. */ |
| 259 |
66 |
| static int pq_perform(pTHX_ SV *self, SV *job) { |
| 260 |
66 |
| SV *backend = pq_backend_of(aTHX_ self); |
| 261 |
66 |
| HV *jh = pq_hv(aTHX_ job, "Punk::Queue::Job"); |
| 262 |
66 |
| HV *row = pq_get_hv(aTHX_ jh, "row"); |
| 263 |
66 |
▶ | SV *name = row ? pq_get(aTHX_ row, "task") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *name = row ? pq_get(aTHX_ row, "task") : NULL; |
66 |
0 |
66 |
|
| 264 |
|
| SV *body, *args; |
| 265 |
|
| IV id, retries; |
| 266 |
66 |
| SV *result = NULL; |
| 267 |
66 |
| int died = 0; |
| 268 |
66 |
| double timeout = 0.0; |
| 269 |
66 |
| const hm_abi *A = pq_hm(aTHX); |
| 270 |
66 |
▶ | void *loop = A ? A->cur_loop(aTHX) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| void *loop = A ? A->cur_loop(aTHX) : NULL; |
51 |
15 |
66 |
|
| 271 |
66 |
| int used_alarm = 0; |
| 272 |
66 |
| SV *old_alrm = NULL; |
| 273 |
|
| |
| 274 |
|
| { |
| 275 |
66 |
▶ | SV *t = row ? pq_get(aTHX_ row, "timeout") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *t = row ? pq_get(aTHX_ row, "timeout") : NULL; |
66 |
0 |
66 |
|
| 276 |
66 |
▶ | if (t && SvOK(t)) timeout = SvNV(t); |
Branch: 2 branches
| Branch | True | False | Total |
| if (t && SvOK(t)) timeout = SvNV(t); |
66 |
0 |
66 |
| if (t && SvOK(t)) timeout = SvNV(t); |
4 |
62 |
66 |
|
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
66 |
▶ | if (!row) croak("Punk::Queue: not a job object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!row) croak("Punk::Queue: not a job object"); |
0 |
66 |
66 |
|
| 280 |
66 |
▶ | id = (pq_get(aTHX_ row, "id") ? SvIV(pq_get(aTHX_ row, "id")) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| id = (pq_get(aTHX_ row, "id") ? SvIV(pq_get(aTHX_ row, "id")) : 0); |
66 |
0 |
66 |
|
| 281 |
66 |
▶ | retries = (pq_get(aTHX_ row, "retries") ? SvIV(pq_get(aTHX_ row, "retries")) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| retries = (pq_get(aTHX_ row, "retries") ? SvIV(pq_get(aTHX_ row, "retries")) : 0); |
66 |
0 |
66 |
|
| 282 |
|
| |
| 283 |
66 |
| body = pq_task_body(aTHX_ self, name); |
| 284 |
66 |
▶ | if (!(body && SvROK(body) && SvTYPE(SvRV(body)) == SVt_PVCV)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(body && SvROK(body) && SvTYPE(SvRV(body)) == SVt_PVCV)) { |
62 |
4 |
66 |
| if (!(body && SvROK(body) && SvTYPE(SvRV(body)) == SVt_PVCV)) { |
62 |
0 |
62 |
| if (!(body && SvROK(body) && SvTYPE(SvRV(body)) == SVt_PVCV)) { |
0 |
62 |
62 |
|
| 285 |
|
| /* A queue holding jobs for a task this process does not know is |
| 286 |
|
| * normal in a mixed deployment - a rolling deploy, or a worker |
| 287 |
|
| * started with a narrower --task list. Fail the job with a clear |
| 288 |
|
| * message; do not take the process down over it. */ |
| 289 |
8 |
▶ | SV *msg = sv_2mortal(Perl_newSVpvf(aTHX_ |
Branch: 2 branches
| Branch | True | False | Total |
| SV *msg = sv_2mortal(Perl_newSVpvf(aTHX_ |
4 |
0 |
4 |
| SV *msg = sv_2mortal(Perl_newSVpvf(aTHX_ |
4 |
0 |
4 |
|
| 290 |
|
| "Punk::Queue: no task registered for '%s'", |
| 291 |
|
| (name && SvOK(name)) ? SvPV_nolen(name) : "(undef)")); |
| 292 |
4 |
| (void)pq_fail_job(aTHX_ backend, id, retries, msg); |
| 293 |
4 |
| return 0; |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
62 |
| args = pq_get(aTHX_ row, "args"); |
| 297 |
|
| |
| 298 |
62 |
▶ | if (timeout > 0 && !loop) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (timeout > 0 && !loop) { |
4 |
58 |
62 |
| if (timeout > 0 && !loop) { |
2 |
2 |
4 |
|
| 299 |
2 |
| HV *sig = get_hv("SIG", GV_ADD); |
| 300 |
2 |
| SV **e = hv_fetchs(sig, "ALRM", 0); |
| 301 |
2 |
▶ | old_alrm = (e && *e && SvOK(*e)) ? newSVsv(*e) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| old_alrm = (e && *e && SvOK(*e)) ? newSVsv(*e) : NULL; |
2 |
0 |
2 |
| old_alrm = (e && *e && SvOK(*e)) ? newSVsv(*e) : NULL; |
2 |
0 |
2 |
| old_alrm = (e && *e && SvOK(*e)) ? newSVsv(*e) : NULL; |
1 |
1 |
2 |
|
| 302 |
|
| /* %SIG is magical and hv_store does not run set-magic: without the |
| 303 |
|
| * explicit SvSETMAGIC, Perl never installs the OS handler and the |
| 304 |
|
| * alarm kills the process with default disposition instead of |
| 305 |
|
| * raising our die. */ |
| 306 |
2 |
| e = hv_stores(sig, "ALRM", pq_alrm_handler(aTHX)); |
| 307 |
2 |
▶ | if (e) SvSETMAGIC(*e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e) SvSETMAGIC(*e); |
2 |
0 |
2 |
| if (e) SvSETMAGIC(*e); |
2 |
0 |
2 |
|
| 308 |
2 |
| (void)alarm((unsigned)(timeout + 0.999)); |
| 309 |
2 |
| used_alarm = 1; |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| { |
| 313 |
62 |
| dSP; |
| 314 |
62 |
| SSize_t n = 0, i; |
| 315 |
62 |
▶ | AV *av = (args && SvROK(args) && SvTYPE(SvRV(args)) == SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| AV *av = (args && SvROK(args) && SvTYPE(SvRV(args)) == SVt_PVAV) |
62 |
0 |
62 |
| AV *av = (args && SvROK(args) && SvTYPE(SvRV(args)) == SVt_PVAV) |
62 |
0 |
62 |
|
| 316 |
124 |
▶ | ? (AV *)SvRV(args) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(args) : NULL; |
62 |
0 |
62 |
|
| 317 |
|
| int count; |
| 318 |
62 |
▶ | if (av) n = av_len(av) + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av) n = av_len(av) + 1; |
62 |
0 |
62 |
|
| 319 |
|
| |
| 320 |
62 |
| ENTER; SAVETMPS; |
| 321 |
62 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
62 |
62 |
|
| 322 |
62 |
▶ | EXTEND(SP, n + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n + 1); |
62 |
0 |
62 |
| EXTEND(SP, n + 1); |
0 |
62 |
62 |
|
| 323 |
62 |
| PUSHs(job); |
| 324 |
83 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
21 |
62 |
83 |
|
| 325 |
21 |
| SV **e = av_fetch(av, i, 0); |
| 326 |
21 |
▶ | PUSHs((e && *e) ? *e : &PL_sv_undef); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs((e && *e) ? *e : &PL_sv_undef); |
21 |
0 |
21 |
| PUSHs((e && *e) ? *e : &PL_sv_undef); |
21 |
0 |
21 |
|
| 327 |
|
| } |
| 328 |
62 |
| PUTBACK; |
| 329 |
62 |
| count = call_sv(body, G_SCALAR | G_EVAL); |
| 330 |
55 |
| SPAGAIN; |
| 331 |
|
| /* +1 before FREETMPS, or the value is freed underneath us. */ |
| 332 |
55 |
▶ | if (count > 0) result = SvREFCNT_inc(POPs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) result = SvREFCNT_inc(POPs); |
55 |
0 |
55 |
|
| 333 |
55 |
▶ | died = SvTRUE(ERRSV) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV) ? 1 : 0; |
55 |
0 |
55 |
|
| 334 |
55 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
20 |
35 |
55 |
|
| 335 |
20 |
▶ | if (result) { SvREFCNT_dec(result); result = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (result) { SvREFCNT_dec(result); result = NULL; } |
20 |
0 |
20 |
|
| 336 |
20 |
▶ | result = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| result = newSVsv(ERRSV); |
20 |
0 |
20 |
|
| 337 |
|
| } |
| 338 |
55 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
55 |
0 |
55 |
|
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| /* A returned future - anything blessed with a get method, the same |
| 342 |
|
| * duck-typing punk_future.h's dispatcher uses - is awaited and its |
| 343 |
|
| * value becomes the result. Inside a worker's loop tick, ->get on a |
| 344 |
|
| * loop-mode future re-enters run_until, so the worker's own timers |
| 345 |
|
| * keep firing while the job awaits; off-loop it blocks, which is the |
| 346 |
|
| * documented degraded behaviour. A get that dies is the job failing. */ |
| 347 |
55 |
▶ | if (!died && result && SvROK(result) && SvOBJECT(SvRV(result))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!died && result && SvROK(result) && SvOBJECT(SvRV(result))) { |
35 |
20 |
55 |
| if (!died && result && SvROK(result) && SvOBJECT(SvRV(result))) { |
35 |
0 |
35 |
| if (!died && result && SvROK(result) && SvOBJECT(SvRV(result))) { |
3 |
32 |
35 |
| if (!died && result && SvROK(result) && SvOBJECT(SvRV(result))) { |
2 |
1 |
3 |
|
| 348 |
2 |
| HV *stash = SvSTASH(SvRV(result)); |
| 349 |
2 |
▶ | if (stash && gv_fetchmethod_autoload(stash, "get", 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (stash && gv_fetchmethod_autoload(stash, "get", 0)) { |
2 |
0 |
2 |
| if (stash && gv_fetchmethod_autoload(stash, "get", 0)) { |
2 |
0 |
2 |
|
| 350 |
2 |
| int gdied = 0; |
| 351 |
|
| SV *got; |
| 352 |
|
| pq_perform_to ctx; |
| 353 |
2 |
| hm_abi_timer *t = NULL; |
| 354 |
|
| |
| 355 |
2 |
| ctx.future = result; |
| 356 |
2 |
| ctx.fired = 0; |
| 357 |
2 |
▶ | if (timeout > 0 && loop) |
Branch: 2 branches
| Branch | True | False | Total |
| if (timeout > 0 && loop) |
1 |
1 |
2 |
| if (timeout > 0 && loop) |
1 |
0 |
1 |
|
| 358 |
1 |
| t = A->timer(aTHX_ loop, timeout, pq_perform_to_cb, &ctx); |
| 359 |
|
| |
| 360 |
2 |
| got = pq_call_meth_ev(aTHX_ result, "get", NULL, 0, 1, &gdied); |
| 361 |
|
| |
| 362 |
|
| /* never cancel after the fire: the handle is freed then */ |
| 363 |
2 |
▶ | if (t && !ctx.fired) A->timer_cancel(aTHX_ loop, t); |
Branch: 2 branches
| Branch | True | False | Total |
| if (t && !ctx.fired) A->timer_cancel(aTHX_ loop, t); |
1 |
1 |
2 |
| if (t && !ctx.fired) A->timer_cancel(aTHX_ loop, t); |
0 |
1 |
1 |
|
| 364 |
|
| |
| 365 |
2 |
| SvREFCNT_dec(result); |
| 366 |
2 |
▶ | if (gdied) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gdied) { |
1 |
1 |
2 |
|
| 367 |
1 |
▶ | if (got) SvREFCNT_dec(got); |
Branch: 1 branch
| Branch | True | False | Total |
| if (got) SvREFCNT_dec(got); |
0 |
1 |
1 |
|
| 368 |
1 |
▶ | result = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| result = newSVsv(ERRSV); |
1 |
0 |
1 |
|
| 369 |
1 |
| died = 1; |
| 370 |
|
| } |
| 371 |
|
| else { |
| 372 |
1 |
▶ | result = got ? got : newSV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| result = got ? got : newSV(0); |
0 |
1 |
1 |
|
| 373 |
|
| } |
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
55 |
▶ | if (used_alarm) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (used_alarm) { |
2 |
53 |
55 |
|
| 378 |
2 |
| HV *sig = get_hv("SIG", GV_ADD); |
| 379 |
|
| SV **e; |
| 380 |
2 |
| (void)alarm(0); |
| 381 |
2 |
▶ | e = hv_stores(sig, "ALRM", old_alrm ? old_alrm : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| e = hv_stores(sig, "ALRM", old_alrm ? old_alrm : newSV(0)); |
1 |
1 |
2 |
|
| 382 |
2 |
▶ | if (e) SvSETMAGIC(*e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e) SvSETMAGIC(*e); |
2 |
0 |
2 |
| if (e) SvSETMAGIC(*e); |
2 |
0 |
2 |
|
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
55 |
▶ | if (died) (void)pq_fail_job(aTHX_ backend, id, retries, result); |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) (void)pq_fail_job(aTHX_ backend, id, retries, result); |
21 |
34 |
55 |
|
| 386 |
34 |
| else (void)pq_finish_job(aTHX_ backend, id, retries, result); |
| 387 |
|
| |
| 388 |
55 |
▶ | if (result) SvREFCNT_dec(result); |
Branch: 1 branch
| Branch | True | False | Total |
| if (result) SvREFCNT_dec(result); |
55 |
0 |
55 |
|
| 389 |
55 |
| return died ? 0 : 1; |
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
|
| #endif /* PQ_QUEUE_H */ |