From d627ba768340403763518bc78a9f56dbe78e6a70 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 6 May 2020 18:15:53 +0200 Subject: [PATCH] Scheduler: Make @list show period and time before next run. --- plugins/Scheduler/plugin.py | 21 +++++++++++++++------ plugins/Scheduler/test.py | 9 +++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/plugins/Scheduler/plugin.py b/plugins/Scheduler/plugin.py index 74483d3fb..cb3178b77 100644 --- a/plugins/Scheduler/plugin.py +++ b/plugins/Scheduler/plugin.py @@ -57,9 +57,9 @@ class Scheduler(callbacks.Plugin): self._restoreEvents(irc) world.flushers.append(self._flush) - def _getNextRunIn(self, first_run, now, period): + def _getNextRunIn(self, first_run, now, period, not_right_now=False): next_run_in = period - ((now - first_run) % period) - if next_run_in < 5: + if not_right_now and next_run_in < 5: # don't run immediatly, it might overwhelm the bot on # startup. next_run_in += period @@ -102,7 +102,7 @@ class Scheduler(callbacks.Plugin): # is 24hours, we want to keep running the command at the # same time of day. next_run_in = self._getNextRunIn( - first_run, now, event['time']) + first_run, now, event['time'], not_right_now=True) self._repeat(ircobj, event['msg'], name, event['time'], event['command'], first_run, next_run_in) @@ -235,9 +235,18 @@ class Scheduler(callbacks.Plugin): L = list(self.events.items()) if L: L.sort() - for (i, (name, command)) in enumerate(L): - L[i] = format('%s: %q', name, command['command']) - irc.reply(format('%L', L)) + replies = [] + now = time.time() + for (i, (name, event)) in enumerate(L): + if event['type'] == 'single': + replies.append(format('%s (in %T): %q', name, + event['time'] - now, event['command'])) + else: + next_run_in = self._getNextRunIn( + event['first_run'], now, event['time']) + replies.append(format('%s (every %T, next run in %T): %q', + name, event['time'], next_run_in, event['command'])) + irc.reply(format('%L', replies)) else: irc.reply(_('There are currently no scheduled commands.')) list = wrap(list) diff --git a/plugins/Scheduler/test.py b/plugins/Scheduler/test.py index fd92d1bea..d4da9c544 100644 --- a/plugins/Scheduler/test.py +++ b/plugins/Scheduler/test.py @@ -41,7 +41,9 @@ class SchedulerTestCase(ChannelPluginTestCase): def testAddRemove(self): self.assertRegexp('scheduler list', 'no.*commands') m = self.assertNotError('scheduler add 5 echo testAddRemove') - self.assertRegexp('scheduler list', 'echo testAddRemove') + self.assertResponse( + 'scheduler list', + '1 (in 4 seconds): "echo testAddRemove"') timeFastForward(2) self.assertNoResponse(' ', timeout=1) timeFastForward(2) @@ -77,7 +79,10 @@ class SchedulerTestCase(ChannelPluginTestCase): 'testRepeat') timeFastForward(5) self.assertResponse(' ', 'testRepeat') - self.assertResponse('scheduler list', 'repeater: "echo testRepeat"') + self.assertResponse( + 'scheduler list', + 'repeater (every 5 seconds, next run in 4 seconds): ' + '"echo testRepeat"') timeFastForward(3) self.assertNoResponse(' ', timeout=1) timeFastForward(2)