Skip to content

Commit

Permalink
Merge pull request #41 from ISISComputingGroup/Ticket5280_memory_leak…
Browse files Browse the repository at this point in the history
…_webserver

Ticket5280 memory leak webserver
  • Loading branch information
FreddieAkeroyd authored Mar 2, 2020
2 parents 37ba3c6 + 664047d commit 43e987d
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import json
import logging
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from logging.handlers import TimedRotatingFileHandler

import tornado.ioloop
import tornado.web
import asyncio

from external_webpage.request_handler_utils import get_detailed_state_of_specific_instrument, \
get_summary_details_of_all_instruments, get_instrument_and_callback
from external_webpage.web_scrapper_manager import WebScrapperManager
Expand All @@ -24,22 +26,23 @@
HOST, PORT = '', 60000


class MyHandler(BaseHTTPRequestHandler):
class MyHandler(tornado.web.RequestHandler):
"""
Handle for web calls for Json Borne
"""

def do_GET(self):
def get(self):
"""
This is called by BaseHTTPRequestHandler every time a client does a GET.
The response is written to self.wfile
"""
path = self.request.uri
instrument = "Not set"
try:
instrument, callback = get_instrument_and_callback(self.path)
instrument, callback = get_instrument_and_callback(path)

# Debug is only needed when debugging
logger.debug("Connection from " + str(self.client_address) + " looking at " + str(instrument))
logger.debug("Connection from {} looking at {}".format(self.request.remote_ip, instrument))

with scraped_data_lock:
if instrument == "ALL":
Expand All @@ -53,45 +56,44 @@ def do_GET(self):
try:
ans_as_json = str(json.dumps(ans))
except Exception as err:
raise ValueError("Unable to convert answer data to JSON: %s" % err.message)
raise ValueError("Unable to convert answer data to JSON: {}".format(err))

response = "{}({})".format(callback, ans_as_json)

self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(response.encode("utf-8"))
self.set_status(200)
self.set_header('Content-type', 'text/html')
self.write(response.encode("utf-8"))
except ValueError as e:
logger.exception("Value Error when getting data from {} for {}: {}".format(
self.client_address, instrument, e))
self.send_response(400)
self.request.remote_ip, instrument, e))
self.set_status(400)
except Exception as e:
logger.exception("Exception when getting data from {} for {}: {}".format(
self.client_address, instrument, e))
self.send_response(404)
self.request.remote_ip, instrument, e))
self.set_status(404)

def log_message(self, format, *args):
""" By overriding this method and doing nothing we disable writing to console
for every client request. Remove this to re-enable """
return


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""


if __name__ == '__main__':
# It can sometime be useful to define a local instrument list to add/override the instrument list do this here
# E.g. to add local instrument local_inst_list = {"localhost": "localhost"}
local_inst_list = {}
web_manager = WebScrapperManager(local_inst_list=local_inst_list)
web_manager.start()

server = ThreadedHTTPServer(('', PORT), MyHandler)
# As documented at https://github.com/tornadoweb/tornado/issues/2608
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

try:
while True:
server.serve_forever()
application = tornado.web.Application([
(r"/", MyHandler),
])
application.listen(PORT)
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print("Shutting down")
web_manager.stop()
Expand Down

0 comments on commit 43e987d

Please sign in to comment.