deiger / Alarm

Code to connect and control alarms by PIMA.
GNU General Public License v3.0
16 stars 11 forks source link

Fail to recover after power outage #3

Closed maorcc closed 4 years ago

maorcc commented 4 years ago

After a power outage, if the pima service starts when the MQTT broker is not yet available, the pima service fails to start with the error below.

python3[342]: Traceback (most recent call last):
python3[342]:   File "pima_server.py", line 298, in <module>
python3[342]:     _mqtt_client.connect(_parsed_args.mqtt_host, _parsed_args.mqtt_port)
python3[342]:   File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 937, in connect
python3[342]:     return self.reconnect()
python3[342]:   File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1071, in reconnect
python3[342]:     sock = self._create_socket_connection()
python3[342]:   File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
python3[342]:     return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
python3[342]:   File "/usr/lib/python3.7/socket.py", line 727, in create_connection
python3[342]:     raise err
python3[342]:   File "/usr/lib/python3.7/socket.py", line 716, in create_connection
python3[342]:     sock.connect(sa)
python3[342]: socket.timeout: timed out
deiger commented 4 years ago

Changed the code to retry.

maorcc commented 4 years ago

With the fist fix I have seen the following error:

  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1071, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/usr/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
OSError: [Errno 113] No route to host

I'v created a pull-request above with a fix.