countdown: poll db for reminders, send to target
every second (!?) a new thread checks the database for reminder countdown items that haven't been reminded about yet. when the at time is reached, the thread sends the reminder and notes that a reminder was sent bss/dr.botzo#11
This commit is contained in:
parent
c81d0519a0
commit
beff6638d0
@ -1,6 +1,8 @@
|
||||
"""Access to countdown items through bot commands."""
|
||||
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
@ -17,8 +19,21 @@ class Countdown(Plugin):
|
||||
|
||||
"""Report on countdown items."""
|
||||
|
||||
def __init__(self, bot, connection, event):
|
||||
"""Initialize some stuff."""
|
||||
self.running_reminders = []
|
||||
self.send_reminders = True
|
||||
|
||||
t = threading.Thread(target=self.reminder_thread)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
super(Countdown, self).__init__(bot, connection, event)
|
||||
|
||||
def start(self):
|
||||
"""Set up handlers."""
|
||||
self.running_reminders = []
|
||||
self.send_reminders = True
|
||||
|
||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+list$',
|
||||
self.handle_item_list, -20)
|
||||
@ -29,12 +44,29 @@ class Countdown(Plugin):
|
||||
|
||||
def stop(self):
|
||||
"""Tear down handlers."""
|
||||
self.running_reminders = []
|
||||
self.send_reminders = False
|
||||
|
||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_list)
|
||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_detail)
|
||||
|
||||
super(Countdown, self).stop()
|
||||
|
||||
def reminder_thread(self):
|
||||
"""After IRC is started up, begin sending reminders."""
|
||||
time.sleep(30)
|
||||
while self.send_reminders:
|
||||
reminders = CountdownItem.objects.filter(is_reminder=True, sent_reminder=False)
|
||||
|
||||
for reminder in reminders:
|
||||
log.debug("%s @ %s", reminder.reminder_message, reminder.at_time)
|
||||
if reminder.at_time <= timezone.now():
|
||||
log.info("sending %s to %s", reminder.reminder_message, reminder.reminder_target)
|
||||
self.bot.reply(None, reminder.reminder_message, explicit_target=reminder.reminder_target)
|
||||
reminder.sent_reminder = True
|
||||
reminder.save()
|
||||
time.sleep(1)
|
||||
|
||||
def handle_item_detail(self, connection, event, match):
|
||||
"""Provide the details of one countdown item."""
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user