let tasks run async

This commit is contained in:
Matthias 2021-09-12 22:26:20 +02:00
parent 0e2db232ae
commit 4baf714c85
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -18,6 +18,7 @@ from django.views.decorators.csrf import csrf_exempt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import PermissionDenied, NotFound, NotAcceptable
from django_q.tasks import async_task
from .models import WebhookEndpoint, WebhookMessage
@ -45,6 +46,7 @@ class WebhookView(CsrfExemptMixin, APIView):
authentication_classes = []
permission_classes = []
model_class = WebhookEndpoint
run_async = False
# Token
TOKEN_NAME = "Token"
@ -69,12 +71,22 @@ class WebhookView(CsrfExemptMixin, APIView):
self.validate_token(payload, headers, request)
# process data
message = self.save_data(payload, headers, request)
self.process_payload(message, payload, headers)
if self.run_async:
async_task(self._process_payload, message.id)
else:
message.worked_on = self.process_payload(message, payload, headers)
message.save()
# return results
return_kwargs = self.get_result(payload, headers, request)
return Response(**return_kwargs)
def _process_payload(self, message_id):
message = WebhookMessage.objects.get(message_id=message_id)
process_result = self.process_payload(message, message.body, message.header)
message.worked_on = process_result
message.save()
# To be overridden
def init(self, request, *args, **kwargs):
self.token = ''