Skip to content

Commit

Permalink
add health check for novel
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Mar 31, 2024
1 parent db9a5f3 commit 70531cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
4 changes: 4 additions & 0 deletions novel/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 10 additions & 11 deletions novel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 70531cf

Please sign in to comment.