Skip to content

Commit

Permalink
fix thread restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
miaucl committed Sep 3, 2024
1 parent ce4789c commit c7f6d2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It is available as python package on [pypi/docker2mqtt](https://pypi.org/p/docke

```bash
pip install docker2mqtt
docker2mqtt --name MyDockerName --events=60 -vvvvv
docker2mqtt --name MyDockerName --events -vvvvv
```

Usage
Expand Down Expand Up @@ -85,7 +85,7 @@ You can use environment variables to control the behavior.
| `mqtt_user`| `MQTT_USER` | | The user to send to the MQTT broker. Leave unset to disable authentication. |
| `mqtt_password`| `MQTT_PASSWD` | | The password to send to the MQTT broker. Leave unset to disable authentication. |
| `mqtt_timeout`| `MQTT_TIMEOUT` | `30` | The timeout for the MQTT connection. |
| `mqtt_topic_prefix`| `MQTT_TOPIC_PREFIX` | `ping` | The MQTT topic prefix. With the default data will be published to `ping/<hostname>`. |
| `mqtt_topic_prefix`| `MQTT_TOPIC_PREFIX` | `docker` | The MQTT topic prefix. With the default data will be published to `docker/<hostname>`. |
| `mqtt_qos`| `MQTT_QOS` | `1` | The MQTT QOS level |
| `destroyed_container_ttl`| `DESTROYED_CONTAINER_TTL` | `86400` | How long, in seconds, before destroyed containers are removed from Home Assistant. Containers won't be removed if the service is restarted before the TTL expires. |
| `stats_record_seconds`| `STATS_RECORD_SECONDS` | `30` | The number of seconds to record state and make an average |
Expand Down
36 changes: 22 additions & 14 deletions docker2mqtt/docker2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
# Loggers
main_logger = logging.getLogger("main")
events_logger = logging.getLogger("events")
stats_logger = logging.getLogger("main")
stats_logger = logging.getLogger("stats")


class Docker2Mqtt:
Expand All @@ -72,7 +72,7 @@ class Docker2Mqtt:
Attributes
----------
version
The version of linux2mqtt
The version of docker2mqtt
cfg
The config for docker2mqtt
b_stats
Expand Down Expand Up @@ -259,10 +259,7 @@ def __init__(self, cfg: Docker2MqttConfig, do_not_exit: bool = False):
try:
if self.b_events:
logging.info("Starting Events thread")
self.docker_events_t = Thread(
target=self._readline_events_thread, daemon=True, name="Events"
)
self.docker_events_t.start()
self._start_readline_events_thread()
started = True
except Exception as e:
main_logger.error("Error while trying to start events thread.")
Expand All @@ -273,10 +270,7 @@ def __init__(self, cfg: Docker2MqttConfig, do_not_exit: bool = False):
if self.b_stats:
started = True
logging.info("Starting Stats thread")
self.docker_stats_t = Thread(
target=self._readline_stats_thread, daemon=True, name="Stats"
)
self.docker_stats_t.start()
self._start_readline_stats_thread()
except Exception as e:
main_logger.error("Error while trying to start stats thread.")
main_logger.error(str(e))
Expand Down Expand Up @@ -337,7 +331,7 @@ def loop(self) -> None:
try:
if self.b_events and not self.docker_events_t.is_alive():
main_logger.warning("Restarting events thread")
self.docker_events_t.start()
self._start_readline_events_thread()
except Exception as e:
main_logger.error("Error while trying to restart events thread.")
main_logger.error(str(e))
Expand All @@ -346,7 +340,7 @@ def loop(self) -> None:
try:
if self.b_stats and not self.docker_stats_t.is_alive():
main_logger.warning("Restarting stats thread")
self.docker_stats_t.start()
self._start_readline_stats_thread()
except Exception as e:
main_logger.error("Error while trying to restart stats thread.")
main_logger.error(str(e))
Expand Down Expand Up @@ -486,7 +480,14 @@ def _mqtt_disconnect(self) -> None:
main_logger.error("MQTT Disconnect: %s", str(e))
raise Docker2MqttConnectionException() from e

def _readline_events_thread(self) -> None:
def _start_readline_events_thread(self) -> None:
"""Start the events thread."""
self.systemctl_events_t = Thread(
target=self._run_readline_events_thread, daemon=True, name="Events"
)
self.systemctl_events_t.start()

def _run_readline_events_thread(self) -> None:
"""Run docker events and continually read lines from it."""
thread_logger = logging.getLogger("event-thread")
thread_logger.setLevel(self.cfg["log_level"].upper())
Expand All @@ -509,7 +510,14 @@ def _readline_events_thread(self) -> None:
thread_logger.error("Error Running Events thread: %s", str(ex))
thread_logger.debug("Waiting for main thread to restart this thread")

def _readline_stats_thread(self) -> None:
def _start_readline_stats_thread(self) -> None:
"""Start the stats thread."""
self.systemctl_stats_t = Thread(
target=self._run_readline_stats_thread, daemon=True, name="Stats"
)
self.systemctl_stats_t.start()

def _run_readline_stats_thread(self) -> None:
"""Run docker events and continually read lines from it."""
thread_logger = logging.getLogger("stats-thread")
thread_logger.setLevel(self.cfg["log_level"].upper())
Expand Down

0 comments on commit c7f6d2f

Please sign in to comment.