rq info management command now cleans up registries when first run (#1107)

* rq info management command now cleans up registries when first run

* Deleted print statement

* Improve CLI test coverage

* Fixed CLI test on Linux
main
Selwin Ong 6 years ago committed by GitHub
parent 905824e8d8
commit 549648bd1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,10 +23,11 @@ from rq.defaults import (DEFAULT_CONNECTION_CLASS, DEFAULT_JOB_CLASS,
DEFAULT_JOB_MONITORING_INTERVAL, DEFAULT_JOB_MONITORING_INTERVAL,
DEFAULT_LOGGING_FORMAT, DEFAULT_LOGGING_DATE_FORMAT) DEFAULT_LOGGING_FORMAT, DEFAULT_LOGGING_DATE_FORMAT)
from rq.exceptions import InvalidJobOperationError from rq.exceptions import InvalidJobOperationError
from rq.registry import FailedJobRegistry from rq.registry import FailedJobRegistry, clean_registries
from rq.utils import import_attribute from rq.utils import import_attribute
from rq.suspension import (suspend as connection_suspend, from rq.suspension import (suspend as connection_suspend,
resume as connection_resume, is_suspended) resume as connection_resume, is_suspended)
from rq.worker_registration import clean_worker_registry
# Disable the warning that Click displays (as of Click version 5.0) when users # Disable the warning that Click displays (as of Click version 5.0) when users
@ -162,7 +163,17 @@ def info(cli_config, interval, raw, only_queues, only_workers, by_queue, queues,
try: try:
with Connection(cli_config.connection): with Connection(cli_config.connection):
refresh(interval, func, queues, raw, by_queue,
if queues:
qs = list(map(cli_config.queue_class, queues))
else:
qs = cli_config.queue_class.all()
for queue in qs:
clean_registries(queue)
clean_worker_registry(queue)
refresh(interval, func, qs, raw, by_queue,
cli_config.queue_class, cli_config.worker_class) cli_config.queue_class, cli_config.worker_class)
except ConnectionError as e: except ConnectionError as e:
click.echo(e) click.echo(e)

@ -86,10 +86,6 @@ def state_symbol(state):
def show_queues(queues, raw, by_queue, queue_class, worker_class): def show_queues(queues, raw, by_queue, queue_class, worker_class):
if queues:
qs = list(map(queue_class, queues))
else:
qs = queue_class.all()
num_jobs = 0 num_jobs = 0
termwidth, _ = click.get_terminal_size() termwidth, _ = click.get_terminal_size()
@ -97,14 +93,14 @@ def show_queues(queues, raw, by_queue, queue_class, worker_class):
max_count = 0 max_count = 0
counts = dict() counts = dict()
for q in qs: for q in queues:
count = q.count count = q.count
counts[q] = count counts[q] = count
max_count = max(max_count, count) max_count = max(max_count, count)
scale = get_scale(max_count) scale = get_scale(max_count)
ratio = chartwidth * 1.0 / scale ratio = chartwidth * 1.0 / scale
for q in qs: for q in queues:
count = counts[q] count = counts[q]
if not raw: if not raw:
chart = green('|' + '' * int(ratio * count)) chart = green('|' + '' * int(ratio * count))
@ -117,22 +113,15 @@ def show_queues(queues, raw, by_queue, queue_class, worker_class):
# print summary when not in raw mode # print summary when not in raw mode
if not raw: if not raw:
click.echo('%d queues, %d jobs total' % (len(qs), num_jobs)) click.echo('%d queues, %d jobs total' % (len(queues), num_jobs))
def show_workers(queues, raw, by_queue, queue_class, worker_class): def show_workers(queues, raw, by_queue, queue_class, worker_class):
if queues:
qs = list(map(queue_class, queues))
workers = set() workers = set()
for queue in qs: for queue in queues:
for worker in worker_class.all(queue=queue): for worker in worker_class.all(queue=queue):
workers.add(worker) workers.add(worker)
else:
qs = queue_class.all()
workers = worker_class.all()
if not by_queue: if not by_queue:
for worker in workers: for worker in workers:
@ -146,7 +135,7 @@ def show_workers(queues, raw, by_queue, queue_class, worker_class):
else: else:
# Display workers by queue # Display workers by queue
queue_dict = {} queue_dict = {}
for queue in qs: for queue in queues:
queue_dict[queue] = worker_class.all(queue=queue) queue_dict[queue] = worker_class.all(queue=queue)
if queue_dict: if queue_dict:
@ -166,7 +155,7 @@ def show_workers(queues, raw, by_queue, queue_class, worker_class):
click.echo('%s %s' % (pad(queue.name + ':', max_length + 1), queues_str)) click.echo('%s %s' % (pad(queue.name + ':', max_length + 1), queues_str))
if not raw: if not raw:
click.echo('%d workers, %d queues' % (len(workers), len(qs))) click.echo('%d workers, %d queues' % (len(workers), len(queues)))
def show_both(queues, raw, by_queue, queue_class, worker_class): def show_both(queues, raw, by_queue, queue_class, worker_class):

@ -184,6 +184,11 @@ class TestRQCli(RQTestCase):
self.assert_normal_execution(result) self.assert_normal_execution(result)
self.assertIn('0 workers, 0 queue', result.output) self.assertIn('0 workers, 0 queue', result.output)
result = runner.invoke(main, ['info', '--by-queue',
'-u', self.redis_url, '--only-workers'])
self.assert_normal_execution(result)
self.assertIn('0 workers, 0 queue', result.output)
queue = Queue(connection=self.connection) queue = Queue(connection=self.connection)
queue.enqueue(say_hello) queue.enqueue(say_hello)
result = runner.invoke(main, ['info', '-u', self.redis_url, '--only-workers']) result = runner.invoke(main, ['info', '-u', self.redis_url, '--only-workers'])
@ -232,6 +237,19 @@ class TestRQCli(RQTestCase):
self.assertTrue(len(pid.read()) > 0) self.assertTrue(len(pid.read()) > 0)
self.assert_normal_execution(result) self.assert_normal_execution(result)
def test_worker_logging_options(self):
"""--quiet and --verbose logging options are supported"""
runner = CliRunner()
args = ['worker', '-u', self.redis_url, '-b']
result = runner.invoke(main, args + ['--verbose'])
self.assert_normal_execution(result)
result = runner.invoke(main, args + ['--quiet'])
self.assert_normal_execution(result)
# --quiet and --verbose are mutually exclusive
result = runner.invoke(main, args + ['--quiet', '--verbose'])
self.assertNotEqual(result.exit_code, 0)
def test_exception_handlers(self): def test_exception_handlers(self):
"""rq worker -u <url> -b --exception-handler <handler>""" """rq worker -u <url> -b --exception-handler <handler>"""
connection = Redis.from_url(self.redis_url) connection = Redis.from_url(self.redis_url)

Loading…
Cancel
Save