Skip to content

Commit

Permalink
feat: Failed transactions will be retried once
Browse files Browse the repository at this point in the history
  • Loading branch information
dbtdsilva committed Jul 25, 2024
1 parent b1e434a commit 5fc1f54
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/services/transaction_import_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5fc1f54

Please sign in to comment.