Handle case of script.at_repeat() immediately calling stop(). Resolves #2061.

This commit is contained in:
Griatch 2020-02-29 12:24:26 +01:00
parent fda51edea6
commit edcd06d531
3 changed files with 21 additions and 10 deletions

View file

@ -3052,7 +3052,8 @@ class CmdScript(COMMAND_DEFAULT_CLASS):
ok = obj.scripts.add(self.rhs, autostart=True)
if not ok:
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))
)
else:

View file

@ -78,11 +78,18 @@ class ScriptHandler(object):
scriptclass, key=key, account=self.obj, autostart=autostart
)
else:
# the normal - adding to an Object
script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=autostart)
# the normal - adding to an Object. We wait to autostart so we can differentiate
# 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:
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
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
def start(self, key):

View file

@ -221,10 +221,13 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase):
self.at_repeat()
# check repeats
callcount = self.ndb._task.callcount
maxcount = self.db_repeats
if maxcount > 0 and maxcount <= callcount:
self.stop()
if self.ndb._task:
# we need to check for the task in case stop() was called
# inside at_repeat() and it already went away.
callcount = self.ndb._task.callcount
maxcount = self.db_repeats
if maxcount > 0 and maxcount <= callcount:
self.stop()
def _step_task(self):
"""
@ -342,9 +345,9 @@ class DefaultScript(ScriptBase):
try:
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)
logger.log_err(e)
return obj, errors