diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/collector/db.py | 2 | ||||
-rw-r--r-- | src/collector/healthcheck.py | 38 | ||||
-rwxr-xr-x | src/collector/healthcheck.sh | 11 | ||||
-rwxr-xr-x | src/collector/main.py | 5 |
4 files changed, 51 insertions, 5 deletions
diff --git a/src/collector/db.py b/src/collector/db.py index 2f16e12..54cdc21 100644 --- a/src/collector/db.py +++ b/src/collector/db.py @@ -49,7 +49,7 @@ class DBClient: await self.collection.find_one({"_id": ObjectId("507f1f77bcf86cd799439011")}) print("Connection to DB - OK") break - except Exception: # pylint: disable=braod-except + except Exception: # pylint: disable=broad-except print(f"WARNING failed to connect to DB - {i} / 4", flush=True) sleep(1) else: diff --git a/src/collector/healthcheck.py b/src/collector/healthcheck.py new file mode 100644 index 0000000..7d336fd --- /dev/null +++ b/src/collector/healthcheck.py @@ -0,0 +1,38 @@ +""" +Send a healthcheck request +""" +import sys +import time +import json + +import requests + + +def check_collector() -> bool: + """Check our collector using /info + + :return: bool + """ + time.sleep(2) # Prevent race condition with redis container healthcheck + + req = requests.get( + "http://localhost:8000/info", + timeout=3, + # verify="./rootCA.crt", + ) + + if req.status_code != 200: + return False + + data = json.loads(req.text) + if isinstance(data["estimated document count"], int) and data["estimated document count"] >= 0: + return req.status_code == 200 + + return False + + +if __name__ == "__main__": + if sys.argv[1] == "COLLECTOR": + if check_collector(): + sys.exit(0) + sys.exit(1) diff --git a/src/collector/healthcheck.sh b/src/collector/healthcheck.sh new file mode 100755 index 0000000..3c5e042 --- /dev/null +++ b/src/collector/healthcheck.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# If mongodb container +if [ $1 = "MONGODB" ] +then + /usr/bin/mongosh --eval 'disableTelemetry()' -u $MONGODB_USERNAME -p $MONGODB_PASSWORD localhost:27017/production /healthcheck-mongodb.js + exit $? +fi + +# If collector +/usr/bin/python3 ./src/collector/healthcheck.py $1 || exit 1 diff --git a/src/collector/main.py b/src/collector/main.py index 36a8ebd..beaf2ea 100755 --- a/src/collector/main.py +++ b/src/collector/main.py @@ -10,8 +10,6 @@ from bson import ( ObjectId, json_util, ) -from dotenv import load_dotenv - from .db import ( DBClient, SearchInput, @@ -19,7 +17,6 @@ from .db import ( from .schema import valid_schema -load_dotenv() # Get credentials if "MONGODB_USERNAME" not in environ or "MONGODB_PASSWORD" not in environ or "MONGODB_COLLECTION" not in environ: print("Missing MONGODB_USERNAME or MONGODB_PASSWORD or MONGODB_COLLECTION in env") @@ -167,4 +164,4 @@ async def info() -> JSONResponse: """ count = await db.estimated_document_count() - return JSONResponse(content={"status": "success", "Estimated document count": count}) + return JSONResponse(content={"status": "success", "estimated document count": count}) |