mirror of https://github.com/peter4431/rq.git
New connection management.
Connections can now be set explicitly on Queues, Workers, and Jobs. Jobs that are implicitly created by Queue or Worker API calls now inherit the connection of their creator's. For all RQ object instances that are created now holds that the "current" connection is used if none is passed in explicitly. The "current" connection is thus hold on to at creation time and won't be changed for the lifetime of the object. Effectively, this means that, given a default Redis connection, say you create a queue Q1, then push another Redis connection onto the connection stack, then create Q2. In that case, Q1 means a queue on the first connection and Q2 on the second connection. This is way more clear than it used to be. Also, I've removed the `use_redis()` call, which was named ugly. Instead, some new alternatives for connection management now exist. You can push/pop connections now: >>> my_conn = Redis() >>> push_connection(my_conn) >>> q = Queue() >>> q.connection == my_conn True >>> pop_connection() == my_conn Also, you can stack them syntactically: >>> conn1 = Redis() >>> conn2 = Redis('example.org', 1234) >>> with Connection(conn1): ... q = Queue() ... with Connection(conn2): ... q2 = Queue() ... q3 = Queue() >>> q.connection == conn1 True >>> q2.connection == conn2 True >>> q3.connection == conn1 True Or, if you only require a single connection to Redis (for most uses): >>> use_connection(Redis())main
parent
a662180fd3
commit
2982486448
@ -0,0 +1,82 @@
|
||||
from contextlib import contextmanager
|
||||
from redis import Redis
|
||||
|
||||
|
||||
class NoRedisConnectionException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class _RedisConnectionStack(object):
|
||||
def __init__(self):
|
||||
self.stack = []
|
||||
|
||||
def _get_current_object(self):
|
||||
try:
|
||||
return self.stack[-1]
|
||||
except IndexError:
|
||||
msg = 'No Redis connection configured.'
|
||||
raise NoRedisConnectionException(msg)
|
||||
|
||||
def pop(self):
|
||||
return self.stack.pop()
|
||||
|
||||
def push(self, connection):
|
||||
self.stack.append(connection)
|
||||
|
||||
def empty(self):
|
||||
del self.stack[:]
|
||||
|
||||
def depth(self):
|
||||
return len(self.stack)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._get_current_object(), name)
|
||||
|
||||
|
||||
_connection_stack = _RedisConnectionStack()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def Connection(connection=None):
|
||||
if connection is None:
|
||||
connection = Redis()
|
||||
_connection_stack.push(connection)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
popped = _connection_stack.pop()
|
||||
assert popped == connection, \
|
||||
'Unexpected Redis connection was popped off the stack. ' \
|
||||
'Check your Redis connection setup.'
|
||||
|
||||
|
||||
def push_connection(redis):
|
||||
"""Pushes the given connection on the stack."""
|
||||
_connection_stack.push(redis)
|
||||
|
||||
|
||||
def pop_connection():
|
||||
"""Pops the topmost connection from the stack."""
|
||||
return _connection_stack.pop()
|
||||
|
||||
|
||||
def use_connection(redis):
|
||||
"""Clears the stack and uses the given connection. Protects against mixed
|
||||
use of use_connection() and stacked connection contexts.
|
||||
"""
|
||||
assert _connection_stack.depth() <= 1, \
|
||||
'You should not mix Connection contexts with use_connection().'
|
||||
_connection_stack.empty()
|
||||
push_connection(redis)
|
||||
|
||||
|
||||
def get_current_connection():
|
||||
"""Returns the current Redis connection (i.e. the topmost on the
|
||||
connection stack).
|
||||
"""
|
||||
return _connection_stack._get_current_object()
|
||||
|
||||
|
||||
__all__ = ['Connection',
|
||||
'get_current_connection', 'push_connection', 'pop_connection',
|
||||
'use_connection']
|
@ -1,28 +0,0 @@
|
||||
class NoRedisConnectionException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class RedisConnectionProxy(object):
|
||||
def __init__(self):
|
||||
self.stack = []
|
||||
|
||||
def _get_current_object(self):
|
||||
try:
|
||||
return self.stack[-1]
|
||||
except IndexError:
|
||||
msg = 'No Redis connection configured.'
|
||||
raise NoRedisConnectionException(msg)
|
||||
|
||||
def pop(self):
|
||||
return self.stack.pop()
|
||||
|
||||
def push(self, db):
|
||||
self.stack.append(db)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._get_current_object(), name)
|
||||
|
||||
|
||||
conn = RedisConnectionProxy()
|
||||
|
||||
__all__ = ['conn']
|
@ -0,0 +1,36 @@
|
||||
from tests import RQTestCase, find_empty_redis_database
|
||||
from tests.fixtures import do_nothing
|
||||
from rq import Queue
|
||||
from rq import Connection
|
||||
|
||||
|
||||
def new_connection():
|
||||
return find_empty_redis_database()
|
||||
|
||||
|
||||
class TestConnectionInheritance(RQTestCase):
|
||||
def test_connection_detection(self):
|
||||
"""Automatic detection of the connection."""
|
||||
q = Queue()
|
||||
self.assertEquals(q.connection, self.testconn)
|
||||
|
||||
def test_connection_stacking(self):
|
||||
"""Connection stacking."""
|
||||
conn1 = new_connection()
|
||||
conn2 = new_connection()
|
||||
|
||||
with Connection(conn1):
|
||||
q1 = Queue()
|
||||
with Connection(conn2):
|
||||
q2 = Queue()
|
||||
self.assertNotEquals(q1.connection, q2.connection)
|
||||
|
||||
def test_connection_pass_thru(self):
|
||||
"""Connection passed through from queues to jobs."""
|
||||
q1 = Queue()
|
||||
with Connection(new_connection()):
|
||||
q2 = Queue()
|
||||
job1 = q1.enqueue(do_nothing)
|
||||
job2 = q2.enqueue(do_nothing)
|
||||
self.assertEquals(q1.connection, job1.connection)
|
||||
self.assertEquals(q2.connection, job2.connection)
|
Loading…
Reference in New Issue