Skip to content

Commit

Permalink
Merge pull request Parallels#49 from codebynumbers/master
Browse files Browse the repository at this point in the history
Create redis connection at start on app launch
  • Loading branch information
nvie committed Sep 3, 2013
2 parents 812e75c + 4b783e5 commit 12e1105
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions rq_dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, app=None, url_prefix='/rq', auth_handler=None):
else:
self.app = None
self.auth_handler = auth_handler
self.redis_conn = None

def init_app(self, app):
"""Initializes the RQ-Dashboard for the specified application."""
Expand Down
18 changes: 14 additions & 4 deletions rq_dashboard/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from redis import Redis
from redis import from_url
from rq import push_connection
from rq import push_connection, pop_connection
from functools import wraps
import times
from flask import Blueprint
Expand All @@ -27,16 +27,26 @@ def authentication_hook():
if auth_handler and not auth_handler():
abort(401)


@dashboard.before_app_first_request
def setup_rq_connection():
if current_app.config.get('REDIS_URL'):
redis_conn = from_url(current_app.config.get('REDIS_URL'))
current_app.redis_conn = from_url(current_app.config.get('REDIS_URL'))
else:
redis_conn = Redis(host=current_app.config.get('REDIS_HOST', 'localhost'),
current_app.redis_conn = Redis(host=current_app.config.get('REDIS_HOST', 'localhost'),
port=current_app.config.get('REDIS_PORT', 6379),
password=current_app.config.get('REDIS_PASSWORD', None),
db=current_app.config.get('REDIS_DB', 0))
push_connection(redis_conn)


@dashboard.before_request
def push_rq_connection():
push_connection(current_app.redis_conn)


@dashboard.teardown_request
def pop_rq_connection(exception=None):
pop_connection()


def jsonify(f):
Expand Down

0 comments on commit 12e1105

Please sign in to comment.