-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add poller and rabbitmq queue containers #2
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| import aio_pika | ||
| from aio_pika import Message | ||
| from aio_pika.abc import AbstractIncomingMessage | ||
|
|
||
|
|
||
| class RabbitMQManager: | ||
| def __init__(self, amqp_url: str, queue_name: str): | ||
| self._url = amqp_url | ||
| self._queue_name = queue_name | ||
| self._connection: aio_pika.abc.AbstractRobustConnection | None = None | ||
| self._channel: aio_pika.abc.AbstractChannel | None = None | ||
| self._queue: aio_pika.abc.AbstractQueue | None = None | ||
|
|
||
| async def connect(self) -> None: | ||
| self._connection = await aio_pika.connect_robust(self._url) | ||
| self._channel = await self._connection.channel() | ||
| self._queue = await self._channel.declare_queue(self._queue_name, durable=True) | ||
|
|
||
| async def send(self, body: bytes) -> None: | ||
| if self._channel is None: | ||
| raise RuntimeError("RabbitMQManager is not connected") | ||
| await self._channel.default_exchange.publish( | ||
| Message(body=body), | ||
| routing_key=self._queue_name, | ||
| ) | ||
|
|
||
| async def consume( | ||
| self, | ||
| handler: Callable[[AbstractIncomingMessage], Awaitable[None]], | ||
| ) -> None: | ||
| if self._queue is None: | ||
| raise RuntimeError("RabbitMQManager is not connected") | ||
| await self._queue.consume(handler) | ||
|
|
||
| async def close(self) -> None: | ||
| if self._connection: | ||
| await self._connection.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| FROM python:3.12-alpine | ||
| COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ | ||
|
|
||
| WORKDIR /project | ||
| COPY pyproject.toml uv.lock ./ | ||
|
|
||
| RUN uv sync --compile-bytecode --no-cache --no-dev | ||
| ENV PATH="/project/.venv/bin:$PATH" | ||
|
|
||
| COPY . . | ||
|
|
||
| WORKDIR /project | ||
| CMD python -m app.poller.poller | ||
|
Gray-Advantage marked this conversation as resolved.
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import asyncio | ||
| import json | ||
| from typing import Any, cast | ||
|
|
||
| import aiohttp | ||
|
|
||
| from app.app import app, setup_app | ||
| from app.core.manager import RabbitMQManager | ||
|
|
||
|
|
||
| async def get_updates(offset: int) -> dict[str, Any]: | ||
| url = f"https://api.telegram.org/bot{app.config.bot.token}/getUpdates" # поменяю | ||
| async with aiohttp.ClientSession() as session: | ||
|
Gray-Advantage marked this conversation as resolved.
|
||
| async with session.get(url, params={"offset": offset, "timeout": 30}) as resp: | ||
| return cast(dict[str, Any], await resp.json()) | ||
|
|
||
|
|
||
| async def poll_and_push() -> None: | ||
| rabbit = RabbitMQManager( | ||
| amqp_url=app.config.rabbitmq.url, | ||
| queue_name=app.config.rabbitmq.input_queue, | ||
| ) | ||
| await rabbit.connect() | ||
|
|
||
| offset = 0 | ||
| while True: | ||
|
Gray-Advantage marked this conversation as resolved.
|
||
| data = await get_updates(offset) | ||
| for update in data.get("result", []): | ||
| offset = update["update_id"] + 1 | ||
| await rabbit.send(json.dumps(update).encode()) | ||
|
|
||
|
|
||
| setup_app() | ||
| asyncio.run(poll_and_push()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from aiohttp.web import run_app | ||
|
|
||
| from app.app.app import setup_app | ||
| from app.app import setup_app | ||
|
|
||
| if __name__ == "__main__": | ||
| run_app(setup_app()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| BOT__TOKEN=0123456789:a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz | ||
|
Gray-Advantage marked this conversation as resolved.
|
||
|
|
||
| DATABASE__HOST=localhost | ||
| DATABASE__PORT=5432 | ||
| DATABASE__USER=postgres | ||
| DATABASE__PASSWORD=postgres | ||
| DATABASE__DATABASE=postgres | ||
|
|
||
| RABBITMQ__HOST=localhost | ||
| RABBITMQ__PORT=5672 | ||
| RABBITMQ__USER=guest | ||
| RABBITMQ__PASSWORD=guest | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.