more grace

more graceful shutdown and error handling
This commit is contained in:
Brian Martin 2019-06-07 00:54:12 -04:00
parent 4e46d7e810
commit 6c429b93e6
5 changed files with 86 additions and 50 deletions

View file

@ -58,8 +58,20 @@ class MQTTHelperBot:
("iot/atr/+", QOS_0),
]
)
asyncio.create_task(self.get_msg())
except ConnectionRefusedError as e:
helperbotlog.Error(e)
pass
except asyncio.CancelledError as e:
pass
except hbmqtt.client.ConnectException as e:
helperbotlog.Error(e)
pass
except Exception as e:
helperbotlog.exception("{}".format(e))
@ -192,25 +204,23 @@ class MQTTServer:
broker = None
async def broker_coro(self):
mqttserverlog.info(
"Starting MQTT Server at {}:{}".format(self.address[0], self.address[1])
)
self.broker = hbmqtt.broker.Broker(config=self.default_config)
try:
mqttserverlog.info(
"Starting MQTT Server at {}:{}".format(self.address[0], self.address[1])
)
self.broker = hbmqtt.broker.Broker(config=self.default_config)
await self.broker.start()
except PermissionError as e:
if "bind" in e.strerror:
mqttserverlog.exception(
"Error binding mqttserver, exiting. Try using a different hostname or IP - {}".format(
e
)
)
exit(1)
except hbmqtt.broker.BrokerException as e:
mqttserverlog.exception(e)
asyncio.create_task(bumper.shutdown())
pass
except Exception as e:
mqttserverlog.exception("{}".format(e))
exit(1)
asyncio.create_task(bumper.shutdown())
def __init__(self, address):
try:
@ -299,9 +309,7 @@ class BumperMQTTServer_Plugin:
)
mqttserverlog.info(
"bot authenticated SN: {} DID: {}".format(
username, didsplit[0]
)
"bot authenticated SN: {} DID: {}".format(username, didsplit[0])
)
authenticated = True
@ -322,9 +330,7 @@ class BumperMQTTServer_Plugin:
if auth:
bumper.client_add(userid, realm, resource)
mqttserverlog.info(
"client authenticated {}".format(userid)
)
mqttserverlog.info("client authenticated {}".format(userid))
authenticated = True
else: