try-catch for race-based integrity errors

This commit is contained in:
jsangmeister 2019-11-26 11:49:06 +01:00
parent 7454540b67
commit f2dd8ebdd9

View File

@ -17,6 +17,7 @@ from django.contrib.sites.shortcuts import get_current_site
from django.core import mail from django.core import mail
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import transaction from django.db import transaction
from django.db.utils import IntegrityError
from django.http.request import QueryDict from django.http.request import QueryDict
from django.utils.encoding import force_bytes, force_text from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
@ -99,6 +100,13 @@ class UserViewSet(ModelViewSet):
result = False result = False
return result return result
# catch IntegrityError, probably being caused by a race condition
def perform_create(self, serializer):
try:
super().perform_create(serializer)
except IntegrityError as e:
raise ValidationError({"detail": str(e)})
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
""" """
Customized view endpoint to update an user. Customized view endpoint to update an user.
@ -360,7 +368,11 @@ class UserViewSet(ModelViewSet):
del data["groups_id"] del data["groups_id"]
db_user = User(**data) db_user = User(**data)
db_user.save(skip_autoupdate=True) try:
db_user.save(skip_autoupdate=True)
except IntegrityError:
# race condition may happen, so skip double users here again
continue
db_user.groups.add(*groups) db_user.groups.add(*groups)
created_users.append(db_user) created_users.append(db_user)
if "importTrackId" in user: if "importTrackId" in user: