```
feat: 添加公司AI管理平台基础架构 添加了完整的FastAPI后端项目结构,包括: - 环境配置文件(.env.example)和项目说明文档(README.md、AGENTS.md) - Dockerfile用于容器化部署 - 核心基础设施:配置管理、数据库连接、调度器、安全认证 - 模块化设计:AI代理、审批流程、审计日志、业务台账等功能模块 - 支持多数据库连接(主库和遗留系统只读库) - AI适配器支持OpenClaw、Hermes、OpenAI兼容接口 - 飞书集成、报表生成、风险监控等企业级功能 - 完整的依赖管理和测试指南 ```
This commit is contained in:
82
app/core/scheduler.py
Normal file
82
app/core/scheduler.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.core.config import get_settings
|
||||
|
||||
|
||||
def attach_scheduler(app: FastAPI) -> None:
|
||||
"""Attach optional APScheduler jobs to the FastAPI application."""
|
||||
|
||||
settings = get_settings()
|
||||
if not settings.scheduler_enabled:
|
||||
return
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
|
||||
from app.core.database import SessionLocal
|
||||
from app.modules.reports.service import ReportService
|
||||
|
||||
scheduler = BackgroundScheduler(timezone="Asia/Shanghai")
|
||||
|
||||
def run_daily_brief() -> None:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
report = ReportService(db).daily_brief()
|
||||
app.state.last_daily_brief = report
|
||||
if (
|
||||
settings.feishu_app_id
|
||||
and settings.feishu_app_secret
|
||||
and settings.feishu_default_chat_id
|
||||
):
|
||||
ReportService(db).push_report(
|
||||
report,
|
||||
receive_id=settings.feishu_default_chat_id,
|
||||
receive_id_type="chat_id",
|
||||
actor="scheduler",
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def run_project_weekly() -> None:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
report = ReportService(db).project_weekly()
|
||||
app.state.last_project_weekly = report
|
||||
if (
|
||||
settings.feishu_app_id
|
||||
and settings.feishu_app_secret
|
||||
and settings.feishu_default_chat_id
|
||||
):
|
||||
ReportService(db).push_report(
|
||||
report,
|
||||
receive_id=settings.feishu_default_chat_id,
|
||||
receive_id_type="chat_id",
|
||||
actor="scheduler",
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
scheduler.add_job(
|
||||
run_daily_brief,
|
||||
trigger="cron",
|
||||
hour=settings.daily_brief_cron_hour,
|
||||
minute=settings.daily_brief_cron_minute,
|
||||
id="daily_brief_push",
|
||||
replace_existing=True,
|
||||
)
|
||||
scheduler.add_job(
|
||||
run_project_weekly,
|
||||
trigger="cron",
|
||||
day_of_week=settings.weekly_project_report_day_of_week,
|
||||
hour=settings.weekly_project_report_cron_hour,
|
||||
minute=settings.weekly_project_report_cron_minute,
|
||||
id="project_weekly_push",
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
@app.on_event("startup")
|
||||
def start_scheduler() -> None:
|
||||
scheduler.start()
|
||||
|
||||
@app.on_event("shutdown")
|
||||
def stop_scheduler() -> None:
|
||||
scheduler.shutdown(wait=False)
|
||||
Reference in New Issue
Block a user