celery-fastapi 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,410 @@
1
+ Metadata-Version: 2.4
2
+ Name: celery-fastapi
3
+ Version: 0.1.0
4
+ Summary: Automatic REST API generation for Celery tasks with FastAPI
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: celery,fastapi,rest,api,tasks,async,queue
8
+ Author: Your Name
9
+ Author-email: your.email@example.com
10
+ Requires-Python: >=3.11,<4.0
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Web Environment
13
+ Classifier: Framework :: FastAPI
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
23
+ Classifier: Topic :: System :: Distributed Computing
24
+ Classifier: Typing :: Typed
25
+ Provides-Extra: all
26
+ Provides-Extra: cli
27
+ Provides-Extra: eventlet
28
+ Provides-Extra: gevent
29
+ Provides-Extra: gunicorn
30
+ Provides-Extra: multipart
31
+ Provides-Extra: orjson
32
+ Provides-Extra: rabbitmq
33
+ Provides-Extra: redis
34
+ Provides-Extra: server
35
+ Provides-Extra: standard
36
+ Provides-Extra: ujson
37
+ Provides-Extra: uvicorn
38
+ Requires-Dist: celery (>=5.3.0)
39
+ Requires-Dist: eventlet (>=0.33.0) ; extra == "eventlet"
40
+ Requires-Dist: fastapi (>=0.100.0)
41
+ Requires-Dist: gevent (>=23.0.0) ; extra == "gevent"
42
+ Requires-Dist: gunicorn (>=21.0.0) ; extra == "gunicorn" or extra == "all"
43
+ Requires-Dist: httpx (>=0.27.0) ; extra == "all"
44
+ Requires-Dist: kombu (>=5.3.0) ; extra == "rabbitmq" or extra == "all"
45
+ Requires-Dist: orjson (>=3.9.0) ; extra == "orjson" or extra == "all"
46
+ Requires-Dist: pydantic (>=2.0.0)
47
+ Requires-Dist: python-multipart (>=0.0.6) ; extra == "multipart" or extra == "all"
48
+ Requires-Dist: redis (>=5.0.0) ; extra == "redis" or extra == "standard" or extra == "all"
49
+ Requires-Dist: rich (>=13.0.0) ; extra == "cli" or extra == "standard" or extra == "all"
50
+ Requires-Dist: typer (>=0.9.0) ; extra == "cli" or extra == "standard" or extra == "all"
51
+ Requires-Dist: ujson (>=5.8.0) ; extra == "ujson"
52
+ Requires-Dist: uvicorn[standard] (>=0.23.0) ; extra == "uvicorn" or extra == "gunicorn" or extra == "server" or extra == "cli" or extra == "standard" or extra == "all"
53
+ Project-URL: Documentation, https://github.com/karailker/celery-fastapi#readme
54
+ Project-URL: Homepage, https://github.com/karailker/celery-fastapi
55
+ Project-URL: Repository, https://github.com/karailker/celery-fastapi
56
+ Description-Content-Type: text/markdown
57
+
58
+ # Celery FastAPI
59
+
60
+ [![CI](https://github.com/karailker/celery-fastapi/actions/workflows/ci.yml/badge.svg)](https://github.com/karailker/celery-fastapi/actions/workflows/ci.yml)
61
+ [![PyPI version](https://badge.fury.io/py/celery-fastapi.svg)](https://badge.fury.io/py/celery-fastapi)
62
+ [![Python Version](https://img.shields.io/pypi/pyversions/celery-fastapi.svg)](https://pypi.org/project/celery-fastapi/)
63
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
64
+
65
+ Automatic REST API generation for Celery tasks with FastAPI. This package seamlessly bridges Celery and FastAPI, automatically creating REST endpoints for all your registered Celery tasks.
66
+
67
+ ## Features
68
+
69
+ - 🚀 **Automatic endpoint generation** - REST APIs created automatically for all Celery tasks
70
+ - 🔧 **Zero configuration** - Works out of the box with sensible defaults
71
+ - 📊 **Task monitoring** - Built-in endpoints for task status, revocation, and worker info
72
+ - 🎯 **App-scoped operations** - Only manages tasks from your specific Celery app, not the entire cluster
73
+ - 🖥️ **CLI support** - Run as a standalone server from command line
74
+ - 📦 **Modular design** - Use as a library or standalone application
75
+ - 🔄 **Queue-aware routing** - Respects Celery queue assignments
76
+ - 📝 **OpenAPI documentation** - Full Swagger/ReDoc support
77
+ - 🔒 **Production ready** - Full uvicorn/gunicorn support with SSL, workers, and all options
78
+ - ⚡ **Full Celery options** - All task options (countdown, eta, priority, etc.)
79
+ - 🔌 **Pool support** - Compatible with eventlet, gevent, prefork, and solo pools
80
+
81
+ ## Requirements
82
+
83
+ - Python 3.11+
84
+ - FastAPI 0.100.0+
85
+ - Celery 5.3.0+
86
+
87
+ ## Installation
88
+
89
+ ```bash
90
+ # Basic installation
91
+ pip install celery-fastapi
92
+
93
+ # With CLI support
94
+ pip install celery-fastapi[cli]
95
+
96
+ # With uvicorn server
97
+ pip install celery-fastapi[server]
98
+
99
+ # With gunicorn for production
100
+ pip install celery-fastapi[gunicorn]
101
+
102
+ # With Redis broker
103
+ pip install celery-fastapi[redis]
104
+
105
+ # With RabbitMQ broker
106
+ pip install celery-fastapi[rabbitmq]
107
+
108
+ # With eventlet/gevent concurrency
109
+ pip install celery-fastapi[eventlet]
110
+ pip install celery-fastapi[gevent]
111
+
112
+ # All extras (recommended for production)
113
+ pip install celery-fastapi[all]
114
+ ```
115
+
116
+ Or with Poetry:
117
+
118
+ ```bash
119
+ poetry add celery-fastapi
120
+ poetry add celery-fastapi --extras cli # for CLI support
121
+ ```
122
+
123
+ ## Quick Start
124
+
125
+ ### As a Python Module
126
+
127
+ ```python
128
+ from celery import Celery
129
+ from celery_fastapi import CeleryFastAPIBridge, create_app
130
+
131
+ # Your existing Celery app
132
+ celery_app = Celery('tasks', broker='redis://localhost:6379/0')
133
+
134
+ @celery_app.task
135
+ def add(x, y):
136
+ return x + y
137
+
138
+ @celery_app.task
139
+ def multiply(x, y):
140
+ return x * y
141
+
142
+ # Option 1: Using create_app factory
143
+ app = create_app(celery_app)
144
+
145
+ # Option 2: Using the Bridge class for more control
146
+ from fastapi import FastAPI
147
+
148
+ fastapi_app = FastAPI(title="My Task API")
149
+ bridge = CeleryFastAPIBridge(celery_app, fastapi_app)
150
+ bridge.register_routes()
151
+ ```
152
+
153
+ Run with uvicorn:
154
+
155
+ ```bash
156
+ uvicorn myapp:app --reload
157
+ ```
158
+
159
+ ### Using the CLI
160
+
161
+ ```bash
162
+ # Start the server (development)
163
+ celery-fastapi serve myapp.celery:celery_app --port 8000 --reload
164
+
165
+ # Production with multiple workers
166
+ celery-fastapi serve myapp.celery:celery_app -w 4 --host 0.0.0.0
167
+
168
+ # With SSL
169
+ celery-fastapi serve myapp.celery:celery_app --ssl-keyfile key.pem --ssl-certfile cert.pem
170
+
171
+ # Using gunicorn (production)
172
+ celery-fastapi serve-gunicorn myapp.celery:celery_app -w 4 -k uvicorn.workers.UvicornWorker
173
+
174
+ # List available routes
175
+ celery-fastapi routes myapp.celery:celery_app
176
+
177
+ # List registered tasks
178
+ celery-fastapi tasks myapp.celery:celery_app
179
+
180
+ # Show active workers
181
+ celery-fastapi workers myapp.celery:celery_app
182
+ ```
183
+
184
+ ## API Endpoints
185
+
186
+ Once running, your Celery tasks are available as REST endpoints:
187
+
188
+ ### Task Execution
189
+
190
+ ```bash
191
+ # Execute a task with basic args
192
+ POST /{task_name_with_slashes}
193
+ Content-Type: application/json
194
+
195
+ {
196
+ "args": [1, 2],
197
+ "kwargs": {}
198
+ }
199
+
200
+ # Execute with advanced Celery options
201
+ POST /myapp/process_data
202
+ Content-Type: application/json
203
+
204
+ {
205
+ "args": ["data.csv"],
206
+ "kwargs": {"output_format": "json"},
207
+ "countdown": 60,
208
+ "priority": 5,
209
+ "queue": "high_priority",
210
+ "time_limit": 300,
211
+ "soft_time_limit": 280
212
+ }
213
+
214
+ # Response
215
+ {
216
+ "task_id": "abc123-def456-...",
217
+ "status": "PENDING"
218
+ }
219
+ ```
220
+
221
+ ### Task Status
222
+
223
+ ```bash
224
+ # Get task status
225
+ GET /tasks/{task_id}
226
+
227
+ # Response
228
+ {
229
+ "task_id": "abc123-def456-...",
230
+ "state": "SUCCESS",
231
+ "result": 3,
232
+ "traceback": null,
233
+ "date_done": "2024-01-15T10:30:00Z"
234
+ }
235
+ ```
236
+
237
+ ### Task Management
238
+
239
+ ```bash
240
+ # Revoke a task
241
+ POST /tasks/{task_id}/revoke
242
+ Content-Type: application/json
243
+
244
+ {
245
+ "terminate": true,
246
+ "signal": "SIGTERM"
247
+ }
248
+
249
+ # Get task result only
250
+ GET /tasks/{task_id}/result
251
+
252
+ # List active workers (filtered to this app's tasks)
253
+ GET /workers
254
+
255
+ # List available tasks in THIS app
256
+ GET /available-tasks
257
+
258
+ # Response
259
+ {
260
+ "app_name": "my_tasks",
261
+ "task_count": 4,
262
+ "tasks": [
263
+ {"name": "my_tasks.add", "queue": "default", ...},
264
+ {"name": "my_tasks.multiply", "queue": "default", ...}
265
+ ]
266
+ }
267
+
268
+ # List queues
269
+ GET /queues
270
+
271
+ # Purge tasks from a queue
272
+ POST /purge
273
+ ```
274
+
275
+ ### List All Tasks
276
+
277
+ ```bash
278
+ # List active, scheduled, reserved, and revoked tasks (filtered to this app only)
279
+ GET /tasks
280
+
281
+ # Response
282
+ {
283
+ "active": {...},
284
+ "scheduled": {...},
285
+ "reserved": {...},
286
+ "revoked": {...}
287
+ }
288
+ ```
289
+
290
+ ## Configuration
291
+
292
+ ### CeleryFastAPIBridge Options
293
+
294
+ ```python
295
+ bridge = CeleryFastAPIBridge(
296
+ celery_app=celery_app,
297
+ fastapi_app=fastapi_app, # Optional, creates new if not provided
298
+ prefix="/api/v1", # URL prefix for all endpoints
299
+ include_status_endpoints=True, # Include /tasks endpoints
300
+ task_filter=lambda name: not name.startswith("internal."), # Filter tasks
301
+ )
302
+ ```
303
+
304
+ ### create_app Options
305
+
306
+ ```python
307
+ app = create_app(
308
+ celery_app, # Celery instance or module path string
309
+ title="My API",
310
+ description="Task API",
311
+ version="1.0.0",
312
+ prefix="/api",
313
+ include_status_endpoints=True,
314
+ fastapi_kwargs={"docs_url": "/swagger"},
315
+ )
316
+ ```
317
+
318
+ ## Integration with Existing FastAPI App
319
+
320
+ ```python
321
+ from fastapi import FastAPI
322
+ from celery_fastapi import CeleryFastAPIBridge
323
+ from myapp import celery_app
324
+
325
+ app = FastAPI()
326
+
327
+ # Your existing routes
328
+ @app.get("/health")
329
+ def health_check():
330
+ return {"status": "healthy"}
331
+
332
+ # Add Celery task endpoints under /celery prefix
333
+ bridge = CeleryFastAPIBridge(
334
+ celery_app,
335
+ app,
336
+ prefix="/celery",
337
+ )
338
+ bridge.register_routes()
339
+ ```
340
+
341
+ ## CLI Reference
342
+
343
+ ```bash
344
+ celery-fastapi --help
345
+
346
+ Commands:
347
+ serve Start the FastAPI server with uvicorn
348
+ serve-gunicorn Start the FastAPI server with Gunicorn
349
+ routes List all generated routes
350
+ tasks List all registered Celery tasks
351
+ workers Show active Celery workers
352
+
353
+ # Serve options (uvicorn)
354
+ celery-fastapi serve myapp:celery_app \
355
+ --host 0.0.0.0 \
356
+ --port 8000 \
357
+ --reload \
358
+ --workers 4 \
359
+ --prefix /api \
360
+ --log-level info \
361
+ --ssl-keyfile key.pem \
362
+ --ssl-certfile cert.pem \
363
+ --proxy-headers \
364
+ --forwarded-allow-ips '*'
365
+
366
+ # Serve options (gunicorn)
367
+ celery-fastapi serve-gunicorn myapp:celery_app \
368
+ --bind 0.0.0.0:8000 \
369
+ --workers 4 \
370
+ --worker-class uvicorn.workers.UvicornWorker \
371
+ --timeout 30 \
372
+ --daemon \
373
+ --pid /var/run/celery-fastapi.pid
374
+ ```
375
+
376
+ ## Development
377
+
378
+ ```bash
379
+ # Clone the repository
380
+ git clone https://github.com/karailker/celery-fastapi.git
381
+ cd celery-fastapi
382
+
383
+ # Install dependencies
384
+ poetry install --extras all
385
+
386
+ # Run tests
387
+ poetry run pytest
388
+
389
+ # Run linting
390
+ poetry run ruff check .
391
+ poetry run mypy celery_fastapi
392
+
393
+ # Format code
394
+ poetry run ruff format .
395
+ ```
396
+
397
+ ## License
398
+
399
+ MIT License - see [LICENSE](LICENSE) file for details.
400
+
401
+ ## Contributing
402
+
403
+ Contributions are welcome! Please feel free to submit a Pull Request.
404
+
405
+ 1. Fork the repository
406
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
407
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
408
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
409
+ 5. Open a Pull Request
410
+