Mercurial > code > home > repos > front-door-lock
changeset 8:caea36c8289f
don't try to reconnect mqtt (was broken); just fail a k8s health check
author | drewp@bigasterisk.com |
---|---|
date | Thu, 30 Nov 2023 22:43:58 -0800 |
parents | 18c0dbfab73a |
children | 6e0d47f9e56d |
files | deploy.yaml front_door_lock.py proxy.yaml |
diffstat | 3 files changed, 24 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/deploy.yaml Sun Oct 15 18:48:38 2023 -0700 +++ b/deploy.yaml Thu Nov 30 22:43:58 2023 -0800 @@ -43,8 +43,14 @@ - uvicorn - '--port=8001' - '--host=0.0.0.0' + - '--no-access-log' - '--reload' - 'front_door_lock:app' + livenessProbe: + httpGet: + path: /health + port: 80 + --- apiVersion: v1 kind: Service
--- a/front_door_lock.py Sun Oct 15 18:48:38 2023 -0700 +++ b/front_door_lock.py Thu Nov 30 22:43:58 2023 -0800 @@ -28,7 +28,7 @@ from starlette.applications import Starlette from starlette.exceptions import HTTPException from starlette.requests import Request -from starlette.responses import JSONResponse +from starlette.responses import JSONResponse, PlainTextResponse from starlette.routing import Route from starlette_exporter import PrometheusMiddleware, handle_metrics from prometheus_client import Gauge @@ -115,21 +115,17 @@ topicRoot: str = 'frontdoorlock' def startup(self): - asyncio.create_task(self._go()) + self.task = asyncio.create_task(self._go()) async def _go(self): self.client = aiomqtt.Client("mqtt1", 1883, client_id="lock-service-%s" % time.time(), keepalive=6) - while True: - try: - async with self.client: - MQTT_CONNECTED.set(1) - await self._handleMessages() - except aiomqtt.MqttError: - MQTT_CONNECTED.set(0) - log.error('mqtt reconnecting', exc_info=True) - await asyncio.sleep(5) - finally: - MQTT_CONNECTED.set(0) + try: + async with self.client: + MQTT_CONNECTED.set(1) + await self._handleMessages() + except aiomqtt.MqttError: + MQTT_CONNECTED.set(0) + log.error('mqtt down', exc_info=True) async def _handleMessages(self): async with self.client.messages() as messages: @@ -186,6 +182,11 @@ raise NotImplementedError(command) return JSONResponse({'ok': True}) +def health(mqtt: MqttConnection, req: Request) -> PlainTextResponse: + if mqtt.task.done(): + return PlainTextResponse('no mqtt task', status_code=500) + return PlainTextResponse('ok') + def main(): graph = PatchableGraph() @@ -195,6 +196,7 @@ app = Starlette(debug=True, on_startup=[mqtt.startup], routes=[ + Route('/health', partial(health, mqtt)), Route('/api/status', partial(status, graph)), Route('/api/output', partial(output, graph)), Route('/api/graph', StaticGraph(graph)),
--- a/proxy.yaml Sun Oct 15 18:48:38 2023 -0700 +++ b/proxy.yaml Thu Nov 30 22:43:58 2023 -0800 @@ -20,6 +20,9 @@ location = /metrics { proxy_pass http://127.0.0.1:8001/metrics; } + location = /health { + proxy_pass http://127.0.0.1:8001/health; + } location = /front-door-lock/metrics { proxy_pass http://127.0.0.1:8001/metrics; }