rqworker.py 997 B

123456789101112131415161718192021222324252627282930313233343536
  1. from django_rq.queues import get_connection
  2. from rq import Retry, Worker
  3. from netbox.config import get_config
  4. from netbox.constants import RQ_QUEUE_DEFAULT
  5. __all__ = (
  6. 'get_queue_for_model',
  7. 'get_rq_retry',
  8. 'get_workers_for_queue',
  9. )
  10. def get_queue_for_model(model):
  11. """
  12. Return the configured queue name for jobs associated with the given model.
  13. """
  14. return get_config().QUEUE_MAPPINGS.get(model, RQ_QUEUE_DEFAULT)
  15. def get_workers_for_queue(queue_name):
  16. """
  17. Returns True if a worker process is currently servicing the specified queue.
  18. """
  19. return Worker.count(get_connection(queue_name))
  20. def get_rq_retry():
  21. """
  22. If RQ_RETRY_MAX is defined and greater than zero, instantiate and return a Retry object to be
  23. used when queuing a job. Otherwise, return None.
  24. """
  25. retry_max = get_config().RQ_RETRY_MAX
  26. retry_interval = get_config().RQ_RETRY_INTERVAL
  27. if retry_max:
  28. return Retry(max=retry_max, interval=retry_interval)