Skip to content

Commit

Permalink
Add basic multiple beanstalkd support
Browse files Browse the repository at this point in the history
  • Loading branch information
fahhemsocialplex committed May 8, 2012
1 parent 0b0b817 commit 76f94f4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
10 changes: 8 additions & 2 deletions jack/beanstalk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
class Client(object):
""" A simple proxy object over the default client """

def __init__(self):
self.conn = Connection(settings.BEANSTALK_HOST, settings.BEANSTALK_PORT)
def __init__(self, request):
if hasattr(request, 'connection'):
self.conn = Connection(request.connection[0], request.connection[1])
elif settings.BEANSTALK_SERVERS:
server = settings.BEANSTALK_SERVERS[0]
self.conn = Connection(server[0], server[1])
else:
raise Exception("No servers defined.")

def __getattr__(self, name):
return getattr(self.conn, name)
Expand Down
17 changes: 17 additions & 0 deletions jack/beanstalk/multiple_beanstalk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.conf import settings

class Middleware(object):
def process_request(self, request):
if 'conn' not in request.COOKIES:
return

conn_id = int(request.COOKIES['conn'])
request.connection = settings.BEANSTALK_SERVERS[conn_id]

def ContextProcessor(request):
if 'conn' not in request.COOKIES:
conn_id = None
else:
conn_id = int(request.COOKIES['conn'])
return {'connections':settings.BEANSTALK_SERVERS,'conn_id':conn_id}

21 changes: 21 additions & 0 deletions jack/beanstalk/templates/beanstalk/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ <h1 style="float: left;font-size: 25px;">
<span style="float: right">
welcome {{ user }} |
<a href="/accounts/logout">logout</a>
<br><span>
<select id="change_connection">
{% for host, port in connections %}
<option value="{{forloop.counter0}}"
{% if conn_id == forloop.counter0 %}
selected="selected"
{% endif %}>
{{host}}:{{port}}
</option>
{% endfor %}
</select>
<script>
(function(){
var sel = document.getElementById('change_connection');
sel.onchange = function(){
document.cookie='conn='+sel.value;
location.reload();
};
})();
</script>
</span>
</span>

<div style="clear: both;"></div>
Expand Down
16 changes: 8 additions & 8 deletions jack/beanstalk/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _multiget(data, keys, default=None):
@login_required
def index(request):
try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand Down Expand Up @@ -62,7 +62,7 @@ def stats_table(request):
@login_required
def tube_stats(request, tube=None):
try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand All @@ -85,7 +85,7 @@ def tube_stats(request, tube=None):
@login_required
def tube_stats_table(request, tube=None):
try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand All @@ -107,7 +107,7 @@ def put(request):
if form.is_valid():

try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand Down Expand Up @@ -135,7 +135,7 @@ def inspect(request, id=None, tube_prefix='', tube=''):
id = None

try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand All @@ -162,7 +162,7 @@ def inspect(request, id=None, tube_prefix='', tube=''):

def _peek_if(request, status, tube):
try:
client = Client()
client = Client(request)
except ConnectionError:
return render_unavailable()

Expand Down Expand Up @@ -205,7 +205,7 @@ def _redirect_to_referer_or(request, dest):
@login_required
def job_delete(request, id):
try:
client = Client()
client = Client(request)
job = client.peek(int(id))

if job is not None:
Expand All @@ -219,7 +219,7 @@ def job_delete(request, id):
@login_required
def job_kick(request, id):
try:
client = Client()
client = Client(request)
client.use(request.POST['tube'])
# The argument to kick is number of jobs not jobId, by default one job
# is kicked.
Expand Down
12 changes: 9 additions & 3 deletions jack/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
}
}

BEANSTALK_HOST = 'localhost'
BEANSTALK_PORT = 11300

LOGIN_REDIRECT_URL = '/'

FLASH_IGNORE_MEDIA = True
Expand Down Expand Up @@ -101,6 +98,15 @@
'beanstalk',
)

### MULTIPLE BEANSTALKD SUPPORT
BEANSTALK_SERVERS = (

This comment has been minimized.

Copy link
@modsaid

modsaid Mar 24, 2013

What is the expected format of the servers to pass here?
is it "localhost:11300" for example?. or ['localhost','11300']? or what?

This comment has been minimized.

Copy link
@andreisavu

andreisavu Mar 24, 2013

Owner

I think ['localhost','11300']

This comment has been minimized.

Copy link
@modsaid

modsaid Mar 24, 2013

I managed to get it working
it expects:
[['127.0.0.1', 11300]]

which represents an array of servers, each represented by an array of host,port tuples

Thanks

)
from django.conf import global_settings
MIDDLEWARE_CLASSES += ('beanstalk.multiple_beanstalk.Middleware',)
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'beanstalk.multiple_beanstalk.ContextProcessor',
)

try:
from local_settings import *
except ImportError:
Expand Down

0 comments on commit 76f94f4

Please sign in to comment.