AppDeamon terminate() not being called

I also upgraded to AppDaemon 2.0.5 this morning, but I have not used the terminate() method before, so I’m not sure if this is a bug or I’m not using it correctly, but it doesn’t seem to be called - at least the self.log() entry isn’t printed.

Should this work?

    def terminate(self):
        # stop the thread
        self.log("stopping engine", level="DEBUG")
        engine.endLoop()

(and yes, I have enabled debug output)

1 Like

OK, I’ll have to have a look, this has been reported before - it does seem to work for me.

Can you post your entire App please?

My actual app has quite a few dependencies, but even this simple one doesn’t call terminate


import appdaemon.appapi as appapi

class TestApp(appapi.AppDaemon):

    def initialize(self):
        #
        self.log("initialize")
        self.run_every(self.period, self.datetime(), 20)

    def terminate(self):
        self.log("terminate")

    def period(self, kwargs):
        self.log("period")

Gives the output

2017-07-17 14:39:53.006769 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/testTerminate.py
2017-07-17 14:39:53.012019 INFO Loading Object testterm using class TestApp from module testTerminate
2017-07-17 14:39:53.014324 INFO testterm: initialize
2017-07-17 14:39:53.017460 INFO testterm: period
2017-07-17 14:40:13.007937 INFO testterm: period
2017-07-17 14:40:23.010088 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/testTerminate.py
2017-07-17 14:40:23.015696 INFO Loading Object testterm using class TestApp from module testTerminate
2017-07-17 14:40:23.017598 INFO testterm: initialize
2017-07-17 14:40:23.021086 INFO testterm: period
2017-07-17 14:40:39.004539 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/testTerminate.py
2017-07-17 14:40:39.011412 INFO Loading Object testterm using class TestApp from module testTerminate
2017-07-17 14:40:39.013863 INFO testterm: initialize
2017-07-17 14:40:39.017069 INFO testterm: period
2017-07-17 14:40:59.007902 INFO testterm: period
2017-07-17 14:41:19.007867 INFO testterm: period
2017-07-17 14:41:39.023020 INFO testterm: period

Just in case you need it

$ python3 --version
Python 3.5.2+

3 Likes

Ok, first let me say that that was a model way to report a bug - find an issue, provide a simple example with no other dependencies, provide full logs showing the issue.

As a result, I was able to reproduce this and fix it in 5 minutes - it will be in the next release, or is on the dev branch if you are feeling adventurous.

In general, if people make it this easy for me to find and fix an issue I am very happy to do so and will get to it a lot quicker :slight_smile:

4 Likes

Thanks - I’ve been on the other end too often, so I know what you need .:slight_smile:

1 Like

@aimc I knew I wasn’t doing anything crazy. :wink:

1 Like

You weren’t (and I wasn’t denigrating your version of the bug report) :slight_smile: I have a lot more time to look at these things now I am through the beta and now that work is less crazy so hopefully all bugfixes will now come through at the rapid pace you have come to expect!

2 Likes

HAHA only jesting. The work you have put into both AppDaemon and … that other app you maintain (which I admittedly have no idea/care about so far) is really intense. I’ve been away from this space for a bit but plan to start contributing again once my current project dies down. I really appreciate your dedication and work in this community. My little home is automated to the current level it is because of your work, and even the few simple things I’ve written have impressed everyone who’s seen them and made my life quite a bit easier. :slight_smile:

2 Likes

I have been running with the dev branch, and I think I have found a problem, after I had a syntax error in my app.

Whatever I do now I get the key error: 'tts' error on reloading the app, until I restart appdaemon.

This is the log file

2017-07-18 10:13:02.008177 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/tts.py
2017-07-18 10:13:02.009156 INFO Terminating Object tts
2017-07-18 10:13:02.013647 INFO tts: DEBUG - terminate
2017-07-18 10:13:02.015411 INFO tts: DEBUG - stopping engine
2017-07-18 10:13:02.017467 INFO tts: DEBUG - Ending Engine thread
2017-07-18 10:13:02.100335 WARNING Logged an error to /var/log/appdaemon/ha_apps_error.log
2017-07-18 10:13:19.010560 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/tts.py
2017-07-18 10:13:19.018781 WARNING Logged an error to /var/log/appdaemon/ha_apps_error.log
2017-07-18 10:15:10.006513 INFO Reloading Module: /home/homeassistant/.homeassistant/apps/tts.py
2017-07-18 10:15:10.009888 WARNING Logged an error to /var/log/appdaemon/ha_apps_error.log

and more significantly, this is the error log. tts is the name of my app

2017-07-18 10:13:02.042664 WARNING ------------------------------------------------------------
2017-07-18 10:13:02.044562 WARNING Unexpected error during loading of tts.py:
2017-07-18 10:13:02.045664 WARNING ------------------------------------------------------------
2017-07-18 10:13:02.086125 WARNING Traceback (most recent call last):
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 897, in read_app
    importlib.reload(conf.modules[module_name])
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/importlib/__init__.py", line 166, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 626, in _exec
  File "<frozen importlib._bootstrap_external>", line 669, in exec_module
  File "<frozen importlib._bootstrap_external>", line 775, in get_code
  File "<frozen importlib._bootstrap_external>", line 735, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/apps/tts.py", line 138
    except ValueError
                    ^
SyntaxError: invalid syntax

2017-07-18 10:13:02.100129 WARNING ------------------------------------------------------------
2017-07-18 10:13:19.013658 WARNING ------------------------------------------------------------
2017-07-18 10:13:19.015502 WARNING Unexpected error during loading of tts.py:
2017-07-18 10:13:19.016059 WARNING ------------------------------------------------------------
2017-07-18 10:13:19.018002 WARNING Traceback (most recent call last):
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 891, in read_app
    term_file(file)
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 549, in term_file
    term_object(key)
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 572, in term_object
    if hasattr(conf.objects[name]["object"], "terminate"):
KeyError: 'tts'

2017-07-18 10:13:19.018563 WARNING ------------------------------------------------------------
2017-07-18 10:15:10.007631 WARNING ------------------------------------------------------------
2017-07-18 10:15:10.007882 WARNING Unexpected error during loading of tts.py:
2017-07-18 10:15:10.008537 WARNING ------------------------------------------------------------
2017-07-18 10:15:10.009472 WARNING Traceback (most recent call last):
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 891, in read_app
    term_file(file)
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 549, in term_file
    term_object(key)
  File "/home/graham/.virtualenvs/homeassistant/lib/python3.5/site-packages/appdaemon/appdaemon.py", line 572, in term_object
    if hasattr(conf.objects[name]["object"], "terminate"):
KeyError: 'tts'

2017-07-18 10:15:10.009713 WARNING ------------------------------------------------------------

OK, that kind of makes sense. I removed a check that prevented that issue from occurring which had the side effect of breaking terminate() - ill work on a better fix sometime today :slight_smile:

OK, I pushed a fix to dev - give it a whirl and report back!

That is working fine now.
Thanks.

1 Like

Hi,

I am missing this function in the docs, is this ment to be a temporary available feature ? I just discovered it and have been able to use it (I think). In my case I am trying to make an appdaemon-app that listens to a websocket to receive data using a worker-thread just as @aimc has shown in his announce-app, I am using terminate() to remove the worker-thread. It seems to work fine but I want to be sure that this is a real feature instead of a- less documented - temporary option.

regards
Cor

EDIT: just see that its not mentioned in the API part of Appdaemon-docs but its mentioned in the “writing Appdaemon Apps” part instead !!

1 Like