feat(scheduler): 增加库存预警每日 9:30 定时邮件发送任务
This commit is contained in:
@ -17,4 +17,8 @@ qrcode[pil]>=7.4.2
|
||||
# [新增] 必须添加,用于处理 token 登录
|
||||
Flask-JWT-Extended==4.6.0
|
||||
# [新增] Excel 处理库 (解决 No module named 'openpyxl' 报错)
|
||||
openpyxl>=3.1.2
|
||||
openpyxl>=3.1.2
|
||||
# [新增] 定时任务调度器 (库存预警每日邮件)
|
||||
APScheduler==3.10.4
|
||||
# [新增] 时区处理 (APScheduler 需要)
|
||||
pytz
|
||||
@ -1,9 +1,39 @@
|
||||
# inventory-backend/run.py
|
||||
from app import create_app
|
||||
|
||||
# Gunicorn 或 uWSGI 会寻找名为 'app' 的实例
|
||||
app = create_app()
|
||||
|
||||
# =========================================================
|
||||
# 启动时注册库存预警定时任务(每天 9:30 北京时)
|
||||
# =========================================================
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
import pytz
|
||||
|
||||
beijing_tz = pytz.timezone('Asia/Shanghai')
|
||||
|
||||
|
||||
def _run_warning_job():
|
||||
with app.app_context():
|
||||
try:
|
||||
from app.services.inventory_task import InventoryWarningService
|
||||
result = InventoryWarningService.check_and_send_warning_emails()
|
||||
print(f"[Scheduler] 库存预警扫描完成: red={result['red_count']}, yellow={result['yellow_count']}")
|
||||
except Exception as e:
|
||||
print(f"[Scheduler] 库存预警任务失败: {e}")
|
||||
|
||||
|
||||
scheduler = BackgroundScheduler(timezone=beijing_tz)
|
||||
scheduler.add_job(
|
||||
func=_run_warning_job,
|
||||
trigger=CronTrigger(hour=9, minute=30, tzinfo=beijing_tz),
|
||||
id='inventory_warning_daily',
|
||||
name='库存预警每日邮件发送',
|
||||
replace_existing=True
|
||||
)
|
||||
scheduler.start()
|
||||
print("✅ 库存预警定时任务已启动(每天 9:30 北京时间执行)")
|
||||
|
||||
if __name__ == '__main__':
|
||||
# =================================================
|
||||
# 路由打印调试 (启动时会在控制台列出所有 URL)
|
||||
|
||||
Reference in New Issue
Block a user