From 830f7e4b11eb0731da0043f74e3f45fc83f70dca Mon Sep 17 00:00:00 2001
From: atoav <dh@atoav.com>
Date: Wed, 20 May 2020 18:33:50 +0200
Subject: [PATCH] Add Request timeout, handle unreachable endpoints

---
 bbbmon/meetings.py | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/bbbmon/meetings.py b/bbbmon/meetings.py
index 0865142..f283774 100644
--- a/bbbmon/meetings.py
+++ b/bbbmon/meetings.py
@@ -41,14 +41,13 @@ def request_meetings(secret: Secret, bbb_url: Url, user_config_path: str) -> Xml
     url = "{}/api/{}?checksum={}".format(bbb_url, call_name, checksum)
 
     try:
-        r = requests.get(url)
+        r = requests.get(url, timeout=3)
+    except requests.exceptions.Timeout as e:
+        # Offline Server!
+        return XmlDictConfig({"meetings":None})
     except:
-        click.echo("{} The URL \"{}\" is unreachable.\n       Check your network connection, and the URL and Secret of the endpoint.".format(click.style('Error:', fg='red', bold=True), url))
-        print()
-        time.sleep(1)
-        if click.confirm(click.style('Do you want to open the config file at {} with your default editor?'.format(user_config_path), fg="yellow"), abort=True):
-            click.edit(filename=user_config_path)
-            exit()
+        # Offline Server!
+        return XmlDictConfig({"meetings":None})
 
     try:
         root = ElementTree.XML(r.text)
@@ -68,12 +67,16 @@ def request_meetings(secret: Secret, bbb_url: Url, user_config_path: str) -> Xml
     return xmldict
 
 
+
 def get_meetings(secret: Secret, bbb_url: Url, user_config_path: str) -> Iterable[XmlDictConfig]:
     """
     Request meetings and return a list of them. Sorted by biggest first
     """
     meetings = []
-    d = request_meetings(secret, bbb_url, user_config_path)
+    try:
+        d = request_meetings(secret, bbb_url, user_config_path)
+    except:
+        return ["unreachable"]
 
     if d["meetings"] is None:
         return []
@@ -137,6 +140,10 @@ def list_meetings(config: Config, leaderboards: bool, n: int, participants: bool
             print("="*click.get_terminal_size()[0])
             print()
 
+        if meeting[0] == "unreachable":
+            printing.print_header(endpoint.name, "No connection..", fancy)
+            continue
+
         # If there are no meetings, skip to next endpoint
         if len(meeting) == 0:
             if show_meetings:
@@ -226,6 +233,10 @@ def meetings_twolines(config: Config, watch: int, fancy: bool):
             print(lines)
             continue
 
+        if meeting[0] == "unreachable":
+            printing.print_header(endpoint.name, "No connection..", fancy)
+            continue
+
         n_running = len(meeting)
         n_recording = len([m for m in meeting if m["recording"] == "true"])
         n_participants = sum([int(m["participantCount"]) for m in meeting])
-- 
GitLab