Skip to content

Commit 26ba8ee

Browse files
authored
fix(hooks): improve hook to avoid missing logs DEV-1728 (#6710)
### 💭 Notes Hooks have been failing for a client probably because of failures in the celery worker. Some submissions have no HookLog at all. This PR adds a PENDING HookLog creation before the celery dispatch and wraps the trigger in transaction.on_commit to ensure data has been saved when it is executed
1 parent 2fe5589 commit 26ba8ee

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

kobo/apps/hook/utils/services.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from django.db import transaction
2+
3+
from ..constants import HookLogStatus
14
from ..models.hook import Hook
25
from ..models.hook_log import HookLog
36
from ..tasks import service_definition_task
@@ -19,9 +22,16 @@ def call_services(asset_uid: str, submission_id: int) -> bool:
1922
success = False
2023

2124
for hook_id in hooks_ids:
22-
if not HookLog.objects.filter(
23-
submission_id=submission_id, hook_id=hook_id
24-
).exists():
25-
success = True
26-
service_definition_task.delay(hook_id, submission_id)
25+
with transaction.atomic():
26+
# Create a pending log in case the celery task fails
27+
log, created = HookLog.objects.get_or_create(
28+
submission_id=submission_id,
29+
hook_id=hook_id,
30+
defaults={'status': HookLogStatus.PENDING.value},
31+
)
32+
if created:
33+
success = True
34+
transaction.on_commit(
35+
lambda: service_definition_task.delay(hook_id, submission_id)
36+
)
2737
return success

0 commit comments

Comments
 (0)