From 14db0ecd264162fbd1ab894eac078ac842a935b4 Mon Sep 17 00:00:00 2001 From: Finnci <4cfinn@gmail.com> Date: Tue, 22 Jan 2019 00:53:14 +0000 Subject: [PATCH] Update/add flag for description logging (#991) * test workers * indent * add docs and add option to the cli * rename flag for cli * logging --- docs/docs/workers.md | 2 +- rq/cli/cli.py | 1 + rq/worker.py | 16 +++++++++++++--- tests/test_worker.py | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/docs/workers.md b/docs/docs/workers.md index e3d205e..25b047e 100644 --- a/docs/docs/workers.md +++ b/docs/docs/workers.md @@ -63,7 +63,7 @@ In addition to `--burst`, `rq worker` also accepts these arguments: * `--connection-class`: Redis connection class to use, defaults to `redis.StrictRedis`. * `--log-format`: Format for the worker logs, defaults to `'%(asctime)s %(message)s'` * `--date-format`: Datetime format for the worker logs, defaults to `'%H:%M:%S'` - +* `--disable-job-desc-logging`: Turn off job description logging. ## Inside the worker diff --git a/rq/cli/cli.py b/rq/cli/cli.py index 81ef810..2b75a16 100755 --- a/rq/cli/cli.py +++ b/rq/cli/cli.py @@ -180,6 +180,7 @@ def info(cli_config, interval, raw, only_queues, only_workers, by_queue, queues, @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') @click.option('--sentry-dsn', envvar='SENTRY_DSN', help='Report exceptions to this Sentry DSN') diff --git a/rq/worker.py b/rq/worker.py index 3e21cf4..bdb5a0f 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -100,6 +100,8 @@ class Worker(object): # `log_result_lifespan` controls whether "Result is kept for XXX seconds" # messages are logged after every job, by default they are. log_result_lifespan = True + # `log_job_description` is used to toggle logging an entire jobs description. + log_job_description = True @classmethod def all(cls, connection=None, job_class=None, queue_class=None, queue=None): @@ -160,7 +162,8 @@ class Worker(object): connection=None, exc_handler=None, exception_handlers=None, default_worker_ttl=DEFAULT_WORKER_TTL, job_class=None, queue_class=None, - job_monitoring_interval=DEFAULT_JOB_MONITORING_INTERVAL): # noqa + job_monitoring_interval=DEFAULT_JOB_MONITORING_INTERVAL, + log_job_description=True): # noqa if connection is None: connection = get_current_connection() self.connection = connection @@ -187,6 +190,7 @@ class Worker(object): self._horse_pid = 0 self._stop_requested = False self.log = logger + self.log_job_description = log_job_description self.failed_queue = get_failed_queue(connection=self.connection, job_class=self.job_class) self.last_cleaned_at = None @@ -519,9 +523,15 @@ class Worker(object): connection=self.connection, job_class=self.job_class) if result is not None: + job, queue = result - self.log.info('{0}: {1} ({2})'.format(green(queue.name), - blue(job.description), job.id)) + if self.log_job_description: + self.log.info('{0}: {1} ({2})'.format(green(queue.name), + blue(job.description), + job.id)) + else: + self.log.info('{0}:{1}'.format(green(queue.name), + job.id)) break except DequeueTimeout: diff --git a/tests/test_worker.py b/tests/test_worker.py index 9a18a37..b826f2b 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -816,6 +816,24 @@ class TestWorker(RQTestCase): w.perform_job(job, q) self.assertNotIn('Result is kept for 10 seconds', [c[0][0] for c in mock_logger_info.call_args_list]) + @mock.patch('rq.worker.logger.info') + def test_log_job_description_true(self, mock_logger_info): + """Check that log_job_description True causes job lifespan to be logged.""" + q = Queue() + w = Worker([q]) + job = q.enqueue(say_hello, args=('Frank',), result_ttl=10) + w.dequeue_job_and_maintain_ttl(10) + self.assertIn("Frank", mock_logger_info.call_args[0][0]) + + @mock.patch('rq.worker.logger.info') + def test_log_job_description_false(self, mock_logger_info): + """Check that log_job_description False causes job lifespan to not be logged.""" + q = Queue() + w = Worker([q], log_job_description=False) + job = q.enqueue(say_hello, args=('Frank',), result_ttl=10) + w.dequeue_job_and_maintain_ttl(10) + self.assertNotIn("Frank", mock_logger_info.call_args[0][0]) + def kill_worker(pid, double_kill): # wait for the worker to be started over on the main process