bug fix on application context with threads

This commit is contained in:
2025-04-15 16:39:05 +01:00
parent 32e3ca5bb0
commit 4685ddc1c8
5 changed files with 36 additions and 49 deletions
+1 -1
View File
@@ -13,13 +13,13 @@ def create_app():
# Load configuration
app.config.from_object(Config)
# Setup CORS
CORS(app)
# Register blueprints
app.register_blueprint(auth_bp)
app.register_blueprint(autocall_bp, url_prefix="/autocall")
# Error Handlers
app.register_error_handler(405, method_not_allowed)
app.register_error_handler(415, unsupported_media_type)
+14 -25
View File
@@ -2,7 +2,9 @@ import requests
from app.config import settings
from app.utils.auth import get_headers
from app.utils.logger import logger
from flask import jsonify
from flask import jsonify, current_app
from app.models.transactions import Transaction
class SimbrellaClient:
@@ -11,9 +13,14 @@ class SimbrellaClient:
@staticmethod
def disbursement(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement"
logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
logger.info(f"Calling Disbursement endpoint with data: {data}")
# Check if the transaction exists
logger.info(f"Checking if transaction exists")
with current_app.app_context():
transaction = Transaction.get_transaction_by_id(transaction_id=data['transactionId'])
logger.info(f"Response from database: {transaction}")
disbursement_data ={
"requestId": data['requestId'],
"transactionId": data['transactionId'],
@@ -45,7 +52,6 @@ class SimbrellaClient:
@staticmethod
def collect_loan(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan"
logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
logger.info(f"Calling CollectLoan endpoint with data: {data}")
collect_loan_data = {
@@ -76,14 +82,7 @@ class SimbrellaClient:
@staticmethod
def verify_transaction():
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/TransactionVerify"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling TransactionVerify endpoint with data: {data}")
try:
# logger.info(f"Here is your TransactionVerify Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"TransactionVerify response: {response.json()}")
return {
"status": "00",
@@ -96,16 +95,9 @@ class SimbrellaClient:
@staticmethod
def refresh_disbursement(data):
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling Disbursement endpoint with data: {data}")
try:
logger.info(f"Here is your Disbursement Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"Disbursement response: {response.json()}")
# return response.json()
return data
@@ -115,19 +107,16 @@ class SimbrellaClient:
@staticmethod
def payment_callback(data):
# api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Payment"
# logger.info(f"BANK_CALL_BASE_URL = {SimbrellaClient.BANK_CALL_BASE_URL}")
# logger.info(f"Calling Payment Callback endpoint with data: {data}")
try:
logger.info(f"Here is your Payment Callback Request data ***** : {data}")
# response = requests.post(api_url, json=data, headers=get_headers())
# logger.info(f"Payment Callback response: {response.json()}")
# return response.json()
return data
except Exception as e:
logger.info(f"Failed to call Payment Callback endpoint: {e}")
raise
raise
@staticmethod
def check_transaction(txn_id):
return run_in_app_context(Transaction.get_transaction_by_id(txn_id))
+3 -1
View File
@@ -1 +1,3 @@
from .transactions import Transaction
from .transactions import Transaction
__all__ = ['Transaction']
+2 -8
View File
@@ -2,14 +2,8 @@ from app.config import settings
def get_headers():
# return {
# "Content-Type": "application/json",
# "x-api_key": settings.BANK_CALL_API_KEY,
# "App-Id": settings.BANK_CALL_APP_ID,
# }
return {
"Content-Type": "application/json",
"x-api-key": "test-api-key-12345",
"App-Id": "app1",
"x-api-key": settings.BANK_CALL_API_KEY,
"App-Id": settings.BANK_CALL_APP_ID,
}
+16 -14
View File
@@ -7,23 +7,25 @@ from app.utils.logger import logger
app = create_app()
kafka = KafkaIntegration()
def start_kafka_consumer():
logger.info("Starting Kafka consumer...")
while True:
try:
def start_kafka_consumer(app):
with app.app_context():
logger.info("Starting Kafka consumer...")
while True:
try:
message = kafka.receive_disbursement_messages(
topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT
)
message = kafka.receive_disbursement_messages(
topic=settings.KAFKA_PAYMENT_TOPIC, timeout=settings.KAFKA_TIMEOUT
)
if message:
logger.info(f"Processed message: {message}")
else:
logger.info("No message received within timeout")
if message:
logger.info(f"Processed message: {message}")
else:
logger.info("No message received within timeout")
except Exception as e:
logger.error(f"Error while receiving message: {e}")
except Exception as e:
logger.error(f"Error while receiving message: {e}")
if __name__ != "__main__":
@@ -32,4 +34,4 @@ if __name__ != "__main__":
wsgi_app = app
# Start kafka in a thread
threading.Thread(target=start_kafka_consumer, daemon=True).start()
threading.Thread(target=start_kafka_consumer, args=(app,), daemon=True).start()