Skip to content

Commit

Permalink
Merge pull request #120 from anentropic/redis-from-url
Browse files Browse the repository at this point in the history
allow to specify main redis conenction params in url form
  • Loading branch information
coleifer committed Oct 11, 2015
2 parents 6782a1e + 52d43c7 commit a1d8158
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/django.rst
Expand Up @@ -37,6 +37,8 @@ Using redis
'backend': 'huey.backends.redis_backend', # required.
'name': 'unique name',
'connection': {'host': 'localhost', 'port': 6379},
# or by url:
# 'connection': {'url': 'redis://localhost/1'}
'always_eager': False, # Defaults to False when running via manage.py run_huey
# Options to pass into the consumer when running ``manage.py run_huey``
Expand Down
17 changes: 13 additions & 4 deletions huey/backends/redis_backend.py
Expand Up @@ -15,6 +15,15 @@ def clean_name(name):
return re.sub('[^a-z0-9]', '', name)


def get_connection(**config):
try:
url = config.pop('url')
except KeyError:
return redis.Redis(**config)
else:
return redis.Redis.from_url(url, **config)


class RedisQueue(BaseQueue):
"""
A simple Queue that uses the redis to store messages
Expand All @@ -30,7 +39,7 @@ def __init__(self, name, **connection):
super(RedisQueue, self).__init__(name, **connection)

self.queue_name = 'huey.redis.%s' % clean_name(name)
self.conn = redis.Redis(**connection)
self.conn = get_connection(**connection)

def write(self, data):
self.conn.lpush(self.queue_name, data)
Expand Down Expand Up @@ -95,7 +104,7 @@ def __init__(self, name, **connection):
super(RedisSchedule, self).__init__(name, **connection)

self.key = 'huey.schedule.%s' % clean_name(name)
self.conn = redis.Redis(**connection)
self.conn = get_connection(**connection)
self._pop = self.conn.register_script(SCHEDULE_POP_LUA)

def convert_ts(self, ts):
Expand All @@ -120,7 +129,7 @@ def __init__(self, name, **connection):
super(RedisDataStore, self).__init__(name, **connection)

self.storage_name = 'huey.results.%s' % clean_name(name)
self.conn = redis.Redis(**connection)
self.conn = get_connection(**connection)

def put(self, key, value):
self.conn.hset(self.storage_name, key, value)
Expand All @@ -143,7 +152,7 @@ def flush(self):
class RedisEventEmitter(BaseEventEmitter):
def __init__(self, channel, **connection):
super(RedisEventEmitter, self).__init__(channel, **connection)
self.conn = redis.Redis(**connection)
self.conn = get_connection(**connection)

def emit(self, message):
self.conn.publish(self.channel, message)
Expand Down

0 comments on commit a1d8158

Please sign in to comment.