Make sure the webserver fails properly if there is no config file.

Create the queue list in a more pythonesque fashion.
This commit is contained in:
Raphaël Vinot 2014-08-26 17:33:28 +02:00
parent 264da48768
commit d7a7da4814

View file

@ -6,10 +6,17 @@ import ConfigParser
import json import json
from flask import Flask, render_template, jsonify from flask import Flask, render_template, jsonify
import flask import flask
import os
# CONFIG # # 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 = ConfigParser.ConfigParser()
cfg.read('../../bin/packages/config.cfg') cfg.read(configfile)
# REDIS # # REDIS #
r_serv = redis.StrictRedis( r_serv = redis.StrictRedis(
@ -35,6 +42,11 @@ def event_stream():
yield 'data: %s\n\n' % json.dumps(msg) 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") @app.route("/_logs")
def logs(): def logs():
return flask.Response(event_stream(), mimetype="text/event-stream") return flask.Response(event_stream(), mimetype="text/event-stream")
@ -42,18 +54,11 @@ def logs():
@app.route("/_stuff", methods=['GET']) @app.route("/_stuff", methods=['GET'])
def stuff(): def stuff():
row1 = [] return jsonify(row1=get_queues(r_serv))
for queue in r_serv.smembers("queues"):
row1.append((queue, r_serv.llen(queue)))
return jsonify(row1=row1)
@app.route("/") @app.route("/")
def index(): def index():
row = []
for queue in r_serv.smembers("queues"):
row.append((queue, r_serv.llen(queue)))
return render_template("index.html") return render_template("index.html")