Worker's "all" and "find_by_key" methods should accept "connection" as argument.

main
Selwin Ong 13 years ago
parent c423cab770
commit 50ba003cee

@ -50,15 +50,17 @@ class Worker(object):
redis_workers_keys = 'rq:workers' redis_workers_keys = 'rq:workers'
@classmethod @classmethod
def all(cls): def all(cls, connection=None):
"""Returns an iterable of all Workers. """Returns an iterable of all Workers.
""" """
conn = get_current_connection() if connection is None:
reported_working = conn.smembers(cls.redis_workers_keys) connection = get_current_connection()
return compact(map(cls.find_by_key, reported_working)) reported_working = connection.smembers(cls.redis_workers_keys)
workers = [cls.find_by_key(key, connection) for key in reported_working]
return compact(workers)
@classmethod @classmethod
def find_by_key(cls, worker_key): def find_by_key(cls, worker_key, connection=None):
"""Returns a Worker instance, based on the naming conventions for """Returns a Worker instance, based on the naming conventions for
naming the internal Redis keys. Can be used to reverse-lookup Workers naming the internal Redis keys. Can be used to reverse-lookup Workers
by their Redis keys. by their Redis keys.
@ -68,14 +70,15 @@ class Worker(object):
if not worker_key.startswith(prefix): if not worker_key.startswith(prefix):
raise ValueError('Not a valid RQ worker key: %s' % (worker_key,)) raise ValueError('Not a valid RQ worker key: %s' % (worker_key,))
conn = get_current_connection() if connection is None:
if not conn.exists(worker_key): connection = get_current_connection()
if not connection.exists(worker_key):
return None return None
name = worker_key[len(prefix):] name = worker_key[len(prefix):]
worker = cls([], name) worker = cls([], name)
queues = conn.hget(worker.key, 'queues') queues = connection.hget(worker.key, 'queues')
worker._state = conn.hget(worker.key, 'state') or '?' worker._state = connection.hget(worker.key, 'state') or '?'
if queues: if queues:
worker.queues = map(Queue, queues.split(',')) worker.queues = map(Queue, queues.split(','))
return worker return worker

Loading…
Cancel
Save