8000 docs: add celery and mailing guideline · code4mk/fastapi-template@8dd44ae · GitHub
[go: up one dir, main page]

Skip to conten 8000 t

Commit 8dd44ae

Browse files
committed
docs: add celery and mailing guideline
1 parent 1b9460b commit 8dd44ae

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

_docs/celery.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
To run the Celery worker and beat, you'll need to use the `celery` command-line interface. Here are the commands:
2+
3+
1. To run a Celery worker:
4+
```bash
5+
celery -A app.celery:app worker --loglevel=INFO
6+
```
7+
8+
2. To run Celery beat (scheduler):
9+
```bash
10+
celery -A app.celery:app beat --loglevel=INFO
11+
```
12+
13+
You can also run both worker and beat in a single command:
14+
```bash
15+
celery -A app.celery:app worker --beat --loglevel=INFO
16+
```
17+
18+
Explanation of the commands:
19+
- `-A app.celery:app`: Specifies the Celery application instance (the path to your app)
20+
- `worker`: Starts a worker process
21+
- `beat`: Starts the scheduler
22+
- `--loglevel=INFO`: Sets the logging level (you can use DEBUG for more detailed logs)
23+
24+
Additional tips:
25+
1. Make sure Redis is running (since you're using it as a broker)
26+
2. You might want to run these commands from the root directory of your project
27+
3. In development, you can add `-P solo` on Windows or if you encounter multiprocessing issues:
28+
```bash
29+
celery -A app.celery:app worker -P solo --loglevel=INFO
30+
```
31+
32+
Would you like me to explain any specific part in more detail?

_docs/mailing.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# simple mailing
2+
3+
```python
4+
from fastapi_pundra.common.mailer.mail import send_mail
5+
6+
await send_mail(
7+
subject="Hello, World!",
8+
to=["test@example.com"],
9+
template_name="welcome_email.html",
10+
context={"name": "John Doe"},
11+
)
12+
```
13+
14+
# background mailing
15+
16+
```python
17+
from fastapi_pundra.common.mailer.mail import send_mail_background
18+
19+
await send_mail_background(
20+
background_tasks=background_tasks,
21+
subject="Hello, World!",
22+
to=["test@example.com"],
23+
template_name="welcome_email.html",
24+
context={"name": "John Doe"},
25+
)
26+
```
27+
28+
## routing tips
29+
30+
- if you want to send mail in background, you can use `background_tasks` in your route
31+
32+
```python
33+
from fastapi import BackgroundTasks
34+
35+
@router.post("/users/registration")
36+
@dto(UserCreateSchema)
37+
async def registration(request: Request, background_tasks: BackgroundTasks) -> JSONResponse:
38+
...
39+
```
40+
41+
> note: background_tasks paramter will be after `request` parameter in your route
42+
43+
# queue mailing with celery
44+
45+
```python
46+
from fastapi_pundra.common.mailer.task import send_email_queue_task
47+
48+
send_email_queue_task.delay(
49+
subject="Hello, World!",
50+
to=["test@example.com"],
51+
template_name="welcome_email.html",
52+
context={"name": "John Doe"},
53+
)
54+
```
55+
56+
# queue mailing with celery with countdown
57+
58+
```python
59+
from fastapi_pundra.common.mailer.task import send_email_queue_task
60+
61+
send_email_queue_task.apply_async(
62+
args=[
63+
f"Welcome, {new_user.name or new_user.email}!",
64+
[new_user.email],
65+
template_name,
66+
context,
67+
],
68+
countdown=120,
69+
)
70+
```

0 commit comments

Comments
 (0)
0