Skip to content

Commit

Permalink
Merge pull request #40 from ISISComputingGroup/time_out_for_url_get
Browse files Browse the repository at this point in the history
Time out on URL get
  • Loading branch information
JamesKingWork authored Feb 13, 2020
2 parents cc3fe96 + d514496 commit 37ba3c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions external_webpage/data_source_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
# Port for configuration
PORT_CONFIG = 8008

# Timeout for url get
URL_GET_TIMEOUT = 60


class DataSourceReader(object):
"""
Expand Down Expand Up @@ -86,7 +89,7 @@ def _get_json_from_info_page(self, port, group_name):
url = 'http://{host}:{port}/group?name={group_name}&format=json'.format(
host=self._host, port=port, group_name=group_name)
try:
page = requests.get(url)
page = requests.get(url, timeout=URL_GET_TIMEOUT)
return page.json()
except Exception as e:
logger.error("URL not found or json not understood: " + str(url))
Expand All @@ -101,7 +104,7 @@ def read_config(self):
"""

# read config
page = requests.get('http://{}:{}/'.format(self._host, PORT_CONFIG))
page = requests.get('http://{}:{}/'.format(self._host, PORT_CONFIG), timeout=URL_GET_TIMEOUT)
content = page.content.decode("utf-8")
corrected_page = content.replace("'", '"')\
.replace("None", "null")\
Expand Down
11 changes: 7 additions & 4 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def do_GET(self):
This is called by BaseHTTPRequestHandler every time a client does a GET.
The response is written to self.wfile
"""
instrument = "Not set"
try:
instrument, callback = get_instrument_and_callback(self.path)

# Warn level so as to avoid many log messages that come from other modules
logger.warning("Connected to from " + str(self.client_address) + " looking at " + str(instrument))
# Debug is only needed when debugging
logger.debug("Connection from " + str(self.client_address) + " looking at " + str(instrument))

with scraped_data_lock:
if instrument == "ALL":
Expand All @@ -61,11 +62,13 @@ def do_GET(self):
self.end_headers()
self.wfile.write(response.encode("utf-8"))
except ValueError as e:
logger.error(e)
logger.exception("Value Error when getting data from {} for {}: {}".format(
self.client_address, instrument, e))
self.send_response(400)
except Exception as e:
logger.exception("Exception when getting data from {} for {}: {}".format(
self.client_address, instrument, e))
self.send_response(404)
logger.error(e)

def log_message(self, format, *args):
""" By overriding this method and doing nothing we disable writing to console
Expand Down

0 comments on commit 37ba3c6

Please sign in to comment.