| 1 |
|
| #ifndef PQ_CLAIM_H |
| 2 |
|
| #define PQ_CLAIM_H |
| 3 |
|
| |
| 4 |
|
| /* pq_claim.h - the parts of a claim that are the same on every backend. |
| 5 |
|
| * |
| 6 |
|
| * The two backends claim a job very differently - PostgreSQL does it in one |
| 7 |
|
| * statement with FOR UPDATE SKIP LOCKED and RETURNING, SQLite does |
| 8 |
|
| * select-then-guarded-update inside BEGIN IMMEDIATE - but the *predicate* |
| 9 |
|
| * that decides which job is next, the order the binds go in, and what a |
| 10 |
|
| * claimed row turns into are identical, and must stay identical. A queue |
| 11 |
|
| * whose claim order differs between backends is a queue whose behaviour |
| 12 |
|
| * depends on its deployment. |
| 13 |
|
| * |
| 14 |
|
| * So the divergent files own the transaction shape and the statement |
| 15 |
|
| * skeleton, and this file owns everything inside them. If a difference in |
| 16 |
|
| * claim semantics ever appears between pq_sqlite.h and pq_pg.h, it is a bug |
| 17 |
|
| * in one of them, not a fact about the database. |
| 18 |
|
| * |
| 19 |
|
| * Include after pq_job.h and pq_sql.h, before the per-backend headers. */ |
| 20 |
|
| |
| 21 |
|
| /* The shared claim predicate, appended to a statement that has already |
| 22 |
|
| * opened its WHERE with `state = 'inactive' AND `. |
| 23 |
|
| * |
| 24 |
|
| * Order matters for prepare_cached: the text must be byte-identical for a |
| 25 |
|
| * given (nq, nt), which it is, because nothing here depends on the values. */ |
| 26 |
237 |
| static void pq_claim_where(pTHX_ SV *sql, SSize_t nq, SSize_t nt) { |
| 27 |
237 |
| pq_sql_in(aTHX_ sql, "queue", nq); |
| 28 |
237 |
▶ | if (nt > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nt > 0) { |
17 |
220 |
237 |
|
| 29 |
17 |
| pq_sql_cat(aTHX_ sql, " AND "); |
| 30 |
17 |
| pq_sql_in(aTHX_ sql, "task", nt); |
| 31 |
|
| } |
| 32 |
237 |
| pq_sql_cat(aTHX_ sql, |
| 33 |
|
| " AND delayed <= ? AND parents_left = 0" |
| 34 |
|
| " AND (expires IS NULL OR expires > ?)"); |
| 35 |
237 |
| } |
| 36 |
|
| |
| 37 |
|
| /* The ordering. Its own function because "highest priority, then oldest" |
| 38 |
|
| * is a promise to the user, not an implementation detail, and it appears in |
| 39 |
|
| * two statements. `id` is the tiebreaker: it gives FIFO within a priority |
| 40 |
|
| * for free, with no second timestamp column to read. */ |
| 41 |
237 |
| static void pq_claim_order(pTHX_ SV *sql) { |
| 42 |
237 |
| pq_sql_cat(aTHX_ sql, " ORDER BY priority DESC, id LIMIT 1"); |
| 43 |
237 |
| } |
| 44 |
|
| |
| 45 |
|
| /* Binds for pq_claim_where, in its placeholder order. */ |
| 46 |
237 |
| static void pq_claim_binds(pTHX_ AV *bind, AV *queues, AV *tasks, double now) { |
| 47 |
237 |
▶ | SSize_t nq = queues ? av_len(queues) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t nq = queues ? av_len(queues) + 1 : 0; |
237 |
0 |
237 |
|
| 48 |
237 |
▶ | SSize_t nt = tasks ? av_len(tasks) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t nt = tasks ? av_len(tasks) + 1 : 0; |
237 |
0 |
237 |
|
| 49 |
|
| SSize_t i; |
| 50 |
474 |
▶ | for (i = 0; i < nq; i++) pq_bind_sv(aTHX_ bind, *av_fetch(queues, i, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nq; i++) pq_bind_sv(aTHX_ bind, *av_fetch(queues, i, 0)); |
237 |
237 |
474 |
|
| 51 |
255 |
▶ | for (i = 0; i < nt; i++) pq_bind_sv(aTHX_ bind, *av_fetch(tasks, i, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nt; i++) pq_bind_sv(aTHX_ bind, *av_fetch(tasks, i, 0)); |
18 |
237 |
255 |
|
| 52 |
237 |
| pq_bind_nv(aTHX_ bind, now); /* delayed <= ? */ |
| 53 |
237 |
| pq_bind_nv(aTHX_ bind, now); /* expires > ? */ |
| 54 |
237 |
| } |
| 55 |
|
| |
| 56 |
|
| /* A claimed row -> the job hashref the caller gets (+1). |
| 57 |
|
| * |
| 58 |
|
| * The in-memory row is patched to reflect the claim rather than re-read. |
| 59 |
|
| * PostgreSQL's RETURNING gives the post-update row already, SQLite's SELECT |
| 60 |
|
| * gives the pre-update one, and normalising here is what stops that |
| 61 |
|
| * difference reaching the caller. */ |
| 62 |
200 |
| static SV *pq_claim_row(pTHX_ AV *row, IV worker_id, double now) { |
| 63 |
200 |
| HV *h = pq_job_row_hv(aTHX_ row); |
| 64 |
200 |
| (void)hv_stores(h, "state", newSVpvs("active")); |
| 65 |
200 |
| (void)hv_stores(h, "started", newSVnv(now)); |
| 66 |
200 |
▶ | (void)hv_stores(h, "worker", worker_id ? newSViv(worker_id) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "worker", worker_id ? newSViv(worker_id) : newSV(0)); |
125 |
75 |
200 |
|
| 67 |
200 |
| return newRV_noinc((SV *)h); |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| /* Both backends need at least one queue to look in, and "none" is a caller |
| 71 |
|
| * bug rather than an empty result - a worker watching no queues would spin |
| 72 |
|
| * forever finding nothing. */ |
| 73 |
237 |
| static void pq_claim_check(pTHX_ AV *queues) { |
| 74 |
237 |
▶ | if (!queues || av_len(queues) < 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!queues || av_len(queues) < 0) |
237 |
0 |
237 |
| if (!queues || av_len(queues) < 0) |
0 |
237 |
237 |
|
| 75 |
0 |
| croak("Punk::Queue: dequeue needs at least one queue"); |
| 76 |
237 |
| } |
| 77 |
|
| |
| 78 |
|
| #endif /* PQ_CLAIM_H */ |