Update/add flag for description logging (#991)

* test workers

* indent

* add docs and add option to the cli

* rename flag for cli

* logging
main
Finnci 6 years ago committed by Selwin Ong
parent 972778d041
commit 14db0ecd26

@ -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`. * `--connection-class`: Redis connection class to use, defaults to `redis.StrictRedis`.
* `--log-format`: Format for the worker logs, defaults to `'%(asctime)s %(message)s'` * `--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'` * `--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 ## Inside the worker

@ -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('--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('--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('--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('--verbose', '-v', is_flag=True, help='Show more output')
@click.option('--quiet', '-q', is_flag=True, help='Show less 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') @click.option('--sentry-dsn', envvar='SENTRY_DSN', help='Report exceptions to this Sentry DSN')

@ -100,6 +100,8 @@ class Worker(object):
# `log_result_lifespan` controls whether "Result is kept for XXX seconds" # `log_result_lifespan` controls whether "Result is kept for XXX seconds"
# messages are logged after every job, by default they are. # messages are logged after every job, by default they are.
log_result_lifespan = True log_result_lifespan = True
# `log_job_description` is used to toggle logging an entire jobs description.
log_job_description = True
@classmethod @classmethod
def all(cls, connection=None, job_class=None, queue_class=None, queue=None): 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, connection=None, exc_handler=None, exception_handlers=None,
default_worker_ttl=DEFAULT_WORKER_TTL, job_class=None, default_worker_ttl=DEFAULT_WORKER_TTL, job_class=None,
queue_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: if connection is None:
connection = get_current_connection() connection = get_current_connection()
self.connection = connection self.connection = connection
@ -187,6 +190,7 @@ class Worker(object):
self._horse_pid = 0 self._horse_pid = 0
self._stop_requested = False self._stop_requested = False
self.log = logger self.log = logger
self.log_job_description = log_job_description
self.failed_queue = get_failed_queue(connection=self.connection, self.failed_queue = get_failed_queue(connection=self.connection,
job_class=self.job_class) job_class=self.job_class)
self.last_cleaned_at = None self.last_cleaned_at = None
@ -519,9 +523,15 @@ class Worker(object):
connection=self.connection, connection=self.connection,
job_class=self.job_class) job_class=self.job_class)
if result is not None: if result is not None:
job, queue = result job, queue = result
self.log.info('{0}: {1} ({2})'.format(green(queue.name), if self.log_job_description:
blue(job.description), job.id)) 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 break
except DequeueTimeout: except DequeueTimeout:

@ -816,6 +816,24 @@ class TestWorker(RQTestCase):
w.perform_job(job, q) 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]) 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): def kill_worker(pid, double_kill):
# wait for the worker to be started over on the main process # wait for the worker to be started over on the main process

Loading…
Cancel
Save