Skip to content

Commit

Permalink
Update reminders every 10 minutes instead of every hour.
Browse files Browse the repository at this point in the history
Release as 0.3.21.

See #97.
  • Loading branch information
nmlorg committed Jun 29, 2024
1 parent 1ea376c commit 30724f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions metabot/modules/reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
from metabot.util import humanize
from metabot.util import pickleutil

PERIOD = 60 * 10 # Run modinit.periodic every 10 minutes.


def modinit(multibot): # pylint: disable=missing-docstring

def _queue():
multibot.loop.queue.puthourly(0, _hourly, jitter=random.random() * 5)
def queue():
now = time.time()
nextrun = (now // PERIOD + 1) * PERIOD
jitter = random.random() * 5
multibot.loop.queue.putwhen(nextrun + jitter, periodic)

if multibot.conf.confdir:
recordsfname = multibot.conf.confdir + '/daily.pickle'
Expand All @@ -32,20 +37,23 @@ def _queue():
recordsfname = None
records = {}

def _hourly():
def periodic():
logging.info('Running periodic.')
try:
multibot.multical.poll()
_daily_messages(multibot, records)
if recordsfname:
pickleutil.dump(recordsfname, records)
finally:
_queue()
queue()

_queue()
queue()


def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too-many-locals,too-many-statements
now = time.time()
startofhour = now // 3600

for botuser, botconf in multibot.conf['bots'].items(): # pylint: disable=too-many-nested-blocks
for groupid, groupconf in botconf['issue37']['moderator'].items():
calcodes, tzinfo, count, days, hour, dow = eventutil.get_group_conf(groupconf)
Expand All @@ -54,14 +62,18 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too

nowdt = datetime.datetime.fromtimestamp(now, tzinfo)
key = (botuser, groupid)
if key in records:
eventtime, lastevents, lastmessage, lasttext, lastsuffix = records[key]
else:
eventtime = 0

if nowdt.hour == hour and not dow & 1 << nowdt.weekday():
if nowdt.hour == hour and not dow & 1 << nowdt.weekday() and (
not eventtime or startofhour > eventtime // 3600):
sendnew = True
eventtime = now
eventdt = nowdt
elif key in records:
elif eventtime:
sendnew = False
eventtime, lastevents, lastmessage, lasttext, lastsuffix = records[key]
eventdt = datetime.datetime.fromtimestamp(eventtime, tzinfo)
else:
continue
Expand Down Expand Up @@ -123,6 +135,7 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too
def reminder_send(bot, groupid, text, photo):
"""Send a message with the given photo + caption, falling back to plain text."""

logging.info('Sending reminder to %s.', groupid)
if photo:
try:
return bot.send_photo(chat_id=groupid,
Expand All @@ -146,6 +159,7 @@ def reminder_send(bot, groupid, text, photo):
def reminder_edit(bot, groupid, message_id, text, isphoto):
"""Edit a photo caption/plain message."""

logging.info('Editing reminder %s/%s.', groupid, message_id)
try:
if isphoto:
return bot.edit_message_caption(chat_id=groupid,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = 'metabot'
version = '0.3.20.2'
version = '0.3.21'
description = 'Modularized, multi-account bot.'
readme = 'README.md'
authors = [
Expand Down

0 comments on commit 30724f8

Please sign in to comment.