Fix global state for running threads
Anirudh Oppiliappan x@icyphox.sh
Tue, 09 Mar 2021 21:46:01 +0530
3 files changed,
13 insertions(+),
4 deletions(-)
M
janny/cleanup.py
→
janny/cleanup.py
@@ -1,7 +1,7 @@
import time import json -from janny.utils import parse_delta +from janny.utils import parse_delta, RUNNING from janny.auth import SESSION as s from janny.config import API_HOST, logger@@ -9,6 +9,9 @@
def clean_up( url: str, kube_resource: str, resource_name: str, kill_time: str, namespace: str ): + """ + Parse the kill_time and create call send_delete_event(). + """ delta = parse_delta(kill_time) secs = delta.total_seconds() time.sleep(secs)@@ -17,9 +20,14 @@ send_delete_event(url, kube_resource, resource_name, namespace)
def send_delete_event(url: str, kube_resource: str, resource_name: str, namespace: str): + """ + Sends a DELETE request to the resource. + """ api_url = f"{API_HOST}{url}/namespaces/{namespace}/{kube_resource}/{resource_name}" response = s.delete(api_url, params={"propagationPolicy": "Background"}) logger.info(f"Sent delete event to {kube_resource}/{resource_name}") response_json = json.loads(response.content) if "Success" not in response_json.values(): logger.error(f"Deletion did not succeed. Recieved: {response_json}") + + RUNNING.remove(resource_name)
M
janny/main.py
→
janny/main.py
@@ -1,12 +1,10 @@
import threading import itertools -from janny.utils import get +from janny.utils import get, RUNNING from janny.cleanup import clean_up from janny.config import logger - -RUNNING = [] def get_resource_urls() -> list:
M
janny/utils.py
→
janny/utils.py
@@ -35,3 +35,6 @@ if match:
parts = {k: int(v) for k, v in match.groupdict().items() if v} return timedelta(**parts) return timedelta(0) + +# List of running threads +RUNNING = list()