| 1 |
|
| #ifndef PQ_JOB_H |
| 2 |
|
| #define PQ_JOB_H |
| 3 |
|
| |
| 4 |
|
| /* pq_job.h - the job row: one column list, one positional decode. |
| 5 |
|
| * |
| 6 |
|
| * Every read path (enqueue RETURNING, the dequeue claim, job_info) selects |
| 7 |
|
| * the same columns in the same order and decodes them through the same |
| 8 |
|
| * function. That is deliberate: a positional fetchrow_arrayref is roughly |
| 9 |
|
| * four times cheaper to build than fetchrow_hashref, but positional decoding |
| 10 |
|
| * with three separate column lists is a bug waiting to happen. One list, |
| 11 |
|
| * one decoder, one place to change. |
| 12 |
|
| * |
| 13 |
|
| * Include after pq_dbi.h and pq_frj.h. */ |
| 14 |
|
| |
| 15 |
|
| /* The column list grows at the tail only, in step with the migrations - |
| 16 |
|
| * positional decode means an insertion in the middle silently shears every |
| 17 |
|
| * later field. */ |
| 18 |
|
| #define PQ_JOB_COLS \ |
| 19 |
|
| "id, task, args, queue, state, priority, attempts, retries, " \ |
| 20 |
|
| "parents_left, lax, created, delayed, started, finished, retried, " \ |
| 21 |
|
| "expires, result, notes, worker, lock_key, cron_id, timeout" |
| 22 |
|
| |
| 23 |
|
| enum { |
| 24 |
|
| PQ_C_ID = 0, PQ_C_TASK, PQ_C_ARGS, PQ_C_QUEUE, PQ_C_STATE, |
| 25 |
|
| PQ_C_PRIORITY, PQ_C_ATTEMPTS, PQ_C_RETRIES, PQ_C_PARENTS_LEFT, |
| 26 |
|
| PQ_C_LAX, PQ_C_CREATED, PQ_C_DELAYED, PQ_C_STARTED, PQ_C_FINISHED, |
| 27 |
|
| PQ_C_RETRIED, PQ_C_EXPIRES, PQ_C_RESULT, PQ_C_NOTES, PQ_C_WORKER, |
| 28 |
|
| PQ_C_LOCK_KEY, PQ_C_CRON_ID, PQ_C_TIMEOUT, PQ_C_MAX |
| 29 |
|
| }; |
| 30 |
|
| |
| 31 |
|
| /* Store a column as-is, or leave the key absent when the column is NULL. |
| 32 |
|
| * Absent rather than undef: `exists $job->{started}` then means "has been |
| 33 |
|
| * claimed", which is a more useful thing to be able to ask than comparing |
| 34 |
|
| * against undef. */ |
| 35 |
2269 |
| static void pq_hv_set_sv(pTHX_ HV *h, const char *k, SV *v) { |
| 36 |
2269 |
| (void)hv_store(h, k, (I32)strlen(k), newSVsv(v), 0); |
| 37 |
2269 |
| } |
| 38 |
|
| |
| 39 |
3586 |
| static void pq_hv_set_iv(pTHX_ HV *h, const char *k, IV v) { |
| 40 |
3586 |
| (void)hv_store(h, k, (I32)strlen(k), newSViv(v), 0); |
| 41 |
3586 |
| } |
| 42 |
|
| |
| 43 |
3830 |
| static void pq_hv_set_nv_or_undef(pTHX_ HV *h, const char *k, SV *v) { |
| 44 |
3830 |
▶ | (void)hv_store(h, k, (I32)strlen(k), |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store(h, k, (I32)strlen(k), |
3830 |
0 |
3830 |
| (void)hv_store(h, k, (I32)strlen(k), |
1640 |
2190 |
3830 |
|
| 45 |
|
| (v && SvOK(v)) ? newSVnv(SvNV(v)) : newSV(0), 0); |
| 46 |
3830 |
| } |
| 47 |
|
| |
| 48 |
|
| /* A fetched row -> a plain hashref, JSON columns decoded. +1, caller owns. |
| 49 |
|
| * |
| 50 |
|
| * Rows are plain hashrefs rather than objects for the same reason |
| 51 |
|
| * Punk::Model's are: they are fast to build, directly JSON-encodable for |
| 52 |
|
| * the REST API in phase 8, and nobody has to learn an accessor API to |
| 53 |
|
| * inspect one at a prompt. */ |
| 54 |
479 |
| static HV *pq_job_row_hv(pTHX_ AV *row) { |
| 55 |
479 |
| HV *h = newHV(); |
| 56 |
479 |
▶ | if (!row) return h; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!row) return h; |
0 |
479 |
479 |
|
| 57 |
|
| |
| 58 |
479 |
| pq_hv_set_iv(aTHX_ h, "id", pq_col_iv(aTHX_ row, PQ_C_ID)); |
| 59 |
479 |
| pq_hv_set_sv(aTHX_ h, "task", pq_col(aTHX_ row, PQ_C_TASK)); |
| 60 |
479 |
| pq_hv_set_sv(aTHX_ h, "queue", pq_col(aTHX_ row, PQ_C_QUEUE)); |
| 61 |
479 |
| pq_hv_set_sv(aTHX_ h, "state", pq_col(aTHX_ row, PQ_C_STATE)); |
| 62 |
479 |
| pq_hv_set_iv(aTHX_ h, "priority", pq_col_iv(aTHX_ row, PQ_C_PRIORITY)); |
| 63 |
479 |
| pq_hv_set_iv(aTHX_ h, "attempts", pq_col_iv(aTHX_ row, PQ_C_ATTEMPTS)); |
| 64 |
479 |
| pq_hv_set_iv(aTHX_ h, "retries", pq_col_iv(aTHX_ row, PQ_C_RETRIES)); |
| 65 |
479 |
| pq_hv_set_iv(aTHX_ h, "parents_left", pq_col_iv(aTHX_ row, PQ_C_PARENTS_LEFT)); |
| 66 |
479 |
| pq_hv_set_iv(aTHX_ h, "lax", pq_col_iv(aTHX_ row, PQ_C_LAX)); |
| 67 |
|
| |
| 68 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "created", pq_col(aTHX_ row, PQ_C_CREATED)); |
| 69 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "delayed", pq_col(aTHX_ row, PQ_C_DELAYED)); |
| 70 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "started", pq_col(aTHX_ row, PQ_C_STARTED)); |
| 71 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "finished", pq_col(aTHX_ row, PQ_C_FINISHED)); |
| 72 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "retried", pq_col(aTHX_ row, PQ_C_RETRIED)); |
| 73 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "expires", pq_col(aTHX_ row, PQ_C_EXPIRES)); |
| 74 |
|
| |
| 75 |
|
| /* the three JSON columns, decoded in C through the frj ABI */ |
| 76 |
479 |
| (void)hv_stores(h, "args", pq_json_decode(aTHX_ pq_col(aTHX_ row, PQ_C_ARGS))); |
| 77 |
479 |
| (void)hv_stores(h, "notes", pq_json_decode(aTHX_ pq_col(aTHX_ row, PQ_C_NOTES))); |
| 78 |
479 |
| (void)hv_stores(h, "result", pq_json_decode(aTHX_ pq_col(aTHX_ row, PQ_C_RESULT))); |
| 79 |
|
| |
| 80 |
|
| { |
| 81 |
479 |
| SV *w = pq_col(aTHX_ row, PQ_C_WORKER); |
| 82 |
479 |
▶ | (void)hv_stores(h, "worker", SvOK(w) ? newSViv(SvIV(w)) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "worker", SvOK(w) ? newSViv(SvIV(w)) : newSV(0)); |
63 |
416 |
479 |
|
| 83 |
|
| } |
| 84 |
479 |
| (void)hv_stores(h, "lock_key", newSVsv(pq_col(aTHX_ row, PQ_C_LOCK_KEY))); |
| 85 |
|
| { |
| 86 |
479 |
| SV *c = pq_col(aTHX_ row, PQ_C_CRON_ID); |
| 87 |
479 |
▶ | (void)hv_stores(h, "cron_id", SvOK(c) ? newSViv(SvIV(c)) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "cron_id", SvOK(c) ? newSViv(SvIV(c)) : newSV(0)); |
0 |
479 |
479 |
|
| 88 |
|
| } |
| 89 |
479 |
| pq_hv_set_nv_or_undef(aTHX_ h, "timeout", pq_col(aTHX_ row, PQ_C_TIMEOUT)); |
| 90 |
479 |
| return h; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| /* ---- Punk::Queue::Job ------------------------------------------------------ |
| 94 |
|
| * |
| 95 |
|
| * The handle a task body receives. A blessed hashref, not an IV-ref to a |
| 96 |
|
| * malloc'd struct: a job object is constructed once per job run, which |
| 97 |
|
| * already costs a database round trip, so the allocation is noise - and the |
| 98 |
|
| * hashref needs no DESTROY, cannot leak, and can be subclassed by a |
| 99 |
|
| * hand-written runner. The IV-ref pattern earns its keep on per-request |
| 100 |
|
| * objects; this is not one. */ |
| 101 |
|
| |
| 102 |
200 |
| static SV *pq_job_new(pTHX_ SV *queue, HV *row) { |
| 103 |
200 |
| HV *h = newHV(); |
| 104 |
|
| SV *rv; |
| 105 |
200 |
| (void)hv_stores(h, "queue", newSVsv(queue)); |
| 106 |
200 |
| (void)hv_stores(h, "row", newRV_inc((SV *)row)); |
| 107 |
200 |
| rv = newRV_noinc((SV *)h); |
| 108 |
200 |
| sv_bless(rv, gv_stashpvs("Punk::Queue::Job", GV_ADD)); |
| 109 |
200 |
| return rv; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* A field off the job's row. Borrowed, or NULL. */ |
| 113 |
181 |
| static SV *pq_job_field(pTHX_ SV *self, const char *k) { |
| 114 |
181 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue::Job"); |
| 115 |
181 |
| HV *row = pq_get_hv(aTHX_ h, "row"); |
| 116 |
181 |
▶ | return row ? pq_get(aTHX_ row, k) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return row ? pq_get(aTHX_ row, k) : NULL; |
181 |
0 |
181 |
|
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| #endif /* PQ_JOB_H */ |