DispelEvilBot/bot/services/heartbeat.py

29 lines
892 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Heartbeat для Docker healthcheck.
Бот периодически обновляет mtime файла; healthcheck считает контейнер живым,
если файл свежий. Если event loop завис — heartbeat перестаёт обновляться.
"""
from __future__ import annotations
import asyncio
import logging
from pathlib import Path
from common.config import get_settings
logger = logging.getLogger(__name__)
HEARTBEAT_INTERVAL = 30
async def run_heartbeat() -> None:
"""Обновлять файл-heartbeat каждые ``HEARTBEAT_INTERVAL`` секунд."""
path = Path(get_settings().heartbeat_path)
while True:
try:
path.touch()
except OSError:
logger.exception("Не удалось обновить heartbeat-файл %s", path)
await asyncio.sleep(HEARTBEAT_INTERVAL)