Skip to content

Commit

Permalink
Make the flask app more pythonic
Browse files Browse the repository at this point in the history
  • Loading branch information
philberty committed Jul 13, 2014
1 parent f5ce6af commit 1aff1af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
8 changes: 4 additions & 4 deletions WatchyServer/StatsAggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

from datetime import datetime

from StatsApp import StatSession_Logs, \
StatSession_Hosts, \
StatSession_Process, \
StatSession_Metrics
from BackendUtil import AsyncBackend
from BackendUtil import BackendDispatch
from StatsServer import StatSession_Logs
from StatsServer import StatSession_Hosts
from StatsServer import StatSession_Process
from StatsServer import StatSession_Metrics

def consumer (func):
def decorated (*args, **kwargs):
Expand Down
32 changes: 0 additions & 32 deletions WatchyServer/StatsServer.py → WatchyServer/StatsApp.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import os
import sys
import json
import traceback
import ServerUtil

from flask import Flask
from flask import jsonify
from flask import render_template

from gevent.pywsgi import WSGIServer

StatSession_Logs = { }
StatSession_Hosts = { }
StatSession_Process = { }
Expand All @@ -19,8 +13,6 @@
sfolder = os.path.join (os.path.dirname (os.path.abspath (__file__)), 'static')
app = Flask ('WatchyServer', template_folder=tfolder, static_folder=sfolder)

from StatsAggregator import UDPStatsServer

@app.route ("/")
def index ():
return render_template ('index.html')
Expand Down Expand Up @@ -73,27 +65,3 @@ def getLogData (key):
@app.route ("/deps/<path:path>")
def statics (path):
return app.send_static_file (path)

class WatchyDServer:
def __init__ (self, cacheLimit, backends, wbind='localhost', wport=8080,
ubind='localhost', uport=8081):
self.web_bind = wbind
self.web_port = wport
self.udp_server = UDPStatsServer (host=ubind, port=uport,
climit=cacheLimit, backends=backends)

def listen (self):
try:
self.udp_server.start ()
ServerUtil.info ('WSGIServer:[gevent] starting http://%s:%i/' \
% (self.web_bind, self.web_port))
http_server = WSGIServer ((self.web_bind, self.web_port), app)
http_server.serve_forever()
except KeyboardInterrupt:
ServerUtil.warning ('Caught keyboard interupt stopping')
except:
ServerUtil.error ("%s" % traceback.format_exc ())
ServerUtil.error ("%s" % sys.exc_info ()[1])
finally:
ServerUtil.info ("Shutting down Stats Aggregator")
self.udp_server.running = False
33 changes: 32 additions & 1 deletion WatchyServer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
__all__ = [ 'StatsAggregator.py', 'StatsData.py', 'StatsServer.py', 'ServerUtil.py' ]
import sys
import traceback
import ServerUtil

from StatsApp import app
from gevent.pywsgi import WSGIServer
from StatsAggregator import UDPStatsServer

class WatchyDServer:
def __init__ (self, cacheLimit, backends, wbind='localhost', wport=8080,
ubind='localhost', uport=8081):
self.web_bind = wbind
self.web_port = wport
self.udp_server = UDPStatsServer (host=ubind, port=uport,
climit=cacheLimit, backends=backends)

def listen (self):
try:
self.udp_server.start ()
ServerUtil.info ('WSGIServer:[gevent] starting http://%s:%i/' \
% (self.web_bind, self.web_port))
http_server = WSGIServer ((self.web_bind, self.web_port), app)
http_server.serve_forever()
except KeyboardInterrupt:
ServerUtil.warning ('Caught keyboard interupt stopping')
except:
ServerUtil.error ("%s" % traceback.format_exc ())
ServerUtil.error ("%s" % sys.exc_info ()[1])
finally:
ServerUtil.info ("Shutting down Stats Aggregator")
self.udp_server.running = False

6 changes: 3 additions & 3 deletions watchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import traceback
import logging.config

import WatchyServer
from WatchyServer import ServerUtil
from WatchyServer import StatsServer
from WatchyServer import BackendUtil

from ConfigParser import RawConfigParser as CParser
Expand Down Expand Up @@ -68,8 +68,8 @@ def serverMain ():
else:
print pid
sys.exit (0)
StatsServer.WatchyDServer (limit, backends, wbind=rbind, wport=rport,
ubind=abind, uport=aport).listen ()
WatchyServer.WatchyDServer (limit, backends, wbind=rbind, wport=rport,
ubind=abind, uport=aport).listen ()

if __name__ == "__main__":
serverMain ()

0 comments on commit 1aff1af

Please sign in to comment.