5037: Remove unnecessary cache=False from arv-copy.
[arvados.git] / sdk / python / arvados / events.py
index 77bc2782841a472f2694b030f5c544f812678114..fc86cb46d4eecfbdd821f0e6f34da5df2061d437 100644 (file)
@@ -13,12 +13,15 @@ _logger = logging.getLogger('arvados.events')
 
 class EventClient(WebSocketClient):
     def __init__(self, url, filters, on_event):
-        ssl_options = None
-        if re.match(r'(?i)^(true|1|yes)$',
-                    config.get('ARVADOS_API_HOST_INSECURE', 'no')):
-            ssl_options={'cert_reqs': ssl.CERT_NONE}
+        # Prefer system's CA certificates (if available)
+        ssl_options = {}
+        certs_path = '/etc/ssl/certs/ca-certificates.crt'
+        if os.path.exists(certs_path):
+            ssl_options['ca_certs'] = certs_path
+        if config.flag_is_true('ARVADOS_API_HOST_INSECURE'):
+            ssl_options['cert_reqs'] = ssl.CERT_NONE
         else:
-            ssl_options={'cert_reqs': ssl.CERT_REQUIRED}
+            ssl_options['cert_reqs'] = ssl.CERT_REQUIRED
         super(EventClient, self).__init__(url, ssl_options=ssl_options)
         self.filters = filters
         self.on_event = on_event
@@ -52,17 +55,18 @@ class PollClient(threading.Thread):
         if filters:
             self.filters = [filters]
         else:
-            self.filters = []
+            self.filters = [[]]
         self.on_event = on_event
         self.poll_time = poll_time
         self.stop = threading.Event()
 
     def run(self):
-        items = self.api.logs().list(limit=1, order="id desc", filters=self.filters[0]).execute()['items']
-        if len(items) > 0:
-            self.id = items[0]["id"]
-        else:
-            self.id = 0
+        self.id = 0
+        for f in self.filters:
+            items = self.api.logs().list(limit=1, order="id desc", filters=f).execute()['items']
+            if items:
+                if items[0]['id'] > self.id:
+                    self.id = items[0]['id']
 
         self.on_event({'status': 200})
 
@@ -77,9 +81,21 @@ class PollClient(threading.Thread):
             self.id = max_id
             self.stop.wait(self.poll_time)
 
+    def run_forever(self):
+        # Have to poll here, otherwise KeyboardInterrupt will never get processed.
+        while not self.stop.is_set():
+            self.stop.wait(1)
+
     def close(self):
         self.stop.set()
-        self.join()
+        try:
+            self.join()
+        except RuntimeError:
+            # "join() raises a RuntimeError if an attempt is made to join the
+            # current thread as that would cause a deadlock. It is also an
+            # error to join() a thread before it has been started and attempts
+            # to do so raises the same exception."
+            pass
 
     def subscribe(self, filters):
         self.on_event({'status': 200})
@@ -91,9 +107,9 @@ class PollClient(threading.Thread):
 
 def subscribe(api, filters, on_event, poll_fallback=15):
     '''
-    api: Must be a newly created from arvados.api(cache=False), not shared with the caller, as it may be used by a background thread.
+    api: a client object retrieved from arvados.api(). The caller should not use this client object for anything else after calling subscribe().
     filters: Initial subscription filters.
-    on_event: The callback when a message is received
+    on_event: The callback when a message is received.
     poll_fallback: If websockets are not available, fall back to polling every N seconds.  If poll_fallback=False, this will return None if websockets are not available.
     '''
     ws = None