feat(ai_agent): 完善AI适配器和服务功能 - 添加OpenClaw和Hermes健康检查接口 - 实现OpenClaw工具调用功能 - 重构AI适配器使用常量定义 - 增加AI技能系统支持 - 更新配置文件中的默认模型提供者设置 refactor(scheduler): 使用常量替换硬编码值 - 将硬编码的actor值替换为ActorValue常量 - 将receive_id_type替换为FeishuReceiveIdType枚举 refactor(audit): 统一审计日志常量使用 - 将硬编码的actor、source、risk_level等值替换为对应常量 - 更新审核服务中的状态和操作常量引用 refactor(approvals): 标准化审批模块常量使用 - 将applicant默认值替换为ActorValue.API常量 - 使用ApprovalStatus常量替代硬编码状态值 - 更新审核操作常量引用 ```
85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
from fastapi import FastAPI
|
|
|
|
from app.core.constants import ActorValue
|
|
from app.core.config import get_settings
|
|
from app.modules.feishu.constants import FeishuReceiveIdType
|
|
|
|
|
|
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=FeishuReceiveIdType.CHAT_ID,
|
|
actor=ActorValue.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=FeishuReceiveIdType.CHAT_ID,
|
|
actor=ActorValue.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)
|