fixes bugs
This commit is contained in:
parent
e675a245e6
commit
59966a2f17
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -49,12 +49,19 @@ async def check_and_send_reminders(bot: Bot) -> None:
|
||||||
Args:
|
Args:
|
||||||
bot: Bot instance
|
bot: Bot instance
|
||||||
"""
|
"""
|
||||||
|
from bot.db.base import async_session_maker
|
||||||
|
from bot.db.operations import update_reminder
|
||||||
|
|
||||||
|
if not async_session_maker:
|
||||||
|
logger.error("Session maker not initialized")
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
time_service = get_time_service()
|
time_service = get_time_service()
|
||||||
current_time = time_service.get_now()
|
current_time = time_service.get_now()
|
||||||
|
|
||||||
# Get due reminders from database
|
# Get due reminders from database using proper async session
|
||||||
async for session in get_session():
|
async with async_session_maker() as session:
|
||||||
due_reminders = await get_due_reminders(session, current_time)
|
due_reminders = await get_due_reminders(session, current_time)
|
||||||
|
|
||||||
if not due_reminders:
|
if not due_reminders:
|
||||||
|
|
@ -74,16 +81,15 @@ async def check_and_send_reminders(bot: Bot) -> None:
|
||||||
|
|
||||||
# Update next_run_at to prevent sending again
|
# Update next_run_at to prevent sending again
|
||||||
# (it will be properly updated when user clicks "Done" or by periodic update)
|
# (it will be properly updated when user clicks "Done" or by periodic update)
|
||||||
from bot.db.operations import update_reminder
|
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
# Temporarily set next_run_at to current + interval to avoid duplicate sends
|
|
||||||
temp_next_run = time_service.calculate_next_occurrence(
|
temp_next_run = time_service.calculate_next_occurrence(
|
||||||
current_run=reminder.next_run_at,
|
current_run=reminder.next_run_at,
|
||||||
days_interval=reminder.days_interval,
|
days_interval=reminder.days_interval,
|
||||||
)
|
)
|
||||||
await update_reminder(session, reminder.id, next_run_at=temp_next_run)
|
await update_reminder(session, reminder.id, next_run_at=temp_next_run)
|
||||||
|
|
||||||
|
# Commit all updates
|
||||||
|
await session.commit()
|
||||||
|
|
||||||
# Small delay to avoid rate limits
|
# Small delay to avoid rate limits
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -176,8 +176,11 @@ async def get_due_reminders(session: AsyncSession, current_time: datetime) -> Li
|
||||||
Returns:
|
Returns:
|
||||||
List of due Reminder instances
|
List of due Reminder instances
|
||||||
"""
|
"""
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
result = await session.execute(
|
result = await session.execute(
|
||||||
select(Reminder)
|
select(Reminder)
|
||||||
|
.options(selectinload(Reminder.user)) # Eager load user relationship
|
||||||
.where(Reminder.is_active == True)
|
.where(Reminder.is_active == True)
|
||||||
.where(Reminder.next_run_at <= current_time)
|
.where(Reminder.next_run_at <= current_time)
|
||||||
.order_by(Reminder.next_run_at)
|
.order_by(Reminder.next_run_at)
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
17
bot/main.py
17
bot/main.py
|
|
@ -38,14 +38,23 @@ async def on_shutdown() -> None:
|
||||||
logger.info("Shutting down reminder bot...")
|
logger.info("Shutting down reminder bot...")
|
||||||
|
|
||||||
# Stop scheduler
|
# Stop scheduler
|
||||||
stop_scheduler()
|
try:
|
||||||
|
stop_scheduler()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error stopping scheduler: {e}")
|
||||||
|
|
||||||
# Close database
|
# Close database
|
||||||
await close_db()
|
try:
|
||||||
|
await close_db()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error closing database: {e}")
|
||||||
|
|
||||||
# Close bot session
|
# Close bot session
|
||||||
if bot:
|
if bot and hasattr(bot, 'session'):
|
||||||
await bot.session.close()
|
try:
|
||||||
|
await bot.session.close()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error closing bot session: {e}")
|
||||||
|
|
||||||
logger.info("Bot shutdown completed")
|
logger.info("Bot shutdown completed")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,11 +45,11 @@ def validate_days_interval(days_str: str) -> Optional[int]:
|
||||||
days_str: Days interval string to validate
|
days_str: Days interval string to validate
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Integer days if valid (>0), None otherwise
|
Integer days if valid (>0 and <=365), None otherwise
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
days = int(days_str.strip())
|
days = int(days_str.strip())
|
||||||
if days > 0:
|
if 0 < days <= 365: # Max 1 year
|
||||||
return days
|
return days
|
||||||
except (ValueError, AttributeError):
|
except (ValueError, AttributeError):
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue