added throttling and batch size to processing overdue loans
This commit was merged in pull request #62.
This commit is contained in:
+23
-3
@@ -1,8 +1,10 @@
|
||||
import time as time_module
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
import requests
|
||||
from app.extensions import db
|
||||
from app.config import settings
|
||||
from app.helpers.response_helper import ResponseHelper
|
||||
from app.helpers.collect_loan_helper import CollectLoanHelper
|
||||
from app.utils.auth import get_headers
|
||||
from app.utils.logger import logger
|
||||
from app.integrations.simbrella import SimbrellaClient
|
||||
@@ -450,15 +452,33 @@ def overdue_loans():
|
||||
if not overdue_loans:
|
||||
logger.info("No overdue loans found.")
|
||||
return ResponseHelper.success(message="No overdue loans found", status_code=200)
|
||||
#get batch size from settings
|
||||
loan_delay_seconds = max(0, settings.OVERDUE_LOAN_DELAY_SECONDS)
|
||||
batch_delay_seconds = max(0, settings.OVERDUE_LOAN_BATCH_DELAY_SECONDS)
|
||||
batch_size = max(1, settings.OVERDUE_LOAN_BATCH_SIZE)
|
||||
|
||||
loan_chunks = list(CollectLoanHelper.chunk_list(overdue_loans, batch_size))
|
||||
logger.info(f"Found {len(loan_chunks)} loan chunks to process.")
|
||||
|
||||
|
||||
# Step 2: Process each loan
|
||||
for loan in overdue_loans:
|
||||
process_overdue_loan(loan)
|
||||
for chunk_index, loan_chunk in enumerate(loan_chunks):
|
||||
logger.info(f"Processing chunk {chunk_index + 1} of {len(loan_chunks)} with {len(loan_chunk)} loans.")
|
||||
for loan in loan_chunk:
|
||||
try:
|
||||
process_overdue_loan(loan)
|
||||
except Exception:
|
||||
logger.exception(f"Failed processing loan {loan.id}")
|
||||
finally:
|
||||
time_module.sleep(loan_delay_seconds)
|
||||
if chunk_index < len(loan_chunks) - 1:
|
||||
logger.info(f"Waiting {batch_delay_seconds} seconds before processing next chunk...")
|
||||
time_module.sleep(batch_delay_seconds) # Delay between chunks
|
||||
|
||||
return ResponseHelper.success(message="Processed overdue loans successfully", status_code=200)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching overdue loans: {e}")
|
||||
logger.exception(f"Error fetching overdue loans: {e}")
|
||||
return ResponseHelper.error("Failed to fetch overdue loans", status_code=500, error=str(e))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user