Skip to content

Commit

Permalink
IPN callback uses celery task instead of broadcasting directly
Browse files Browse the repository at this point in the history
[#188769223]
  • Loading branch information
uraniumanchor committed Jan 15, 2025
1 parent 927261d commit 47f2e5e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
33 changes: 31 additions & 2 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.test import TransactionTestCase

from tracker import models
from tracker.tasks import post_donation_to_postbacks

from . import randgen
Expand All @@ -16,7 +17,35 @@ def test_task_calls_post(self, post):
event.save()
donor = randgen.generate_donor(self.rand)
donor.save()
donation = randgen.generate_donation(self.rand, event=event)
parent = randgen.generate_bid(
self.rand,
allowuseroptions=True,
min_children=0,
max_children=0,
event=event,
state='OPENED',
)[0]
parent.save()
approved = randgen.generate_bid(
self.rand, parent=parent, allow_children=False, state='OPENED'
)[0]
approved.save()
pending = randgen.generate_bid(self.rand, parent=parent, state='PENDING')[0]
pending.save()
donation = randgen.generate_donation(self.rand, event=event, min_amount=10)
donation.save()
approved_bid = models.DonationBid.objects.create(
donation=donation, bid=approved, amount=5 - donation.amount
)
pending_bid = models.DonationBid.objects.create(
donation=donation, bid=pending, amount=donation.amount - 5
)
post_donation_to_postbacks(donation.id)
post.assert_called_with(donation)
with self.subTest('called at all'):
post.assert_called_with(donation)

with self.subTest('called with prefetch filter'):
self.assertIn(approved_bid, donation.bids.all())
self.assertIn(pending_bid, donation.bids.all())
self.assertIn(approved_bid, post.call_args[0][0].bids.all())
self.assertNotIn(pending_bid, post.call_args[0][0].bids.all())
4 changes: 4 additions & 0 deletions tracker/eventutil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import traceback

import requests
Expand All @@ -12,6 +13,8 @@
import tracker.viewutil as viewutil
from tracker.consumers.processing import broadcast_new_donation_to_processors

logger = logging.getLogger(__name__)


def post_donation_to_postbacks(donation):
event_donations = filters.run_model_query('donation', {'event': donation.event.id})
Expand Down Expand Up @@ -62,6 +65,7 @@ def post_donation_to_postbacks(donation):
timeout=5,
)
except Exception:
logger.exception('Error sending postback')
viewutil.tracker_log(
'postback_url', traceback.format_exc(), event=donation.event
)
19 changes: 13 additions & 6 deletions tracker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
from celery import shared_task
from celery.utils.log import get_task_logger
from channels.layers import get_channel_layer

from . import eventutil, prizeutil, util
from .models import Donation, Prize
from django.db.models import Prefetch

logger = get_task_logger(__name__)


@shared_task
def post_donation_to_postbacks(donation_id):
donation = Donation.objects.get(pk=donation_id)
from . import eventutil, models

donation = models.Donation.objects.prefetch_related(
Prefetch('bids', queryset=models.DonationBid.objects.public())
).get(pk=donation_id)
eventutil.post_donation_to_postbacks(donation)


@shared_task
def celery_test():
from . import util

async_to_sync(get_channel_layer().group_send)(
'celery',
{
Expand All @@ -28,10 +32,13 @@ def celery_test():

@shared_task(bind=True, max_retries=0)
def draw_prize(self, prize_or_pk):
if isinstance(prize_or_pk, Prize):
from . import models, prizeutil

if isinstance(prize_or_pk, models.Prize):
prize = prize_or_pk
else:
prize = Prize.objects.get(pk=prize_or_pk)
prize = models.Prize.objects.get(pk=prize_or_pk)

drawn, msg = prizeutil.draw_prize(prize)
if drawn:
logger.info(f'Drew {len(msg["winners"])} winner(s) for Prize #{prize.pk}')
Expand Down
11 changes: 8 additions & 3 deletions tracker/views/donateviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.views.decorators.http import require_POST
from paypal.standard.forms import PayPalPaymentsForm

from tracker import eventutil, forms, models, paypalutil, viewutil
from tracker import forms, models, paypalutil, viewutil
from tracker.analytics import AnalyticsEventTypes, analytics

from . import common as views_common
Expand Down Expand Up @@ -275,7 +275,12 @@ def ipn(request):
template=donation.event.donationemailtemplate,
context=formatContext,
)
eventutil.post_donation_to_postbacks(donation)
from tracker import settings, tasks

if settings.TRACKER_HAS_CELERY:
tasks.post_donation_to_postbacks.delay(donation.id)
else:
tasks.post_donation_to_postbacks(donation.id)
_track_donation_completed(donation)

elif donation.transactionstate == 'CANCELLED':
Expand All @@ -288,7 +293,7 @@ def ipn(request):
viewutil.tracker_log('paypal', str(e))
except Exception as e:
# just to make sure we have a record of it somewhere
logging.error('ERROR IN IPN RESPONSE, FIX IT')
logging.error(f'ERROR IN IPN RESPONSE, FIX IT - {type(e)}', exc_info=e)
if ipnObj:
paypalutil.log_ipn(
ipnObj,
Expand Down

0 comments on commit 47f2e5e

Please sign in to comment.