Handle case of script.at_repeat() immediately calling stop(). Resolves #2061.
This commit is contained in:
parent
fda51edea6
commit
edcd06d531
3 changed files with 21 additions and 10 deletions
|
|
@ -3052,7 +3052,8 @@ class CmdScript(COMMAND_DEFAULT_CLASS):
|
||||||
ok = obj.scripts.add(self.rhs, autostart=True)
|
ok = obj.scripts.add(self.rhs, autostart=True)
|
||||||
if not ok:
|
if not ok:
|
||||||
result.append(
|
result.append(
|
||||||
"\nScript %s could not be added and/or started on %s."
|
"\nScript %s could not be added and/or started on %s "
|
||||||
|
"(or it started and immediately shut down)."
|
||||||
% (self.rhs, obj.get_display_name(caller))
|
% (self.rhs, obj.get_display_name(caller))
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -78,11 +78,18 @@ class ScriptHandler(object):
|
||||||
scriptclass, key=key, account=self.obj, autostart=autostart
|
scriptclass, key=key, account=self.obj, autostart=autostart
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# the normal - adding to an Object
|
# the normal - adding to an Object. We wait to autostart so we can differentiate
|
||||||
script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=autostart)
|
# a failing creation from a script that immediately starts/stops.
|
||||||
|
script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=False)
|
||||||
if not script:
|
if not script:
|
||||||
logger.log_err("Script %s could not be created and/or started." % scriptclass)
|
logger.log_err("Script %s failed to be created/started." % scriptclass)
|
||||||
return False
|
return False
|
||||||
|
if autostart:
|
||||||
|
script.start()
|
||||||
|
if not script.id:
|
||||||
|
# this can happen if the script has repeats=1 or calls stop() in at_repeat.
|
||||||
|
logger.log_info("Script %s started and then immediately stopped; "
|
||||||
|
"it could probably be a normal function." % scriptclass)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def start(self, key):
|
def start(self, key):
|
||||||
|
|
|
||||||
|
|
@ -221,10 +221,13 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase):
|
||||||
self.at_repeat()
|
self.at_repeat()
|
||||||
|
|
||||||
# check repeats
|
# check repeats
|
||||||
callcount = self.ndb._task.callcount
|
if self.ndb._task:
|
||||||
maxcount = self.db_repeats
|
# we need to check for the task in case stop() was called
|
||||||
if maxcount > 0 and maxcount <= callcount:
|
# inside at_repeat() and it already went away.
|
||||||
self.stop()
|
callcount = self.ndb._task.callcount
|
||||||
|
maxcount = self.db_repeats
|
||||||
|
if maxcount > 0 and maxcount <= callcount:
|
||||||
|
self.stop()
|
||||||
|
|
||||||
def _step_task(self):
|
def _step_task(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -342,9 +345,9 @@ class DefaultScript(ScriptBase):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = create.create_script(**kwargs)
|
obj = create.create_script(**kwargs)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
|
logger.log_trace()
|
||||||
errors.append("The script '%s' encountered errors and could not be created." % key)
|
errors.append("The script '%s' encountered errors and could not be created." % key)
|
||||||
logger.log_err(e)
|
|
||||||
|
|
||||||
return obj, errors
|
return obj, errors
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue