lessormq-celery 0.4.10__tar.gz
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.
- lessormq_celery-0.4.10/.gitignore +7 -0
- lessormq_celery-0.4.10/LICENSE +201 -0
- lessormq_celery-0.4.10/PKG-INFO +206 -0
- lessormq_celery-0.4.10/README.md +180 -0
- lessormq_celery-0.4.10/pyproject.toml +50 -0
- lessormq_celery-0.4.10/src/lessormq_celery/__init__.py +25 -0
- lessormq_celery-0.4.10/src/lessormq_celery/checks.py +51 -0
- lessormq_celery-0.4.10/src/lessormq_celery/py.typed +0 -0
- lessormq_celery-0.4.10/src/lessormq_celery/request.py +116 -0
- lessormq_celery-0.4.10/src/lessormq_celery/task.py +120 -0
- lessormq_celery-0.4.10/src/lessormq_celery/transport.py +537 -0
- lessormq_celery-0.4.10/tests/__init__.py +0 -0
- lessormq_celery-0.4.10/tests/e2e_app.py +108 -0
- lessormq_celery-0.4.10/tests/fake.py +75 -0
- lessormq_celery-0.4.10/tests/test_end_to_end.py +261 -0
- lessormq_celery-0.4.10/tests/test_task.py +155 -0
- lessormq_celery-0.4.10/tests/test_transport.py +287 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lessormq-celery
|
|
3
|
+
Version: 0.4.10
|
|
4
|
+
Summary: Celery on LessorMQ, the message queue over HTTP where the server decides and workers only claim, run, ack or fail: a kombu transport and a base task that leave retries to the server
|
|
5
|
+
Project-URL: Homepage, https://lessormq.dev
|
|
6
|
+
Project-URL: Documentation, https://github.com/gsid-nl/lessormq/tree/main/clients/celery
|
|
7
|
+
Project-URL: Source, https://github.com/gsid-nl/lessormq
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: celery,job-queue,kombu,lessormq,message-queue,worker
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Framework :: Celery
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: System :: Distributed Computing
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: celery<6,>=5.5
|
|
20
|
+
Requires-Dist: kombu<6,>=5.5
|
|
21
|
+
Requires-Dist: lessormq>=0.4.10
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# LessorMQ for Celery
|
|
28
|
+
|
|
29
|
+
Celery on [LessorMQ](https://lessormq.dev), the message queue over HTTP where the server
|
|
30
|
+
decides and workers only claim, run, ack or fail. It is a kombu transport (broker URL
|
|
31
|
+
`lessormq://`) and a base task class. Celery publishes and runs tasks as usual, but
|
|
32
|
+
retries, backoff, the number of attempts and the dead-letter set are settings of the
|
|
33
|
+
queue on the server: `retry()` and `autoretry_for` fail the attempt and leave the rest
|
|
34
|
+
to the queue, and nothing is ever republished by the worker.
|
|
35
|
+
|
|
36
|
+
The package is `lessormq-celery`, for Celery 5.5 or later on Python 3.10 or later. It
|
|
37
|
+
builds on `lessormq`, the
|
|
38
|
+
[Python client](https://github.com/gsid-nl/lessormq/tree/main/clients/python).
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
pip install lessormq-celery
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Configure
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import os
|
|
50
|
+
|
|
51
|
+
import lessormq_celery # registers the lessormq:// broker URL
|
|
52
|
+
from celery import Celery
|
|
53
|
+
|
|
54
|
+
app = Celery("shop", task_cls=lessormq_celery.Task)
|
|
55
|
+
app.conf.update(
|
|
56
|
+
broker_url="lessormq://mq.example.com",
|
|
57
|
+
broker_transport_options={"api_key": os.environ["LESSORMQ_KEY"]},
|
|
58
|
+
result_backend="redis://localhost/0", # optional; any backend but rpc://
|
|
59
|
+
worker_prefetch_multiplier=1,
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`lessormq://` speaks HTTPS; `lessormq+http://` plain HTTP, for a server inside the same
|
|
64
|
+
cluster. The key may also be the URL's password, `lessormq://:KEY@mq.example.com`, which
|
|
65
|
+
Celery masks in its logs. Every task must derive from `lessormq_celery.Task`, which
|
|
66
|
+
`task_cls` does for `@app.task` and `@shared_task`; a `base=` class of your own derives
|
|
67
|
+
from it too. The worker refuses to start otherwise, naming the tasks.
|
|
68
|
+
|
|
69
|
+
Transport options (`broker_transport_options`):
|
|
70
|
+
|
|
71
|
+
| Option | Default | |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `api_key` | the URL's password | a key with the `publish` scope (to send tasks) and the `consume` scope (to run a worker) |
|
|
74
|
+
| `wait_seconds` | 5 | how long a claim waits for a message (at most 20) |
|
|
75
|
+
| `lease_seconds` | the queue's | lease per claim, and per extension |
|
|
76
|
+
| `extend_every` | a third of the lease | seconds between extensions of a held lease |
|
|
77
|
+
| `consumer` | host:pid | the worker's name in the server's list of workers |
|
|
78
|
+
| `route` | none | the path this worker runs tasks through (VPN exit, proxy, account), see [routes](https://lessormq.dev/docs/concepts) |
|
|
79
|
+
| `http_retries`, `retry_delay`, `timeout` | 3, 0.1, 10 | the HTTP client's, see the Python client |
|
|
80
|
+
|
|
81
|
+
There is no option for retries, delays or dead-lettering: set those on the queue
|
|
82
|
+
(`PATCH /v1/queues/{queue}`, see [queue settings](https://lessormq.dev/docs/concepts)).
|
|
83
|
+
A Celery queue is the LessorMQ queue of the same name (`celery` by default).
|
|
84
|
+
|
|
85
|
+
## Dispatch
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from lessormq_celery import PermanentFailure
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@app.task(bind=True)
|
|
92
|
+
def send_invoice(self, invoice_id: int) -> None:
|
|
93
|
+
invoice = Invoice.objects.filter(pk=invoice_id).first()
|
|
94
|
+
if invoice is None:
|
|
95
|
+
raise PermanentFailure(f"no invoice {invoice_id}")
|
|
96
|
+
mailer.send(invoice)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
send_invoice.delay(1042)
|
|
100
|
+
send_invoice.apply_async((1042,), countdown=600, queue="mail")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
- Tasks with the JSON serializer (Celery's default) are stored as JSON, so a message
|
|
104
|
+
reads as what it is in the queue and the dead-letter set: `{"payload": [[1042], {},
|
|
105
|
+
{...}], "headers": {"task": "shop.tasks.send_invoice", ...}}`. Other serializers are
|
|
106
|
+
kept as base64.
|
|
107
|
+
- `countdown` and `eta` become the message's `available_at`: the server holds the task
|
|
108
|
+
back, not the worker.
|
|
109
|
+
- The task id is the message's dedup key (with its retry count): a publish Celery
|
|
110
|
+
repeats after an error is the same message, and while a task is waiting or running,
|
|
111
|
+
sending the same task id again returns it.
|
|
112
|
+
- Canvas works (chains, groups, chords). With a result backend that has no native
|
|
113
|
+
chords, `celery.chord_unlock` polls with Celery's own retry, which publishes its next
|
|
114
|
+
run; that is the one place where a retry is a new message.
|
|
115
|
+
|
|
116
|
+
## Run workers
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
celery -A shop worker -Q celery,mail --concurrency 4
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The worker is Celery's (tested with the prefork, threads and solo pools). It claims a
|
|
123
|
+
message when it has room for one (`worker_prefetch_multiplier` per process), and extends
|
|
124
|
+
the lease of every message it holds, running or waiting, from a thread. Keep the
|
|
125
|
+
prefetch multiplier at 1: a held message is leased, and no idle worker can have it.
|
|
126
|
+
|
|
127
|
+
With several queues the worker claims from each in turn, and each claim waits up to
|
|
128
|
+
`wait_seconds` on an empty queue: lower it when one worker serves many queues. On SIGTERM
|
|
129
|
+
(warm shutdown) the worker finishes the tasks it runs, acks or fails them, and gives the
|
|
130
|
+
messages it held but had not started back to the queue without counting an attempt.
|
|
131
|
+
|
|
132
|
+
## How it behaves
|
|
133
|
+
|
|
134
|
+
The queue decides everything that happens after a failure; the worker only reports it.
|
|
135
|
+
|
|
136
|
+
| Celery | LessorMQ |
|
|
137
|
+
|---|---|
|
|
138
|
+
| a task returns | the message is acked |
|
|
139
|
+
| a task raises | the message is failed with the exception's class, message and traceback; the queue's `backoff` and `max_attempts` decide whether and when it runs again, and when it goes to the dead-letter set |
|
|
140
|
+
| `PermanentFailure` raised (or mixed into your exception: `class BadInput(ValueError, PermanentFailure)`), or `Reject(requeue=False)` | failed as permanent; the queue's `on_permanent` decides (by default: the dead-letter set at once) |
|
|
141
|
+
| `self.retry()`, `autoretry_for` | the attempt fails as above. `countdown`, `eta`, `max_retries`, `retry_backoff` and new arguments are ignored (logged once per task): the task runs again with its arguments after the queue's backoff. `retry()` always raises, also with `throw=False` |
|
|
142
|
+
| `max_retries`, `default_retry_delay`, `retry_jitter` | not used: the queue's `max_attempts` and `backoff` |
|
|
143
|
+
| `self.request.retries` | the attempts before this one, from the server; `self.request.lessormq_attempt` and `lessormq_max_attempts` are the server's counts |
|
|
144
|
+
| state `RETRY` / `FAILURE` in the result backend | `RETRY` (and `on_retry`, `task_retry`) for a failed attempt the queue will try again; `FAILURE` (and `on_failure`, `task_failure`) on the last attempt and for a permanent failure. So `AsyncResult.get()` waits for the outcome of the last attempt |
|
|
145
|
+
| `acks_late` | always on: the message is settled after the task ran. The worker refuses a task with it off |
|
|
146
|
+
| `acks_on_failure_or_timeout`, `reject_on_worker_lost` | not used: a failure is a fail, whatever Celery would do with the message |
|
|
147
|
+
| a child process that dies (`WorkerLostError`), `time_limit` | a failed attempt, counted by the server |
|
|
148
|
+
| `retry_on_worker_lost` | refused: the server counts the attempt and decides |
|
|
149
|
+
| `soft_time_limit` | the task's own exception, failed as any other |
|
|
150
|
+
| `Reject(requeue=True)` | the message is released: back to the queue, the attempt not counted |
|
|
151
|
+
| `Ignore`, an expired task (`expires`) | acked |
|
|
152
|
+
| a worker killed without a warm shutdown | its leases run out; the server counts the attempts and hands the messages out again |
|
|
153
|
+
| a failed-task store | not needed: the queue's dead-letter set (status `failed`) keeps them for the queue's retention; inspect and replay them with the admin API |
|
|
154
|
+
| `priority` | ignored: LessorMQ hands out messages roughly in order of availability |
|
|
155
|
+
| remote control (`celery inspect`, `revoke`), events, Flower | not carried: LessorMQ has no broadcast. Workers and their messages are in the server's list of workers and the queue's stats |
|
|
156
|
+
| `celery purge` | not supported; delete messages with the admin API |
|
|
157
|
+
| `result_backend = "rpc://"` | refused: it needs reply queues on the broker. Any other result backend works |
|
|
158
|
+
|
|
159
|
+
- A message Celery rejects without running it (an unknown task, for one, during a
|
|
160
|
+
deploy) is an ordinary failure, so the next attempt can reach a worker that knows the
|
|
161
|
+
task. A message on the queue that this transport did not write is failed as permanent.
|
|
162
|
+
- Delivery is at least once: a task can run twice, when its lease ran out under a slow
|
|
163
|
+
worker or an ack got lost. Make tasks safe to repeat.
|
|
164
|
+
|
|
165
|
+
## Moving from another queue
|
|
166
|
+
|
|
167
|
+
- Point `broker_url` at LessorMQ, create the app with `task_cls=lessormq_celery.Task`,
|
|
168
|
+
and derive your own base task classes from it.
|
|
169
|
+
- Move the retry policy to the queue: `max_attempts` is `max_retries` plus one (Celery's
|
|
170
|
+
default is 3 retries: 4 attempts), `backoff` replaces `countdown`, `retry_backoff` and
|
|
171
|
+
`retry_jitter`. Exceptions that should never be retried become `PermanentFailure`;
|
|
172
|
+
`autoretry_for` no longer narrows what is retried, since every failure is reported and
|
|
173
|
+
the queue decides.
|
|
174
|
+
- `retry()` calls can stay; their arguments other than `exc` stop mattering.
|
|
175
|
+
- Replace `rpc://` with another result backend, and remote control (revoking, rate
|
|
176
|
+
limits at runtime) with the admin API: pause a queue, delete or replay messages.
|
|
177
|
+
- Run old and new side by side: two Celery apps (or two queues on two brokers), and
|
|
178
|
+
move task routes one at a time. Drain the old broker before you switch it off.
|
|
179
|
+
- Workers on different network paths (VPN exits, proxies) set `route`, and the queue's
|
|
180
|
+
`route_policy` keeps a message away from a route it failed on.
|
|
181
|
+
|
|
182
|
+
## Development
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
cd clients/celery
|
|
186
|
+
python3 -m venv .venv
|
|
187
|
+
.venv/bin/pip install -e ../python -e '.[dev]'
|
|
188
|
+
.venv/bin/pytest
|
|
189
|
+
.venv/bin/mypy
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
The unit tests run the transport against a scripted server and the task and request
|
|
193
|
+
classes without a broker. The end-to-end tests start real Celery workers (prefork) on a
|
|
194
|
+
running server, in a tenant of their own that they remove afterwards: a task run and
|
|
195
|
+
acked, a failing task back after the queue's backoff with the server's attempt count
|
|
196
|
+
and then in the dead-letter set, `retry()` with its countdown ignored, a permanent
|
|
197
|
+
failure in the dead-letter set at once, a child process that dies, `countdown`, a warm
|
|
198
|
+
shutdown, and a chord. Without these variables they are skipped:
|
|
199
|
+
|
|
200
|
+
```sh
|
|
201
|
+
LESSORMQ_URL=http://127.0.0.1:8080 LESSORMQ_OPERATOR_KEY=lmq_... .venv/bin/pytest tests/test_end_to_end.py
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The protocol calls are the Python client's, which passes the
|
|
205
|
+
[conformance suite](https://github.com/gsid-nl/lessormq/tree/main/conformance) through
|
|
206
|
+
`clients/python/tests/conformance_adapter.py`.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# LessorMQ for Celery
|
|
2
|
+
|
|
3
|
+
Celery on [LessorMQ](https://lessormq.dev), the message queue over HTTP where the server
|
|
4
|
+
decides and workers only claim, run, ack or fail. It is a kombu transport (broker URL
|
|
5
|
+
`lessormq://`) and a base task class. Celery publishes and runs tasks as usual, but
|
|
6
|
+
retries, backoff, the number of attempts and the dead-letter set are settings of the
|
|
7
|
+
queue on the server: `retry()` and `autoretry_for` fail the attempt and leave the rest
|
|
8
|
+
to the queue, and nothing is ever republished by the worker.
|
|
9
|
+
|
|
10
|
+
The package is `lessormq-celery`, for Celery 5.5 or later on Python 3.10 or later. It
|
|
11
|
+
builds on `lessormq`, the
|
|
12
|
+
[Python client](https://github.com/gsid-nl/lessormq/tree/main/clients/python).
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pip install lessormq-celery
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Configure
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import os
|
|
24
|
+
|
|
25
|
+
import lessormq_celery # registers the lessormq:// broker URL
|
|
26
|
+
from celery import Celery
|
|
27
|
+
|
|
28
|
+
app = Celery("shop", task_cls=lessormq_celery.Task)
|
|
29
|
+
app.conf.update(
|
|
30
|
+
broker_url="lessormq://mq.example.com",
|
|
31
|
+
broker_transport_options={"api_key": os.environ["LESSORMQ_KEY"]},
|
|
32
|
+
result_backend="redis://localhost/0", # optional; any backend but rpc://
|
|
33
|
+
worker_prefetch_multiplier=1,
|
|
34
|
+
)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`lessormq://` speaks HTTPS; `lessormq+http://` plain HTTP, for a server inside the same
|
|
38
|
+
cluster. The key may also be the URL's password, `lessormq://:KEY@mq.example.com`, which
|
|
39
|
+
Celery masks in its logs. Every task must derive from `lessormq_celery.Task`, which
|
|
40
|
+
`task_cls` does for `@app.task` and `@shared_task`; a `base=` class of your own derives
|
|
41
|
+
from it too. The worker refuses to start otherwise, naming the tasks.
|
|
42
|
+
|
|
43
|
+
Transport options (`broker_transport_options`):
|
|
44
|
+
|
|
45
|
+
| Option | Default | |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `api_key` | the URL's password | a key with the `publish` scope (to send tasks) and the `consume` scope (to run a worker) |
|
|
48
|
+
| `wait_seconds` | 5 | how long a claim waits for a message (at most 20) |
|
|
49
|
+
| `lease_seconds` | the queue's | lease per claim, and per extension |
|
|
50
|
+
| `extend_every` | a third of the lease | seconds between extensions of a held lease |
|
|
51
|
+
| `consumer` | host:pid | the worker's name in the server's list of workers |
|
|
52
|
+
| `route` | none | the path this worker runs tasks through (VPN exit, proxy, account), see [routes](https://lessormq.dev/docs/concepts) |
|
|
53
|
+
| `http_retries`, `retry_delay`, `timeout` | 3, 0.1, 10 | the HTTP client's, see the Python client |
|
|
54
|
+
|
|
55
|
+
There is no option for retries, delays or dead-lettering: set those on the queue
|
|
56
|
+
(`PATCH /v1/queues/{queue}`, see [queue settings](https://lessormq.dev/docs/concepts)).
|
|
57
|
+
A Celery queue is the LessorMQ queue of the same name (`celery` by default).
|
|
58
|
+
|
|
59
|
+
## Dispatch
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from lessormq_celery import PermanentFailure
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@app.task(bind=True)
|
|
66
|
+
def send_invoice(self, invoice_id: int) -> None:
|
|
67
|
+
invoice = Invoice.objects.filter(pk=invoice_id).first()
|
|
68
|
+
if invoice is None:
|
|
69
|
+
raise PermanentFailure(f"no invoice {invoice_id}")
|
|
70
|
+
mailer.send(invoice)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
send_invoice.delay(1042)
|
|
74
|
+
send_invoice.apply_async((1042,), countdown=600, queue="mail")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- Tasks with the JSON serializer (Celery's default) are stored as JSON, so a message
|
|
78
|
+
reads as what it is in the queue and the dead-letter set: `{"payload": [[1042], {},
|
|
79
|
+
{...}], "headers": {"task": "shop.tasks.send_invoice", ...}}`. Other serializers are
|
|
80
|
+
kept as base64.
|
|
81
|
+
- `countdown` and `eta` become the message's `available_at`: the server holds the task
|
|
82
|
+
back, not the worker.
|
|
83
|
+
- The task id is the message's dedup key (with its retry count): a publish Celery
|
|
84
|
+
repeats after an error is the same message, and while a task is waiting or running,
|
|
85
|
+
sending the same task id again returns it.
|
|
86
|
+
- Canvas works (chains, groups, chords). With a result backend that has no native
|
|
87
|
+
chords, `celery.chord_unlock` polls with Celery's own retry, which publishes its next
|
|
88
|
+
run; that is the one place where a retry is a new message.
|
|
89
|
+
|
|
90
|
+
## Run workers
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
celery -A shop worker -Q celery,mail --concurrency 4
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The worker is Celery's (tested with the prefork, threads and solo pools). It claims a
|
|
97
|
+
message when it has room for one (`worker_prefetch_multiplier` per process), and extends
|
|
98
|
+
the lease of every message it holds, running or waiting, from a thread. Keep the
|
|
99
|
+
prefetch multiplier at 1: a held message is leased, and no idle worker can have it.
|
|
100
|
+
|
|
101
|
+
With several queues the worker claims from each in turn, and each claim waits up to
|
|
102
|
+
`wait_seconds` on an empty queue: lower it when one worker serves many queues. On SIGTERM
|
|
103
|
+
(warm shutdown) the worker finishes the tasks it runs, acks or fails them, and gives the
|
|
104
|
+
messages it held but had not started back to the queue without counting an attempt.
|
|
105
|
+
|
|
106
|
+
## How it behaves
|
|
107
|
+
|
|
108
|
+
The queue decides everything that happens after a failure; the worker only reports it.
|
|
109
|
+
|
|
110
|
+
| Celery | LessorMQ |
|
|
111
|
+
|---|---|
|
|
112
|
+
| a task returns | the message is acked |
|
|
113
|
+
| a task raises | the message is failed with the exception's class, message and traceback; the queue's `backoff` and `max_attempts` decide whether and when it runs again, and when it goes to the dead-letter set |
|
|
114
|
+
| `PermanentFailure` raised (or mixed into your exception: `class BadInput(ValueError, PermanentFailure)`), or `Reject(requeue=False)` | failed as permanent; the queue's `on_permanent` decides (by default: the dead-letter set at once) |
|
|
115
|
+
| `self.retry()`, `autoretry_for` | the attempt fails as above. `countdown`, `eta`, `max_retries`, `retry_backoff` and new arguments are ignored (logged once per task): the task runs again with its arguments after the queue's backoff. `retry()` always raises, also with `throw=False` |
|
|
116
|
+
| `max_retries`, `default_retry_delay`, `retry_jitter` | not used: the queue's `max_attempts` and `backoff` |
|
|
117
|
+
| `self.request.retries` | the attempts before this one, from the server; `self.request.lessormq_attempt` and `lessormq_max_attempts` are the server's counts |
|
|
118
|
+
| state `RETRY` / `FAILURE` in the result backend | `RETRY` (and `on_retry`, `task_retry`) for a failed attempt the queue will try again; `FAILURE` (and `on_failure`, `task_failure`) on the last attempt and for a permanent failure. So `AsyncResult.get()` waits for the outcome of the last attempt |
|
|
119
|
+
| `acks_late` | always on: the message is settled after the task ran. The worker refuses a task with it off |
|
|
120
|
+
| `acks_on_failure_or_timeout`, `reject_on_worker_lost` | not used: a failure is a fail, whatever Celery would do with the message |
|
|
121
|
+
| a child process that dies (`WorkerLostError`), `time_limit` | a failed attempt, counted by the server |
|
|
122
|
+
| `retry_on_worker_lost` | refused: the server counts the attempt and decides |
|
|
123
|
+
| `soft_time_limit` | the task's own exception, failed as any other |
|
|
124
|
+
| `Reject(requeue=True)` | the message is released: back to the queue, the attempt not counted |
|
|
125
|
+
| `Ignore`, an expired task (`expires`) | acked |
|
|
126
|
+
| a worker killed without a warm shutdown | its leases run out; the server counts the attempts and hands the messages out again |
|
|
127
|
+
| a failed-task store | not needed: the queue's dead-letter set (status `failed`) keeps them for the queue's retention; inspect and replay them with the admin API |
|
|
128
|
+
| `priority` | ignored: LessorMQ hands out messages roughly in order of availability |
|
|
129
|
+
| remote control (`celery inspect`, `revoke`), events, Flower | not carried: LessorMQ has no broadcast. Workers and their messages are in the server's list of workers and the queue's stats |
|
|
130
|
+
| `celery purge` | not supported; delete messages with the admin API |
|
|
131
|
+
| `result_backend = "rpc://"` | refused: it needs reply queues on the broker. Any other result backend works |
|
|
132
|
+
|
|
133
|
+
- A message Celery rejects without running it (an unknown task, for one, during a
|
|
134
|
+
deploy) is an ordinary failure, so the next attempt can reach a worker that knows the
|
|
135
|
+
task. A message on the queue that this transport did not write is failed as permanent.
|
|
136
|
+
- Delivery is at least once: a task can run twice, when its lease ran out under a slow
|
|
137
|
+
worker or an ack got lost. Make tasks safe to repeat.
|
|
138
|
+
|
|
139
|
+
## Moving from another queue
|
|
140
|
+
|
|
141
|
+
- Point `broker_url` at LessorMQ, create the app with `task_cls=lessormq_celery.Task`,
|
|
142
|
+
and derive your own base task classes from it.
|
|
143
|
+
- Move the retry policy to the queue: `max_attempts` is `max_retries` plus one (Celery's
|
|
144
|
+
default is 3 retries: 4 attempts), `backoff` replaces `countdown`, `retry_backoff` and
|
|
145
|
+
`retry_jitter`. Exceptions that should never be retried become `PermanentFailure`;
|
|
146
|
+
`autoretry_for` no longer narrows what is retried, since every failure is reported and
|
|
147
|
+
the queue decides.
|
|
148
|
+
- `retry()` calls can stay; their arguments other than `exc` stop mattering.
|
|
149
|
+
- Replace `rpc://` with another result backend, and remote control (revoking, rate
|
|
150
|
+
limits at runtime) with the admin API: pause a queue, delete or replay messages.
|
|
151
|
+
- Run old and new side by side: two Celery apps (or two queues on two brokers), and
|
|
152
|
+
move task routes one at a time. Drain the old broker before you switch it off.
|
|
153
|
+
- Workers on different network paths (VPN exits, proxies) set `route`, and the queue's
|
|
154
|
+
`route_policy` keeps a message away from a route it failed on.
|
|
155
|
+
|
|
156
|
+
## Development
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
cd clients/celery
|
|
160
|
+
python3 -m venv .venv
|
|
161
|
+
.venv/bin/pip install -e ../python -e '.[dev]'
|
|
162
|
+
.venv/bin/pytest
|
|
163
|
+
.venv/bin/mypy
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The unit tests run the transport against a scripted server and the task and request
|
|
167
|
+
classes without a broker. The end-to-end tests start real Celery workers (prefork) on a
|
|
168
|
+
running server, in a tenant of their own that they remove afterwards: a task run and
|
|
169
|
+
acked, a failing task back after the queue's backoff with the server's attempt count
|
|
170
|
+
and then in the dead-letter set, `retry()` with its countdown ignored, a permanent
|
|
171
|
+
failure in the dead-letter set at once, a child process that dies, `countdown`, a warm
|
|
172
|
+
shutdown, and a chord. Without these variables they are skipped:
|
|
173
|
+
|
|
174
|
+
```sh
|
|
175
|
+
LESSORMQ_URL=http://127.0.0.1:8080 LESSORMQ_OPERATOR_KEY=lmq_... .venv/bin/pytest tests/test_end_to_end.py
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The protocol calls are the Python client's, which passes the
|
|
179
|
+
[conformance suite](https://github.com/gsid-nl/lessormq/tree/main/conformance) through
|
|
180
|
+
`clients/python/tests/conformance_adapter.py`.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "lessormq-celery"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Celery on LessorMQ, the message queue over HTTP where the server decides and workers only claim, run, ack or fail: a kombu transport and a base task that leave retries to the server"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
# The release writes the version of the tag into the lessormq requirement as well.
|
|
13
|
+
dependencies = ["celery>=5.5,<6", "kombu>=5.5,<6", "lessormq>=0.4.10"]
|
|
14
|
+
keywords = ["celery", "kombu", "message-queue", "job-queue", "worker", "lessormq"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Framework :: Celery",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Topic :: System :: Distributed Computing",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
dev = ["pytest>=8", "mypy>=1.10"]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://lessormq.dev"
|
|
30
|
+
Documentation = "https://github.com/gsid-nl/lessormq/tree/main/clients/celery"
|
|
31
|
+
Source = "https://github.com/gsid-nl/lessormq"
|
|
32
|
+
|
|
33
|
+
[tool.hatch.version]
|
|
34
|
+
path = "src/lessormq_celery/__init__.py"
|
|
35
|
+
|
|
36
|
+
[tool.hatch.build.targets.wheel]
|
|
37
|
+
packages = ["src/lessormq_celery"]
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
testpaths = ["tests"]
|
|
41
|
+
pythonpath = ["."]
|
|
42
|
+
|
|
43
|
+
[tool.mypy]
|
|
44
|
+
strict = true
|
|
45
|
+
files = ["src"]
|
|
46
|
+
# Celery has no type hints and kombu only some: allow building on them.
|
|
47
|
+
ignore_missing_imports = true
|
|
48
|
+
disallow_subclassing_any = false
|
|
49
|
+
disallow_untyped_calls = false
|
|
50
|
+
disallow_untyped_decorators = false
|