-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56ee8d9
commit b411b2f
Showing
18 changed files
with
276 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import logging | ||
import traceback | ||
|
||
from celery_once import QueueOnce | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from openforms.logging import logevent | ||
from openforms.registrations.base import BasePlugin | ||
from openforms.registrations.exceptions import ( | ||
RegistrationFailed, | ||
RegistrationRelatedActionFailed, | ||
TooManyRegistrationAttempts, | ||
) | ||
from openforms.submissions.constants import RegistrationStatuses | ||
from openforms.submissions.models import Submission | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RegistrationInteractionTask(QueueOnce): | ||
def __call__(self, *args, **kwargs): | ||
"""Run the celery task but handle error raising""" | ||
try: | ||
super().__call__(*args, **kwargs) | ||
except Exception as exc: | ||
submission = Submission.objects.get(id=args[0]) | ||
plugin = self._get_registration_plugin(submission) | ||
|
||
should_raise = self._handle_exception(submission, plugin, exc) | ||
if should_raise: | ||
raise exc | ||
|
||
def _handle_exception( | ||
self, submission: "Submission", plugin: BasePlugin | None, exc: Exception | ||
) -> bool: | ||
"""Handle the exception based on the task | ||
Cases: | ||
1. Pre-registration: | ||
It should never interrupt the chain. In case the pre-registration fails, it will continue to the registration | ||
task. This then check if pre-registration was completed and if not, it will raise RegistrationRelatedActionFailed. | ||
2. Registration: | ||
It interrupts the chain if the submission was not completed or not pre-registered, and if an exception occurs | ||
when retrying. | ||
3. Update status: | ||
It interrupts the chain if it is retrying. | ||
""" | ||
should_raise = True | ||
if self.name == "openforms.registrations.tasks.pre_registration": | ||
should_raise = False | ||
|
||
logger.exception("ZGW pre-registration raised %s", exc) | ||
submission.save_registration_status( | ||
RegistrationStatuses.failed, | ||
{"traceback": traceback.format_exc()}, | ||
) | ||
elif ( | ||
self.name == "openforms.registrations.tasks.register_submission" | ||
and isinstance(exc, TooManyRegistrationAttempts) | ||
): | ||
should_raise = False | ||
|
||
logevent.registration_attempts_limited(submission) | ||
|
||
submission.registration_status = RegistrationStatuses.failed | ||
submission.needs_on_completion_retry = False | ||
submission.registration_result = None | ||
submission.save() | ||
elif self.name == "openforms.registrations.tasks.register_submission": | ||
should_raise = submission.needs_on_completion_retry or isinstance( | ||
exc, (RegistrationRelatedActionFailed, ValidationError) | ||
) | ||
|
||
logevent.registration_failure(submission, exc, plugin) | ||
if isinstance(exc, RegistrationFailed): | ||
logger.warning( | ||
"Registration using plugin '%r' for submission '%s' failed", | ||
plugin, | ||
submission, | ||
) | ||
else: | ||
logger.error( | ||
"Registration using plugin '%r' for submission '%s' unexpectedly errored", | ||
plugin, | ||
submission, | ||
exc_info=True, | ||
) | ||
|
||
submission.save_registration_status( | ||
RegistrationStatuses.failed, | ||
{"traceback": traceback.format_exc()}, | ||
set_retry_flag=should_raise, | ||
) | ||
|
||
elif self.name == "openforms.payments.tasks.update_submission_payment_status": | ||
should_raise = submission.needs_on_completion_retry | ||
|
||
logger.info("Updating submission payment registration failed", exc_info=exc) | ||
|
||
submission.needs_on_completion_retry = True | ||
submission.save(update_fields=["needs_on_completion_retry"]) | ||
|
||
return should_raise | ||
|
||
def _get_registration_plugin(self, submission: "Submission") -> BasePlugin | None: | ||
plugin = None | ||
if registration_backend := submission.registration_backend: | ||
registry = registration_backend._meta.get_field("backend").registry | ||
backend = registration_backend.backend | ||
plugin = registry[backend] | ||
|
||
return plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.