rqworker.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import logging
  2. from django_rq.management.commands.rqworker import Command as _Command
  3. from netbox.registry import registry
  4. DEFAULT_QUEUES = ('high', 'default', 'low')
  5. logger = logging.getLogger('netbox.rqworker')
  6. class Command(_Command):
  7. """
  8. Subclass django_rq's built-in rqworker to listen on all configured queues if none are specified (instead
  9. of only the 'default' queue).
  10. """
  11. def handle(self, *args, **options):
  12. # Setup system jobs.
  13. for job, kwargs in registry['system_jobs'].items():
  14. try:
  15. interval = kwargs['interval']
  16. except KeyError:
  17. raise TypeError("System job must specify an interval (in minutes).")
  18. logger.debug(f"Scheduling system job {job.name} (interval={interval})")
  19. job.enqueue_once(**kwargs)
  20. # Run the worker with scheduler functionality
  21. options['with_scheduler'] = True
  22. # If no queues have been specified on the command line, listen on all configured queues.
  23. if len(args) < 1:
  24. queues = ', '.join(DEFAULT_QUEUES)
  25. logger.warning(
  26. f"No queues have been specified. This process will service the following queues by default: {queues}"
  27. )
  28. args = DEFAULT_QUEUES
  29. super().handle(*args, **options)