Skip to content

Commit

Permalink
add health check for api server
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Mar 31, 2024
1 parent fb56e28 commit db9a5f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions apiserver/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:8888/health || exit 1

RUN mkdir /cache
VOLUME /cache

Expand Down
14 changes: 13 additions & 1 deletion apiserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
" ".join([STOCKS, INDICES, CRYPTOS, CURRENCIES, COMMODITIES])
)

lastUpdatedTime = time.time()


class apiHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, format, *args):
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit db9a5f3

Please sign in to comment.