feat: handle SIG_TERM on exit

This commit is contained in:
Cyrille Nofficial 2022-05-19 18:23:18 +02:00
parent 2bc1458eb7
commit a442c00b36

View File

@ -25,6 +25,7 @@ Options:
""" """
import logging import logging
import os import os
import signal, sys
from docopt import docopt from docopt import docopt
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
@ -41,12 +42,22 @@ logging.basicConfig(level=logging.INFO)
default_client_id = "robocar-pca9685-python" default_client_id = "robocar-pca9685-python"
class SignalHandler:
def __init__(self, client: mqtt.Client):
self._client = client
def sigterm_handler(self, _signo, _stack_frame):
logger.info("exit on SIG_TERM")
self._client.disconnect()
sys.exit(0)
def init_mqtt_steering_client(steering_controller: actuator.PWMSteering, broker_host: str, user: str, password: str, client_id: str, def init_mqtt_steering_client(steering_controller: actuator.PWMSteering, broker_host: str, user: str, password: str, client_id: str,
steering_topic: str) -> mqtt.Client: steering_topic: str) -> mqtt.Client:
logger.info("Start steering part") logger.info("Start steering part")
client = mqtt.Client(client_id=client_id, clean_session=True, userdata=None, protocol=mqtt.MQTTv311) client = mqtt.Client(client_id=client_id, clean_session=True, userdata=None, protocol=mqtt.MQTTv311)
def on_connect(client, userdata, flags, rc): def on_connect(client: mqtt.Client, userdata, flags, rc):
logger.info("Register callback on topic %s", steering_topic) logger.info("Register callback on topic %s", steering_topic)
# Subscribing in on_connect() means that if we lose the connection and # Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed. # reconnect then subscriptions will be renewed.
@ -58,7 +69,7 @@ def init_mqtt_steering_client(steering_controller: actuator.PWMSteering, broker_
steering_msg.ParseFromString(msg.payload) steering_msg.ParseFromString(msg.payload)
steering_controller.run_threaded(steering_msg.Steering) steering_controller.run_threaded(steering_msg.Steering)
except: except:
logger.debug("unexpected error: unable to process steering, skip message") logger.error("unexpected error: unable to process steering, skip message")
client.username_pw_set(user, password) client.username_pw_set(user, password)
client.on_connect = on_connect client.on_connect = on_connect
@ -86,7 +97,7 @@ def init_mqtt_throttle_client(throttle_controller: actuator.PWMThrottle, broker_
throttle_msg.ParseFromString(msg.payload) throttle_msg.ParseFromString(msg.payload)
throttle_controller.run_threaded(throttle_msg.Throttle) throttle_controller.run_threaded(throttle_msg.Throttle)
except: except:
logger.debug("unexpected error: unable to process throttle, skip message") logger.error("unexpected error: unable to process throttle, skip message")
client.username_pw_set(user, password) client.username_pw_set(user, password)
client.on_connect = on_connect client.on_connect = on_connect
@ -129,6 +140,9 @@ def execute_from_command_line():
else: else:
raise Exception('invalid mode selected, must be steering or throttle') raise Exception('invalid mode selected, must be steering or throttle')
sh = SignalHandler(client)
signal.signal(signal.SIGTERM, sh.sigterm_handler)
client.loop_forever() client.loop_forever()