From c90a2001614afd92b3316ccaad2ce5336460913d Mon Sep 17 00:00:00 2001 From: Diana Kim Date: Fri, 17 May 2024 15:27:45 +0900 Subject: [PATCH] Isolated bot startup into a separate command --- Makefile | 3 ++- poetry.lock | 36 ++++++++++++++++++++++++++++++++++-- pyproject.toml | 1 + src/bot/apps.py | 11 +++++++---- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index cb36470..f9db949 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ collectstatic: # Применение собранных миграций к базе данных, на основе сформированных моделей. migrate: + export RUN_BOT=false cd $(PROJECT_DIR) && $(DJANGO_RUN) migrate --no-input @@ -84,7 +85,7 @@ clear-db: # Запуск сервера разработки через Uvicorn run-dev: - cd $(DJANGO_DIR) && poetry run uvicorn core.asgi_dev:application --reload + export RUN_BOT=true; cd $(DJANGO_DIR) && poetry run uvicorn core.asgi_dev:application --reload # Запуск сервера продакшена через Uvicorn run-prod: diff --git a/poetry.lock b/poetry.lock index c39d813..df897ef 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. [[package]] name = "anyio" @@ -745,6 +745,24 @@ completion = ["shtab (>=1.1.0)"] docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (<7.2.5)", "sphinx (>=3.5)", "sphinx-lint"] testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy", "pytest-ruff (>=0.2.1)"] +[[package]] +name = "loguru" +version = "0.7.2" +description = "Python logging made (stupidly) simple" +optional = false +python-versions = ">=3.5" +files = [ + {file = "loguru-0.7.2-py3-none-any.whl", hash = "sha256:003d71e3d3ed35f0f8984898359d65b79e5b21943f78af86aa5491210429b8eb"}, + {file = "loguru-0.7.2.tar.gz", hash = "sha256:e671a53522515f34fd406340ee968cb9ecafbc4b36c679da03c18fd8d0bd51ac"}, +] + +[package.dependencies] +colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""} +win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""} + +[package.extras] +dev = ["Sphinx (==7.2.5)", "colorama (==0.4.5)", "colorama (==0.4.6)", "exceptiongroup (==1.1.3)", "freezegun (==1.1.0)", "freezegun (==1.2.2)", "mypy (==v0.910)", "mypy (==v0.971)", "mypy (==v1.4.1)", "mypy (==v1.5.1)", "pre-commit (==3.4.0)", "pytest (==6.1.2)", "pytest (==7.4.0)", "pytest-cov (==2.12.1)", "pytest-cov (==4.1.0)", "pytest-mypy-plugins (==1.9.3)", "pytest-mypy-plugins (==3.0.0)", "sphinx-autobuild (==2021.3.14)", "sphinx-rtd-theme (==1.3.0)", "tox (==3.27.1)", "tox (==4.11.0)"] + [[package]] name = "more-itertools" version = "10.2.0" @@ -1453,6 +1471,20 @@ platformdirs = ">=3.9.1,<5" docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.2,!=7.3)", "sphinx-argparse (>=0.4)", "sphinxcontrib-towncrier (>=0.2.1a0)", "towncrier (>=23.6)"] test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7)", "packaging (>=23.1)", "pytest (>=7.4)", "pytest-env (>=0.8.2)", "pytest-freezer (>=0.4.8)", "pytest-mock (>=3.11.1)", "pytest-randomly (>=3.12)", "pytest-timeout (>=2.1)", "setuptools (>=68)", "time-machine (>=2.10)"] +[[package]] +name = "win32-setctime" +version = "1.1.0" +description = "A small Python utility to set file creation time on Windows" +optional = false +python-versions = ">=3.5" +files = [ + {file = "win32_setctime-1.1.0-py3-none-any.whl", hash = "sha256:231db239e959c2fe7eb1d7dc129f11172354f98361c4fa2d6d2d7e278baa8aad"}, + {file = "win32_setctime-1.1.0.tar.gz", hash = "sha256:15cf5750465118d6929ae4de4eb46e8edae9a5634350c01ba582df868e932cb2"}, +] + +[package.extras] +dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] + [[package]] name = "xattr" version = "1.1.0" @@ -1529,4 +1561,4 @@ test = ["pytest"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "b072bfcc2b324fca75978d987bb4463deb1b81adfec6a159f48369107fba75db" +content-hash = "23804b5e833b06eb0c9fe421db8d8e1a4b52b3feb8119b6aaf455f23fbbe7100" diff --git a/pyproject.toml b/pyproject.toml index 12dbee8..2cae848 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ package-mode = false python = "^3.12" django = "^5.0.4" uvicorn = "^0.29.0" +loguru = "^0.7.2" python-telegram-bot = "^21.1.1" poetry-plugin-export = "^1.7.1" django-environ= "^0.11.2" diff --git a/src/bot/apps.py b/src/bot/apps.py index d7c4da3..94f4f08 100644 --- a/src/bot/apps.py +++ b/src/bot/apps.py @@ -1,3 +1,5 @@ +import os + from django.apps import AppConfig from django_asgi_lifespan.signals import asgi_shutdown @@ -10,11 +12,12 @@ class BotConfig(AppConfig): def ready(self) -> None: """Perform bot start when the Django application is fully loaded.""" - from bot.bot_interface import Bot + if os.getenv('RUN_BOT', 'false').lower() == 'true': + from bot.bot_interface import Bot - self.bot = Bot() - asgi_shutdown.connect(self.stop_bot) - self.bot.start() + self.bot = Bot() + asgi_shutdown.connect(self.stop_bot) + self.bot.start() def stop_bot(self, **kwargs): """Stop the bot if it was running."""