mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 19:22:45 +01:00
Scheduler: add scheduled task persistence.
The list of tasks scheduled with the Scheduler plugin is now saved on exit, and restored upon restart. Previously all scheduled tasks would be forgotten upon bot restart, which was undesirable behavior.
This commit is contained in:
parent
2d265f9d82
commit
42efc79ef1
@ -28,6 +28,10 @@
|
|||||||
###
|
###
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
import cPickle as pickle
|
||||||
|
|
||||||
import supybot.conf as conf
|
import supybot.conf as conf
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
@ -36,6 +40,10 @@ import supybot.schedule as schedule
|
|||||||
import supybot.callbacks as callbacks
|
import supybot.callbacks as callbacks
|
||||||
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
||||||
_ = PluginInternationalization('Scheduler')
|
_ = PluginInternationalization('Scheduler')
|
||||||
|
import supybot.world as world
|
||||||
|
|
||||||
|
datadir = conf.supybot.directories.data()
|
||||||
|
filename = conf.supybot.directories.data.dirize('Scheduler.pickle')
|
||||||
|
|
||||||
class Scheduler(callbacks.Plugin):
|
class Scheduler(callbacks.Plugin):
|
||||||
def __init__(self, irc):
|
def __init__(self, irc):
|
||||||
@ -58,28 +66,20 @@ class Scheduler(callbacks.Plugin):
|
|||||||
except IOError, e:
|
except IOError, e:
|
||||||
self.log.debug('Unable to open pickle file: %s', e)
|
self.log.debug('Unable to open pickle file: %s', e)
|
||||||
return
|
return
|
||||||
for name, event in eventdict.iteritems():
|
try:
|
||||||
ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
|
for name, event in eventdict.iteritems():
|
||||||
try:
|
ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
|
||||||
if event['type'] == 'single': # non-repeating event
|
if event['type'] == 'single': # non-repeating event
|
||||||
n = None
|
|
||||||
if schedule.schedule.counter > int(name):
|
|
||||||
# counter not reset, we're probably reloading the plugin
|
|
||||||
# though we'll never know for sure, because other
|
|
||||||
# plugins can schedule stuff, too.
|
|
||||||
n = int(name)
|
|
||||||
self._add(ircobj, event['msg'],
|
self._add(ircobj, event['msg'],
|
||||||
event['time'], event['command'], n)
|
event['time'], event['command'])
|
||||||
elif event['type'] == 'repeat': # repeating event
|
elif event['type'] == 'repeat': # repeating event
|
||||||
self._repeat(ircobj, event['msg'], name,
|
self._repeat(ircobj, event['msg'], name,
|
||||||
event['time'], event['command'], False)
|
event['time'], event['command'])
|
||||||
except AssertionError, e:
|
except AssertionError, e:
|
||||||
if str(e) == 'An event with the same name has already been scheduled.':
|
if str(e) == 'An event with the same name has already been scheduled.':
|
||||||
# we must be reloading the plugin, event is still scheduled
|
pass # we must be reloading the plugin
|
||||||
self.log.info('Event %s already exists, adding to dict.' % (name,))
|
else:
|
||||||
self.events[name] = event
|
raise
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def _flush(self):
|
def _flush(self):
|
||||||
try:
|
try:
|
||||||
@ -95,7 +95,6 @@ class Scheduler(callbacks.Plugin):
|
|||||||
self.log.warning('File error: %s', e)
|
self.log.warning('File error: %s', e)
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
self._flush()
|
|
||||||
world.flushers.remove(self._flush)
|
world.flushers.remove(self._flush)
|
||||||
self.__parent.die()
|
self.__parent.die()
|
||||||
|
|
||||||
@ -108,6 +107,16 @@ class Scheduler(callbacks.Plugin):
|
|||||||
self.Proxy(irc.irc, msg, tokens)
|
self.Proxy(irc.irc, msg, tokens)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
def _add(self, irc, msg, t, command):
|
||||||
|
f = self._makeCommandFunction(irc, msg, command)
|
||||||
|
id = schedule.addEvent(f, t)
|
||||||
|
f.eventId = id
|
||||||
|
self.events[str(id)] = {'command':command,
|
||||||
|
'msg':msg,
|
||||||
|
'time':t,
|
||||||
|
'type':'single'}
|
||||||
|
return id
|
||||||
|
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
def add(self, irc, msg, args, seconds, command):
|
def add(self, irc, msg, args, seconds, command):
|
||||||
"""<seconds> <command>
|
"""<seconds> <command>
|
||||||
@ -118,10 +127,8 @@ class Scheduler(callbacks.Plugin):
|
|||||||
command was given in (with no prefixed nick, a consequence of using
|
command was given in (with no prefixed nick, a consequence of using
|
||||||
echo). Do pay attention to the quotes in that example.
|
echo). Do pay attention to the quotes in that example.
|
||||||
"""
|
"""
|
||||||
f = self._makeCommandFunction(irc, msg, command)
|
t = time.time() + seconds
|
||||||
id = schedule.addEvent(f, time.time() + seconds)
|
id = self._add(irc, msg, t, command)
|
||||||
f.eventId = id
|
|
||||||
self.events[str(id)] = command
|
|
||||||
irc.replySuccess(format(_('Event #%i added.'), id))
|
irc.replySuccess(format(_('Event #%i added.'), id))
|
||||||
add = wrap(add, ['positiveInt', 'text'])
|
add = wrap(add, ['positiveInt', 'text'])
|
||||||
|
|
||||||
@ -146,9 +153,9 @@ class Scheduler(callbacks.Plugin):
|
|||||||
irc.error(_('Invalid event id.'))
|
irc.error(_('Invalid event id.'))
|
||||||
remove = wrap(remove, ['lowered'])
|
remove = wrap(remove, ['lowered'])
|
||||||
|
|
||||||
def _repeat(self, irc, msg, name, seconds, command, now=True):
|
def _repeat(self, irc, msg, name, seconds, command):
|
||||||
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
||||||
id = schedule.addPeriodicEvent(f, seconds, name, now)
|
id = schedule.addPeriodicEvent(f, seconds, name)
|
||||||
assert id == name
|
assert id == name
|
||||||
self.events[name] = {'command':command,
|
self.events[name] = {'command':command,
|
||||||
'msg':msg,
|
'msg':msg,
|
||||||
@ -168,10 +175,7 @@ class Scheduler(callbacks.Plugin):
|
|||||||
if name in self.events:
|
if name in self.events:
|
||||||
irc.error(_('There is already an event with that name, please '
|
irc.error(_('There is already an event with that name, please '
|
||||||
'choose another name.'), Raise=True)
|
'choose another name.'), Raise=True)
|
||||||
self.events[name] = command
|
self._repeat(irc, msg, name, seconds, command)
|
||||||
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
|
||||||
id = schedule.addPeriodicEvent(f, seconds, name)
|
|
||||||
assert id == name
|
|
||||||
# We don't reply because the command runs immediately.
|
# We don't reply because the command runs immediately.
|
||||||
# But should we? What if the command doesn't have visible output?
|
# But should we? What if the command doesn't have visible output?
|
||||||
# irc.replySuccess()
|
# irc.replySuccess()
|
||||||
@ -187,8 +191,11 @@ class Scheduler(callbacks.Plugin):
|
|||||||
if L:
|
if L:
|
||||||
L.sort()
|
L.sort()
|
||||||
for (i, (name, command)) in enumerate(L):
|
for (i, (name, command)) in enumerate(L):
|
||||||
L[i] = format('%s: %q', name, command)
|
L[i] = format('%s: %q', name, command['command'])
|
||||||
irc.reply(format('%L', L))
|
irc.reply(format('%L', L))
|
||||||
|
irc.reply(schedule.schedule.schedule)
|
||||||
|
irc.reply(schedule.schedule.events)
|
||||||
|
irc.reply(schedule.schedule.counter)
|
||||||
else:
|
else:
|
||||||
irc.reply(_('There are currently no scheduled commands.'))
|
irc.reply(_('There are currently no scheduled commands.'))
|
||||||
list = wrap(list)
|
list = wrap(list)
|
||||||
|
Loading…
Reference in New Issue
Block a user