diff --git a/rq/cli/cli.py b/rq/cli/cli.py index e621441..6cdefcf 100755 --- a/rq/cli/cli.py +++ b/rq/cli/cli.py @@ -23,10 +23,11 @@ from rq.defaults import (DEFAULT_CONNECTION_CLASS, DEFAULT_JOB_CLASS, DEFAULT_JOB_MONITORING_INTERVAL, DEFAULT_LOGGING_FORMAT, DEFAULT_LOGGING_DATE_FORMAT) 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.suspension import (suspend as connection_suspend, 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 @@ -162,7 +163,17 @@ def info(cli_config, interval, raw, only_queues, only_workers, by_queue, queues, try: 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) except ConnectionError as e: click.echo(e) @@ -178,9 +189,9 @@ def info(cli_config, interval, raw, only_queues, only_workers, by_queue, queues, @click.option('--log-format', type=str, default=DEFAULT_LOGGING_FORMAT, help='Set the format of the logs') @click.option('--date-format', type=str, default=DEFAULT_LOGGING_DATE_FORMAT, help='Set the date format of the logs') @click.option('--name', '-n', help='Specify a different name') -@click.option('--results-ttl', type=int, default=DEFAULT_RESULT_TTL , help='Default results timeout to be used') -@click.option('--worker-ttl', type=int, default=DEFAULT_WORKER_TTL , help='Default worker timeout to be used') -@click.option('--job-monitoring-interval', type=int, default=DEFAULT_JOB_MONITORING_INTERVAL , help='Default job monitoring interval to be used') +@click.option('--results-ttl', type=int, default=DEFAULT_RESULT_TTL, help='Default results timeout to be used') +@click.option('--worker-ttl', type=int, default=DEFAULT_WORKER_TTL, help='Default worker timeout to be used') +@click.option('--job-monitoring-interval', type=int, default=DEFAULT_JOB_MONITORING_INTERVAL, help='Default job monitoring interval to be used') @click.option('--disable-job-desc-logging', is_flag=True, help='Turn off description logging.') @click.option('--verbose', '-v', is_flag=True, help='Show more output') @click.option('--quiet', '-q', is_flag=True, help='Show less output') diff --git a/rq/cli/helpers.py b/rq/cli/helpers.py index 49ba255..851f319 100644 --- a/rq/cli/helpers.py +++ b/rq/cli/helpers.py @@ -86,10 +86,6 @@ def state_symbol(state): 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 termwidth, _ = click.get_terminal_size() @@ -97,14 +93,14 @@ def show_queues(queues, raw, by_queue, queue_class, worker_class): max_count = 0 counts = dict() - for q in qs: + for q in queues: count = q.count counts[q] = count max_count = max(max_count, count) scale = get_scale(max_count) ratio = chartwidth * 1.0 / scale - for q in qs: + for q in queues: count = counts[q] if not raw: chart = green('|' + '█' * int(ratio * count)) @@ -117,21 +113,14 @@ def show_queues(queues, raw, by_queue, queue_class, worker_class): # print summary when not in raw mode 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): - if queues: - qs = list(map(queue_class, queues)) - - workers = set() - for queue in qs: - for worker in worker_class.all(queue=queue): - workers.add(worker) - - else: - qs = queue_class.all() - workers = worker_class.all() + workers = set() + for queue in queues: + for worker in worker_class.all(queue=queue): + workers.add(worker) if not by_queue: @@ -146,7 +135,7 @@ def show_workers(queues, raw, by_queue, queue_class, worker_class): else: # Display workers by queue queue_dict = {} - for queue in qs: + for queue in queues: queue_dict[queue] = worker_class.all(queue=queue) 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)) 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): diff --git a/tests/test_cli.py b/tests/test_cli.py index 060558c..cf4df3b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -184,6 +184,11 @@ class TestRQCli(RQTestCase): self.assert_normal_execution(result) 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.enqueue(say_hello) 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.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): """rq worker -u -b --exception-handler """ connection = Redis.from_url(self.redis_url)