Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler: add daily events #1558

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions plugins/Scheduler/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import math
import shutil
import tempfile
import datetime

import supybot.conf as conf
import supybot.utils as utils
Expand Down Expand Up @@ -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):
""" <name> <time_of_day> <command>

Schedules a command <command> to run daily at a given
time <time_of_day>. The format of <time_of_day> 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
Expand Down
25 changes: 25 additions & 0 deletions plugins/Scheduler/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# POSSIBILITY OF SUCH DAMAGE.
###

import datetime

from supybot.test import *

import supybot.schedule as schedule
Expand Down Expand Up @@ -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',
Expand Down
Loading