# fastagent **Repository Path**: kadycui/fastagent ## Basic Information - **Project Name**: fastagent - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-16 - **Last Updated**: 2025-08-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 一个基于 FastAPI 的 RPA(机器人流程自动化)API 服务,集成了 Celery 异步任务队列、Tortoise-ORM 数据库操作、Redis 缓存、WebSocket 实时通信和定时任务等功能,适合自动化任务调度与管理。 ## 目录结构 ``` . ├── app/ │ ├── api/ # API 路由与接口 │ │ ├── v1/ # v1 版本接口 │ │ └── urls.py # 路由注册 │ ├── common/ # 通用工具 │ │ ├── database.py # 数据库连接 │ │ ├── cache.py # Redis 缓存 │ │ └── logging.py # 日志配置 │ ├── config/ # 配置文件 │ │ └── config.py # 系统配置 │ ├── middleware/ # 中间件 │ │ ├── cors.py # 跨域中间件 │ │ └── http_timing.py # HTTP 计时中间件 │ ├── models/ # ORM 数据模型 │ │ ├── rpa.py # RPA 任务模型 │ │ └── user.py # 用户模型 │ ├── schemas/ # Pydantic 数据结构 │ │ ├── response.py # 响应模型 │ │ ├── rpa.py # RPA 数据验证模型 │ │ └── user.py # 用户数据验证模型 │ ├── services/ # 业务逻辑处理 │ │ ├── rpa_job_service.py # RPA 任务服务 │ │ └── user_service.py # 用户服务 │ ├── tasks/ # Celery 任务 │ │ ├── task1.py # 任务组 1 │ │ └── task2.py # 任务组 2(定时任务) │ ├── templates/ # 模板文件(用于仪表盘) │ ├── static/ # 静态文件 │ ├── main.py # FastAPI 应用入口 │ ├── celery_worker.py # Celery worker 配置 │ └── start_celery.sh # Celery 启动脚本 ├── build/ # 构建相关 ├── deploy/ # 部署相关 ├── logs/ # 日志 └── README.md ``` ## 核心功能 ### 1. FastAPI Web 服务 - 入口:`app/main.py` - 支持自定义文档、静态文件、CORS、HTTP 计时中间件 - 数据库与 Redis 生命周期管理 - 路由注册集中在 `app/api/urls.py` ### 2. RPA 任务管理 API - 主要接口在 `app/api/v1/rpa_job.py` - 支持任务的增删改查(CRUD) - 数据模型见 `app/models/rpa.py` - 业务逻辑封装在 `app/services/rpa_job_service.py` - 响应结构统一封装在 `app/schemas/response.py` ### 3. 实时数据仪表盘 - WebSocket 接口:`app/api/v1/ws.py` - 仪表盘 API:`app/api/v1/dashboard.py` - 支持实时数据更新和历史数据查询 - 提供任务统计、月度报表等功能 ### 4. Celery 异步任务与定时任务 - 配置见 `app/celery_worker.py` - 任务实现见 `app/tasks/` - 支持定时同步(每 5 分钟)、自动重启失败任务、每日报告等 - 时区设置为亚洲/上海 ### 5. 数据库与缓存 - 数据库:Tortoise-ORM,MySQL - 缓存:Redis,支持生命周期管理 - 数据库连接配置:`app/common/database.py` - Redis 连接配置:`app/common/cache.py` ## 运行与部署 ### 本地运行 ```bash # 启动 FastAPI 服务 python -m backend.main # 或使用脚本 bash app/start_uvicorn.sh ``` ### Celery 启动 ```bash # 使用脚本启动 Celery bash app/start_celery.sh # 或手动启动 # 启动 worker celery -A app.celery_worker worker --loglevel=info # 启动定时任务 celery -A app.celery_worker beat --loglevel=info # 启动监控 celery -A app.celery_worker flower --port=5555 --address=0.0.0.0 ``` ### Docker 部署 ```bash docker-compose up --scale worker=3 -d ``` ### 测试 ```bash PYTHONPATH=$(pwd) pytest app/tests/ ``` ## 主要依赖 - Python 3.12+ - FastAPI - Tortoise-ORM - Celery - Redis - MySQL - Jinja2 (用于仪表盘模板) ## 配置说明 主要配置项位于 `app/config/config.py`,可通过环境变量覆盖: - 数据库配置:MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE - Redis 配置:REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_DB - Web 服务配置:WEB_HOST, WEB_PORT - 影刀 API 配置:YINGDAO_TOKEN_URL, ACCESS_KEY_ID, ACCESS_KEY_SECRET ## UML 类图 ```mermaid classDiagram class JobRecords { int id str app_uuid str app_name date start_date date end_date ResultStatusEnum result_status datetime exec_start_time datetime exec_end_time int exec_duration str result_detail datetime created_at datetime updated_at str other_info } class RpaJobService { +get_jobs() +create_job() +update_job() +delete_job() } class ResponseBase { int code str message Optional data } class ResponseList { int total List data } class ResponseId { int id } class CeleryWorker { +Celery celery_app +beat_schedule } class FastAPIApp { +register_routers() +register_cors_middleware() +register_static_file() +register_docs() +register_http_timing_middleware() } RpaJobService --> JobRecords ResponseList --|> ResponseBase ResponseId --|> ResponseBase FastAPIApp --> RpaJobService FastAPIApp --> CeleryWorker ``` ## 功能特点 - **RESTful API**:提供标准的 RESTful API 接口,支持 RPA 任务的全生命周期管理 - **实时监控**:通过 WebSocket 实现仪表盘数据实时更新 - **定时任务**:支持多种定时策略(固定间隔、Cron 表达式) - **自动重试**:自动检测并重启失败的 RPA 任务 - **数据统计**:提供任务成功率、执行时长等统计数据 - **生命周期管理**:优雅处理数据库和 Redis 连接的生命周期