Skip to content

Commit

Permalink
Fix delay in integration domain addition
Browse files Browse the repository at this point in the history
This commit extends the domain views to eliminate the response wait time on the
client by returning the HTTP 202 Accepted status code. This change allows the
request to be processed asynchronously, preventing client-side timeouts due to
longer domain addition tasks.

Fixes: #138
Signed-off-by: Francisco Trivino <[email protected]>
  • Loading branch information
f-trivino committed Oct 15, 2024
1 parent cffbfdd commit 5de842e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/ipa-tuura/domains/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

import logging
import threading

from django.http import Http404
from domains.adapters import DomainSerializer
Expand Down Expand Up @@ -38,20 +39,27 @@ def create(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
logger.info(f"domain create {serializer.validated_data}")

try:
add_domain(serializer.validated_data)
except RuntimeError as e:
return Response(str(e), status=status.HTTP_405_METHOD_NOT_ALLOWED)
except Exception as e:
raise e
else:
self.perform_create(serializer)

# reset the writable interface
ipa = IPA()
ipa._reset_instance()

return Response(serializer.data, status=status.HTTP_201_CREATED)
# Define the background process for handling the domain addition
def process_domain_creation():
try:
add_domain(serializer.validated_data)
except RuntimeError as e:
return Response(str(e), status=status.HTTP_405_METHOD_NOT_ALLOWED)
except Exception as e:
raise e
else:
self.perform_create(serializer)

# reset the writable interface
ipa = IPA()
ipa._reset_instance()

# Run the domain creation logic in a separate thread
thread = threading.Thread(target=process_domain_creation)
thread.start()

# Return the created response
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)

# handles GETs for 1 Domain
def retrieve(self, request, *args, **kwargs):
Expand Down

0 comments on commit 5de842e

Please sign in to comment.