From 5fc1f548f46ce1e8b530a51f50250d349350e190 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Thu, 25 Jul 2024 18:59:46 +0200 Subject: [PATCH] feat: Failed transactions will be retried once --- src/services/transaction_import_service.py | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/services/transaction_import_service.py b/src/services/transaction_import_service.py index 5a64bda..51c74b3 100644 --- a/src/services/transaction_import_service.py +++ b/src/services/transaction_import_service.py @@ -77,19 +77,33 @@ def import_file(self, file: str) -> bool: return True def __store_transactions(self, transactions: List[Transaction]) -> Tuple[List[Transaction], List[Transaction]]: - stored_transactions = [] - failed_transactions = [] - for index, transaction in enumerate(transactions): + stored_txs = [] + failed_txs = [] + + # Insert transactions to be imported + for index, tx in enumerate(transactions): try: - logging.debug(f'Importing "{transaction.description}" from {transaction.date} ' + logging.debug(f'Importing "{tx.description}" from {tx.date} ' f'({index+1} out of {len(transactions)})') - stored_transaction = self.api.transactions.store_transaction(transaction) - stored_transactions.append(stored_transaction) + stored_transaction = self.api.transactions.store_transaction(tx) + stored_txs.append(stored_transaction) except HTTPError as http_error: - logging.error(f'Skipping transaction after failing to insert ' - f'(transaction: {transaction}, error: {http_error})') - failed_transactions.append(transaction) - return (stored_transactions, failed_transactions) + logging.error(f'Failed to insert transaction: {tx}, error: {http_error}') + failed_txs.append(tx) + + # Retry failed transactions + failed_after_retry_txs = [] + for tx in enumerate(failed_txs): + try: + logging.debug(f'Re-trying importing "{tx.description}" from {tx.date} ' + f'({index+1} out of {len(failed_txs)})') + stored_transaction = self.api.transactions.store_transaction(tx) + stored_txs.append(stored_transaction) + except HTTPError as http_error: + logging.error(f'Skipping insertion of retried transaction (transaction: {tx}, error: {http_error})') + failed_after_retry_txs.append(tx) + + return (stored_txs, failed_after_retry_txs) def __find_account_matching_file(self, file: str) -> Tuple[Account, ModuleType]: file_basename = os.path.basename(file)