From ef3787d8342f0babf8c5f0dfa7d5a00ae3f2810f Mon Sep 17 00:00:00 2001 From: T-101 Date: Tue, 26 Sep 2023 16:20:17 +0300 Subject: [PATCH 1/2] add daily events --- plugins/Scheduler/plugin.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/plugins/Scheduler/plugin.py b/plugins/Scheduler/plugin.py index 91d4ca907..0ca66bb9e 100644 --- a/plugins/Scheduler/plugin.py +++ b/plugins/Scheduler/plugin.py @@ -33,6 +33,7 @@ import math import shutil import tempfile +import datetime import supybot.conf as conf import supybot.utils as utils @@ -272,6 +273,36 @@ def repeat(self, irc, msg, args, optlist, name, seconds, command): getopts({'delay': 'positiveInt'}), 'nonInt', 'positiveInt', 'text']) + @internationalizeDocstring + def daily(self, irc, msg, args, name, time_of_day, command): + """ + + Schedules a command to run daily at a given + time . The format of is HH:MM:SS + """ + name = name.lower() + if name in self.events: + irc.error(_('There is already an event with that name, please ' + 'choose another name.'), Raise=True) + epoch = int(time.time()) + seconds_until_next_run = 0 + while True: + dt = datetime.datetime.fromtimestamp(epoch + seconds_until_next_run) + try: + hours, minutes, seconds = map(int, time_of_day.split(":")) + except ValueError: + irc.error(_('Unable to parse time_of_day'), Raise=True) + if dt.hour == hours and dt.minute == minutes and dt.second == seconds: + break + seconds_until_next_run += 1 + + self._repeat(irc.network, msg, name, 60 * 60 * 24, command, + epoch + seconds_until_next_run, seconds_until_next_run) + # Reply status since the command is run at a later time + irc.replySuccess() + + daily = wrap(daily, ['nonInt', 'nonInt', 'text']) + @internationalizeDocstring def list(self, irc, msg, args): """takes no arguments From 466941cf3f9d12200b24c99ce2ceb93f7985c45e Mon Sep 17 00:00:00 2001 From: T-101 Date: Tue, 26 Sep 2023 16:20:41 +0300 Subject: [PATCH 2/2] tests for daily events --- plugins/Scheduler/test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/plugins/Scheduler/test.py b/plugins/Scheduler/test.py index a01069173..ef958b5c0 100644 --- a/plugins/Scheduler/test.py +++ b/plugins/Scheduler/test.py @@ -29,6 +29,8 @@ # POSSIBILITY OF SUCH DAMAGE. ### +import datetime + from supybot.test import * import supybot.schedule as schedule @@ -107,6 +109,29 @@ def testRepeat(self): timeFastForward(5) self.assertNoResponse(' ', timeout=1) + # This test fails if run later + def test01Daily(self): + self.assertRegexp('scheduler list', 'no.*commands') + dt = datetime.datetime.now() + datetime.timedelta(seconds=10) + event_time = "{}:{}:{}".format(dt.hour, dt.minute, dt.second) + + self.assertRegexp('scheduler daily dailytask {} echo testDaily'.format(event_time), + 'The operation succeeded.') + + self.assertNoResponse(' ', timeout=1) + timeFastForward(10) + self.assertResponse(' ', 'testDaily') + + timeFastForward(60 * 60 * 24 - 2) # Two seconds before event time on the following day + self.assertNoResponse(' ', timeout=1) + timeFastForward(2) + self.assertResponse(' ', 'testDaily') + + timeFastForward(60 * 60 * 12) # Sanity checking that future events also work + self.assertNoResponse(' ', timeout=1) + timeFastForward(60 * 60 * 12) + self.assertResponse(' ', 'testDaily') + def testRepeatDelay(self): self.assertNoResponse( 'scheduler repeat --delay 5 repeater 20 echo testRepeat',