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:
Daniel Folkinshteyn 2010-09-01 02:02:47 -04:00
parent cf6fcb9928
commit e5e9cbba69

View File

@ -28,18 +28,73 @@
### ###
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
from supybot.commands import * from supybot.commands import *
import supybot.schedule as schedule import supybot.schedule as schedule
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
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):
self.__parent = super(Scheduler, self) self.__parent = super(Scheduler, self)
self.__parent.__init__(irc) self.__parent.__init__(irc)
self.events = {} self.events = {}
self._restoreEvents(irc)
world.flushers.append(self._flush)
def _restoreEvents(self, irc):
try:
pkl = open(filename, 'rb')
try:
eventdict = pickle.load(pkl)
except Exception, e:
self.log.debug('Unable to load pickled data: %s', e)
return
finally:
pkl.close()
except IOError, e:
self.log.debug('Unable to open pickle file: %s', e)
return
try:
for name, event in eventdict.iteritems():
ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
if event['type'] == 'single': # non-repeating event
self._add(ircobj, event['msg'],
event['time'], event['command'])
elif event['type'] == 'repeat': # repeating event
self._repeat(ircobj, event['msg'], name,
event['time'], event['command'])
except AssertionError, e:
if str(e) == 'An event with the same name has already been scheduled.':
pass # we must be reloading the plugin
else:
raise
def _flush(self):
try:
pklfd, tempfn = tempfile.mkstemp(suffix='scheduler', dir=datadir)
pkl = os.fdopen(pklfd, 'wb')
try:
pickle.dump(self.events, pkl)
except Exception, e:
self.log.warning('Unable to store pickled data: %s', e)
pkl.close()
shutil.move(tempfn, filename)
except (IOError, shutil.Error), e:
self.log.warning('File error: %s', e)
def die(self):
world.flushers.remove(self._flush)
self.__parent.die()
def _makeCommandFunction(self, irc, msg, command, remove=True): def _makeCommandFunction(self, irc, msg, command, remove=True):
"""Makes a function suitable for scheduling from command.""" """Makes a function suitable for scheduling from command."""
@ -52,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
def add(self, irc, msg, args, seconds, command): def add(self, irc, msg, args, seconds, command):
"""<seconds> <command> """<seconds> <command>
@ -61,10 +126,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'])
@ -88,6 +151,15 @@ 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):
f = self._makeCommandFunction(irc, msg, command, remove=False)
id = schedule.addPeriodicEvent(f, seconds, name)
assert id == name
self.events[name] = {'command':command,
'msg':msg,
'time':seconds,
'type':'repeat'}
def repeat(self, irc, msg, args, name, seconds, command): def repeat(self, irc, msg, args, name, seconds, command):
"""<name> <seconds> <command> """<name> <seconds> <command>
@ -100,10 +172,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()
@ -118,8 +187,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)