|
| 1 | +# Task Queue App |
| 2 | + |
| 3 | +The `fluid.scheduler` module is a simple yet powerful distributed task producer ([TaskScheduler][fluid.scheduler.TaskScheduler]) and consumer ([TaskConsumer][fluid.scheduler.TaskConsumer]) system for executing tasks. |
| 4 | +The middleware for distributing tasks can be configured via the [TaskBroker][fluid.scheduler.TaskBroker] interface. |
| 5 | + |
| 6 | +A redis task broker is provided for convenience. |
| 7 | + |
| 8 | +## Tasks Consumer |
| 9 | + |
| 10 | +Create a task consumer, register tasks from modules, and run the consumer. |
| 11 | + |
| 12 | +```python |
| 13 | +import asyncio |
| 14 | +from typing import Any |
| 15 | +from fluid.scheduler import TaskConsumer |
| 16 | +import task_module_a, task_module_b |
| 17 | + |
| 18 | + |
| 19 | +def task_consumer(**kwargs: Any) -> TaskConsumer: |
| 20 | + consumer = TaskConsumer(**kwargs) |
| 21 | + consumer.register_from_module(task_module_a) |
| 22 | + consumer.register_from_module(task_module_b) |
| 23 | + return consumer |
| 24 | + |
| 25 | + |
| 26 | +if __name__ == "__main__": |
| 27 | + consumer = task_consumer() |
| 28 | + asyncio.run(consumer.run()) |
| 29 | +``` |
| 30 | + |
| 31 | +## FastAPI Integration |
| 32 | + |
| 33 | +The `TaskConsumer` can be integrated with FastAPI so that |
| 34 | +tasks can be queued via HTTP requests. |
| 35 | + |
| 36 | +```python |
| 37 | +import uvicorn |
| 38 | +from fluid.scheduler.endpoints import setup_fastapi |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + consumer = task_consumer() |
| 42 | + app = setup_fastapi(consumer) |
| 43 | + uvicorn.run(app) |
| 44 | +``` |
| 45 | + |
| 46 | +You can test via the example provided |
| 47 | + |
| 48 | +```bash |
| 49 | +$ python -m examples.simple_fastapi |
| 50 | +``` |
| 51 | + |
| 52 | +and check the openapi UI at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs). |
| 53 | + |
| 54 | + |
| 55 | +## Task App Command Line |
| 56 | + |
| 57 | +The [TaskConsumer][fluid.scheduler.TaskConsumer] or [TaskScheduler][fluid.scheduler.TaskScheduler] can be run with the command line tool to allow for an even richer API. |
| 58 | + |
| 59 | +```python |
| 60 | +from fluid.scheduler.cli import TaskManagerCLI |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + consumer = task_consumer() |
| 64 | + TaskManagerCLI(setup_fastapi(consumer))() |
| 65 | +``` |
| 66 | + |
| 67 | +This features requires to install the package with the `cli` extra. |
| 68 | + |
| 69 | +```bash |
| 70 | +$ pip install aio-fluid[cli] |
| 71 | +``` |
| 72 | + |
| 73 | +```bash |
| 74 | +$ python -m examples.simple_cli |
| 75 | +Usage: python -m examples.simple_cli [OPTIONS] COMMAND [ARGS]... |
| 76 | + |
| 77 | +Options: |
| 78 | + --help Show this message and exit. |
| 79 | + |
| 80 | +Commands: |
| 81 | + exec Execute a registered task |
| 82 | + ls List all tasks with their schedules |
| 83 | + serve Start app server |
| 84 | +``` |
| 85 | + |
| 86 | +The command line tools provides a powerful interface to execute tasks, parameters are |
| 87 | +passed as optional arguments using he standard click interface. |
0 commit comments