Add autosync rabbitmq consumer

This commit is contained in:
Zoe Roux 2024-03-20 00:29:13 +01:00
parent 44bb88910f
commit 6937a982d4
No known key found for this signature in database
7 changed files with 52 additions and 0 deletions

1
autosync/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

8
autosync/Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM python:3.12
WORKDIR /app
COPY ./requirements.txt .
RUN pip3 install -r ./requirements.txt
COPY . .
ENTRYPOINT ["python3", "-m", "autosync"]

View File

@ -0,0 +1,35 @@
import os
import pika
from pika import spec
from pika.adapters.blocking_connection import BlockingChannel
import pika.credentials
def callback(
ch: BlockingChannel,
method: spec.Basic.Deliver,
properties: spec.BasicProperties,
body: bytes,
):
print(f" [x] {method.routing_key}:{body}")
def main():
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=os.environ.get("RABBITMQ_HOST", "rabbitmq"),
credentials=pika.credentials.PlainCredentials(
os.environ.get("RABBITMQ_DEFAULT_USER", "guest"),
os.environ.get("RABBITMQ_DEFAULT_PASS", "guest"),
),
)
)
channel = connection.channel()
channel.exchange_declare(exchange="events.watched", exchange_type="topic")
result = channel.queue_declare("", exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange="events.watched", queue=queue_name, routing_key="#")
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()

View File

@ -0,0 +1,4 @@
#!/usr/bin/env python
import autosync
autosync.main()

2
autosync/pyproject.toml Normal file
View File

@ -0,0 +1,2 @@
[tool.ruff.format]
indent-style = "tab"

View File

@ -0,0 +1 @@
pika

View File

@ -5,6 +5,7 @@
aiohttp
jsons
watchfiles
pika
]);
dotnet = with pkgs.dotnetCorePackages;
combinePackages [