Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set timeout seconds and use logging module #4

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading