Skip to content

Commit 2204735

Browse files
Adds rq retry options (#12588)
* adds rq retry options #12327 * Clean up docs; disable retries of failed jobs by default * Pass a Retry object only if RQ_RETRY_MAX is non-zero --------- Co-authored-by: jeremystretch <[email protected]>
1 parent 0df6a57 commit 2204735

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

docs/configuration/miscellaneous.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,25 @@ This parameter defines the URL of the repository that will be checked for new Ne
204204
Default: `300`
205205

206206
The maximum execution time of a background task (such as running a custom script), in seconds.
207+
208+
---
209+
210+
## RQ_RETRY_INTERVAL
211+
212+
!!! note
213+
This parameter was added in NetBox v3.5.
214+
215+
Default: `60`
216+
217+
This parameter controls how frequently a failed job is retried, up to the maximum number of times specified by `RQ_RETRY_MAX`. This must be either an integer specifying the number of seconds to wait between successive attempts, or a list of such values. For example, `[60, 300, 3600]` will retry the task after 1 minute, 5 minutes, and 1 hour.
218+
219+
---
220+
221+
## RQ_RETRY_MAX
222+
223+
!!! note
224+
This parameter was added in NetBox v3.5.
225+
226+
Default: `0` (retries disabled)
227+
228+
The maximum number of times a background task will be retried before being marked as failed.

netbox/core/models/jobs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from netbox.config import get_config
1717
from netbox.constants import RQ_QUEUE_DEFAULT
1818
from utilities.querysets import RestrictedQuerySet
19-
from utilities.rqworker import get_queue_for_model
19+
from utilities.rqworker import get_queue_for_model, get_rq_retry
2020

2121
__all__ = (
2222
'Job',
@@ -219,5 +219,6 @@ def trigger_webhooks(self, event):
219219
event=event,
220220
data=self.data,
221221
timestamp=str(timezone.now()),
222-
username=self.user.username
222+
username=self.user.username,
223+
retry=get_rq_retry()
223224
)

netbox/extras/webhooks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from netbox.constants import RQ_QUEUE_DEFAULT
1010
from netbox.registry import registry
1111
from utilities.api import get_serializer_for_model
12+
from utilities.rqworker import get_rq_retry
1213
from utilities.utils import serialize_object
1314
from .choices import *
1415
from .models import Webhook
@@ -116,5 +117,6 @@ def flush_webhooks(queue):
116117
snapshots=data['snapshots'],
117118
timestamp=str(timezone.now()),
118119
username=data['username'],
119-
request_id=data['request_id']
120+
request_id=data['request_id'],
121+
retry=get_rq_retry()
120122
)

netbox/netbox/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
REMOTE_AUTH_GROUP_SEPARATOR = getattr(configuration, 'REMOTE_AUTH_GROUP_SEPARATOR', '|')
141141
REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
142142
RQ_DEFAULT_TIMEOUT = getattr(configuration, 'RQ_DEFAULT_TIMEOUT', 300)
143+
RQ_RETRY_INTERVAL = getattr(configuration, 'RQ_RETRY_INTERVAL', 60)
144+
RQ_RETRY_MAX = getattr(configuration, 'RQ_RETRY_MAX', 0)
143145
SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/')
144146
SEARCH_BACKEND = getattr(configuration, 'SEARCH_BACKEND', 'netbox.search.backends.CachedValueSearchBackend')
145147
SECURE_SSL_REDIRECT = getattr(configuration, 'SECURE_SSL_REDIRECT', False)

netbox/utilities/rqworker.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from django_rq.queues import get_connection
2-
from rq import Worker
2+
from rq import Retry, Worker
33

44
from netbox.config import get_config
55
from netbox.constants import RQ_QUEUE_DEFAULT
66

77
__all__ = (
88
'get_queue_for_model',
9+
'get_rq_retry',
910
'get_workers_for_queue',
1011
)
1112

@@ -22,3 +23,14 @@ def get_workers_for_queue(queue_name):
2223
Returns True if a worker process is currently servicing the specified queue.
2324
"""
2425
return Worker.count(get_connection(queue_name))
26+
27+
28+
def get_rq_retry():
29+
"""
30+
If RQ_RETRY_MAX is defined and greater than zero, instantiate and return a Retry object to be
31+
used when queuing a job. Otherwise, return None.
32+
"""
33+
retry_max = get_config().RQ_RETRY_MAX
34+
retry_interval = get_config().RQ_RETRY_INTERVAL
35+
if retry_max:
36+
return Retry(max=retry_max, interval=retry_interval)

0 commit comments

Comments
 (0)