Scheduler: Preserve network across restarts.

This commit is contained in:
Valentin Lorentz 2020-05-12 18:32:37 +02:00
parent 27edb4f6c9
commit a9eb31f1b5
1 changed files with 20 additions and 14 deletions

View File

@ -79,7 +79,9 @@ class Scheduler(callbacks.Plugin):
self.log.debug('Unable to open pickle file: %s', e)
return
for name, event in eventdict.items():
ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
# old DBs don't have the "network", let's take the current network
# instead.
network = event.get('network', irc.network)
try:
if event['type'] == 'single': # non-repeating event
n = None
@ -88,8 +90,7 @@ class Scheduler(callbacks.Plugin):
# though we'll never know for sure, because other
# plugins can schedule stuff, too.
n = int(name)
self._add(ircobj, event['msg'],
event['time'], event['command'], n)
self._add(network, event['msg'], event['time'], event['command'], n)
elif event['type'] == 'repeat': # repeating event
now = time.time()
first_run = event.get('first_run')
@ -104,7 +105,7 @@ class Scheduler(callbacks.Plugin):
next_run_in = self._getNextRunIn(
first_run, now, event['time'], not_right_now=True)
self._repeat(ircobj, event['msg'], name,
self._repeat(network, event['msg'], name,
event['time'], event['command'], first_run, next_run_in)
except AssertionError as e:
if str(e) == 'An event with the same name has already been scheduled.':
@ -132,22 +133,25 @@ class Scheduler(callbacks.Plugin):
world.flushers.remove(self._flush)
self.__parent.die()
def _makeCommandFunction(self, irc, msg, command, remove=True):
def _makeCommandFunction(self, network, msg, command, remove=True):
"""Makes a function suitable for scheduling from command."""
tokens = callbacks.tokenize(command,
channel=msg.channel, network=irc.network)
def f():
# If the network isn't available, pick any other one
irc = world.getIrc(network) or world.ircs[0]
tokens = callbacks.tokenize(command,
channel=msg.channel, network=irc.network)
if remove:
del self.events[str(f.eventId)]
self.Proxy(irc.irc, msg, tokens)
self.Proxy(irc, msg, tokens)
return f
def _add(self, irc, msg, t, command, name=None):
f = self._makeCommandFunction(irc, msg, command)
def _add(self, network, msg, t, command, name=None):
f = self._makeCommandFunction(network, msg, command)
id = schedule.addEvent(f, t, name)
f.eventId = id
self.events[str(id)] = {'command':command,
'msg':msg,
'network': network,
'time':t,
'type':'single'}
return id
@ -163,7 +167,7 @@ class Scheduler(callbacks.Plugin):
echo). Do pay attention to the quotes in that example.
"""
t = time.time() + seconds
id = self._add(irc, msg, t, command)
id = self._add(irc.network, msg, t, command)
irc.replySuccess(format(_('Event #%i added.'), id))
add = wrap(add, ['positiveInt', 'text'])
@ -188,14 +192,15 @@ class Scheduler(callbacks.Plugin):
irc.error(_('Invalid event id.'))
remove = wrap(remove, ['lowered'])
def _repeat(self, irc, msg, name, seconds, command, first_run, next_run_in):
f = self._makeCommandFunction(irc, msg, command, remove=False)
def _repeat(self, network, msg, name, seconds, command, first_run, next_run_in):
f = self._makeCommandFunction(network, msg, command, remove=False)
f_wrapper = schedule.schedule.makePeriodicWrapper(f, seconds, name)
assert first_run is not None
id = schedule.addEvent(f_wrapper, time.time() + next_run_in, name)
assert id == name
self.events[name] = {'command':command,
'msg':msg,
'network': network,
'time':seconds,
'type':'repeat',
'first_run': first_run,
@ -218,7 +223,8 @@ class Scheduler(callbacks.Plugin):
'choose another name.'), Raise=True)
next_run_in = opts.get('delay', 0)
first_run = time.time() + next_run_in
self._repeat(irc, msg, name, seconds, command, first_run, next_run_in)
self._repeat(
irc.network, msg, name, seconds, command, first_run, next_run_in)
# We don't reply because the command runs immediately.
# But should we? What if the command doesn't have visible output?
# irc.replySuccess()