fix (reminders): Fix startup logic.

This commit is contained in:
2023-08-18 23:00:29 +03:00
parent 0499946fad
commit 47d58bea37

View File

@@ -12,7 +12,7 @@ Max 25 reminders (propagating Discord restriction)
""" """
from typing import Optional from typing import Optional
import datetime as dt import datetime as dt
from cachetools import TTLCache from cachetools import TTLCache, LRUCache
import discord import discord
from discord.ext import commands as cmds from discord.ext import commands as cmds
@@ -21,7 +21,7 @@ from discord.app_commands import Transform
from discord.ui.select import select, SelectOption from discord.ui.select import select, SelectOption
from dateutil.parser import parse, ParserError from dateutil.parser import parse, ParserError
from data import RowModel, Registry from data import RowModel, Registry, WeakCache
from data.queries import ORDER from data.queries import ORDER
from data.columns import Integer, String, Timestamp, Bool from data.columns import Integer, String, Timestamp, Bool
@@ -64,6 +64,7 @@ class ReminderData(Registry, name='reminders'):
CREATE INDEX reminder_users ON reminders (userid); CREATE INDEX reminder_users ON reminders (userid);
""" """
_tablename_ = 'reminders' _tablename_ = 'reminders'
_cache_ = WeakCache(LRUCache(1000))
reminderid = Integer(primary=True) reminderid = Integer(primary=True)
@@ -192,17 +193,19 @@ class Reminders(LionCog):
async def cog_load(self): async def cog_load(self):
await self.data.init() await self.data.init()
if self.executor and self.bot.is_ready():
await self.on_ready()
@LionCog.listener()
async def on_ready(self):
if self.executor: if self.executor:
if self.monitor and self.monitor._monitor_task:
self.monitor._monitor_task.cancel()
# Attach and populate the reminder monitor # Attach and populate the reminder monitor
self.monitor = ReminderMonitor(executor=self.execute_reminder) self.monitor = ReminderMonitor(executor=self.execute_reminder)
await self.reload_reminders() await self.reload_reminders()
if self.bot.is_ready:
self.monitor.start()
@LionCog.listener()
async def on_ready(self):
if self.executor and not self.monitor._monitor_task:
# Start firing reminders # Start firing reminders
self.monitor.start() self.monitor.start()
@@ -312,7 +315,7 @@ class Reminders(LionCog):
# Skip any expired repeats, to avoid spamming requests after downtime # Skip any expired repeats, to avoid spamming requests after downtime
# TODO: Is this actually dst safe? # TODO: Is this actually dst safe?
while next_time.timestamp() <= now.timestamp(): while next_time.timestamp() <= now.timestamp():
next_time + dt.timedelta(seconds=reminder.interval) next_time = next_time + dt.timedelta(seconds=reminder.interval)
await reminder.update(remind_at=next_time) await reminder.update(remind_at=next_time)
self.monitor.schedule_task(reminder.reminderid, reminder.timestamp) self.monitor.schedule_task(reminder.reminderid, reminder.timestamp)
logger.debug( logger.debug(