Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task experimentation #1

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
services:
app:
depends_on:
- rabbitmq
build:
context: .
target: development
Expand All @@ -15,3 +17,11 @@ services:
- .:/app
tty: true
stdin_open: true
rabbitmq:
image: 'rabbitmq:4.0-management'
environment:
- 'RABBITMQ_DEFAULT_USER=admin'
- 'RABBITMQ_DEFAULT_PASS=secret'
ports:
- '5672:5672'
- '15672:15672'
4 changes: 3 additions & 1 deletion env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
UID=YOUR_UID
GID=YOUR_GID
POETRY_VERSION=1.5.1
POETRY_VERSION=1.8.4
DEV=true

RABBITMQ_URL="amqp://admin:secret@rabbitmq:5672"
582 changes: 513 additions & 69 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typer = "^0.12.5"
uuid6 = "^2024.7.10"
python-ulid = "^2.7.0"
jinja2 = "^3.1.4"
dramatiq = {extras = ["rabbitmq", "watch"], version = "^1.17.0"}

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.2"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
poetry==1.5.1
poetry==1.8.4
25 changes: 25 additions & 0 deletions tasks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Task experimentation with dramatiq

Three terminals...

1. RabbitMQ

```sh
docker compose up rabbitmq
```

1. Dramatiq workers

```sh
docker compose run app bash
poetry run dramatiq tasks.test_task
```

1. Send messages

```sh
docker compose exec app bash
poetry run python
>>> from tasks.test_task import sleep_and_say_hi
>>> [sleep_and_say_hi.send() for i in range(10)]
```
Empty file added tasks/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tasks/broker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from typing import Self

import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker

class DramatiqRabbitmqBroker:

@classmethod
def from_env(cls) -> Self:
rabbitmq_url = os.environ.get("RABBITMQ_URL")
if not rabbitmq_url:
raise ValueError("RABBITMQ_URL environment variable not configured!")
return cls(rabbitmq_url)

def __init__(self, rabbitmq_url: str):
self.rabbitmq_broker = RabbitmqBroker(url=rabbitmq_url)
dramatiq.set_broker(self.rabbitmq_broker)

def set_up_queue(self, name: str):
if name not in self.rabbitmq_broker.get_declared_queues():
self.rabbitmq_broker.declare_queue(name)

17 changes: 17 additions & 0 deletions tasks/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import time
from random import randrange

import dramatiq

from tasks.broker import DramatiqRabbitmqBroker

QUEUE_NAME = "sleep"

BROKER = DramatiqRabbitmqBroker.from_env()
BROKER.set_up_queue(QUEUE_NAME)

@dramatiq.actor(queue_name=QUEUE_NAME)
def sleep_and_say_hi() -> None:
sleep_period = randrange(1, 10)
time.sleep(sleep_period)
print(f"Hi, I slept {sleep_period} seconds!")