From eb015de8f112741d12ec9ae2d9ff73459804c75f Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Sat, 20 Jul 2019 14:50:08 +0500 Subject: [PATCH] check any pending vm scheduling on start --- main.py | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/main.py b/main.py index 11db59b..aeb7fb6 100644 --- a/main.py +++ b/main.py @@ -144,36 +144,36 @@ def main(vm_prefix, host_prefix): host=config("ETCD_HOST"), port=int(config("ETCD_PORT")) ) - events_iterator = client.watch_prefix(vm_prefix, timeout=10) + for events_iterator in [client.get_prefix(vm_prefix), + client.watch_prefix(vm_prefix, timeout=10)]: + for e in events_iterator: + try: + e.value = json.loads(e.value) + except json.JSONDecodeError: + logging.error(f"Invalid JSON {e.value}") + continue - for e in events_iterator: - try: - e.value = json.loads(e.value) - except json.JSONDecodeError: - logging.error(f"Invalid JSON {e.value}") - continue + logging.debug(e.key, e.value) - logging.debug(e.key, e.value) + e_status = e.value["status"] - e_status = e.value["status"] + if e_status == "TIMEOUT": + logging.info("Timeout") + hosts = client.get_prefix(host_prefix, value_in_json=True) + dead_hosts = dead_host_detection(hosts) + dead_host_mitigation(client, dead_hosts) - if e_status == "TIMEOUT": - logging.info("Timeout") - hosts = client.get_prefix(host_prefix, value_in_json=True) - dead_hosts = dead_host_detection(hosts) - dead_host_mitigation(client, dead_hosts) - - elif e_status == "REQUESTED_NEW": - host_name = get_suitable_host( - client, vm_prefix, host_prefix, e.value["specs"] - ) - if host_name: - e.value["status"] = "SCHEDULED_DEPLOY" - e.value["hostname"] = host_name - client.put(e.key, json.dumps(e.value)) - else: - # email admin - print("No Resource Left. Emailing admin....") + elif e_status == "REQUESTED_NEW": + host_name = get_suitable_host( + client, vm_prefix, host_prefix, e.value["specs"] + ) + if host_name: + e.value["status"] = "SCHEDULED_DEPLOY" + e.value["hostname"] = host_name + client.put(e.key, json.dumps(e.value)) + else: + # email admin + print("No Resource Left. Emailing admin....") if __name__ == "__main__":