sage/bin/backend_accounting.py
2026-05-21 18:38:34 +08:00

60 lines
1.8 KiB
Python

#!/usr/bin/env python
"""
独立运行的LLM后台计费程序。
从 sage.py 的 llmage 模块中提取,避免多进程模式下重复运行。
"""
import os
import sys
import asyncio
import signal
# 切换到 Sage 工作目录
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'py3', 'lib', 'python3.10', 'site-packages'))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pkgs'))
from appPublic.folderUtils import ProgramPath
from appPublic.jsonConfig import getConfig
from sqlor.dbpools import DBPools
from appPublic.log import MyLogger, debug, exception, info
from accounting.init import load_accounting
from llmage.init import load_llmage
# 初始化配置
p = ProgramPath()
config = getConfig(NS={'workdir': os.getcwd(), 'ProgramPath': p})
DBPools(config.databases)
# 导入 llmage 的计费函数
from llmage.accounting import (
backend_accounting
)
def main():
logger = MyLogger('backend_accounting', levelname='info',
logfile=os.path.join(os.getcwd(), 'logs', 'backend_accounting.log'))
info(f'Backend accounting process started (PID: {os.getpid()})')
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
def handle_signal(signum, frame):
info(f'Received signal {signum}, shutting down...')
for task in asyncio.all_tasks(loop):
task.cancel()
loop.stop()
signal.signal(signal.SIGTERM, handle_signal)
signal.signal(signal.SIGINT, handle_signal)
load_accounting()
try:
loop.run_until_complete(backend_accounting())
except asyncio.CancelledError:
pass
finally:
loop.close()
info('Backend accounting process stopped.')
if __name__ == '__main__':
main()