Fixed so refurbished Scripts work normally. Added script.fire() method to fire the script on-demand, as suggested in #420. Also added as method remaining_repeats() to be able to get how many more times the script will fire.
This commit is contained in:
parent
64fc8f0b2a
commit
d6e6c12939
2 changed files with 35 additions and 21 deletions
|
|
@ -227,12 +227,23 @@ def format_script_list(scripts):
|
||||||
table.align = 'r'
|
table.align = 'r'
|
||||||
for script in scripts:
|
for script in scripts:
|
||||||
nextrep = script.time_until_next_repeat()
|
nextrep = script.time_until_next_repeat()
|
||||||
|
if nextrep is None:
|
||||||
|
nextrep = "PAUS" if script.db._paused_time else "--"
|
||||||
|
else:
|
||||||
|
nextrep = "%ss" % nextrep
|
||||||
|
|
||||||
|
maxrepeat = script.repeats
|
||||||
|
if maxrepeat:
|
||||||
|
rept = "%i/%i" % (maxrepeat - script.remaining_repeats(), maxrepeat)
|
||||||
|
else:
|
||||||
|
rept = "-/-"
|
||||||
|
|
||||||
table.add_row([script.id,
|
table.add_row([script.id,
|
||||||
script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>",
|
script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>",
|
||||||
script.key,
|
script.key,
|
||||||
script.interval if script.interval > 0 else "--",
|
script.interval if script.interval > 0 else "--",
|
||||||
"%ss" % nextrep if nextrep else "--",
|
nextrep,
|
||||||
"%i/%i" % (script.remaining_repeats(), script.repeats) if script.repeats else "--",
|
rept,
|
||||||
"*" if script.persistent else "-",
|
"*" if script.persistent else "-",
|
||||||
script.typeclass_path.rsplit('.', 1)[-1],
|
script.typeclass_path.rsplit('.', 1)[-1],
|
||||||
crop(script.desc, width=20)])
|
crop(script.desc, width=20)])
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ class ExtendedLoopingCall(LoopingCall):
|
||||||
self()
|
self()
|
||||||
else:
|
else:
|
||||||
if self.repeats is not None:
|
if self.repeats is not None:
|
||||||
# need to ignore the first reschedule
|
# need to compensate for the first reschedule
|
||||||
self.repeats += 1
|
self.repeats += 1
|
||||||
if start_delay is not None and start_delay >= 0:
|
if start_delay is not None and start_delay >= 0:
|
||||||
# we set start_delay after the _reshedule call to make
|
# we set start_delay after the _reshedule call to make
|
||||||
|
|
@ -82,23 +82,21 @@ class ExtendedLoopingCall(LoopingCall):
|
||||||
nulling start_delay and stopping if
|
nulling start_delay and stopping if
|
||||||
number of repeats is reached.
|
number of repeats is reached.
|
||||||
"""
|
"""
|
||||||
|
self.start_delay = None
|
||||||
if self.repeats is not None:
|
if self.repeats is not None:
|
||||||
self.repeats -= 1
|
self.repeats -= 1
|
||||||
self.start_delay = None
|
|
||||||
super(ExtendedLoopingCall, self)._reschedule()
|
super(ExtendedLoopingCall, self)._reschedule()
|
||||||
# we cannot kill the task here, it seems the
|
# for self.repeats <= 0, don't kill loop here; the callback
|
||||||
# callback will often not have time to be
|
# won't have time to be called in some situations. Kill
|
||||||
# triggered if we do. So kill from outside
|
# externally by checking task.repeats <= 0.
|
||||||
# by checking repeats <= 0.
|
|
||||||
#if self.repeats <= 0:
|
|
||||||
# self.stop()
|
|
||||||
|
|
||||||
def fire(self):
|
def fire(self):
|
||||||
"Force-fire the callback"
|
"Force-fire the callback"
|
||||||
assert self.running ("Tried to fire an ExtendedLoopingCall "
|
assert self.running, ("Tried to fire an ExtendedLoopingCall "
|
||||||
"that was not running.")
|
"that was not running.")
|
||||||
if self.call is not None:
|
if self.call is not None:
|
||||||
self.call.cancel()
|
self.call.cancel()
|
||||||
|
self._expectNextCallAt = self.clock.seconds()
|
||||||
self()
|
self()
|
||||||
|
|
||||||
def next_call_time(self):
|
def next_call_time(self):
|
||||||
|
|
@ -106,9 +104,10 @@ class ExtendedLoopingCall(LoopingCall):
|
||||||
Return the time in seconds until the next call. This takes
|
Return the time in seconds until the next call. This takes
|
||||||
start_delay into account.
|
start_delay into account.
|
||||||
"""
|
"""
|
||||||
currentTime = self.clock.seconds()
|
if self.running:
|
||||||
return self._expectNextCallAt - currentTime
|
currentTime = self.clock.seconds()
|
||||||
|
return self._expectNextCallAt - currentTime
|
||||||
|
return None
|
||||||
|
|
||||||
#
|
#
|
||||||
# Base script, inherit from Script below instead.
|
# Base script, inherit from Script below instead.
|
||||||
|
|
@ -139,15 +138,15 @@ class ScriptBase(TypeClass):
|
||||||
# the script was paused; restarting
|
# the script was paused; restarting
|
||||||
repeats = self.db._paused_repeats or self.dbobj.db_repeats
|
repeats = self.db._paused_repeats or self.dbobj.db_repeats
|
||||||
self.ndb._task.start(self.dbobj.db_interval,
|
self.ndb._task.start(self.dbobj.db_interval,
|
||||||
now=False,
|
now=False,
|
||||||
start_delay=self.ndb._paused_time,
|
start_delay=self.db._paused_time,
|
||||||
repeats=repeats)
|
repeats=repeats)
|
||||||
del self.db._paused_time
|
del self.db._paused_time
|
||||||
del self.db._paused_repeats
|
del self.db._paused_repeats
|
||||||
else:
|
else:
|
||||||
# starting script anew
|
# starting script anew
|
||||||
self.ndb._task.start(self.dbobj.db_interval,
|
self.ndb._task.start(self.dbobj.db_interval,
|
||||||
now=self.dbobj.db_start_delay,
|
now=not self.dbobj.db_start_delay,
|
||||||
repeats=self.dbobj.db_repeats)
|
repeats=self.dbobj.db_repeats)
|
||||||
|
|
||||||
def _stop_task(self):
|
def _stop_task(self):
|
||||||
|
|
@ -181,6 +180,7 @@ class ScriptBase(TypeClass):
|
||||||
# check repeats
|
# check repeats
|
||||||
repeats = self.ndb._task.repeats
|
repeats = self.ndb._task.repeats
|
||||||
if repeats is not None and repeats <= 0:
|
if repeats is not None and repeats <= 0:
|
||||||
|
print "stopping script!"
|
||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
def _step_task(self):
|
def _step_task(self):
|
||||||
|
|
@ -202,7 +202,10 @@ class ScriptBase(TypeClass):
|
||||||
"""
|
"""
|
||||||
task = self.ndb._task
|
task = self.ndb._task
|
||||||
if task:
|
if task:
|
||||||
return int(task.next_call_time())
|
try:
|
||||||
|
return int(task.next_call_time())
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def remaining_repeats(self):
|
def remaining_repeats(self):
|
||||||
|
|
@ -284,8 +287,8 @@ class ScriptBase(TypeClass):
|
||||||
"""
|
"""
|
||||||
This stops a running script and stores its active state.
|
This stops a running script and stores its active state.
|
||||||
"""
|
"""
|
||||||
#print "pausing", self.key, self.time_until_next_repeat()
|
|
||||||
task = self.ndb._task
|
task = self.ndb._task
|
||||||
|
print "pausing", self.key, self.time_until_next_repeat()
|
||||||
if task:
|
if task:
|
||||||
self.db._paused_time = task.next_call_time()
|
self.db._paused_time = task.next_call_time()
|
||||||
self.db._paused_repeats = task.repeats
|
self.db._paused_repeats = task.repeats
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue