Pass a Retry object only if RQ_RETRY_MAX is non-zero

This commit is contained in:
jeremystretch 2023-05-16 13:28:20 -04:00
parent 5c404cf604
commit e3f5c806b8
4 changed files with 18 additions and 7 deletions

View File

@ -223,6 +223,6 @@ This parameter controls how frequently a failed job is retried, up to the maximu
!!! note !!! note
This parameter was added in NetBox v3.5. This parameter was added in NetBox v3.5.
Default: `0` Default: `0` (retries disabled)
The maximum number of times a background task will be retried before being marked as failed. The maximum number of times a background task will be retried before being marked as failed.

View File

@ -9,7 +9,6 @@ from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from rq import Retry
from core.choices import JobStatusChoices from core.choices import JobStatusChoices
from extras.constants import EVENT_JOB_END, EVENT_JOB_START from extras.constants import EVENT_JOB_END, EVENT_JOB_START
@ -17,7 +16,7 @@ from extras.utils import FeatureQuery
from netbox.config import get_config from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT from netbox.constants import RQ_QUEUE_DEFAULT
from utilities.querysets import RestrictedQuerySet from utilities.querysets import RestrictedQuerySet
from utilities.rqworker import get_queue_for_model from utilities.rqworker import get_queue_for_model, get_rq_retry
__all__ = ( __all__ = (
'Job', 'Job',
@ -221,5 +220,5 @@ class Job(models.Model):
data=self.data, data=self.data,
timestamp=str(timezone.now()), timestamp=str(timezone.now()),
username=self.user.username, username=self.user.username,
retry=Retry(max=get_config().RQ_RETRY_MAX, interval=get_config().RQ_RETRY_INTERVAL) retry=get_rq_retry()
) )

View File

@ -4,12 +4,12 @@ import hmac
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils import timezone from django.utils import timezone
from django_rq import get_queue from django_rq import get_queue
from rq import Retry
from netbox.config import get_config from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT from netbox.constants import RQ_QUEUE_DEFAULT
from netbox.registry import registry from netbox.registry import registry
from utilities.api import get_serializer_for_model from utilities.api import get_serializer_for_model
from utilities.rqworker import get_rq_retry
from utilities.utils import serialize_object from utilities.utils import serialize_object
from .choices import * from .choices import *
from .models import Webhook from .models import Webhook
@ -118,5 +118,5 @@ def flush_webhooks(queue):
timestamp=str(timezone.now()), timestamp=str(timezone.now()),
username=data['username'], username=data['username'],
request_id=data['request_id'], request_id=data['request_id'],
retry=Retry(max=get_config().RQ_RETRY_MAX, interval=get_config().RQ_RETRY_INTERVAL) retry=get_rq_retry()
) )

View File

@ -1,11 +1,12 @@
from django_rq.queues import get_connection from django_rq.queues import get_connection
from rq import Worker from rq import Retry, Worker
from netbox.config import get_config from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT from netbox.constants import RQ_QUEUE_DEFAULT
__all__ = ( __all__ = (
'get_queue_for_model', 'get_queue_for_model',
'get_rq_retry',
'get_workers_for_queue', 'get_workers_for_queue',
) )
@ -22,3 +23,14 @@ def get_workers_for_queue(queue_name):
Returns True if a worker process is currently servicing the specified queue. Returns True if a worker process is currently servicing the specified queue.
""" """
return Worker.count(get_connection(queue_name)) return Worker.count(get_connection(queue_name))
def get_rq_retry():
"""
If RQ_RETRY_MAX is defined and greater than zero, instantiate and return a Retry object to be
used when queuing a job. Otherwise, return None.
"""
retry_max = get_config().RQ_RETRY_MAX
retry_interval = get_config().RQ_RETRY_INTERVAL
if retry_max:
return Retry(max=retry_max, interval=retry_interval)