ualex73 / monitor_docker

Monitor Docker containers from Home Assistant
Apache License 2.0
301 stars 37 forks source link

Add reload capability #124

Closed derekoharrow closed 4 months ago

derekoharrow commented 1 year ago

Could you please add the ability to "reload" this integration.

This will help alleviate the issue caused by #104 and avoid the need to have to re-start Home Assistant to get around containers no longer being available.

Thanks!

ualex73 commented 6 months ago

Can you test the latest version, I believe it is fixed in this one. If confirmed, I will make a new version.

yuejon commented 4 months ago

This is still an issue with the current version. The only way for me to get the integration to recognize containers that have been recreated is to restart HASS.

airdrummingfool commented 4 months ago

I am still seeing errors in the HA logs after recreating containers. Here's the relevant log entries from HA after upgrading ZWave-JS-UI using Docker Compose (docker compose pull; docker compose up -d).

Logs

``` 2024-06-26 10:53:14.503 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] c63fce3e018f_config-zwave-js-ui-1: Container not available anymore (2) (DockerError(404, 'No such container: c63fce3e018f_config-zwave-js-ui-1')) Traceback (most recent call last): File "/config/custom_components/monitor_docker/helpers.py", line 752, in _initGetContainer self._container = await self._api.containers.get(self._name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 95, in get data = await self.docker._query_json( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json async with self._query( File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__ resp = await self._coro ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query raise DockerError(response.status, json.loads(what.decode("utf8"))) aiodocker.exceptions.DockerError: DockerError(404, 'No such container: c63fce3e018f_config-zwave-js-ui-1') 2024-06-26 10:53:14.510 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] c63fce3e018f_config-zwave-js-ui-1: Problem during start of monitoring 2024-06-26 10:53:25.608 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] config-zwave-js-ui-1: Container not available anymore (3) (DockerError(404, 'No such container: c63fce3e018feb571a52779e1360a0d7f8056f94e89c5dd4668a29ad43453d55')) Traceback (most recent call last): File "/config/custom_components/monitor_docker/helpers.py", line 775, in _run await self._run_container_info() File "/config/custom_components/monitor_docker/helpers.py", line 818, in _run_container_info raw: dict = await self._container.show() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 177, in show data = await self.docker._query_json( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json async with self._query( File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__ resp = await self._coro ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query raise DockerError(response.status, json.loads(what.decode("utf8"))) aiodocker.exceptions.DockerError: DockerError(404, 'No such container: c63fce3e018feb571a52779e1360a0d7f8056f94e89c5dd4668a29ad43453d55') ```

However, it looks like the container info is being updated correctly - for example, the CPU and memory percentage numbers are fluctuating and look to be correct when compared to docker compose stats zwave-js-ui.

image

Small edit/update: it seems like monitor_docker might be deleting and re-creating the relevant entities each time the container is recreated. That's not ideal, but it doesn't cause any issues in my use-case.

ualex73 commented 4 months ago

I am still seeing errors in the HA logs after recreating containers. Here's the relevant log entries from HA after upgrading ZWave-JS-UI using Docker Compose (docker compose pull; docker compose up -d).

Logs

2024-06-26 10:53:14.503 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] c63fce3e018f_config-zwave-js-ui-1: Container not available anymore (2) (DockerError(404, 'No such container: c63fce3e018f_config-zwave-js-ui-1'))
Traceback (most recent call last):
File "/config/custom_components/monitor_docker/helpers.py", line 752, in _initGetContainer
self._container = await self._api.containers.get(self._name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 95, in get
data = await self.docker._query_json(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json
async with self._query(
File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__
resp = await self._coro
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query
raise DockerError(response.status, json.loads(what.decode("utf8")))
aiodocker.exceptions.DockerError: DockerError(404, 'No such container: c63fce3e018f_config-zwave-js-ui-1')
2024-06-26 10:53:14.510 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] c63fce3e018f_config-zwave-js-ui-1: Problem during start of monitoring
2024-06-26 10:53:25.608 ERROR (MainThread) [custom_components.monitor_docker.helpers] [Docker] config-zwave-js-ui-1: Container not available anymore (3) (DockerError(404, 'No such container: c63fce3e018feb571a52779e1360a0d7f8056f94e89c5dd4668a29ad43453d55'))
Traceback (most recent call last):
File "/config/custom_components/monitor_docker/helpers.py", line 775, in _run
await self._run_container_info()
File "/config/custom_components/monitor_docker/helpers.py", line 818, in _run_container_info
raw: dict = await self._container.show()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 177, in show
data = await self.docker._query_json(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json
async with self._query(
File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__
resp = await self._coro
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query
raise DockerError(response.status, json.loads(what.decode("utf8")))
aiodocker.exceptions.DockerError: DockerError(404, 'No such container: c63fce3e018feb571a52779e1360a0d7f8056f94e89c5dd4668a29ad43453d55')

However, it looks like the container info is being updated correctly - for example, the CPU and memory percentage numbers are fluctuating and look to be correct when compared to docker compose stats zwave-js-ui. image

Small edit/update: it seems like monitor_docker might be deleting and re-creating the relevant entities each time the container is recreated. That's not ideal, but it doesn't cause any issues in my use-case.

Does the error repeat? Or is only seen once of twice?

Yes, I know it does a delete and re-create of the entities when a container recreated. There is no other easy/safe way to make this better ... Most people loose entities, so that's why it is done in this way.

airdrummingfool commented 4 months ago

Does the error repeat? Or is only seen once of twice?

As far as I can tell they do not repeat. I have also observed instances where no errors are logged when containers are recreated.

Yes, I know it does a delete and re-create of the entities when a container recreated. There is no other easy/safe way to make this better ... Most people loose entities, so that's why it is done in this way.

Totally understandable. Thanks for the fix!


@yuejon sorry if I hijacked your error report - I was chiming in to agree but as I was writing my comment I realized that nothing was actually broken for me.

yuejon commented 4 months ago

@airdrummingfool - no problem

@ualex73 - Here's the log when I recreate the ESPhome container. It looks similar to @airdrummingfool's log but none of the entities related to ESPhome are updated (I'm not using allinone). Turning off the switch entity also does not stop the container.

Logger: custom_components.monitor_docker.helpers
Source: custom_components/monitor_docker/helpers.py:394
integration: Monitor Docker (documentation, issues)
First occurred: 8:10:15 AM (1 occurrences)
Last logged: 8:10:15 AM

[Docker]: run_docker_events (cannot access local variable 'taskcreated' where it is not associated with a value)
Traceback (most recent call last):
  File "/config/custom_components/monitor_docker/helpers.py", line 394, in _run_docker_events
    if self._event_destroy and not taskcreated:
                                   ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'taskcreated' where it is not associated with a value
Logger: custom_components.monitor_docker.helpers
Source: custom_components/monitor_docker/helpers.py:775
integration: Monitor Docker (documentation, issues)
First occurred: 8:10:18 AM (1 occurrences)
Last logged: 8:10:18 AM

[Docker] ESPHome: Container not available anymore (3) (DockerError(404, 'No such container: 31cae92ee8a76f3f1fa6394ecd87aac8c0c08adcb1c5bdde94b363739a7e4fef'))
Traceback (most recent call last):
  File "/config/custom_components/monitor_docker/helpers.py", line 775, in _run
    await self._run_container_info()
  File "/config/custom_components/monitor_docker/helpers.py", line 818, in _run_container_info
    raw: dict = await self._container.show()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 177, in show
    data = await self.docker._query_json(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json
    async with self._query(
  File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__
    resp = await self._coro
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query
    raise DockerError(response.status, json.loads(what.decode("utf8")))
aiodocker.exceptions.DockerError: DockerError(404, 'No such container: 31cae92ee8a76f3f1fa6394ecd87aac8c0c08adcb1c5bdde94b363739a7e4fef')
Logger: custom_components.monitor_docker.helpers
Source: custom_components/monitor_docker/helpers.py:1232
integration: Monitor Docker (documentation, issues)
First occurred: 8:16:33 AM (1 occurrences)
Last logged: 8:16:33 AM

[Docker] ESPHome: Can not start container (DockerError(404, 'No such container: 31cae92ee8a76f3f1fa6394ecd87aac8c0c08adcb1c5bdde94b363739a7e4fef'))
yuejon commented 4 months ago

Another point to add is, if I don't restart HASS, it starts to fill up my log as it can't find the new container:

Logger: custom_components.monitor_docker.helpers
Source: custom_components/monitor_docker/helpers.py:775
integration: Monitor Docker ([documentation](https://github.com/ualex73/monitor_docker), [issues](https://github.com/ualex73/monitor_docker/issues))
First occurred: 8:10:18 AM (443 occurrences)
Last logged: 8:54:43 AM

[Docker] ESPHome: Container not available anymore (3) (DockerError(404, 'No such container: 31cae92ee8a76f3f1fa6394ecd87aac8c0c08adcb1c5bdde94b363739a7e4fef'))
ualex73 commented 4 months ago

@yuejon are you on the latest version?

yuejon commented 4 months ago

Yes. I'm on 1.18

ualex73 commented 4 months ago

Can you install the master ?

yuejon commented 4 months ago

I should already be on the master? I installed it via HACS default

image

ualex73 commented 4 months ago

@yuejon it looks to be still 1.18 ... I just released 1.19, in 6-12 hours, can you try that one? It should have fixed it.

yuejon commented 4 months ago

Cool. Will do that and let you know. Thanks for the quick turnaround.

yuejon commented 4 months ago

Just downloaded this and the issue does seem to be fixed. Thanks again!

Not sure if this should be closed though, as the original post by @derekoharrow was a feature request for the capability to reload the integration without having to restart HA.

ualex73 commented 4 months ago

@yuejon good to hear it is resolved, and yes, I will close this one. If it still persists, please open a new ticket.