@neralabs/rerail-pack-celery 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,462 @@
1
+ .policy "celery-practice-countdown-past-visibility-timeout" revision 1
2
+ .description "Keep Celery countdowns, etas and retry delays under the broker's redelivery limit (one hour on Redis, 30 minutes on SQS and RabbitMQ by default), or the waiting task runs twice or fails."
3
+ .language python
4
+ .mode steer
5
+ .scope include "**/*.py"
6
+ // Rerail's own rule, not a port. A worker holds an eta or countdown task unacknowledged until it runs. Redis and SQS
7
+ // hand an unacknowledged message to another worker once the visibility timeout passes (Redis: one hour by default,
8
+ // SQS: 30 minutes), so a task delayed longer runs twice, or is redelivered again and again in a loop; RabbitMQ (3.8.17
9
+ // and later) closes the channel of a consumer holding a message past its consumer_timeout (30 minutes by default), and
10
+ // the worker fails with PreconditionFailed. Celery's Redis and SQS caveats and its calling guide (eta and countdown)
11
+ // say so.
12
+ // Reported, at the delay, when its value is provably longer than the threshold:
13
+ // - `countdown=` and `eta=` on calls resolved to a task, never by method name alone: `.apply_async(...)`, `.set(...)`
14
+ // and `.signature(...)` on a task or signature (the library's cel_task_ref, which takes a Celery wrapper package's
15
+ // `@task` imported in a module that imports celery), `.send_task(...)`,
16
+ // `celery.signature(...)`; `.retry(...)` on a bound task's own first parameter (`self.retry`, `task.retry`) or on
17
+ // a task;
18
+ // - `eta=` the current time plus a timedelta: `datetime.now()`, `utcnow()`, `today()`, Django's `timezone.now()` or
19
+ // `timezone.localtime()` (resolved through imports), `+`/`-` timedeltas in any order and chained, through names;
20
+ // - a task's `default_retry_delay=`, when the task retries with that delay: it calls `.retry()` without a
21
+ // countdown (in its body or a function it runs), or sets `autoretry_for` without `retry_backoff`;
22
+ // - a task's `retry_backoff_max=`, when it sets `autoretry_for` and `retry_backoff`, and its retries reach the cap:
23
+ // the backoff before the last retry (`retry_backoff * 2 ** (max_retries - 1)`, max_retries from `retry_kwargs`,
24
+ // else `max_retries=`, else Celery's 3; `None` is unbounded) is past the threshold.
25
+ // Values it reads: int and float literals (a float's whole seconds), `+` and `*` of values, file-local constants,
26
+ // `timedelta(...)` with readable fields and its `.total_seconds()`, `min(a, b)` (its smallest readable argument:
27
+ // `min(delay, 86_400)` can wait a day) and `max(a, b)` (any readable argument); and names, resolved by scope (module,
28
+ // enclosing function or own function), through every assignment the name may still hold (one not replaced by a later
29
+ // unconditional one): each is a path the task can take. `TWO_HOURS = 2 * 60 * 60`, a local `later = timezone.now() +
30
+ // timedelta(days=1)`, `step = 600` then `if urgent: step = 7200`, a decorator factory's `max_retries = 60`.
31
+ // The threshold, by the run's broker (the schemes of every broker URL literal of its broker_url settings and
32
+ // `Celery(broker=...)`, as the literal or an environment default). The schemes are Kombu's transport names, a fixed
33
+ // list (nothing to resolve):
34
+ // - RabbitMQ (amqp, amqps, pyamqp, librabbitmq only): the largest `"x-consumer-timeout": ms` queue argument the run
35
+ // sets, else RabbitMQ's default consumer_timeout, 1800 seconds; reported with a RabbitMQ message;
36
+ // - otherwise the largest literal `visibility_timeout` the run sets outside tests (a `"visibility_timeout": N` pair
37
+ // or keyword anywhere, `x["visibility_timeout"] = N`, `<x>.visibility_timeout = N`), else 1800 seconds for SQS
38
+ // (sqs only) and 3600 for Redis, mixed brokers (past every named default) or a broker it cannot name.
39
+ // ship.apply_async((order_id,), countdown=60 * 60 * 2) reported, at the countdown (Redis, no visibility_timeout)
40
+ // later = timezone.now() + timedelta(days=1)
41
+ // ship.apply_async((order_id,), eta=later) reported
42
+ // ship.apply_async((order_id,), countdown=45 * 60) reported on SQS and RabbitMQ, not on Redis
43
+ // ship.apply_async((order_id,), countdown=3600) not reported on Redis: at the threshold, not past it
44
+ // Left alone: values at or under the threshold, and values it cannot read (`countdown=delay` of a parameter,
45
+ // `settings.X`, another module's constant, a loop variable, a binding replaced before the end of its scope);
46
+ // `.apply_async`, `.set` and `.retry` on receivers that are not Celery tasks (a pool's `apply_async`, a client's
47
+ // `retry`); a run that sets the threshold's value (visibility_timeout, or x-consumer-timeout on RabbitMQ) to a value
48
+ // it cannot read, or a non-RabbitMQ run whose broker_transport_options is not a dict (read from configuration: the
49
+ // team chose a value);
50
+ // test code and test settings files; `default_retry_delay` on a task that never retries with it, and
51
+ // `retry_backoff_max` on a task whose retries never reach it.
52
+ // Gaps: a broker chosen by an environment variable with no default is taken for Redis (one hour), so a delay between
53
+ // 30 and 60 minutes on such a RabbitMQ or SQS run is missed; RabbitMQ's consumer_timeout raised in rabbitmq.conf or a
54
+ // policy, outside the repository, is not seen (the message names it); a countdown that grows over retries with no cap
55
+ // (`countdown=30 * 2 ** self.request.retries`) is left alone; `options={...}` on signatures, Task subclasses' class
56
+ // attributes, `self.apply_async`/`self.retry` in a Task subclass's methods, `min()` or `max()` inside arithmetic, and
57
+ // a constant built from another constant (`DAY = 24 * HOUR`) are not read; the `.retry()` that makes a task use its
58
+ // default_retry_delay is matched by name in the task's own call graph.
59
+
60
+ #include "lib/celery.rerail"
61
+
62
+ .violation PastVisibility(at: node, secs: number, limit: number) "This task waits {secs} s, past the {limit} s visibility timeout: the broker redelivers it to a second worker. Schedule far-off work from the database; a longer timeout delays crashed-task recovery."
63
+ .violation PastConsumerTimeout(at: node, secs: number, limit: number) "This task stays unacknowledged {secs} s, past RabbitMQ's {limit} s consumer_timeout: the channel closes. Schedule far-off work from the database, or raise consumer_timeout in rabbitmq.conf."
64
+ // The run's threshold and broker are repository facts (cv_threshold, cv_rabbit: cross_file, no node); each delay is
65
+ // stated in its own file (cv_delay_at), with what its task needs of other files (lib/celery's needs, cel_is), and
66
+ // compared with the threshold here.
67
+ PastVisibility(at, n, t) :- cv_delay_at(at, n, nq, nk), cel_is(nq, nk), cv_threshold(t), n > t, !cv_rabbit().
68
+ PastConsumerTimeout(at, n, t) :- cv_delay_at(at, n, nq, nk), cel_is(nq, nk), cv_threshold(t), n > t, cv_rabbit().
69
+ // A task's default_retry_delay, when the task retries with it through a function it runs (cv_retries_q, by symbol).
70
+ PastVisibility(at, n, t) :- cv_default_delay(f, at, n, nq, nk), cel_is(nq, nk), pt_def(f, fq), cv_retries_q(fq),
71
+ cv_threshold(t), n > t, !cv_rabbit().
72
+ PastConsumerTimeout(at, n, t) :- cv_default_delay(f, at, n, nq, nk), cel_is(nq, nk), pt_def(f, fq), cv_retries_q(fq),
73
+ cv_threshold(t), n > t, cv_rabbit().
74
+ // A task's retry_backoff_max, when its retries reach it: the longest backoff before a retry is past the threshold.
75
+ PastVisibility(at, n, t) :- cv_backoff_cap(f, at, n, nq, nk), cel_is(nq, nk), cv_backoff_top(f, d), cv_threshold(t),
76
+ n > t, d > t, !cv_rabbit().
77
+ PastConsumerTimeout(at, n, t) :- cv_backoff_cap(f, at, n, nq, nk), cel_is(nq, nk), cv_backoff_top(f, d),
78
+ cv_threshold(t), n > t, d > t, cv_rabbit().
79
+ // cv_delay_at(at, n, nq, nk): the delay at is n seconds: countdown= or eta= on a send or a task's retry; or a task's
80
+ // default_retry_delay when the task retries with it in its own file (autoretry_for without retry_backoff).
81
+ .decl cv_delay_at(at: node, n: number, nq: symbol, nk: symbol)
82
+ cv_delay_at(at, n, nq, nk) :- cv_delay_site(c, at, n, nq, nk), cv_live(c).
83
+ cv_delay_at(at, n, nq, nk) :- cv_default_delay(f, at, n, nq, nk), cel_task_option(f, "autoretry_for", _, nq, nk),
84
+ !cv_backoff_factor(f, _).
85
+ // cv_default_delay(f, at, n, nq, nk): task f's default_retry_delay= at is n seconds.
86
+ .decl cv_default_delay(f: node, at: node, n: number, nq: symbol, nk: symbol)
87
+ cv_default_delay(f, at, n, nq, nk) :- cel_task_option(f, "default_retry_delay", at, nq, nk), cv_delay(at, n),
88
+ cv_live(f).
89
+ // cv_backoff_cap(f, at, n, nq, nk): task f's retry_backoff_max= at is n seconds.
90
+ .decl cv_backoff_cap(f: node, at: node, n: number, nq: symbol, nk: symbol)
91
+ cv_backoff_cap(f, at, n, nq, nk) :- cel_task_option(f, "retry_backoff_max", at, nq, nk), cv_delay(at, n), cv_live(f).
92
+
93
+ // ---- where a delay is given ---------------------------------------------------------------------------------------
94
+ // cv_delay_site(c, v, n, nq, nk): call c delays its task by n seconds, given as v: `countdown=v`, or `eta=v` with v now
95
+ // plus a timedelta.
96
+ .decl cv_delay_site(c: node, v: node, n: number, nq: symbol, nk: symbol)
97
+ cv_delay_site(c, v, n, nq, nk) :- cv_delay_call(c, nq, nk), call_arg(c, -1, "countdown", v), cv_delay(v, n).
98
+ cv_delay_site(c, v, n, nq, nk) :- cv_delay_call(c, nq, nk), call_arg(c, -1, "eta", v), cv_time(v, n).
99
+ // cv_delay_call(c, nq, nk): a call that takes a task's delay, on a receiver resolved to a task (a look-alike's
100
+ // `.apply_async` or `.set` is not one): `.apply_async(...)` on a task or signature, `.send_task(...)` (cel_send),
101
+ // `.set(...)`/`.signature(...)` on a task or signature, `celery.signature(...)`, or a retry of a task.
102
+ .decl cv_delay_call(c: node, nq: symbol, nk: symbol)
103
+ cv_delay_call(c, nq, nk) :- cel_send(c, "send_task", nq, nk).
104
+ cv_delay_call(c, nq, nk) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, m),
105
+ (m = "apply_async" ; m = "set" ; m = "signature"), unparen(o, t), cel_task_ref(t, nq, nk).
106
+ cv_delay_call(c, "", "") :- kind(c, "call"), field(c, "function", f), sym_of(f, q),
107
+ (q = "celery.signature" ; q = "celery.canvas.signature" ; q = "celery.canvas.Signature" ; q = "celery.Signature").
108
+ cv_delay_call(c, nq, nk) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, "retry"), unparen(o, r),
109
+ cv_task_handle(r, nq, nk).
110
+ // cv_task_handle(r, nq, nk): r is a task: a bound task's first parameter, read in that task, or a task (lib/celery's
111
+ // cel_task_ref, which also takes a Celery wrapper package's `@task` in a module that imports celery).
112
+ .decl cv_task_handle(r: node, nq: symbol, nk: symbol)
113
+ cv_task_handle(r, nq, nk) :- kind(r, "identifier"), text(r, x), look(r, x, g), cel_task_bound(g, nq, nk),
114
+ sig_param(g, _, nm, 0, _, _, _), text(nm, x).
115
+ cv_task_handle(r, nq, nk) :- cel_task_ref(r, nq, nk).
116
+
117
+ // ---- names --------------------------------------------------------------------------------------------------------
118
+ // cv_name(e, s, x, w): e reads name x, which resolves to scope s (any scope: module, enclosing function, own
119
+ // function), where `x = w` is a binding that may still hold at the end of s (live_asg: a later unconditional rebinding
120
+ // drops an earlier one). Each such value is one the name can hold on some path, so a value relation reads every one:
121
+ // `step = 600` then `if urgent: step = 7200` can wait 7200 s. One level only (a name's value is read without
122
+ // following further names, except `now`), so `t = t + timedelta(...)` cannot loop.
123
+ .decl cv_name(e: node, s: node, x: symbol, w: node)
124
+ cv_name(e, s, x, w) :- unparen(e, u), kind(u, "identifier"), text(u, x), look(u, x, s), live_asg(s, x, w).
125
+
126
+ // ---- times --------------------------------------------------------------------------------------------------------
127
+ // cv_time(e, n): e is the current time plus n seconds: a now call, a name bound to one, `+`/`-` of a timedelta
128
+ // (either way round for `+`, chained), or a name bound to such an expression (`later = now + timedelta(days=1)`).
129
+ .decl cv_now_fn(q: symbol)
130
+ cv_now_fn("datetime.datetime.now"). cv_now_fn("datetime.datetime.utcnow"). cv_now_fn("datetime.datetime.today").
131
+ cv_now_fn("django.utils.timezone.now").
132
+ .decl cv_now_call(e: node)
133
+ cv_now_call(e) :- unparen(e, u), kind(u, "call"), field(u, "function", f), sym_of(f, q), cv_now_fn(q).
134
+ cv_now_call(e) :- unparen(e, u), kind(u, "call"), field(u, "function", f), sym_of(f, "django.utils.timezone.localtime"),
135
+ !call_arg(u, _, _, _).
136
+ .decl cv_now(e: node)
137
+ cv_now(e) :- cv_now_call(e).
138
+ cv_now(e) :- cv_name(e, _, _, w), cv_now_call(w).
139
+ .decl cv_time_expr(e: node, n: number)
140
+ cv_time_expr(e, 0) :- cv_now(e).
141
+ cv_time_expr(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "+"), field(u, "left", l), field(u, "right", r),
142
+ ((cv_time_expr(l, a), cv_td_value(r, b)) ; (cv_time_expr(r, a), cv_td_value(l, b))), n = a + b, n <= 100000000.
143
+ cv_time_expr(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "-"), field(u, "left", l), field(u, "right", r),
144
+ cv_time_expr(l, a), cv_td_value(r, b), n = a - b, n >= -100000000.
145
+ .decl cv_time(e: node, n: number)
146
+ cv_time(e, n) :- cv_time_expr(e, n).
147
+ cv_time(e, n) :- cv_name(e, _, _, w), cv_time_expr(w, n).
148
+
149
+ // ---- task options -------------------------------------------------------------------------------------------------
150
+ // cv_retries_q(f): task f, by symbol, retries with its default_retry_delay: a `.retry()` call without countdown or eta
151
+ // in a function it runs (f itself, or one python/calls' reach finds from it, over definitions' symbols). The call
152
+ // graph spans files, so this is a repository fact over symbols (cross_file); a file states its own functions'
153
+ // retries (cv_bare_retry).
154
+ .decl cv_retries_q(f: symbol) cross_file
155
+ cv_retries_q(fq) :- cv_bare_retry(fq).
156
+ cv_retries_q(fq) :- reach(fq, gq), cv_bare_retry(gq).
157
+ .decl cv_bare_retry(g: symbol)
158
+ cv_bare_retry(gq) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "retry"), nearest(c, g, scope),
159
+ !call_arg(c, -1, "countdown", _), !call_arg(c, -1, "eta", _), pt_def(g, gq).
160
+ // cv_backoff_factor(f, k): f sets autoretry_for and retry_backoff, whose first delay is k seconds (True: 1).
161
+ .decl cv_backoff_factor(f: node, k: number)
162
+ cv_backoff_factor(f, 1) :- cel_task_option(f, "autoretry_for", _, nq, nk), cel_task_flag(f, "retry_backoff", nq, nk).
163
+ cv_backoff_factor(f, k) :- cel_task_option(f, "autoretry_for", _, nq, nk), cel_task_option(f, "retry_backoff", v, nq, nk),
164
+ cv_num(v, k), k > 0.
165
+ // cv_max_retries(f, m): f retries at most m times; -1 is unbounded (`None`). retry_kwargs' max_retries overrides the
166
+ // option; Celery's default is 3. An unreadable value gives no row.
167
+ .decl cv_max_retries(f: node, m: number)
168
+ cv_max_retries(f, m) :- cv_retry_kwarg(f, v), cv_count(v, m).
169
+ cv_max_retries(f, m) :- cel_task(f, _, _), !cv_retry_kwarg(f, _), cel_task_option(f, "max_retries", v, _, _),
170
+ cv_count(v, m).
171
+ cv_max_retries(f, 3) :- cel_task(f, _, _), !cv_retry_kwarg(f, _), !cv_task_opt(f, "max_retries").
172
+ .decl cv_task_opt(f: node, k: symbol)
173
+ cv_task_opt(f, k) :- cel_task_option(f, k, _, _, _).
174
+ .decl cv_retry_kwarg(f: node, v: node)
175
+ cv_retry_kwarg(f, v) :- cel_task_option(f, "retry_kwargs", d, _, _), unparen(d, m), kind(m, "dictionary"),
176
+ child(m, p, _), kind(p, "pair"), field(p, "key", k), str(k, "max_retries"), field(p, "value", v).
177
+ .decl cv_count(v: node, m: number)
178
+ cv_count(v, -1) :- unparen(v, u), kind(u, "none").
179
+ cv_count(v, m) :- cv_num(v, m).
180
+ // cv_backoff(f, i, d): before retry i (0-based) autoretry waits up to d seconds (retry_backoff * 2 ** i); the walk
181
+ // stops at the last retry, or once d is past every threshold a run can have (100000000 seconds, the bound of the
182
+ // values read). cv_backoff_top(f, d): the longest of them, compared with the run's threshold in the output.
183
+ .decl cv_backoff(f: node, i: number, d: number)
184
+ cv_backoff(f, 0, k) :- cv_backoff_factor(f, k), cv_max_retries(f, m), m != 0.
185
+ cv_backoff(f, i + 1, d * 2) :- cv_backoff(f, i, d), i < 40, d <= 100000000, cv_max_retries(f, m),
186
+ (m = -1 ; i + 1 < m).
187
+ .decl cv_backoff_top(f: node, d: number)
188
+ cv_backoff_top(f, d) :- cv_backoff(f, _, _), d = max x : { cv_backoff(f, _, x) }.
189
+
190
+ // ---- values -------------------------------------------------------------------------------------------------------
191
+ // Values past 100000000 seconds (three years) are not followed, so arithmetic stays bounded.
192
+ // cv_lit(e, n): e is n whole seconds of number literals and file-local constants, with `+` and `*`. An int or float
193
+ // value's text has whole part n (the match, tested where the value is read).
194
+ .decl cv_lit(e: node, n: number)
195
+ cv_lit(e, n) :- unparen(e, u), (kind(u, "integer") ; kind(u, "float")), value(u, v),
196
+ match("(?<w>[0-9]{1,9})(\\.[0-9]*)?", v), n = to_number(w).
197
+ cv_lit(e, n) :- unparen(e, u), kind(u, "identifier"), static_value(u, v), match("(?<w>[0-9]{1,9})(\\.[0-9]*)?", v),
198
+ n = to_number(w).
199
+ cv_lit(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "+"), field(u, "left", l), field(u, "right", r),
200
+ cv_lit(l, a), cv_lit(r, b), n = a + b, n <= 100000000.
201
+ cv_lit(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "*"), field(u, "left", l), field(u, "right", r),
202
+ cv_lit(l, a), cv_lit(r, b), (b = 0 ; a <= 100000000 / b), n = a * b, n <= 100000000.
203
+ // cv_leaf(e, n): cv_lit, or a name (cv_name) with a binding that is one (`TWO_HOURS = 2 * 60 * 60`, a local
204
+ // `delay = 7200`, an enclosing function's `max_retries = 60`).
205
+ .decl cv_leaf(e: node, n: number)
206
+ cv_leaf(e, n) :- cv_lit(e, n).
207
+ cv_leaf(e, n) :- cv_name(e, _, _, w), unparen(e, u), !static_value(u, _), cv_lit(w, n).
208
+ // cv_num(e, n): e is n whole seconds: `+` and `*` of cv_leaf values.
209
+ .decl cv_num(e: node, n: number)
210
+ cv_num(e, n) :- cv_leaf(e, n).
211
+ cv_num(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "+"), field(u, "left", l), field(u, "right", r),
212
+ cv_num(l, a), cv_num(r, b), n = a + b, n <= 100000000.
213
+ cv_num(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "*"), field(u, "left", l), field(u, "right", r),
214
+ cv_num(l, a), cv_num(r, b), (b = 0 ; a <= 100000000 / b), n = a * b, n <= 100000000.
215
+ // cv_td(c, n): call c is `timedelta(...)` of n whole seconds; every argument readable.
216
+ .decl cv_td_call(c: node)
217
+ cv_td_call(c) :- kind(c, "call"), field(c, "function", f), sym_of(f, "datetime.timedelta").
218
+ .decl cv_td_unit(i: number, k: symbol, s: number)
219
+ cv_td_unit(0, "days", 86400). cv_td_unit(1, "seconds", 1). cv_td_unit(2, "microseconds", 0).
220
+ cv_td_unit(3, "milliseconds", 0). cv_td_unit(4, "minutes", 60). cv_td_unit(5, "hours", 3600).
221
+ cv_td_unit(6, "weeks", 604800).
222
+ .decl cv_td_part(c: node, a: node, n: number)
223
+ cv_td_part(c, a, n) :- cv_td_call(c), call_arg(c, i, k, a), (cv_td_unit(i, _, s) ; (i = -1, cv_td_unit(_, k, s))),
224
+ cv_num(a, x), (s = 0 ; x <= 100000000 / s), n = x * s.
225
+ .decl cv_td_open(c: node)
226
+ cv_td_open(c) :- cv_td_call(c), call_arg(c, _, _, a), !cv_td_part(c, a, _).
227
+ cv_td_open(c) :- cv_td_call(c), field(c, "arguments", al), child(al, a, _),
228
+ (kind(a, "list_splat") ; kind(a, "dictionary_splat")).
229
+ .decl cv_td(c: node, n: number)
230
+ cv_td(c, n) :- cv_td_call(c), !cv_td_open(c), n = sum x : { cv_td_part(c, a, x) }.
231
+ // cv_td_value(e, n): e is a timedelta of n seconds: a call, or a name (cv_name) with a binding that is one.
232
+ .decl cv_td_value(e: node, n: number)
233
+ cv_td_value(e, n) :- unparen(e, u), cv_td(u, n).
234
+ cv_td_value(e, n) :- cv_name(e, _, _, w), unparen(w, c), cv_td(c, n).
235
+ // cv_secs(e, n): e is n seconds: a number (cv_num), a timedelta's `.total_seconds()`, or `+` and `*` of those.
236
+ .decl cv_secs(e: node, n: number)
237
+ cv_secs(e, n) :- cv_num(e, n).
238
+ cv_secs(e, n) :- unparen(e, u), kind(u, "call"), field(u, "function", f), attr_of(f, o, "total_seconds"),
239
+ !call_arg(u, _, _, _), cv_td_value(o, n).
240
+ cv_secs(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "+"), field(u, "left", l), field(u, "right", r),
241
+ cv_secs(l, a), cv_secs(r, b), n = a + b, n <= 100000000.
242
+ cv_secs(e, n) :- unparen(e, u), kind(u, "binary_operator"), op(u, "*"), field(u, "left", l), field(u, "right", r),
243
+ cv_secs(l, a), cv_secs(r, b), (b = 0 ; a <= 100000000 / b), n = a * b, n <= 100000000.
244
+ // cv_delay(e, n): the delay e can reach n seconds: cv_secs, `min(a, b, ...)` its smallest readable argument (an
245
+ // unreadable one, as `delay` in `min(delay, 86_400)`, is bounded by the others), `max(...)` any readable argument.
246
+ .decl cv_delay(e: node, n: number)
247
+ cv_delay(e, n) :- cv_secs(e, n).
248
+ cv_delay(e, n) :- unparen(e, u), cv_builtin_call(u, "min"), posarg(u, _, a), cv_secs(a, n), !cv_min_below(u, n).
249
+ cv_delay(e, n) :- unparen(e, u), cv_builtin_call(u, "max"), posarg(u, _, a), cv_secs(a, n).
250
+ .decl cv_min_below(u: node, n: number)
251
+ cv_min_below(u, n) :- cv_builtin_call(u, "min"), posarg(u, _, a), cv_secs(a, n), posarg(u, _, b), cv_secs(b, m), m < n.
252
+ // cv_builtin_call(u, x): `x(a, b, ...)` with two or more positional arguments, x the builtin (no binding in the file).
253
+ .decl cv_builtin_call(u: node, x: symbol)
254
+ cv_builtin_call(u, x) :- kind(u, "call"), field(u, "function", f), kind(f, "identifier"), text(f, x),
255
+ (x = "min" ; x = "max"), builtin_ref(f, x), posarg(u, 1, _), !cv_splat_arg(u).
256
+ .decl cv_splat_arg(u: node)
257
+ cv_splat_arg(u) :- kind(u, "call"), field(u, "arguments", al), child(al, a, _), kind(a, "list_splat").
258
+
259
+ // ---- the threshold ------------------------------------------------------------------------------------------------
260
+ // cv_vt_value(v): the run sets a visibility_timeout to v, outside tests.
261
+ .decl cv_vt_value(v: node)
262
+ cv_vt_value(v) :- kind(p, "pair"), field(p, "key", k), str(k, "visibility_timeout"), field(p, "value", v), cv_live(p).
263
+ cv_vt_value(v) :- call_arg(c, -1, "visibility_timeout", v), cv_live(c).
264
+ cv_vt_value(v) :- kind(a, "assignment"), field(a, "left", l), kind(l, "subscript"), field(l, "subscript", s),
265
+ str(s, "visibility_timeout"), field(a, "right", v), cv_live(a).
266
+ cv_vt_value(v) :- kind(a, "assignment"), field(a, "left", l), attr_of(l, _, "visibility_timeout"), field(a, "right", v),
267
+ cv_live(a).
268
+ cv_vt_value(v) :- cel_setting("visibility_timeout", v, s), cv_live(s).
269
+ // cv_vt_open(): the run sets a visibility_timeout the rule cannot read, or passes broker_transport_options it cannot
270
+ // read (a dict from configuration may hold one).
271
+ .decl cv_vt_open()
272
+ cv_vt_open() :- cv_vt_value(v), !cv_secs(v, _).
273
+ cv_vt_open() :- cel_setting("broker_transport_options", v, s), cv_live(s), !cv_dict(v).
274
+ // cv_dict(v): a dict literal without `**` spread, or a name (cv_name) every live binding of which is one.
275
+ .decl cv_dict(v: node)
276
+ cv_dict(v) :- unparen(v, u), kind(u, "dictionary"), !cv_dict_spread(u).
277
+ cv_dict(v) :- cv_name(v, s, x, _), !cv_dict_open(s, x).
278
+ .decl cv_dict_open(s: node, x: symbol)
279
+ cv_dict_open(s, x) :- cv_name(_, s, x, w), unparen(w, d), (!kind(d, "dictionary") ; cv_dict_spread(d)).
280
+ .decl cv_dict_spread(d: node)
281
+ cv_dict_spread(d) :- kind(d, "dictionary"), child(d, s, _), kind(s, "dictionary_splat").
282
+ .decl cv_vt_known(n: number)
283
+ cv_vt_known(n) :- cv_vt_value(v), cv_secs(v, n).
284
+ // cv_threshold(t): on RabbitMQ, the largest `x-consumer-timeout` the run gives a queue (milliseconds), else its
285
+ // default consumer_timeout of 30 minutes; elsewhere the largest visibility_timeout the run sets, else the broker's
286
+ // default: 30 minutes on SQS, one hour on Redis or a broker the rule cannot name. No row when the value it would take
287
+ // is unreadable. A fact of the whole run, read from every file's settings: cross_file, a number alone.
288
+ .decl cv_threshold(t: number) cross_file
289
+ cv_threshold(1800) :- cv_rabbit(), !cv_ct_open(), !cv_ct_known(_).
290
+ cv_threshold(t) :- cv_rabbit(), !cv_ct_open(), cv_ct_known(_), m = max n : { cv_ct_known(n) }, t = m / 1000.
291
+ cv_threshold(t) :- !cv_rabbit(), !cv_vt_open(), cv_vt_known(_), t = max n : { cv_vt_known(n) }.
292
+ cv_threshold(1800) :- cv_sqs(), !cv_vt_open(), !cv_vt_known(_).
293
+ cv_threshold(3600) :- !cv_rabbit(), !cv_sqs(), !cv_vt_open(), !cv_vt_known(_).
294
+ // cv_ct_value(v): the run gives a queue a consumer timeout of v milliseconds (`{"x-consumer-timeout": v}` in its
295
+ // queue arguments), outside tests.
296
+ .decl cv_ct_value(v: node)
297
+ cv_ct_value(v) :- kind(p, "pair"), field(p, "key", k), str(k, "x-consumer-timeout"), field(p, "value", v), cv_live(p).
298
+ .decl cv_ct_open()
299
+ cv_ct_open() :- cv_ct_value(v), !cv_secs(v, _).
300
+ .decl cv_ct_known(n: number)
301
+ cv_ct_known(n) :- cv_ct_value(v), cv_secs(v, n).
302
+ // cv_broker_value(v): the run's broker URL: a broker_url setting, or `Celery(..., broker=v)`.
303
+ .decl cv_broker_value(v: node)
304
+ cv_broker_value(v) :- cel_setting("broker_url", v, s), cv_live(s).
305
+ cv_broker_value(v) :- kind(c, "call"), field(c, "function", f), cel_app_cls_ref(f),
306
+ call_arg(c, -1, "broker", v), cv_live(c).
307
+ // cv_broker_scheme(s): the URL scheme of a broker value's string literal: the value itself (a string or f-string), or
308
+ // the default of an environment read inside it (`os.environ.get("X", "amqp://...")`, `env("X", default=...)`). Other
309
+ // strings in it are not URLs (`url.replace("amqps://", "pyamqp://")`).
310
+ .decl cv_broker_scheme(s: symbol)
311
+ cv_broker_scheme(s) :- cv_broker_value(v), cv_url_literal(v, n), text(n, t),
312
+ match("[a-zA-Z]*['\"]+(?<s>[a-z0-9+]+)://.*", t).
313
+ .decl cv_url_literal(v: node, n: node)
314
+ cv_url_literal(v, n) :- cv_broker_value(v), unparen(v, n), kind(n, "string").
315
+ cv_url_literal(v, n) :- cv_broker_value(v), (unparen(v, c) ; within(c, v)), kind(c, "call"), field(c, "function", f),
316
+ cv_env_fn(f), (posarg(c, i, n), i >= 1 ; call_arg(c, -1, "default", n)), kind(n, "string").
317
+ // cv_env_fn(f): a function that reads configuration with a default: os.environ.get, os.getenv, environ's env(...),
318
+ // env.str(...), decouple's config(...).
319
+ .decl cv_env_fn(f: node)
320
+ cv_env_fn(f) :- (kind(f, "identifier"), text(f, x) ; attr_of(f, _, x)),
321
+ (x = "get" ; x = "getenv" ; x = "env" ; x = "str" ; x = "config").
322
+ .decl cv_rabbit_scheme(s: symbol)
323
+ cv_rabbit_scheme("amqp"). cv_rabbit_scheme("amqps"). cv_rabbit_scheme("pyamqp"). cv_rabbit_scheme("librabbitmq").
324
+ // cv_rabbit(): every broker scheme the run names is RabbitMQ's, and it names one. cv_sqs(): every one is SQS's. Facts
325
+ // of the whole run, read from every file's broker settings: cross_file, with no column.
326
+ .decl cv_rabbit() cross_file
327
+ cv_rabbit() :- cv_broker_scheme(s), cv_rabbit_scheme(s), !cv_other_broker().
328
+ .decl cv_other_broker()
329
+ cv_other_broker() :- cv_broker_scheme(s), !cv_rabbit_scheme(s).
330
+ .decl cv_sqs() cross_file
331
+ cv_sqs() :- cv_broker_scheme("sqs"), !cv_not_sqs().
332
+ .decl cv_not_sqs()
333
+ cv_not_sqs() :- cv_broker_scheme(s), s != "sqs".
334
+
335
+ // ---- files --------------------------------------------------------------------------------------------------------
336
+ // cv_live(n): n is outside test code and test settings files (`test*.py`, `*_test*.py`).
337
+ .decl cv_live(n: node)
338
+ cv_live(n) :- docof(n, d), !cel_test_doc(d), !cv_test_settings(d).
339
+ .decl cv_test_settings(d: number)
340
+ cv_test_settings(d) :- file_name(d, s), (glob("test*.py", s) ; glob("*_test*.py", s)).
341
+
342
+ // The cases, `upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/`: `proj/` is a Redis
343
+ // run with no visibility_timeout (threshold one hour), in which each line ending in `# caught` is reported and no other
344
+ // line; it holds delays read through names and signatures, and look-alike receivers. `vt_set/` sets visibility_timeout
345
+ // to 12 hours; `vt_config/` sets it from the environment and `vt_options/` reads its transport options from
346
+ // configuration (both silent); `rabbit/` uses RabbitMQ (30 minutes, its own message); `rabbit_queue_timeout/` gives its
347
+ // queue a consumer timeout of three hours, `rabbit_queue_timeout_env/` one from configuration (silent); `sqs/` uses SQS
348
+ // (30 minutes) and `sqs_vt/` raises its visibility timeout to four hours; `mixed/` names RabbitMQ and Redis (one hour);
349
+ // `env_broker/` picks RabbitMQ or Redis from the environment, with scheme strings only in a replace() call (one hour).
350
+ .test cases exactly
351
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/__init__.py"
352
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/celery.py"
353
+ .fixture "proj/jobs.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/jobs.py"
354
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/tasks.py"
355
+ .fixture "proj/tests/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/tests/__init__.py"
356
+ .fixture "proj/tests/test_tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/proj/tests/test_tasks.py"
357
+ .expect _ at "proj/tasks.py":20
358
+ .expect _ at "proj/tasks.py":21
359
+ .expect _ at "proj/tasks.py":22
360
+ .expect _ at "proj/tasks.py":23
361
+ .expect _ at "proj/tasks.py":24
362
+ .expect _ at "proj/tasks.py":25
363
+ .expect _ at "proj/tasks.py":26
364
+ .expect _ at "proj/tasks.py":27
365
+ .expect _ at "proj/tasks.py":28
366
+ .expect _ at "proj/tasks.py":30
367
+ .expect _ at "proj/tasks.py":31
368
+ .expect _ at "proj/tasks.py":60
369
+ .expect _ at "proj/tasks.py":68
370
+ .expect _ at "proj/tasks.py":71
371
+ .expect _ at "proj/tasks.py":79
372
+ .expect _ at "proj/tasks.py":87
373
+ .expect PastVisibility at "proj/tasks.py":94
374
+ .expect PastVisibility at "proj/tasks.py":146
375
+ .expect PastVisibility at "proj/tasks.py":152
376
+ .expect PastVisibility at "proj/tasks.py":156
377
+ .expect PastVisibility at "proj/tasks.py":157
378
+ .expect PastVisibility at "proj/tasks.py":160
379
+ .expect PastVisibility at "proj/tasks.py":161
380
+ .expect PastVisibility at "proj/tasks.py":165
381
+ .expect PastVisibility at "proj/tasks.py":186
382
+ .expect PastVisibility at "proj/tasks.py":187
383
+ .expect PastVisibility at "proj/tasks.py":188
384
+ .expect PastVisibility at "proj/tasks.py":208
385
+ .expect PastVisibility at "proj/tasks.py":209
386
+ .end
387
+
388
+ .test vt_set exactly
389
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_set/proj/__init__.py"
390
+ .fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_set/proj/settings.py"
391
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_set/proj/tasks.py"
392
+ .expect _ at "proj/tasks.py":15
393
+ .expect _ at "proj/tasks.py":16
394
+ .end
395
+
396
+ .test vt_config exactly
397
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_config/proj/__init__.py"
398
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_config/proj/celery.py"
399
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_config/proj/tasks.py"
400
+ .end
401
+
402
+ .test vt_options exactly
403
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_options/proj/__init__.py"
404
+ .fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_options/proj/settings.py"
405
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/vt_options/proj/tasks.py"
406
+ .end
407
+
408
+ .test rabbit exactly
409
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit/proj/__init__.py"
410
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit/proj/celery.py"
411
+ .fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit/proj/settings.py"
412
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit/proj/tasks.py"
413
+ .expect PastConsumerTimeout at "proj/tasks.py":9
414
+ .expect PastConsumerTimeout at "proj/tasks.py":14
415
+ .expect PastConsumerTimeout at "proj/tasks.py":15
416
+ .expect PastConsumerTimeout at "proj/tasks.py":16
417
+ .expect PastConsumerTimeout at "proj/tasks.py":21
418
+ .expect PastConsumerTimeout at "proj/tasks.py":33
419
+ .end
420
+
421
+ .test rabbit_queue_timeout exactly
422
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout/proj/__init__.py"
423
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout/proj/celery.py"
424
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout/proj/tasks.py"
425
+ .expect PastConsumerTimeout at "proj/tasks.py":11
426
+ .end
427
+
428
+ .test rabbit_queue_timeout_env exactly
429
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout_env/proj/__init__.py"
430
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout_env/proj/celery.py"
431
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/rabbit_queue_timeout_env/proj/tasks.py"
432
+ .end
433
+
434
+ .test sqs_vt exactly
435
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs_vt/proj/__init__.py"
436
+ .fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs_vt/proj/settings.py"
437
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs_vt/proj/tasks.py"
438
+ .expect PastVisibility at "proj/tasks.py":12
439
+ .end
440
+
441
+ .test mixed exactly
442
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/mixed/proj/__init__.py"
443
+ .fixture "proj/settings.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/mixed/proj/settings.py"
444
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/mixed/proj/tasks.py"
445
+ .expect PastVisibility at "proj/tasks.py":11
446
+ .end
447
+
448
+ .test sqs exactly
449
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs/proj/__init__.py"
450
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs/proj/celery.py"
451
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/sqs/proj/tasks.py"
452
+ .expect PastVisibility at "proj/tasks.py":14
453
+ .expect PastVisibility at "proj/tasks.py":15
454
+ .expect PastVisibility at "proj/tasks.py":16
455
+ .end
456
+
457
+ .test env_broker exactly
458
+ .fixture "proj/__init__.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/env_broker/proj/__init__.py"
459
+ .fixture "proj/celery.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/env_broker/proj/celery.py"
460
+ .fixture "proj/tasks.py" from "../upstream/celery-practice/authored/celery-practice-countdown-past-visibility-timeout/env_broker/proj/tasks.py"
461
+ .expect _ at "proj/tasks.py":10
462
+ .end