Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for transaction to complete before calling webhooks #2675

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions onadata/apps/restservice/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def call_webhooks(sender, **kwargs): # pylint: disable=unused-argument
"""
instance = kwargs["instance"]

call_service_async.apply_async(
args=[instance.pk, instance.get_full_dict()], countdown=1
)
call_service_async.apply_async(args=[instance.pk], countdown=1)


trigger_webhook.connect(call_webhooks, dispatch_uid="call_webhooks")
8 changes: 1 addition & 7 deletions onadata/apps/restservice/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@app.task()
def call_service_async(instance_pk, latest_json=None):
def call_service_async(instance_pk):
"""Async function that calls call_service()."""
# Pin to master just incase the Instance was created and is not available
# in the replicas
Expand All @@ -22,10 +22,4 @@ def call_service_async(instance_pk, latest_json=None):
# service
return

if latest_json is None:
instance.json = instance.get_full_dict()

else:
instance.json = latest_json

call_service(instance)
30 changes: 14 additions & 16 deletions onadata/apps/viewer/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import django.dispatch
from django.conf import settings
from django.db import transaction
from django.db.models.signals import post_save

from onadata.apps.logger.models import Instance
Expand Down Expand Up @@ -37,29 +38,26 @@ def _post_process_submissions(instance):

def post_save_submission(sender, **kwargs): # pylint: disable=unused-argument
"""
Calls webhooks and OSM data processing for ParsedInstance model.
Calls webhooks and OSM data processing for ParsedInstance/Instance model.
"""
parsed_instance = kwargs.get("instance")
created = kwargs.get("created")
instance = kwargs.get("instance")

if created and isinstance(instance, ParsedInstance):
# Get submission from ParsedInstance
instance = instance.instance

if created:
_post_process_submissions(parsed_instance.instance)
if isinstance(instance, Instance):
# Trigger webhooks only if the Instance has been commited by using
# transaction.on_commit. In case, the transaction is rolled back,
# the webhooks will not be called. Also, ensures getting the Instance
# again from the database later will not return stale data
transaction.on_commit(lambda: _post_process_submissions(instance))


post_save.connect(
post_save_submission, sender=ParsedInstance, dispatch_uid="post_save_submission"
)


def process_saved_submission(sender, **kwargs): # pylint: disable=unused-argument
"""
Calls webhooks and OSM data processing for Instance model.
"""
instance = kwargs.get("instance")
if instance:
_post_process_submissions(instance)


process_submission.connect(
process_saved_submission, sender=Instance, dispatch_uid="process_saved_submission"
post_save_submission, sender=Instance, dispatch_uid="process_saved_submission"
)