Skip to content

Commit

Permalink
Update/add flag for description logging (rq#991)
Browse files Browse the repository at this point in the history
* test workers

* indent

* add docs and add option to the cli

* rename flag for cli

* logging
  • Loading branch information
finnci authored and selwin committed Jan 22, 2019
1 parent 972778d commit 14db0ec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/docs/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions rq/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 13 additions & 3 deletions rq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -160,7 +162,8 @@ def __init__(self, queues, name=None, default_result_ttl=DEFAULT_RESULT_TTL,
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
Expand All @@ -187,6 +190,7 @@ def __init__(self, queues, name=None, default_result_ttl=DEFAULT_RESULT_TTL,
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
Expand Down Expand Up @@ -519,9 +523,15 @@ def dequeue_job_and_maintain_ttl(self, timeout):
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:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,24 @@ class TestWorker(Worker):
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
Expand Down

0 comments on commit 14db0ec

Please sign in to comment.