diff --git a/apiserver/Dockerfile b/apiserver/Dockerfile index f78ea48..95cf9a0 100644 --- a/apiserver/Dockerfile +++ b/apiserver/Dockerfile @@ -7,6 +7,10 @@ RUN pip install --no-cache-dir -r /app/requirements.txt RUN pyarmor gen --output /app /app/main.py +RUN apk --update --no-cache add curl + +HEALTHCHECK --start-period=10s --interval=60s --timeout=3s CMD curl -f http://localhost:8888/health || exit 1 + RUN mkdir /cache VOLUME /cache diff --git a/apiserver/main.py b/apiserver/main.py index 9f9b669..6f772c1 100644 --- a/apiserver/main.py +++ b/apiserver/main.py @@ -51,6 +51,8 @@ " ".join([STOCKS, INDICES, CRYPTOS, CURRENCIES, COMMODITIES]) ) +lastUpdatedTime = time.time() + class apiHandler(http.server.BaseHTTPRequestHandler): def log_message(self, format, *args): @@ -67,6 +69,13 @@ def do_GET(self): response = json.dumps( {**crypto_status, **currency_status, **commodity_status} ) + elif self.path == "/health": + if time.time() - lastUpdatedTime > 1200: + self.send_response(500) + else: + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() else: message = {"message": "Not Found"} response = json.dumps(message) @@ -150,7 +159,7 @@ def get_xui_status(): "up": bytes_to_speed(status["obj"]["netIO"]["up"]), "down": bytes_to_speed(status["obj"]["netIO"]["down"]), "usage": format_bytes(status["obj"]["netTraffic"]["recv"]), - "online": random.choice(online["obj"]) if online["obj"] else '-', + "online": random.choice(online["obj"]) if online["obj"] else "-", } return info @@ -211,6 +220,9 @@ def update_status(symbols): with open(MAPPING[symbols][1], "w") as file: json.dump(MAPPING[symbols][0], file) + global lastUpdatedTime + lastUpdatedTime = time.time() + if __name__ == "__main__": load_all_cache()