From e1e633fa76c9c57431445a0e8806c6952023e6c9 Mon Sep 17 00:00:00 2001 From: Antoine Goutenoir Date: Thu, 15 Jul 2021 05:00:37 +0200 Subject: [PATCH] chore: improve error handling in production env --- flaskr/controllers/main_controller.py | 29 ++++++++++++++--------------- flaskr/models.py | 48 +++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py index e129026..9362cd6 100644 --- a/flaskr/controllers/main_controller.py +++ b/flaskr/controllers/main_controller.py @@ -254,7 +254,6 @@ def estimate(): # register new estimation request, more accurately public_id=estimation.public_id, extension='html' )) - # return render_template("estimate-debrief.html", form=form) return show_form() @@ -720,9 +719,6 @@ def compute(): # process the queue of estimation requests return _respond(errmsg) -unavailable_statuses = [StatusEnum.pending, StatusEnum.working] - - @main.route("/estimation/.") def consult_estimation(public_id, extension): try: @@ -741,13 +737,16 @@ def consult_estimation(public_id, extension): if extension in ['xhtml', 'html', 'htm']: - if estimation.status in unavailable_statuses: + if estimation.status in estimation.unavailable_statuses: return render_template( "estimation-queue-wait.html", estimation=estimation ) else: - estimation_output = estimation.get_output_dict() + try: + estimation_output = estimation.get_output_dict() + except Exception as e: + return abort(404) estimation_sum = 0 if estimation_output: for city in estimation_output['cities']: @@ -762,16 +761,16 @@ def consult_estimation(public_id, extension): elif extension in ['yaml', 'yml']: - if estimation.status in unavailable_statuses: - abort(404) + if not estimation.is_available(): + return abort(404) return u"%s" % yaml_dump(estimation.get_output_dict()) # return estimation.output_yaml elif 'csv' == extension: - if estimation.status in unavailable_statuses: - abort(404) + if not estimation.is_available(): + return abort(404) si = StringIO() cw = csv.writer(si, quoting=csv.QUOTE_ALL) @@ -811,7 +810,7 @@ def consult_estimation(public_id, extension): ) else: - abort(404) + return abort(404) def get_locations(addresses): @@ -877,8 +876,8 @@ def get_trips_csv(public_id, destination_index=0): except Exception as e: return abort(500) - if estimation.status in unavailable_statuses: - abort(404) + if not estimation.is_available(): + return abort(404) si = StringIO() cw = csv.writer(si, quoting=csv.QUOTE_ALL) @@ -891,12 +890,12 @@ def get_trips_csv(public_id, destination_index=0): results = estimation.get_output_dict() if not 'cities' in results: - abort(500) + return abort(500) cities_length = len(results['cities']) if 0 == cities_length: - abort(500, Response("No cities in results.")) + return abort(500, Response("No cities in results.")) destination_index = min(destination_index, cities_length - 1) destination_index = max(destination_index, 0) diff --git a/flaskr/models.py b/flaskr/models.py index d26707e..93b650e 100755 --- a/flaskr/models.py +++ b/flaskr/models.py @@ -42,6 +42,8 @@ class Estimation(db.Model): default=lambda: generate_unique_id(), unique=True ) + + unavailable_statuses = [StatusEnum.pending, StatusEnum.working] status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending) email = db.Column(db.Unicode(1024)) @@ -121,8 +123,6 @@ class Estimation(db.Model): return join(runs_dir, self.public_id) def set_output_dict(self, output): - # with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf: - # shelf['output'] = output shelf = shelve.open( filename=self.get_output_filename(), flag='c', # read/write, create if needed @@ -139,21 +139,25 @@ class Estimation(db.Model): output_filename = self.get_output_filename() if isfile(output_filename): - # Perhaps we'll need a mutex around here - # from threading import Lock - # mutex = Lock() - # mutex.acquire() - - # Not using the `with …` syntax, but we may in python3 - shelf = shelve.open( - filename=output_filename, - flag='r', - protocol=2 - ) - self._output_dict = shelf['output'] - shelf.close() - - # mutex.release() + try: + # Perhaps we'll need a mutex around here + # from threading import Lock + # mutex = Lock() + # mutex.acquire() + + # Not using the `with …` syntax, but we may in python3 + shelf = shelve.open( + filename=output_filename, + flag='r', + protocol=2 + ) + self._output_dict = shelf['output'] + shelf.close() + + # mutex.release() + except Exception as e: + + return None else: self._output_dict = None else: @@ -176,10 +180,16 @@ class Estimation(db.Model): def get_models(self): if self._models is None: - mdl_slugs = self.models_slugs.split("\n") - self._models = [m for m in models if m.slug in mdl_slugs] + slugs = self.models_slugs.split("\n") + self._models = [m for m in models if m.slug in slugs] return self._models + def is_available(self): + if self.status in self.unavailable_statuses: + return False + # We might add more conditions here, such as file data availability + return True + # BACKOFFICE CONFIGURATION #################################################### -- libgit2 0.21.2