mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 01:48:38 -06:00
Closes #6519: Avoid querying applicable webhooks for every object
This commit is contained in:
parent
f561b2d955
commit
8d0a3c8e69
@ -1,5 +1,6 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@ -71,18 +72,29 @@ def flush_webhooks(queue):
|
|||||||
Flush a list of object representation to RQ for webhook processing.
|
Flush a list of object representation to RQ for webhook processing.
|
||||||
"""
|
"""
|
||||||
rq_queue = get_queue('default')
|
rq_queue = get_queue('default')
|
||||||
|
webhooks_cache = {
|
||||||
|
'type_create': {},
|
||||||
|
'type_update': {},
|
||||||
|
'type_delete': {},
|
||||||
|
}
|
||||||
|
|
||||||
for data in queue:
|
for data in queue:
|
||||||
|
|
||||||
# Collect Webhooks that apply for this object and action
|
|
||||||
content_type = data['content_type']
|
|
||||||
action_flag = {
|
action_flag = {
|
||||||
ObjectChangeActionChoices.ACTION_CREATE: 'type_create',
|
ObjectChangeActionChoices.ACTION_CREATE: 'type_create',
|
||||||
ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
|
ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
|
||||||
ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
|
ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
|
||||||
}[data['event']]
|
}[data['event']]
|
||||||
# TODO: Cache these so we're not calling multiple times for bulk operations
|
content_type = data['content_type']
|
||||||
webhooks = Webhook.objects.filter(content_types=content_type, enabled=True, **{action_flag: True})
|
|
||||||
|
# Cache applicable Webhooks
|
||||||
|
if content_type not in webhooks_cache[action_flag]:
|
||||||
|
webhooks_cache[action_flag][content_type] = Webhook.objects.filter(
|
||||||
|
**{action_flag: True},
|
||||||
|
content_types=content_type,
|
||||||
|
enabled=True
|
||||||
|
)
|
||||||
|
webhooks = webhooks_cache[action_flag][content_type]
|
||||||
|
|
||||||
for webhook in webhooks:
|
for webhook in webhooks:
|
||||||
rq_queue.enqueue(
|
rq_queue.enqueue(
|
||||||
|
Loading…
Reference in New Issue
Block a user