Skip to content

Commit

Permalink
Expose --max-idle-time RQ flag in rqworker management command (#688)
Browse files Browse the repository at this point in the history
Django-rq currently does not expose the --max-idle-time flag that python-rq
is able to use under the hood to gracefully shut down a worker after a given
amount of seconds. Exposing this functionality proves useful in the event when
django-rq workers are run within an autoscaling situation where scale-down time
cannot be controlled (e.g.: Azure Container Apps).

By running the worker with ./manage.py rqworker --max-idle-time 60 default
the worker can gracefully shutdown before it gets killed by autoscaling, avoiding
interrupted jobs that fail with an error of:

Work-horse terminated unexpectedly; waitpid returned 15 (signal 15);

Since --max-jobs is already exposed in the rqworker management command,
exposing this option is trivial.
  • Loading branch information
indispeq authored Jan 4, 2025
1 parent cd05d2f commit bb57d41
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django_rq/management/commands/rqworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def add_arguments(self, parser):
help='Turns debug mode on or off.')
parser.add_argument('--max-jobs', action='store', default=None, dest='max_jobs', type=int,
help='Maximum number of jobs to execute')
parser.add_argument('--max-idle-time', action='store', default=None, dest='max_idle_time', type=int,
help='Seconds to wait for job before shutting down')
parser.add_argument('--serializer', action='store', default='rq.serializers.DefaultSerializer', dest='serializer',
help='Specify a custom Serializer.')
parser.add_argument('args', nargs='*', type=str,
Expand Down Expand Up @@ -93,7 +95,7 @@ def handle(self, *args, **options):

w.work(
burst=options.get('burst', False), with_scheduler=options.get('with_scheduler', False),
logging_level=level, max_jobs=options['max_jobs']
logging_level=level, max_jobs=options['max_jobs'], max_idle_time=options['max_idle_time']
)
except ConnectionError as e:
self.stderr.write(str(e))
Expand Down

0 comments on commit bb57d41

Please sign in to comment.