Skip to content

Commit

Permalink
More docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Feb 9, 2025
1 parent 6c3b0d4 commit 04ee3a8
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 232 deletions.
1 change: 0 additions & 1 deletion docs/reference/task_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ from fastapi.scheduler import TaskManager

::: fluid.scheduler.TaskManager


::: fluid.scheduler.TaskManagerConfig

::: fluid.scheduler.consumer.TaskDispatcher
13 changes: 13 additions & 0 deletions docs/reference/task_scheduler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Task Scheduler

The task scheduler [TaskScheduler][fluid.scheduler.TaskScheduler] inherits
from the [TaskConsumer][fluid.scheduler.TaskConsumer] to add scheduling of
periodic tasks.

It can be imported from `fluid.scheduler`:

```python
from fastapi.scheduler import TaskScheduler
```

::: fluid.scheduler.TaskScheduler
52 changes: 52 additions & 0 deletions docs/tutorials/task_app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Task Queue App

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.
The middleware for distributing tasks can be configured via the [TaskBroker][fluid.scheduler.TaskBroker] interface.

A redis task broker is provided for convenience.

## Tasks Consumer

Create a task consumer, register tasks from modules, and run the consumer.

```python
import asyncio
from typing import Any
from fluid.scheduler import TaskConsumer
import task_module_a, task_module_b


def task_consumer(**kwargs: Any) -> TaskConsumer:
consumer = TaskConsumer(**kwargs)
consumer.register_from_module(task_module_a)
consumer.register_from_module(task_module_b)
return consumer


if __name__ == "__main__":
consumer = task_consumer()
asyncio.run(consumer.run())
```

## FastAPI Integration

The `TaskConsumer` can be integrated with FastAPI so that
tasks can be queued via HTTP requests.

```python
import uvicorn
from fluid.scheduler.endpoints import setup_fastapi

if __name__ == "__main__":
consumer = task_consumer()
app = setup_fastapi(consumer)
uvicorn.run(app)
```

You can test via the example provided

```bash
python -m examples.simple_fastapi
```

and check the openapi UI at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs).
24 changes: 15 additions & 9 deletions docs/tutorials/task_queue.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Task Queue

The `fluid.scheduler` module is a simple yet powerful distributed task producer (TaskScheduler) and consumer (TaskConsumer) system for executing tasks.
The middleware for distributing tasks can be configured via the Broker interface.
A redis broker is provided for convenience.

## Tasks
# Tasks

Tasks are standard python async functions decorated with the [@task][fluid.scheduler.task] decorator.

Expand Down Expand Up @@ -42,7 +36,7 @@ async def say_hi(ctx: TaskRun[TaskParams]) -> None:

## Task Types

There are two types of tasks implemented
There are few types of tasks implemented, lets take a look at them.

### IO Bound Tasks

Expand Down Expand Up @@ -89,10 +83,22 @@ Both IO and CPU bound tasks can be periodically scheduled via the `schedule` key
There are two types of scheduling, the most common is the [every][fluid.scheduler.every] function that takes a `timedelta` object.

```python
import asyncio
from datetime import timedelta
from fluid.scheduler import task, TaskContext, every
from fluid.scheduler import task, TaskRun, every

@task(schedule=every(timedelta(seconds=1)))
async def scheduled(ctx: TaskRun) -> None:
await asyncio.sleep(0.1)
```

You can also use the [crontab][fluid.scheduler.crontab] function to schedule tasks using cron expressions.

```python
import asyncio
from fluid.scheduler import task, TaskRun, crontab

@task(schedule=crontab(hours='*/2'))
async def scheduled(ctx: TaskRun) -> None:
await asyncio.sleep(0.1)
```
8 changes: 8 additions & 0 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import asyncio

from examples.tasks import task_scheduler
from fluid.utils import log

if __name__ == "__main__":
log.config()
asyncio.run(task_scheduler().run())
10 changes: 10 additions & 0 deletions examples/simple_fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import uvicorn

from examples.tasks import task_scheduler
from fluid.scheduler.endpoints import setup_fastapi
from fluid.utils import log

if __name__ == "__main__":
log.config()
app = setup_fastapi(task_scheduler())
uvicorn.run(app)
8 changes: 6 additions & 2 deletions examples/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
from fluid.scheduler.endpoints import setup_fastapi


def task_app() -> FastAPI:
def task_scheduler() -> TaskScheduler:
task_manager = TaskScheduler()
task_manager.register_from_dict(globals())
return setup_fastapi(task_manager)
return task_manager


def task_app() -> FastAPI:
return setup_fastapi(task_scheduler())


class Sleep(BaseModel):
Expand Down
7 changes: 7 additions & 0 deletions fluid/scheduler/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@


class TaskManagerCLI(LazyGroup):
"""CLI for TaskManager
This class provides a CLI for a TaskManager Application.
It requires to install the `cli` extra dependencies.
"""

def __init__(
self,
task_manager_app: TaskManagerApp,
Expand Down
5 changes: 4 additions & 1 deletion fluid/utils/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def is_stopping(self) -> bool:

@abstractmethod
async def run(self) -> None:
"""run the worker"""
"""run the worker
THis is the main entry point of the worker.
"""


class RunningWorker(Worker):
Expand Down
Loading

0 comments on commit 04ee3a8

Please sign in to comment.