From 1f12678468fd1462510cb0e96d5d170a8ccfa3d8 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 18 Nov 2011 16:55:29 +0100 Subject: [PATCH] Get Worker fetch methods. --- rq/worker.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rq/worker.py b/rq/worker.py index 362d719..188b3aa 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -18,6 +18,29 @@ def iterable(x): return hasattr(x, '__iter__') class Worker(object): + redis_worker_namespace_prefix = 'rq:worker:' + + @classmethod + def all(cls): + """Returns an iterable of all Workers. + """ + prefix = cls.redis_worker_namespace_prefix + return map(cls.from_worker_key, conn.keys('%s*' % prefix)) + + @classmethod + def from_worker_key(cls, worker_key): + """Returns a Worker instance, based on the naming conventions for naming + the internal Redis keys. Can be used to reverse-lookup Queues by their + Redis keys. + """ + prefix = cls.redis_worker_namespace_prefix + name = worker_key[len(prefix):] + if not worker_key.startswith(prefix): + raise ValueError('Not a valid RQ worker key: %s' % (worker_key,)) + name = worker_key[len(prefix):] + return Worker([], name) + + def __init__(self, queues, name=None, rv_ttl=500): if isinstance(queues, Queue): queues = [queues]