Skip to content

Commit e8e6591

Browse files
authored
Merge pull request #97 from AlessioBugetti/move-worker-script
Move worker script
2 parents c167cc8 + fd906cf commit e8e6591

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ First, you may want to take a look at the project structure and understand what
561561
├── app # Main application directory.
562562
│ ├── __init__.py # Initialization file for the app package.
563563
│ ├── main.py # Main entry point of the FastAPI application.
564-
├── worker.py # Worker script for background tasks.
564+
565565
│ │
566566
│ ├── api # Folder containing API-related logic.
567567
│ │ ├── __init__.py
@@ -598,11 +598,16 @@ First, you may want to take a look at the project structure and understand what
598598
│ │ │ ├── cache_exceptions.py # Exceptions related to cache operations.
599599
│ │ │ └── http_exceptions.py # HTTP-related exceptions.
600600
│ │ │
601-
│ │ └── utils # Utility functions and helpers.
601+
│ │ ├── utils # Utility functions and helpers.
602+
│ │ │ ├── __init__.py
603+
│ │ │ ├── cache.py # Cache-related utilities.
604+
│ │ │ ├── queue.py # Utilities for task queue management.
605+
│ │ │ └── rate_limit.py # Rate limiting utilities.
606+
│ │ │
607+
│ │ └── worker # Worker script for background tasks.
602608
│ │ ├── __init__.py
603-
│ │ ├── cache.py # Cache-related utilities.
604-
│ │ ├── queue.py # Utilities for task queue management.
605-
│ │ └── rate_limit.py # Rate limiting utilities.
609+
│ │ ├── settings.py # Worker configuration and settings.
610+
│ │ └── functions.py # Async task definitions and management.
606611
│ │
607612
│ ├── crud # CRUD operations for the application.
608613
│ │ ├── __init__.py
@@ -1242,7 +1247,7 @@ For `client-side caching`, all you have to do is let the `Settings` class define
12421247

12431248
### 5.10 ARQ Job Queues
12441249

1245-
Create the background task in `app/worker.py`:
1250+
Create the background task in `app/core/worker/functions.py`:
12461251

12471252
```python
12481253
...
@@ -1252,7 +1257,7 @@ async def sample_background_task(ctx, name: str) -> str:
12521257
return f"Task {name} is complete!"
12531258
```
12541259

1255-
Then add the function to the `WorkerSettings` class `functions` variable:
1260+
Then add the function to the `WorkerSettings` class `functions` variable in `app/core/worker/settings.py`:
12561261

12571262
```python
12581263
# -------- class --------
@@ -1288,7 +1293,7 @@ If you are using `docker compose`, the worker is already running.
12881293
If you are doing it from scratch, run while in the `root` folder:
12891294

12901295
```sh
1291-
poetry run arq src.app.worker.WorkerSettings
1296+
poetry run arq src.app.core.worker.settings.WorkerSettings
12921297
```
12931298

12941299
### 5.11 Rate Limiting
@@ -1469,7 +1474,7 @@ poetry run uvicorn src.app.main:app --reload
14691474
And for the worker:
14701475

14711476
```sh
1472-
poetry run arq src.app.worker.WorkerSettings
1477+
poetry run arq src.app.core.worker.settings.WorkerSettings
14731478
```
14741479

14751480
## 6. Running in Production

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ services:
2525
build:
2626
context: .
2727
dockerfile: Dockerfile
28-
command: arq app.worker.WorkerSettings
28+
command: arq app.core.worker.settings.WorkerSettings
2929
env_file:
3030
- ./src/.env
3131
depends_on:

src/app/core/worker/__init__.py

Whitespace-only changes.
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import asyncio
22
import logging
33
import uvloop
4-
from arq.connections import RedisSettings
54
from arq.worker import Worker
65

7-
from .core.config import settings
8-
96
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
107

118
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
129

13-
REDIS_QUEUE_HOST = settings.REDIS_QUEUE_HOST
14-
REDIS_QUEUE_PORT = settings.REDIS_QUEUE_PORT
15-
1610

1711
# -------- background tasks --------
1812
async def sample_background_task(ctx: Worker, name: str) -> str:
@@ -27,12 +21,3 @@ async def startup(ctx: Worker) -> None:
2721

2822
async def shutdown(ctx: Worker) -> None:
2923
logging.info("Worker end")
30-
31-
32-
# -------- class --------
33-
class WorkerSettings:
34-
functions = [sample_background_task]
35-
redis_settings = RedisSettings(host=REDIS_QUEUE_HOST, port=REDIS_QUEUE_PORT)
36-
on_startup = startup
37-
on_shutdown = shutdown
38-
handle_signals = False

src/app/core/worker/settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from arq.connections import RedisSettings
2+
from ...core.config import settings
3+
from .functions import sample_background_task, startup, shutdown
4+
5+
REDIS_QUEUE_HOST = settings.REDIS_QUEUE_HOST
6+
REDIS_QUEUE_PORT = settings.REDIS_QUEUE_PORT
7+
8+
9+
class WorkerSettings:
10+
functions = [sample_background_task]
11+
redis_settings = RedisSettings(host=REDIS_QUEUE_HOST, port=REDIS_QUEUE_PORT)
12+
on_startup = startup
13+
on_shutdown = shutdown
14+
handle_signals = False

0 commit comments

Comments
 (0)