49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""Фоновые задачи: долгие операции с серверами и уведомление пользователя о результате."""
|
|
import logging
|
|
|
|
import dramatiq
|
|
from Telegram.messages import send
|
|
from Telegram.models import Chat
|
|
|
|
from . import services
|
|
from .bot import texts
|
|
from .models import Point
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# установка обновляет систему и ставит панель, это долго
|
|
TIME_LIMIT_MS = 60 * 60 * 1000
|
|
|
|
|
|
def _notify(chat_id: int, text: str, keyboard=None):
|
|
send(Chat.objects.get(pk=chat_id), text, keyboard=keyboard, parse_mode='HTML')
|
|
|
|
|
|
def _run_installation(point: Point, chat_id: int, operation, *args):
|
|
try:
|
|
operation(point, *args)
|
|
except Exception as error:
|
|
logger.exception('Point %s installation failed', point)
|
|
_notify(chat_id, texts.install_failed(point, error), keyboard=[[texts.SERVER_MENU]])
|
|
return
|
|
_notify(chat_id, texts.installed(point), keyboard=[[texts.SERVER_MENU]])
|
|
|
|
|
|
# повтор не нужен: пользователь сам решает, переустанавливать ли после ошибки
|
|
@dramatiq.actor(max_retries=0, time_limit=TIME_LIMIT_MS)
|
|
def install_point(point_id: int, chat_id: int):
|
|
_run_installation(Point.objects.get(pk=point_id), chat_id, services.install_point)
|
|
|
|
|
|
@dramatiq.actor(max_retries=0, time_limit=TIME_LIMIT_MS)
|
|
def reinstall_point(point_id: int, chat_id: int, address: str | None = None, password: str | None = None):
|
|
_run_installation(Point.objects.get(pk=point_id), chat_id, services.reinstall, address, password)
|
|
|
|
|
|
@dramatiq.actor(max_retries=0, time_limit=TIME_LIMIT_MS)
|
|
def release_point(point_id: int, chat_id: int):
|
|
point = Point.objects.get(pk=point_id)
|
|
name = point.name
|
|
passwords, errors = services.release_point(point)
|
|
_notify(chat_id, texts.released(name, passwords, errors))
|