Skip to content

Commit

Permalink
Isolated bot startup into a separate command
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaKim9319 committed May 17, 2024
1 parent 1a1ef28 commit c90a200
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ collectstatic:

# Применение собранных миграций к базе данных, на основе сформированных моделей.
migrate:
export RUN_BOT=false
cd $(PROJECT_DIR) && $(DJANGO_RUN) migrate --no-input


Expand Down Expand Up @@ -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:
Expand Down
36 changes: 34 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 7 additions & 4 deletions src/bot/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from django.apps import AppConfig
from django_asgi_lifespan.signals import asgi_shutdown

Expand All @@ -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."""
Expand Down

0 comments on commit c90a200

Please sign in to comment.