From d7a7da4814aa05ee027b0a8839c086113d9df3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 26 Aug 2014 17:33:28 +0200 Subject: [PATCH] Make sure the webserver fails properly if there is no config file. Create the queue list in a more pythonesque fashion. --- var/www/Flask_server.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py index 7bef0295..214cb22f 100755 --- a/var/www/Flask_server.py +++ b/var/www/Flask_server.py @@ -6,10 +6,17 @@ import ConfigParser import json from flask import Flask, render_template, jsonify import flask +import os # CONFIG # +configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg') +if not os.path.exists(configfile): + raise Exception('Unable to find the configuration file. \ + Did you set environment variables? \ + Or activate the virtualenv.') + cfg = ConfigParser.ConfigParser() -cfg.read('../../bin/packages/config.cfg') +cfg.read(configfile) # REDIS # r_serv = redis.StrictRedis( @@ -35,6 +42,11 @@ def event_stream(): yield 'data: %s\n\n' % json.dumps(msg) +def get_queues(r): + # We may want to put the llen in a pipeline to do only one query. + return [(queue, r.llen(queue)) for queue in r.smembers("queues")] + + @app.route("/_logs") def logs(): return flask.Response(event_stream(), mimetype="text/event-stream") @@ -42,18 +54,11 @@ def logs(): @app.route("/_stuff", methods=['GET']) def stuff(): - row1 = [] - for queue in r_serv.smembers("queues"): - row1.append((queue, r_serv.llen(queue))) - return jsonify(row1=row1) + return jsonify(row1=get_queues(r_serv)) @app.route("/") def index(): - row = [] - for queue in r_serv.smembers("queues"): - row.append((queue, r_serv.llen(queue))) - return render_template("index.html")