From 0651fb6ff06bf4b729f19b4a57a0690027d03a87 Mon Sep 17 00:00:00 2001 From: Dantenerosas Date: Wed, 29 Nov 2023 16:15:48 +0300 Subject: [PATCH] refactored docker-compose --- Dockerfile | 17 +++++++++++++++ docker-compose.yml | 45 ++++++++++++++++++++++++++++++++++++++-- src/core/rabbitmq.py | 4 ++-- src/core/redis_client.py | 2 +- src/web/main.py | 8 +++---- web.Dockerfile | 13 ++++++++++++ 6 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 Dockerfile create mode 100644 web.Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a9592b8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.11.4 + +WORKDIR /app + +COPY poetry.lock pyproject.toml /app/ + +RUN apt-get -y update +RUN apt-get -y upgrade +RUN apt-get install -y ffmpeg + +RUN pip install poetry + +RUN poetry install --no-root + +COPY .. /app + +CMD poetry run python main.py diff --git a/docker-compose.yml b/docker-compose.yml index ff16034..e2deca3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,19 +1,60 @@ version: "2.1" +networks: + network: + services: + web_service: + container_name: web_service + build: + context: . + dockerfile: web.Dockerfile + ports: + - "8000:8000" + depends_on: + redis: + condition: service_started + rabbitmq: + condition: service_healthy + restart: always + networks: + - network + download_service: + container_name: download_service + build: + context: . + dockerfile: Dockerfile + depends_on: + redis: + condition: service_started + rabbitmq: + condition: service_healthy + restart: always + networks: + - network rabbitmq: + container_name: rabbitmq image: rabbitmq:3.10.7-management hostname: rabbitmq restart: always + healthcheck: + test: rabbitmq-diagnostics -q ping + interval: 30s + timeout: 30s + retries: 3 environment: - RABBITMQ_DEFAULT_USER=guest - RABBITMQ_DEFAULT_PASS=guest volumes: - ./rabbitmq:/var/lib/rabbitmq ports: - - 15672:15672 - - 5672:5672 + - "15672:15672" + - "5672:5672" + networks: + - network redis: container_name: redis_video_downloader image: redis:latest ports: - "6379:6379" + networks: + - network diff --git a/src/core/rabbitmq.py b/src/core/rabbitmq.py index 4b9ef45..4d9db02 100644 --- a/src/core/rabbitmq.py +++ b/src/core/rabbitmq.py @@ -14,7 +14,7 @@ async def on_message(message: AbstractIncomingMessage, queue) -> None: async def get_messages(inner_queue) -> None: - async with await connect("amqp://guest:guest@localhost/") as connection: + async with await connect("amqp://guest:guest@rabbitmq/") as connection: channel = await connection.channel() await channel.set_qos(prefetch_count=1) @@ -30,7 +30,7 @@ async def get_messages(inner_queue) -> None: async def publish_message_with_task_done(task: dict | list) -> None: queue_name = "tasks_done" - async with await connect("amqp://guest:guest@localhost/") as connection: + async with await connect("amqp://guest:guest@rabbitmq/") as connection: # Creating channel channel = await connection.channel() diff --git a/src/core/redis_client.py b/src/core/redis_client.py index adb17c1..d9e5de1 100644 --- a/src/core/redis_client.py +++ b/src/core/redis_client.py @@ -9,7 +9,7 @@ class RedisClient: TASKS_DONE_NAME = "tasks_done" def __init__(self): - self.connection = redis.Redis(host="localhost", port=6379, db=0) + self.connection = redis.Redis(host="redis_video_downloader", port=6379, db=0) async def _set_task(self, queue_name: str, link: str, task: dict | list, ) -> int: async with self.connection as connection: diff --git a/src/web/main.py b/src/web/main.py index 161266c..1cc954c 100644 --- a/src/web/main.py +++ b/src/web/main.py @@ -1,4 +1,5 @@ import json +import os import uvicorn import logging @@ -15,8 +16,7 @@ from src.web.schemes.submit import SubmitIn, CheckIn, DeleteFromS3, CopyToAnothe app = FastAPI( title="video_downloader", openapi_url=f"/api/v1/openapi.json" ) - -templates = Jinja2Templates(directory="templates") +templates = Jinja2Templates(directory=f"{os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}/web/templates") app.add_middleware( CORSMiddleware, @@ -102,7 +102,7 @@ async def get_url_for_download_video(request: Request, data: SubmitIn = Depends( return JSONResponse({"result": links_to_download_video}) # TODO: учесть, что если делать запрос CURL\urllib3\etc, в теле может быть несколько ссылок -> должно быть создано несколько задач - async with await connect("amqp://guest:guest@localhost/") as connection: + async with await connect("amqp://guest:guest@rabbitmq/") as connection: # Creating a channel channel = await connection.channel() body = [ @@ -204,4 +204,4 @@ async def delete_video_from_s3(data: CopyToAnotherBucketS3): ) -uvicorn.run("src.web.main:app", host="0.0.0.0", log_level="info") +uvicorn.run("src.web.main:app", host="0.0.0.0", port=8000, log_level="info") diff --git a/web.Dockerfile b/web.Dockerfile new file mode 100644 index 0000000..750abe1 --- /dev/null +++ b/web.Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.11.4 + +WORKDIR /app + +COPY poetry.lock pyproject.toml /app/ + +RUN pip install poetry + +RUN poetry install --no-root + +COPY .. /app + +CMD poetry run python main_web.py \ No newline at end of file