From dc142e05682ed2976e677b5b08f5d623cf9136fd Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Sun, 1 Oct 2017 12:28:56 +0200 Subject: [PATCH] mirror-health: move up-to-date check to a function --- .../roles/files/mirror_health/mirror-health | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/roles/files/mirror_health/mirror-health b/modules/roles/files/mirror_health/mirror-health index ed87f5664..1d4fae0e3 100755 --- a/modules/roles/files/mirror_health/mirror-health +++ b/modules/roles/files/mirror_health/mirror-health @@ -29,8 +29,8 @@ def last_modified(response): lm = calendar.timegm(parsedate(response.headers['last-modified'])) return lm -while True: - start = time.time() +def check_uptodate(): + global latest_ts for host in HOSTS: try: lm = last_modified(retrieve_from_host(host, URL)) @@ -42,14 +42,20 @@ while True: local_lm = last_modified(retrieve_from_host('localhost', URL)) logging.debug("lm for localhost: %s", local_lm) if local_lm < latest_ts: + logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts) + return False + return True + +while True: + start = time.time() + if check_uptodate(): + logging.info("considering myself healthy") + open(HEALTH_FILE, 'w').write("OK") + else: try: - logging.info("considering myself unhealthy my ts=%s latest_ts=%s", local_lm, latest_ts) os.remove(HEALTH_FILE) except OSError: pass - else: - logging.info("considering myself healthy") - open(HEALTH_FILE, 'w').write("OK") sleep_time = start + INTERVAL - time.time() logging.debug("sleeping for %d seconds", sleep_time) time.sleep(sleep_time) -- 2.20.1