增加日志
This commit is contained in:
33
app.py
33
app.py
@@ -13,6 +13,7 @@ from lib.db import execute_query
|
||||
from lib.report_generator import generate_daily_report
|
||||
from lib.api import register_blueprints
|
||||
from lib.db_init import init_database_tables
|
||||
from lib.logger import log_info, log_error, log_warning
|
||||
|
||||
# 创建 Flask 应用
|
||||
app = Flask(__name__,
|
||||
@@ -27,11 +28,11 @@ os.makedirs(os.path.join('public', 'reports'), exist_ok=True)
|
||||
register_blueprints(app)
|
||||
|
||||
# 应用启动时自动初始化数据库表
|
||||
print('正在初始化数据库表...')
|
||||
log_info('正在初始化数据库表...', 'app')
|
||||
if init_database_tables():
|
||||
print('数据库表初始化成功')
|
||||
log_info('数据库表初始化成功', 'app')
|
||||
else:
|
||||
print('数据库表初始化失败,请检查数据库连接')
|
||||
log_error('数据库表初始化失败,请检查数据库连接', 'app')
|
||||
|
||||
|
||||
# ==================== 页面路由 ====================
|
||||
@@ -46,7 +47,7 @@ def index():
|
||||
|
||||
def scheduled_job():
|
||||
"""定时任务:每天 8:01 自动生成日报"""
|
||||
print(f'[{datetime.now()}] 开始执行定时任务...')
|
||||
log_info(f'开始执行定时任务...', 'scheduler')
|
||||
|
||||
try:
|
||||
# 获取所有启用的配置
|
||||
@@ -55,31 +56,35 @@ def scheduled_job():
|
||||
)
|
||||
|
||||
if not configs:
|
||||
print('没有启用的配置,跳过生成')
|
||||
log_warning('没有启用的配置,跳过生成', 'scheduler')
|
||||
return
|
||||
|
||||
print(f'找到 {len(configs)} 个启用的配置')
|
||||
log_info(f'找到 {len(configs)} 个启用的配置', 'scheduler')
|
||||
|
||||
# 为每个配置生成日报
|
||||
success_count = 0
|
||||
fail_count = 0
|
||||
for config in configs:
|
||||
print(f'正在生成:{config["config_name"]}')
|
||||
log_info(f'正在生成:{config["config_name"]} (ID: {config["id"]})', 'scheduler')
|
||||
result = generate_daily_report(config['id'])
|
||||
|
||||
if result['success']:
|
||||
print(f' ✓ 成功:{result["total_orders"]} 条订单,金额:{result["total_amount"]}')
|
||||
log_info(f' ✓ 成功:{result["total_orders"]} 条订单,金额:{result["total_amount"]}', 'scheduler')
|
||||
success_count += 1
|
||||
else:
|
||||
print(f' ✗ 失败:{result["message"]}')
|
||||
log_error(f' ✗ 失败:{result["message"]}', 'scheduler')
|
||||
fail_count += 1
|
||||
|
||||
print('定时任务执行完成')
|
||||
log_info(f'定时任务执行完成,成功 {success_count} 个,失败 {fail_count} 个', 'scheduler')
|
||||
except Exception as e:
|
||||
print(f'定时任务执行失败:{e}')
|
||||
log_error(f'定时任务执行失败:{e}', 'scheduler')
|
||||
|
||||
|
||||
# 启动定时任务
|
||||
scheduler = BackgroundScheduler(timezone='Asia/Shanghai')
|
||||
scheduler.add_job(scheduled_job, 'cron', hour=8, minute=1)
|
||||
scheduler.start()
|
||||
print('定时任务已启动:每天 08:01 执行')
|
||||
log_info('定时任务已启动:每天 08:01 执行', 'app')
|
||||
|
||||
|
||||
# ==================== 启动应用 ====================
|
||||
@@ -87,6 +92,6 @@ print('定时任务已启动:每天 08:01 执行')
|
||||
if __name__ == '__main__':
|
||||
# 支持沙箱环境和本地环境
|
||||
port = int(os.environ.get('DEPLOY_RUN_PORT', os.environ.get('PORT', 5000)))
|
||||
print(f'启动服务器:http://localhost:{port}')
|
||||
print(f'数据库:{os.environ.get("DB_HOST", "haoslm2.xicp.net")}:{os.environ.get("DB_PORT", "10216")}')
|
||||
log_info(f'启动服务器:http://localhost:{port}', 'app')
|
||||
log_info(f'数据库:{os.environ.get("DB_HOST", "haoslm2.xicp.net")}:{os.environ.get("DB_PORT", "10216")}', 'app')
|
||||
app.run(host='0.0.0.0', port=port, debug=True)
|
||||
|
||||
Reference in New Issue
Block a user