Skip to content

Commit

Permalink
Set timeout seconds and use logging module (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhellander committed Apr 17, 2024
1 parent 7c0906e commit 7523037
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
FROM python:3.8.10-alpine
COPY . /app

COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY event_listener.py /app/event_listener.py

ENTRYPOINT python event_listener.py

46 changes: 31 additions & 15 deletions event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
import os
import requests

import logging

# Configure logging
logging.basicConfig(
level=logging.DEBUG, # Adjust as needed (DEBUG, ERROR, WARNING, INFO)
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
# logging.FileHandler("event_listener.log"), # Log to a file
logging.StreamHandler() # Also log to the console
]
)


STUDIO_SERVICE_NAME = os.environ.get("STUDIO_SERVICE_NAME", None)
STUDIO_SERVICE_PORT = os.environ.get("STUDIO_SERVICE_PORT", None)
APP_STATUS_ENDPOINT = os.environ.get("APP_STATUS_ENDPOINT", None)
Expand All @@ -12,10 +26,10 @@
APP_STATUS_URL = f"{BASE_URL}/{APP_STATUS_ENDPOINT}"
APP_STATUSES_URL = f"{BASE_URL}/{APP_STATUSES_ENDPOINT}"

print(f"STUDIO_SERVICE_NAME: {STUDIO_SERVICE_NAME}", flush=True)
print(f"STUDIO_SERVICE_PORT: {STUDIO_SERVICE_PORT}", flush=True)
print(f"APP_STATUS_ENDPOINT: {APP_STATUS_ENDPOINT}", flush=True)
print(f"APP_STATUSES_ENDPOINT: {APP_STATUSES_ENDPOINT}", flush=True)
logging.debug(f"STUDIO_SERVICE_NAME: {STUDIO_SERVICE_NAME}")
logging.debug(f"STUDIO_SERVICE_PORT: {STUDIO_SERVICE_PORT}")
logging.debug(f"APP_STATUS_ENDPOINT: {APP_STATUS_ENDPOINT}")
logging.debug(f"APP_STATUSES_ENDPOINT: {APP_STATUSES_ENDPOINT}")

K8S_STATUS_MAP = {
"CrashLoopBackOff": "Error",
Expand Down Expand Up @@ -48,8 +62,8 @@ def sync_all_statuses():

data = {"values": values}

print(f"DATA: {data}", flush=True)
print("Syncing all statuses...", flush=True)
logging.debug(f"DATA: {data}")
logging.debug("Syncing all statuses...")

post(APP_STATUSES_URL, data=data)

Expand All @@ -59,26 +73,27 @@ def init_event_listener():
api.list_namespaced_pod,
namespace=namespace,
label_selector=label_selector,
timeout_seconds=0,
):
pod = event["object"]

status = get_status(pod)

print(f"Synchronizing status: {status}", flush=True)
logging.info(f"Synchronizing status: {status}")

# status = pod.status.phase
release = pod.metadata.labels["release"]

event_type = event["type"]

if latest_status.get(release) == status:
print("Status not changed, skipping...")
logging.info("Status not changed, skipping...")

latest_status[release] = status

if event_type != "DELETED":
print(f"EVENT_TYPE: {event_type}", flush=True)
print(f"STATUS: {status}", flush=True)
logging.info(f"EVENT_TYPE: {event_type}")
logging.info(f"STATUS: {status}")

data = {
"release": release,
Expand Down Expand Up @@ -130,15 +145,16 @@ def post(url: str, data: dict):
try:
response = requests.post(url, data=data, verify=False)

print(f"RESPONSE STATUS CODE: {response.status_code}")
print(f"RESPONSE TEXT: {response.text}")
logging.info(f"RESPONSE STATUS CODE: {response.status_code}")
logging.info(f"RESPONSE TEXT: {response.text}")

except requests.exceptions.RequestException:
print("Service did not respond.")
except requests.exceptions.RequestException as err:
logging.error(err)
logging.error("Service did not respond.")


if __name__ == "__main__":
print("Starting event listener...", flush=True)
logging.info("Starting event listener...")

sync_all_statuses()
init_event_listener()

0 comments on commit 7523037

Please sign in to comment.