Skip to content

Commit

Permalink
refactor: retry sending transaction to transaction service layer
Browse files Browse the repository at this point in the history
- Removed the command functionality from the command layer
- Added tests to the command logic
  • Loading branch information
Tiago-Salles authored and igobranco committed Dec 13, 2024
1 parent 5373e8d commit 82e7a9c
Show file tree
Hide file tree
Showing 4 changed files with 764 additions and 55 deletions.
25 changes: 2 additions & 23 deletions apps/billing/management/commands/retry_sage_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.core.management.base import BaseCommand

from apps.billing.models import SageX3TransactionInformation
from apps.billing.services.transaction_service import TransactionService


Expand Down Expand Up @@ -30,32 +29,12 @@ def add_arguments(self, parser):
"--transaction_id", type=str, required=False, help="The transaction_id to retry to send to SageX3"
)

@staticmethod
def _sagex3_transaction_info_query(transaction_id):
if transaction_id:
return SageX3TransactionInformation.objects.filter(transaction__transaction_id=transaction_id)
else:
return SageX3TransactionInformation.objects.filter(
status__in=[SageX3TransactionInformation.FAILED, SageX3TransactionInformation.PENDING]
)

def handle(self, *args, **kwargs) -> str | None:
transaction_id = kwargs["transaction_id"]
start = time.time()
self.stdout.write("\nGetting failed transactions with Sage X3...\n")
sagex3_to_retry = self.__class__._sagex3_transaction_info_query(transaction_id)
total_count = sagex3_to_retry.count()
counters = {"success": 0, "failed": 0}
try:
for sagex3_failed_transaction in sagex3_to_retry:
if TransactionService(sagex3_failed_transaction.transaction).run_steps_to_send_transaction():
counters["success"] += 1
else:
counters["failed"] += 1
except Exception as e:
self.stdout.write(f"Error while retrying: {e}")
counters["failed"] += 1
counters = TransactionService.retry_sending_transactions(transaction_id=transaction_id)
finish = time.time() - start
self.stdout.write(f"\n----- {total_count} Transactions were retried -----\n")
self.stdout.write(f"\n----- {counters['total_count']} Transactions were retried -----\n")
self.stdout.write(f"\nSUCCESSFULL RETRIES: {counters['success']} FAILED RETRIES: {counters['failed']}\n")
self.stdout.write(f"\nThe time to retry all transactions was {finish}\n")
42 changes: 42 additions & 0 deletions apps/billing/services/transaction_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,45 @@ def run_steps_to_send_transaction(self) -> bool:
transaction=self.transaction,
)
return False

@staticmethod
def sagex3_transaction_info_query(transaction_id: str | None):
"""
This method returns the transactions that need to be retried and if
a `transaction_id` was provided, it will return only the corresponding transaction.
This method must always return a list, regardless of its length.
"""

if transaction_id:
result = SageX3TransactionInformation.objects.filter(transaction__transaction_id=transaction_id)

return list(result)

result = SageX3TransactionInformation.objects.filter(
status__in=[SageX3TransactionInformation.FAILED, SageX3TransactionInformation.PENDING]
)

return list(result)

@staticmethod
def retry_sending_transactions(transaction_id: str | None):
"""
This method retries sending transactions and also is
possible to retry an specific one by providing the `transaction_id`.
"""

sagex3_to_retry = TransactionService.sagex3_transaction_info_query(transaction_id)
counters = {"success": 0, "failed": 0, "total_count": len(sagex3_to_retry)}

for sagex3_failed_transaction in sagex3_to_retry:
try:
if TransactionService(sagex3_failed_transaction.transaction).run_steps_to_send_transaction():
counters["success"] += 1
else:
counters["failed"] += 1
except Exception as e:
print(f"Error while retrying: {e}")
counters["failed"] += 1

return counters
Loading

0 comments on commit 82e7a9c

Please sign in to comment.