Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

1 participant

@selwin
Owner

Hi Vincent,

This is just a small patch to make Worker accept a connection argument, consistent with Queue.

I use this feature to find active workers in a django app I wrote: https://github.com/ui/django-rq . Please take a look at it when you're free and let me know if you have any comments or suggestions :)

@selwin selwin merged commit 50ba003 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 16, 2012
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 9 deletions.
  1. +12 −9 rq/worker.py
View
21 rq/worker.py
@@ -50,15 +50,17 @@ class Worker(object):
redis_workers_keys = 'rq:workers'
@classmethod
- def all(cls):
+ def all(cls, connection=None):
"""Returns an iterable of all Workers.
"""
- conn = get_current_connection()
- reported_working = conn.smembers(cls.redis_workers_keys)
- return compact(map(cls.find_by_key, reported_working))
+ if connection is None:
+ connection = get_current_connection()
+ reported_working = connection.smembers(cls.redis_workers_keys)
+ workers = [cls.find_by_key(key, connection) for key in reported_working]
+ return compact(workers)
@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
naming the internal Redis keys. Can be used to reverse-lookup Workers
by their Redis keys.
@@ -68,14 +70,15 @@ def find_by_key(cls, worker_key):
if not worker_key.startswith(prefix):
raise ValueError('Not a valid RQ worker key: %s' % (worker_key,))
- conn = get_current_connection()
- if not conn.exists(worker_key):
+ if connection is None:
+ connection = get_current_connection()
+ if not connection.exists(worker_key):
return None
name = worker_key[len(prefix):]
worker = cls([], name)
- queues = conn.hget(worker.key, 'queues')
- worker._state = conn.hget(worker.key, 'state') or '?'
+ queues = connection.hget(worker.key, 'queues')
+ worker._state = connection.hget(worker.key, 'state') or '?'
if queues:
worker.queues = map(Queue, queues.split(','))
return worker
Something went wrong with that request. Please try again.