Scheduler: handle event persistence on plugin reload.

Write data to disk on unload; populate events dict with events which are
still scheduled on reload.
This commit is contained in:
Daniel Folkinshteyn 2010-09-01 16:37:55 -04:00 committed by Valentin Lorentz
parent 42efc79ef1
commit f6e3698c24
1 changed files with 20 additions and 11 deletions

View File

@ -66,20 +66,28 @@ 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
try: for name, event in eventdict.iteritems():
for name, event in eventdict.iteritems(): ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
ircobj = callbacks.ReplyIrcProxy(irc, event['msg']) try:
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']) event['time'], event['command'], n)
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']) 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.':
pass # we must be reloading the plugin # we must be reloading the plugin, event is still scheduled
else: self.log.info('Event %s already exists, adding to dict.' % (name,))
raise self.events[name] = event
else:
raise
def _flush(self): def _flush(self):
try: try:
@ -95,6 +103,7 @@ 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()
@ -107,9 +116,9 @@ 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): def _add(self, irc, msg, t, command, name=None):
f = self._makeCommandFunction(irc, msg, command) f = self._makeCommandFunction(irc, msg, command)
id = schedule.addEvent(f, t) id = schedule.addEvent(f, t, name)
f.eventId = id f.eventId = id
self.events[str(id)] = {'command':command, self.events[str(id)] = {'command':command,
'msg':msg, 'msg':msg,