llmage/wwwroot/api/retry_accounting.dspy

37 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import json
from datetime import datetime
result = {'success': False, 'message': ''}
try:
dbname = get_module_dbname('llmage')
luid = params_kw.get('id') or params_kw.get('llmusageid')
if not luid:
result['message'] = '缺少llmusageid参数'
else:
async with DBPools().sqlorContext(dbname) as sor:
# 1. 重置 llmusage 记账状态为 created让后台循环重新处理
await sor.U('llmusage', {
'id': luid,
'accounting_status': 'created'
})
# 2. 更新失败记录:标记已处理,增加重试次数
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
await sor.execute("""
UPDATE llmusage_accounting_failed
SET handled = '1',
retry_count = IFNULL(retry_count, 0) + 1,
handled_time = ${now}$,
handled_note = CONCAT(IFNULL(handled_note, ''), '[', ${now}$, '] 触发重试; ')
WHERE llmusageid = ${luid}$
""", {'luid': luid, 'now': now})
result['success'] = True
result['message'] = '已重置为待记账状态,后台循环将重新处理'
except Exception as e:
result['message'] = str(e)
return json.dumps(result, ensure_ascii=False, default=str)