summaryrefslogtreecommitdiff
path: root/src/collector
diff options
context:
space:
mode:
authorVictor Näslund <victor@sunet.se>2022-11-13 22:28:12 +0100
committerVictor Näslund <victor@sunet.se>2022-11-13 22:28:12 +0100
commitdd901cd2cfc2b72b18ea0fcac0cc478b33198d2d (patch)
treefefa4853f4a9b49655208af46e48e31edff1e538 /src/collector
parent563607809d993c9e496423829b1f93def22a4aac (diff)
better mongodb handling
Diffstat (limited to 'src/collector')
-rw-r--r--src/collector/db.py2
-rw-r--r--src/collector/healthcheck.py38
-rwxr-xr-xsrc/collector/healthcheck.sh11
-rwxr-xr-xsrc/collector/main.py5
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})