From 70531cfd6b59f378e6d79dd3526404a4df53a67e Mon Sep 17 00:00:00 2001 From: sgrtye Date: Sun, 31 Mar 2024 16:21:31 +0100 Subject: [PATCH] add health check for novel --- novel/Dockerfile | 4 ++++ novel/main.py | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/novel/Dockerfile b/novel/Dockerfile index f78ea48..d808c99 100644 --- a/novel/Dockerfile +++ b/novel/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:8008/health || exit 1 + RUN mkdir /cache VOLUME /cache diff --git a/novel/main.py b/novel/main.py index 24e5edc..075d09a 100644 --- a/novel/main.py +++ b/novel/main.py @@ -22,7 +22,7 @@ UNAVAILABLE_IPS = ["38.154.227.167", "154.95.36.199"] -checkedTime = time.time() +lastUpdatedTime = time.time() class HealthCheckHandler(http.server.BaseHTTPRequestHandler): @@ -45,17 +45,16 @@ def do_GET(self): } response = json.dumps(filtered_titles) self.wfile.write(response.encode("utf-8")) - elif self.path == "/status": - global checkedTime + elif self.path == "/health": + global lastUpdatedTime global loopTime - - self.send_response(200) - self.send_header("Content-type", "text/plain") - self.end_headers() - if time.time() - checkedTime < loopTime: - self.wfile.write(b"OK") + + if time.time() - lastUpdatedTime > loopTime: + self.send_response(500) else: - self.wfile.write(b"Failed") + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() else: self.send_response(404) self.send_header("Content-type", "text/plain") @@ -198,7 +197,7 @@ def get_book_title(url, proxy=None): if index == len(proxies) - 1: raise e - checkedTime = time.time() + lastUpdatedTime = time.time() time.sleep(sleepInterval) i = (i + 1) % len(books)