mirror-health: move up-to-date check to a function
authorJulien Cristau <jcristau@debian.org>
Sun, 1 Oct 2017 10:28:56 +0000 (12:28 +0200)
committerJulien Cristau <jcristau@debian.org>
Sun, 1 Oct 2017 10:36:34 +0000 (12:36 +0200)
modules/roles/files/mirror_health/mirror-health

index ed87f56..1d4fae0 100755 (executable)
@@ -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)