Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
588f2770b7 | ||
|
|
ad743d6ab7 | ||
|
|
6145d0b005 | ||
|
|
d147fcb070 | ||
|
|
6196b93e97 | ||
|
|
b51933d625 | ||
|
|
40ff118798 | ||
|
|
d470677abe | ||
|
|
6eac3c6100 | ||
|
|
d22513730a | ||
|
|
613cece94b | ||
|
|
5cd1c12323 | ||
|
|
dcf73b25bf | ||
|
|
64a67e9c8d | ||
|
|
b6160af3c5 | ||
|
|
2ea41dfb75 | ||
|
|
cbcc419f0b | ||
|
|
687949beb2 | ||
|
|
fedaa5f5df | ||
|
|
c272c87e1f | ||
|
|
8a052ff910 | ||
|
|
d1c7a4b8c0 | ||
|
|
1a104338e8 | ||
|
|
5dd47b2d01 | ||
|
|
a28de60b1c | ||
|
|
e4a807b2a0 | ||
|
|
d0e7ed2771 | ||
|
|
05c0088f1b | ||
|
|
7f66cfd21e | ||
|
|
05b48f3656 | ||
|
|
07fe83b7b5 | ||
|
|
5fa1cd741e | ||
|
|
3e1d986c1c | ||
|
|
2cf95fd1c1 | ||
|
|
09b6811391 | ||
|
|
fb23d62fb2 | ||
|
|
eb4ecff6ed | ||
|
|
ffdf931cad | ||
|
|
5a538863b1 | ||
|
|
f6e0130035 | ||
|
|
6f882845c2 | ||
|
|
5654f83644 | ||
|
|
6d15420b08 | ||
|
|
5469eca450 | ||
|
|
94fd46c8ca | ||
|
|
57bd964d22 | ||
|
|
aac3a187d1 |
No files matched your search
+10
-1
@@ -26,8 +26,17 @@ RABBITMQ_USER=rabbitmq
|
||||
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
|
||||
RABBITMQ_VHOST=/
|
||||
|
||||
# Billing queue / retry settings
|
||||
BILLING_QUEUE_NAME=billing.api.calls
|
||||
# Max retry attempts (excluding initial consumption)
|
||||
BILLING_RETRY_TIMES=3
|
||||
# Base delay seconds for exponential backoff: base * 2^retry_count
|
||||
BILLING_RETRY_BASE_DELAY_SECONDS=30
|
||||
# Max delay seconds cap
|
||||
BILLING_RETRY_MAX_DELAY_SECONDS=1800
|
||||
|
||||
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
|
||||
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
|
||||
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
|
||||
# Optional: key ID used to support key rotation identification
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
@@ -21,9 +21,14 @@ class BillingMessageConsumer:
|
||||
|
||||
def __init__(self):
|
||||
self.queue_name = os.getenv("BILLING_QUEUE_NAME") or "billing.api.calls"
|
||||
self.retry_queue_name = f"{self.queue_name}.retry"
|
||||
self.connection = None
|
||||
self.channel = None
|
||||
self.consuming = False
|
||||
# retry times from env, default 3
|
||||
self.max_retries = int(os.getenv("BILLING_RETRY_TIMES", "3"))
|
||||
self.retry_base_delay_seconds = int(os.getenv("BILLING_RETRY_BASE_DELAY_SECONDS", "30"))
|
||||
self.retry_max_delay_seconds = int(os.getenv("BILLING_RETRY_MAX_DELAY_SECONDS", "1800"))
|
||||
self._setup_connection()
|
||||
|
||||
def _setup_connection(self):
|
||||
@@ -48,6 +53,14 @@ class BillingMessageConsumer:
|
||||
|
||||
# Declare queue
|
||||
self.channel.queue_declare(queue=self.queue_name, durable=True)
|
||||
self.channel.queue_declare(
|
||||
queue=self.retry_queue_name,
|
||||
durable=True,
|
||||
arguments={
|
||||
"x-dead-letter-exchange": "",
|
||||
"x-dead-letter-routing-key": self.queue_name,
|
||||
},
|
||||
)
|
||||
|
||||
# Set QoS, only process one message at a time
|
||||
self.channel.basic_qos(prefetch_count=1)
|
||||
@@ -124,6 +137,29 @@ class BillingMessageConsumer:
|
||||
body: Message body
|
||||
"""
|
||||
message_data = None
|
||||
settled = False
|
||||
raw_body = body.decode("utf-8", errors="replace") if isinstance(body, (bytes, bytearray)) else str(body)
|
||||
|
||||
def _safe_ack() -> None:
|
||||
nonlocal settled
|
||||
if settled:
|
||||
return
|
||||
settled = True
|
||||
try:
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
except Exception:
|
||||
logger.error("Failed to ack message", exc_info=True)
|
||||
|
||||
def _safe_nack(requeue: bool) -> None:
|
||||
nonlocal settled
|
||||
if settled:
|
||||
return
|
||||
settled = True
|
||||
try:
|
||||
channel.basic_nack(delivery_tag=method.delivery_tag, requeue=requeue)
|
||||
except Exception:
|
||||
logger.error("Failed to nack message", exc_info=True)
|
||||
|
||||
try:
|
||||
# Parse message
|
||||
message_data = json.loads(body)
|
||||
@@ -137,27 +173,68 @@ class BillingMessageConsumer:
|
||||
|
||||
if success:
|
||||
# Ack if processed successfully
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
_safe_ack()
|
||||
logger.info(f"Message processed and acknowledged: {message_data.get('user_id')}")
|
||||
else:
|
||||
# If failed, ack and drop (avoid infinite retry)
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
logger.error(f"Message processing failed, message dropped: {message_data.get('user_id')}")
|
||||
# Retry with limited attempts
|
||||
retry_count = self._get_retry_count(properties)
|
||||
if retry_count < self.max_retries:
|
||||
try:
|
||||
delay_ms = self._get_retry_delay_ms(retry_count)
|
||||
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms)
|
||||
_safe_ack()
|
||||
logger.warning(
|
||||
f"Message processing failed, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}, tool={message_data.get('tool_name')}, "
|
||||
)
|
||||
except Exception as pub_err:
|
||||
logger.error(f"Failed to requeue message for retry: {pub_err}", exc_info=True)
|
||||
# Requeue original message
|
||||
_safe_nack(requeue=True)
|
||||
else:
|
||||
# Exceeded retries; ack and drop
|
||||
_safe_ack()
|
||||
self._log_retry_exceeded(
|
||||
reason="handler_returned_false",
|
||||
retry_count=retry_count,
|
||||
properties=properties,
|
||||
method=method,
|
||||
message_data=message_data,
|
||||
raw_body=raw_body,
|
||||
)
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
try:
|
||||
db.close()
|
||||
except Exception:
|
||||
logger.error("Failed to close db session", exc_info=True)
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Message format error: {str(e)}, body: {body}")
|
||||
# Format error, ack directly (do not requeue)
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
_safe_ack()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Exception occurred while processing message: {str(e)}", exc_info=True)
|
||||
# On exception, ack and drop (avoid infinite retry)
|
||||
try:
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
logger.error(f"Exception handling failed, message dropped")
|
||||
retry_count = self._get_retry_count(properties)
|
||||
if message_data is not None and retry_count < self.max_retries:
|
||||
delay_ms = self._get_retry_delay_ms(retry_count)
|
||||
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms, error=str(e))
|
||||
_safe_ack()
|
||||
logger.warning(
|
||||
f"Exception during processing, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}"
|
||||
)
|
||||
else:
|
||||
_safe_ack()
|
||||
self._log_retry_exceeded(
|
||||
reason="exception",
|
||||
retry_count=retry_count,
|
||||
properties=properties,
|
||||
method=method,
|
||||
message_data=message_data,
|
||||
error=str(e),
|
||||
raw_body=raw_body,
|
||||
)
|
||||
except:
|
||||
# If even ack fails, log error but do not raise
|
||||
logger.error("Unable to acknowledge message", exc_info=True)
|
||||
@@ -173,9 +250,62 @@ class BillingMessageConsumer:
|
||||
int: Retry count
|
||||
"""
|
||||
if properties and properties.headers:
|
||||
return properties.headers.get("x-retry-count", 0)
|
||||
raw = properties.headers.get("x-retry-count", 0)
|
||||
try:
|
||||
return int(raw)
|
||||
except Exception:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
def _get_retry_delay_ms(self, current_retry_count: int) -> int:
|
||||
delay_seconds = self.retry_base_delay_seconds * (2 ** current_retry_count)
|
||||
if delay_seconds > self.retry_max_delay_seconds:
|
||||
delay_seconds = self.retry_max_delay_seconds
|
||||
return int(delay_seconds * 1000)
|
||||
|
||||
def _publish_delayed_retry(self, channel, message_data: dict, next_retry_count: int, delay_ms: int, error: str | None = None) -> None:
|
||||
headers: dict[str, str | int] = {"x-retry-count": next_retry_count}
|
||||
if error:
|
||||
headers["x-last-error"] = error
|
||||
props = pika.BasicProperties(
|
||||
delivery_mode=2,
|
||||
headers=headers,
|
||||
expiration=str(delay_ms),
|
||||
)
|
||||
channel.basic_publish(exchange="", routing_key=self.retry_queue_name, body=json.dumps(message_data), properties=props)
|
||||
|
||||
def _log_retry_exceeded(self, reason: str, retry_count: int, properties, method, message_data: dict | None, error: str | None = None, raw_body: str | None = None) -> None:
|
||||
headers = {}
|
||||
if properties and properties.headers:
|
||||
try:
|
||||
headers = dict(properties.headers)
|
||||
except Exception:
|
||||
headers = {}
|
||||
event = {
|
||||
"reason": reason,
|
||||
"retry_count": retry_count,
|
||||
"max_retries": self.max_retries,
|
||||
"queue": self.queue_name,
|
||||
"routing_key": getattr(method, "routing_key", None),
|
||||
"delivery_tag": getattr(method, "delivery_tag", None),
|
||||
"headers": headers,
|
||||
"user_id": (message_data or {}).get("user_id"),
|
||||
"service_id": (message_data or {}).get("service_id"),
|
||||
"api_id": (message_data or {}).get("api_id"),
|
||||
"tool_name": (message_data or {}).get("tool_name"),
|
||||
"call_start_time": (message_data or {}).get("call_start_time"),
|
||||
"call_end_time": (message_data or {}).get("call_end_time"),
|
||||
}
|
||||
if error:
|
||||
event["error"] = error
|
||||
if raw_body is not None:
|
||||
event["raw_body"] = raw_body
|
||||
try:
|
||||
payload = json.dumps(event, ensure_ascii=False, default=str)
|
||||
except Exception:
|
||||
payload = repr(event)
|
||||
logger.error(f"ALERT billing message retries exceeded: {payload}")
|
||||
|
||||
|
||||
def start_billing_consumer():
|
||||
"""Start billing message consumer"""
|
||||
|
||||
@@ -22,8 +22,6 @@ def get_config(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get platform configuration settings without authentication."""
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
try:
|
||||
# Create service instance
|
||||
sys_config_service = SysConfigService(db)
|
||||
@@ -90,9 +88,6 @@ def get_homepage_config(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get homepage configuration settings without authentication."""
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
try:
|
||||
# Create service instance
|
||||
sys_config_service = SysConfigService(db)
|
||||
|
||||
@@ -40,8 +40,6 @@ def get_order_service(db: Session = Depends(get_db)) -> UserWalletHistoryService
|
||||
@router.get("/info", response_model=dict)
|
||||
def get_user(request: Request, user_wallet: UserWalletService = Depends(get_user_wallet), user_task: UserTaskService = Depends(get_user_task_service), resource_group_service: ResourceGroupService = Depends(get_resource_group_service)):
|
||||
"""Get current user information and wallet balance."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user_response = UserResponse()
|
||||
user_wallet_resp = UserWalletResponse()
|
||||
user_wallet_resp.balance = 0.00
|
||||
|
||||
@@ -11,17 +11,20 @@ class McpCallLogRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def create(self, call_log: McpCallLog) -> McpCallLog:
|
||||
def create(self, call_log: McpCallLog, commit: bool = True) -> McpCallLog:
|
||||
self.db.add(call_log)
|
||||
self.db.commit()
|
||||
self.db.refresh(call_log)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(call_log)
|
||||
else:
|
||||
self.db.flush()
|
||||
return call_log
|
||||
|
||||
def get_by_id(self, log_id: str) -> Optional[McpCallLog]:
|
||||
return self.db.query(McpCallLog).filter(McpCallLog.id == log_id).first()
|
||||
|
||||
def update_status(
|
||||
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None
|
||||
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None, commit: bool = True
|
||||
) -> bool:
|
||||
log_record = self.get_by_id(log_id)
|
||||
if log_record:
|
||||
@@ -30,7 +33,10 @@ class McpCallLogRepository:
|
||||
log_record.error_msg = error_msg
|
||||
if wallet_history_id:
|
||||
log_record.wallet_history_id = wallet_history_id
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import Optional, List, Tuple, cast
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, literal_column
|
||||
from sqlalchemy import Boolean, func, literal_column
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.dialects.mysql import insert as mysql_insert
|
||||
from sqlalchemy.sql.schema import Table
|
||||
@@ -91,14 +91,12 @@ class StatsMcpServiceDateRepository:
|
||||
total = query.scalar()
|
||||
return int(total or 0)
|
||||
|
||||
def increment(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
def increment(self, service_id: str, stats_date: datetime, inc: int = 1, commit: bool = True):
|
||||
"""Atomically increment call_count for (service_id, stats_date) under high concurrency.
|
||||
|
||||
Implementation uses MySQL's ON DUPLICATE KEY UPDATE to avoid race conditions.
|
||||
If the row does not exist, it will be inserted with call_count=inc; otherwise, call_count is increased.
|
||||
"""
|
||||
# Logger for debug purposes
|
||||
print(f"Incrementing call count for service {service_id} on {stats_date} by {inc}")
|
||||
|
||||
# Cast mapped table for typing compatibility with mysql_insert
|
||||
table: Table = cast(Table, StatsMcpServiceDate.__table__)
|
||||
@@ -114,7 +112,10 @@ class StatsMcpServiceDateRepository:
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
self.db.execute(ondup)
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
|
||||
def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
"""Fallback increment strategy without dialect-specific upsert.
|
||||
|
||||
@@ -296,7 +296,7 @@ class UserWalletHistoryRepository:
|
||||
)
|
||||
return total, history
|
||||
|
||||
def add_consume_record(self, wallet_history: UserWalletHistory) -> Optional[UserWalletHistory]:
|
||||
def add_consume_record(self, wallet_history: UserWalletHistory, commit: bool = True) -> Optional[UserWalletHistory]:
|
||||
"""
|
||||
Add consume record to database.
|
||||
|
||||
@@ -310,12 +310,18 @@ class UserWalletHistoryRepository:
|
||||
# Add the wallet history record to database
|
||||
# The created_at and updated_at fields should be set by the caller
|
||||
self.db.add(wallet_history)
|
||||
self.db.commit()
|
||||
self.db.refresh(wallet_history)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(wallet_history)
|
||||
else:
|
||||
self.db.flush()
|
||||
return wallet_history
|
||||
except Exception as e:
|
||||
self.logging.error(f"Failed to add consume record: {str(e)}")
|
||||
self.db.rollback()
|
||||
try:
|
||||
self.db.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
|
||||
@@ -435,4 +441,4 @@ class UserWalletHistoryRepository:
|
||||
result.append({"stats_day": current_day, "count": amounts_by_day.get(current_day, 0.0)})
|
||||
current_day += timedelta(days=1)
|
||||
|
||||
return result
|
||||
return result
|
||||
@@ -32,13 +32,16 @@ class UserWalletRepository:
|
||||
"""Get user wallet with row-level lock for update"""
|
||||
return self.db.query(UserWallet).filter(UserWallet.user_id == user_id).with_for_update().first()
|
||||
|
||||
def update_balance(self, user_id: str, new_balance: float) -> bool:
|
||||
def update_balance(self, user_id: str, new_balance: float, commit: bool = True) -> bool:
|
||||
"""Update user balance"""
|
||||
wallet = self.get_by_user_id(user_id)
|
||||
if wallet:
|
||||
wallet.balance = new_balance
|
||||
wallet.updated_at = datetime.now(timezone.utc)
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ Billing message handler service
|
||||
"""
|
||||
|
||||
import json
|
||||
from re import finditer
|
||||
import uuid
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
@@ -44,41 +45,60 @@ class BillingMessageHandler:
|
||||
Returns:
|
||||
bool: Whether processing was successful
|
||||
"""
|
||||
call_log_id: Optional[str] = None
|
||||
tx = self.db.begin()
|
||||
try:
|
||||
# Parse message
|
||||
billing_message = self._parse_message(message_data)
|
||||
if not billing_message:
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Create API call log
|
||||
call_log_id = self._create_call_log(billing_message)
|
||||
if not call_log_id:
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Update stats_mcp_service_date record
|
||||
# Bucket stats by hour (UTC), aligned to full hours
|
||||
|
||||
start_dt = billing_message.call_start_time
|
||||
start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc)
|
||||
# Store as naive UTC datetime to match MySQL DATETIME behavior
|
||||
stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
|
||||
self.stats_repo.increment(billing_message.service_id, stats_date, 1)
|
||||
stats_date = start_utc.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
|
||||
self.stats_repo.increment(billing_message.service_id, stats_date, 1, commit=False)
|
||||
|
||||
# Process billing logic
|
||||
if billing_message.call_success and billing_message.unit_price > 0:
|
||||
success = self._process_billing(billing_message, call_log_id)
|
||||
success = self._process_billing(billing_message, call_log_id, commit=False)
|
||||
if not success:
|
||||
# Update record status to failed
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed")
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed", commit=False)
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Update record status to processed
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED)
|
||||
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
|
||||
return True
|
||||
|
||||
if not self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, commit=False):
|
||||
tx.rollback()
|
||||
return False
|
||||
tx.commit()
|
||||
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
|
||||
# Update wallet cache after successful commit
|
||||
try:
|
||||
wallet = self.wallet_repo.get_by_user_id(billing_message.user_id)
|
||||
if wallet:
|
||||
self._update_wallet_cache(billing_message.user_id, Decimal(str(wallet.balance)))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to process billing message: {str(e)}", exc_info=True)
|
||||
try:
|
||||
tx.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
if call_log_id:
|
||||
try:
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing exception")
|
||||
except Exception:
|
||||
logger.exception("Failed to update call log status after exception")
|
||||
return False
|
||||
finally:
|
||||
if tx.is_active:
|
||||
tx.rollback()
|
||||
|
||||
def _parse_message(self, message_data: dict) -> Optional[BillingMessage]:
|
||||
"""
|
||||
@@ -146,14 +166,14 @@ class BillingMessageHandler:
|
||||
updated_at=datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
created_log = self.call_log_repo.create(call_log)
|
||||
created_log = self.call_log_repo.create(call_log, commit=False)
|
||||
return created_log.id
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create API call log record: {str(e)}")
|
||||
return None
|
||||
|
||||
def _process_billing(self, billing_message: BillingMessage, call_log_id: str) -> bool:
|
||||
def _process_billing(self, billing_message: BillingMessage, call_log_id: str, commit: bool = True) -> bool:
|
||||
"""
|
||||
Process actual billing logic
|
||||
|
||||
@@ -183,7 +203,7 @@ class BillingMessageHandler:
|
||||
|
||||
# Execute deduction
|
||||
new_balance = current_balance - amount
|
||||
success = self.wallet_repo.update_balance(user_id, float(new_balance))
|
||||
success = self.wallet_repo.update_balance(user_id, float(new_balance), commit=commit)
|
||||
if not success:
|
||||
logger.error(f"Failed to update user wallet balance - User ID: {user_id}")
|
||||
return False
|
||||
@@ -206,22 +226,15 @@ class BillingMessageHandler:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
created_history = self.wallet_history_repo.add_consume_record(wallet_history)
|
||||
created_history = self.wallet_history_repo.add_consume_record(wallet_history, commit=commit)
|
||||
if not created_history:
|
||||
logger.error(f"Failed to create wallet history record - User ID: {user_id}")
|
||||
# Rollback balance update
|
||||
self.wallet_repo.update_balance(user_id, float(current_balance))
|
||||
return False
|
||||
|
||||
logger.info(f"Wallet history record created successfully - User ID: {user_id}, History ID: {history_id}, Created at: {now}")
|
||||
|
||||
# Update API call record's actual deduction amount and associated history record ID
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id)
|
||||
|
||||
|
||||
|
||||
# Update wallet balance cache in Redis
|
||||
self._update_wallet_cache(user_id, new_balance)
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id, commit=commit)
|
||||
|
||||
logger.info(
|
||||
f"Billing processing completed successfully - User ID: {user_id}, Amount deducted: {amount}, Balance: {current_balance} -> {new_balance}"
|
||||
|
||||
@@ -37,7 +37,7 @@ class UserApiKeyRepository:
|
||||
return cached_model
|
||||
|
||||
# Query from database if not in cache
|
||||
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey).first()
|
||||
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey, UserApiKey.is_deleted == 0).first()
|
||||
if user_apikey:
|
||||
# Cache the result using new SQLAlchemy-specific method
|
||||
CacheUtils.set_sqlalchemy_cache(cache_key, user_apikey, 300)
|
||||
|
||||
@@ -30,7 +30,7 @@ class RabbitMQClient:
|
||||
logger.error(f"Failed to establish RabbitMQ connection: {str(e)}")
|
||||
raise
|
||||
|
||||
def publish(self, queue: str, message: str, persistent: bool = True):
|
||||
def publish(self, queue: str, message: str, persistent: bool = True, headers: dict | None = None):
|
||||
"""Publish message to queue"""
|
||||
try:
|
||||
# Ensure connection is available
|
||||
@@ -46,7 +46,9 @@ class RabbitMQClient:
|
||||
# Publish message
|
||||
properties = None
|
||||
if persistent:
|
||||
properties = pika.BasicProperties(delivery_mode=2)
|
||||
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
|
||||
elif headers:
|
||||
properties = pika.BasicProperties(headers=headers)
|
||||
|
||||
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
|
||||
logger.info(f"Message successfully published to queue: {queue}, message length: {len(message)} bytes")
|
||||
@@ -61,7 +63,10 @@ class RabbitMQClient:
|
||||
|
||||
# Retry sending
|
||||
self.channel.queue_declare(queue=queue, durable=True)
|
||||
properties = pika.BasicProperties(delivery_mode=2) if persistent else None
|
||||
if persistent:
|
||||
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
|
||||
else:
|
||||
properties = pika.BasicProperties(headers=headers or None)
|
||||
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
|
||||
logger.info(f"Message retry successful to queue {queue}")
|
||||
except Exception as retry_error:
|
||||
|
||||
@@ -33,7 +33,6 @@ def verify_token(token: str, db: Session) -> Optional[User]:
|
||||
set_model_cache(token_cache_key, user_access_token, TOKEN_CACHE_EXPIRE_TIME)
|
||||
|
||||
logging.info(f"query token info")
|
||||
print(f'user_access_token: {user_access_token}, expire_at: {user_access_token.expire_at},token_cache_key: {token_cache_key}')
|
||||
# Check if token is expired
|
||||
expire_at = user_access_token.expire_at
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user