1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

14132 Add EventRule - change webhook and add in script processing to events (#14267)

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Arthur Hanson
2023-11-30 13:36:33 -08:00
committed by GitHub
parent b83fcc6077
commit a38a38218b
55 changed files with 1569 additions and 589 deletions

View File

@@ -16,7 +16,7 @@ from extras.constants import EVENT_JOB_END, EVENT_JOB_START
from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT
from utilities.querysets import RestrictedQuerySet
from utilities.rqworker import get_queue_for_model, get_rq_retry
from utilities.rqworker import get_queue_for_model
__all__ = (
'Job',
@@ -168,8 +168,8 @@ class Job(models.Model):
self.status = JobStatusChoices.STATUS_RUNNING
self.save()
# Handle webhooks
self.trigger_webhooks(event=EVENT_JOB_START)
# Handle events
self.process_event(event=EVENT_JOB_START)
def terminate(self, status=JobStatusChoices.STATUS_COMPLETED, error=None):
"""
@@ -186,8 +186,8 @@ class Job(models.Model):
self.completed = timezone.now()
self.save()
# Handle webhooks
self.trigger_webhooks(event=EVENT_JOB_END)
# Handle events
self.process_event(event=EVENT_JOB_END)
@classmethod
def enqueue(cls, func, instance, name='', user=None, schedule_at=None, interval=None, **kwargs):
@@ -224,27 +224,18 @@ class Job(models.Model):
return job
def trigger_webhooks(self, event):
from extras.models import Webhook
def process_event(self, event):
"""
Process any EventRules relevant to the passed job event (i.e. start or stop).
"""
from extras.models import EventRule
from extras.events import process_event_rules
rq_queue_name = get_config().QUEUE_MAPPINGS.get('webhook', RQ_QUEUE_DEFAULT)
rq_queue = django_rq.get_queue(rq_queue_name, is_async=False)
# Fetch any webhooks matching this object type and action
webhooks = Webhook.objects.filter(
# Fetch any event rules matching this object type and action
event_rules = EventRule.objects.filter(
**{f'type_{event}': True},
content_types=self.object_type,
enabled=True
)
for webhook in webhooks:
rq_queue.enqueue(
"extras.webhooks_worker.process_webhook",
webhook=webhook,
model_name=self.object_type.model,
event=event,
data=self.data,
timestamp=timezone.now().isoformat(),
username=self.user.username,
retry=get_rq_retry()
)
process_event_rules(event_rules, self.object_type.model, event, self.data, self.user.username)