From ac1d419acf9b2e37f1569f16fbd17e53582824c6 Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Wed, 17 Jul 2019 11:16:01 +0200
Subject: [PATCH] Decrement nTryes only in case of zmq timeout

---
 cal_tools/cal_tools/tools.py | 35 ++++++++++++++---------------------
 1 file changed, 14 insertions(+), 21 deletions(-)

diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py
index bde970f7e..db7977329 100644
--- a/cal_tools/cal_tools/tools.py
+++ b/cal_tools/cal_tools/tools.py
@@ -470,6 +470,7 @@ def get_from_db(device, constant, condition, empty_constant,
     :return: Calibration constant, metadata
     """
     from iCalibrationDB import ConstantMetaData, Versions
+    import zmq
 
     if device:
         metadata = ConstantMetaData()
@@ -477,33 +478,25 @@ def get_from_db(device, constant, condition, empty_constant,
         metadata.detector_condition = condition
         if creation_time is None:
             metadata.calibration_constant_version = Versions.Now(device=device)
-            while ntries > 0:
-                this_interface = get_random_db_interface(cal_db_interface)
-                try:
-                    metadata.retrieve(this_interface, timeout=timeout,
-                                      meta_only=meta_only)
-                    break
-                except Exception as e:
-                    if verbosity > 0:
-                        print(e)
-                    ntries -= 1
         else:
             metadata.calibration_constant_version = Versions.Timespan(
                 device=device,
                 start=creation_time)
 
-            while ntries > 0:
-                this_interface = get_random_db_interface(cal_db_interface)
-                try:
-                    metadata.retrieve(this_interface,
-                                      when=creation_time.isoformat(),
-                                      timeout=timeout, meta_only=meta_only)
-                    break
-                except Exception as e:
-                    if verbosity > 0:
-                        print(e)
-
+        while ntries > 0:
+            this_interface = get_random_db_interface(cal_db_interface)
+            try:
+                metadata.retrieve(this_interface, timeout=timeout,
+                                  meta_only=meta_only)
+                break
+            except Exception as e:
+                if verbosity > 0:
+                    print(e)
+                if isinstance(e, zmq.error.Again):
                     ntries -= 1
+                else:
+                    ntries = 0
+
         if ntries > 0:
             if verbosity > 0:
                 if constant.name not in already_printed or verbosity > 1:
-- 
GitLab