mirror of
https://github.com/progval/Limnoria.git
synced 2025-05-03 00:41:11 -05:00
Scheduler: Preserve network across restarts.
This commit is contained in:
parent
27edb4f6c9
commit
a9eb31f1b5
@ -79,7 +79,9 @@ class Scheduler(callbacks.Plugin):
|
|||||||
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.items():
|
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:
|
try:
|
||||||
if event['type'] == 'single': # non-repeating event
|
if event['type'] == 'single': # non-repeating event
|
||||||
n = None
|
n = None
|
||||||
@ -88,8 +90,7 @@ class Scheduler(callbacks.Plugin):
|
|||||||
# though we'll never know for sure, because other
|
# though we'll never know for sure, because other
|
||||||
# plugins can schedule stuff, too.
|
# plugins can schedule stuff, too.
|
||||||
n = int(name)
|
n = int(name)
|
||||||
self._add(ircobj, event['msg'],
|
self._add(network, event['msg'], event['time'], event['command'], n)
|
||||||
event['time'], event['command'], n)
|
|
||||||
elif event['type'] == 'repeat': # repeating event
|
elif event['type'] == 'repeat': # repeating event
|
||||||
now = time.time()
|
now = time.time()
|
||||||
first_run = event.get('first_run')
|
first_run = event.get('first_run')
|
||||||
@ -104,7 +105,7 @@ class Scheduler(callbacks.Plugin):
|
|||||||
next_run_in = self._getNextRunIn(
|
next_run_in = self._getNextRunIn(
|
||||||
first_run, now, event['time'], not_right_now=True)
|
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)
|
event['time'], event['command'], first_run, next_run_in)
|
||||||
except AssertionError as e:
|
except AssertionError as 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.':
|
||||||
@ -132,22 +133,25 @@ class Scheduler(callbacks.Plugin):
|
|||||||
world.flushers.remove(self._flush)
|
world.flushers.remove(self._flush)
|
||||||
self.__parent.die()
|
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."""
|
"""Makes a function suitable for scheduling from command."""
|
||||||
tokens = callbacks.tokenize(command,
|
|
||||||
channel=msg.channel, network=irc.network)
|
|
||||||
def f():
|
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:
|
if remove:
|
||||||
del self.events[str(f.eventId)]
|
del self.events[str(f.eventId)]
|
||||||
self.Proxy(irc.irc, msg, tokens)
|
self.Proxy(irc, msg, tokens)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
def _add(self, irc, msg, t, command, name=None):
|
def _add(self, network, msg, t, command, name=None):
|
||||||
f = self._makeCommandFunction(irc, msg, command)
|
f = self._makeCommandFunction(network, msg, command)
|
||||||
id = schedule.addEvent(f, t, name)
|
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,
|
||||||
|
'network': network,
|
||||||
'time':t,
|
'time':t,
|
||||||
'type':'single'}
|
'type':'single'}
|
||||||
return id
|
return id
|
||||||
@ -163,7 +167,7 @@ class Scheduler(callbacks.Plugin):
|
|||||||
echo). Do pay attention to the quotes in that example.
|
echo). Do pay attention to the quotes in that example.
|
||||||
"""
|
"""
|
||||||
t = time.time() + seconds
|
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))
|
irc.replySuccess(format(_('Event #%i added.'), id))
|
||||||
add = wrap(add, ['positiveInt', 'text'])
|
add = wrap(add, ['positiveInt', 'text'])
|
||||||
|
|
||||||
@ -188,14 +192,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, first_run, next_run_in):
|
def _repeat(self, network, msg, name, seconds, command, first_run, next_run_in):
|
||||||
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
f = self._makeCommandFunction(network, msg, command, remove=False)
|
||||||
f_wrapper = schedule.schedule.makePeriodicWrapper(f, seconds, name)
|
f_wrapper = schedule.schedule.makePeriodicWrapper(f, seconds, name)
|
||||||
assert first_run is not None
|
assert first_run is not None
|
||||||
id = schedule.addEvent(f_wrapper, time.time() + next_run_in, name)
|
id = schedule.addEvent(f_wrapper, time.time() + next_run_in, name)
|
||||||
assert id == name
|
assert id == name
|
||||||
self.events[name] = {'command':command,
|
self.events[name] = {'command':command,
|
||||||
'msg':msg,
|
'msg':msg,
|
||||||
|
'network': network,
|
||||||
'time':seconds,
|
'time':seconds,
|
||||||
'type':'repeat',
|
'type':'repeat',
|
||||||
'first_run': first_run,
|
'first_run': first_run,
|
||||||
@ -218,7 +223,8 @@ class Scheduler(callbacks.Plugin):
|
|||||||
'choose another name.'), Raise=True)
|
'choose another name.'), Raise=True)
|
||||||
next_run_in = opts.get('delay', 0)
|
next_run_in = opts.get('delay', 0)
|
||||||
first_run = time.time() + next_run_in
|
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.
|
# 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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user