Files
pomodoro_tg_bot/pomodoro_bot/timer_manager.py
Sergey Bahmatov 7775c0ce83
All checks were successful
continuous-integration/drone/push Build is passing
logs timer_manager
2025-03-31 12:39:57 +05:00

39 lines
1.4 KiB
Python

import asyncio
from .user_manager import user_manager
from .models import UserState
class TimerManager:
def __init__(self, bot):
self.bot = bot
self.tasks = {}
async def start_timer(self, user_id, duration, chat_id, label):
await self.stop_timer(user_id)
async def timer():
print(f"[DEBUG] Timer started for user {user_id}, label={label}, sleeping for {duration} sec")
await asyncio.sleep(duration)
print(f"[DEBUG timer] label={label!r}")
if label == 'Pomodoro':
try:
await user_manager.increment_pomodoros(user_id)
print(f"[DEBUG] Pomodoro count incremented for {user_id}")
except Exception as e:
print(f"[ERROR] Failed to increment pomodoros: {e}")
await self.bot.send_message(chat_id, f"{label} завершён!")
loop = asyncio.get_running_loop()
task = loop.create_task(timer())
print(f"[DEBUG] Timer task created for user {user_id} with label {label}")
user = user_manager.get_user(user_id)
user.current_timer = label
user.task = task
self.tasks[user_id] = task
async def stop_timer(self, user_id):
user = user_manager.get_user(user_id)
if user.task:
user.task.cancel()
user.task = None
user.current_timer = None
self.tasks.pop(user_id, None)