Skip to content

Commit

Permalink
205545 parallelize tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Jun 26, 2024
1 parent 8a12882 commit e909d3b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
12 changes: 1 addition & 11 deletions src/hope_payment_gateway/apps/fsp/western_union/handlers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
from hope_payment_gateway.apps.fsp.western_union.endpoints.send_money import send_money
from hope_payment_gateway.apps.gateway.models import (
FinancialServiceProvider,
FinancialServiceProviderConfig,
PaymentRecord,
)
from hope_payment_gateway.apps.gateway.models import FinancialServiceProvider, FinancialServiceProviderConfig
from hope_payment_gateway.apps.gateway.registry import FSPProcessor


class WesternUnionHandler(FSPProcessor):
def notify(self, records):
to_process = list(PaymentRecord.objects.filter(id__in=records).values_list("id", flat=True))
records.update(marked_for_payment=True)
for record in PaymentRecord.objects.filter(id__in=to_process):
send_money(record.get_payload())

def get_configuration(self, config_key, delivery_mechanism):
wu = FinancialServiceProvider.objects.get(vision_vendor_number="1900723202")
Expand Down
43 changes: 26 additions & 17 deletions src/hope_payment_gateway/apps/fsp/western_union/tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from typing import List

from constance import config

from hope_payment_gateway.apps.fsp.western_union.endpoints.das import (
das_countries_currencies,
das_delivery_option_template,
)
from hope_payment_gateway.apps.fsp.western_union.endpoints.send_money import send_money
from hope_payment_gateway.apps.fsp.western_union.models import Corridor
from hope_payment_gateway.apps.gateway.models import (
FinancialServiceProvider,
Expand All @@ -19,26 +21,33 @@

@app.task
def western_union_send_task(vision_vendor_number="1900723202", tag=None, threshold=10000):
fsps = FinancialServiceProvider.objects.filter(vision_vendor_number=vision_vendor_number)
threshold = threshold or config.DEFAULT_THREASHOLD
fsp = FinancialServiceProvider.objects.get(vision_vendor_number=vision_vendor_number)
threshold = threshold or config.WESTERN_UNION_THREASHOLD

records_count = 0

for fsp in fsps:
qs = PaymentInstruction.objects.filter(status=PaymentInstructionState.READY, fsp=fsp)
if tag:
qs = qs.filter(tag=tag)

for pi in qs:
records = pi.paymentrecord_set.filter(status=PaymentRecordState.PENDING, marked_for_payment=False)
records_count += records.count()
if records_count > threshold:
break

logging.info(f"Sending {records_count} records {pi} to Western Union")
fsp.strategy.notify(records)
pi.status = PaymentInstructionState.PROCESSED
pi.save()
qs = PaymentInstruction.objects.filter(status=PaymentInstructionState.READY, fsp=fsp)
if tag:
qs = qs.filter(tag=tag)

for pi in qs:
records = pi.paymentrecord_set.filter(status=PaymentRecordState.PENDING, marked_for_payment=False)
records_count += records.count()
if records_count > threshold:
break

logging.info(f"Sending {records_count} records {pi} to Western Union")
records_ids = list(records.values_list("id", flat=True))
western_union_notify.delay(records_ids)
pi.status = PaymentInstructionState.PROCESSED
pi.save()


@app.task
def western_union_notify(to_process_ids: List[PaymentRecord]) -> None:
PaymentRecord.objects.filter(id__in=to_process_ids).update(marked_for_payment=True)
for record in PaymentRecord.objects.filter(id__in=to_process_ids):
send_money(record.get_payload())


@app.task
Expand Down
2 changes: 1 addition & 1 deletion src/hope_payment_gateway/config/fragments/constance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hope_payment_gateway.config.settings import CACHE_URL

CONSTANCE_CONFIG = {
"DEFAULT_THREASHOLD": (
"WESTERN_UNION_THREASHOLD": (
10000,
"Hourly threshold of calls to be made to Western Union API",
int,
Expand Down
5 changes: 3 additions & 2 deletions tests/western_union/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
)
@pytest.mark.django_db
@patch("hope_payment_gateway.apps.fsp.western_union.handlers.send_money")
# @patch("hope_payment_gateway.apps.fsp.western_union.handlers.send_money")
@patch("hope_payment_gateway.apps.fsp.western_union.tasks.send_money")
def test_send_money_task(mock_class, wu, rec_a, rec_b, total):
instr_a = PaymentInstructionFactory(status=PaymentInstructionState.READY, fsp=wu, tag="tag")
instr_b = PaymentInstructionFactory(status=PaymentInstructionState.READY, fsp=wu, tag="tag")
Expand All @@ -41,5 +42,5 @@ def test_send_money_task(mock_class, wu, rec_a, rec_b, total):
5, parent__status=PaymentRecordState.PENDING, status=PaymentRecordState.PENDING, marked_for_payment=True
)

western_union_send_task(vision_vendor_number="1900723202", tag="tag", threshold=10)
western_union_send_task(tag="tag", threshold=10)
assert len(mock_class.mock_calls) == total

0 comments on commit e909d3b

Please sign in to comment.