archery-auto-approve 是一个基于 Go 1.21+ 的守护进程,用于在北京时间非人工审批时段自动审批 Archery 待处理工单。
默认规则如下,且现在已经支持通过 config.yaml 配置:
schedule.workdays中指定的工作日里,business_hours表示人工审批时间段,不执行自动审批- 工作日中落在人工审批时间段之外的时间自动审批
- 非工作日是否自动审批由
schedule.weekends_auto_approve控制 - 自动审批备注固定可配置,默认值为
系统自动审批(非工作时间)
- Viper 读取
config.yaml - 支持
.env注入认证信息 - 支持在配置文件中定义自动审批时间规则与时区
- 支持 Token 直连认证,或使用用户名/密码调用
/api/auth/token/获取 JWT,并在过期后用/api/auth/token/refresh/刷新 - 定时轮询待审批工单
- 按 Archery OpenAPI v1.3.0 调用
GET /api/v1/workflow/和POST /api/v1/workflow/audit/ - 审批前会再次查询当前工单状态,已通过则跳过,避免重复审批
- 审批失败自动重试 3 次
- zap JSON 日志
- 并发审批控制
SIGINT/SIGTERM优雅退出- 可选
GET /healthz健康检查 - 单元测试覆盖时间判断与 API 模拟调用
- 默认每
300秒执行一次,也就是每5分钟轮询一次 - 配置项为 config.yaml 里的
poll_interval - 如果你想改成每分钟执行一次,可以设置
poll_interval: 60
archery-auto-approve/
├── api/
│ ├── client.go
│ ├── fields.go
│ └── workflow.go
├── config/
│ └── config.go
├── model/
│ └── workflow.go
├── scheduler/
│ └── scheduler.go
├── utils/
│ ├── log.go
│ └── time.go
├── config.yaml
├── Dockerfile
├── docker-compose.yml
├── go.mod
├── main.go
└── README.md
优先在 config.yaml 配置基础参数,并把敏感认证信息放在 .env。
config.yaml 示例:
archery:
base_url: "https://your-archery.example.com"
token: ""
refresh_token: ""
token_ttl: 3600
auth_scheme: "Bearer"
workflow_list_path: "/api/v1/workflow/"
workflow_approve_path: "/api/v1/workflow/audit/"
workflow_approve_alt: "/api/workflow/approve/"
token_path: "/api/auth/token/"
token_refresh_path: "/api/auth/token/refresh/"
login_path: "/api/v1/user/login/"
schedule:
timezone: "Asia/Shanghai"
workdays:
- "monday"
- "tuesday"
- "wednesday"
- "thursday"
- "friday"
business_hours:
start: "10:00"
end: "19:00"
weekends_auto_approve: true
poll_interval: 300
log_level: "info"
max_concurrent: 5
approval_remark: "系统自动审批(非工作时间)"
approver: ""
pending_statuses:
- "workflow_manreviewing"
retry_count: 3
retry_backoff_sec: 2
health:
enabled: true
port: 8080.env 示例可参考 .env.example:
ARCHERY_AUTO_APPROVE_ARCHERY_BASE_URL=https://your-archery.example.com
ARCHERY_AUTO_APPROVE_ARCHERY_USERNAME=admin
ARCHERY_AUTO_APPROVE_ARCHERY_PASSWORD=xxxxx
ARCHERY_AUTO_APPROVE_ARCHERY_TOKEN=
ARCHERY_AUTO_APPROVE_ARCHERY_REFRESH_TOKEN=
ARCHERY_AUTO_APPROVE_ARCHERY_AUTH_SCHEME=Bearer
ARCHERY_AUTO_APPROVE_ARCHERY_TOKEN_PATH=/api/auth/token/
ARCHERY_AUTO_APPROVE_ARCHERY_TOKEN_REFRESH_PATH=/api/auth/token/refresh/说明:
- 程序启动时会自动读取项目根目录
.env。 - 基础运行参数从 config.yaml 读取,敏感认证信息建议放在 .env。
- 自动审批时间规则从
schedule读取:workdays表示需要套用人工审批时间段的星期business_hours表示人工审批时间段weekends_auto_approve控制非workdays日期是否自动审批
- 如果已持有 Token,可直接通过
.env或config.yaml填写archery.token。 - 如果不填 Token,则会使用
.env中的username/password调用你提供的/api/auth/token/,从返回体中提取access作为审批请求的 Bearer Token。 - 如果
access token失效且已拿到refresh,程序会先调用/api/auth/token/refresh/续期。 - 如果 JWT 接口不可用,仍会回退到
login_path,并兼容旧的{ "token": "..." }返回结构。 - 根据 openapi.json,待审批状态默认使用
workflow_manreviewing。 - 当前工单列表查询只支持
/api/v1/workflow/,不再兼容旧版/api/workflow/列表接口。 - 根据 openapi.json,审批请求里的
audit_type已调整为文档枚举值pass。 approver留空时,程序会自动使用.env里的登录用户名作为审批执行人。
go run main.go启动后程序会先立即执行一次轮询,之后再按 poll_interval 周期继续执行。
如果你已经编译成二进制,也可以这样启动:
go build -o archery-auto-approve .
./archery-auto-approve项目已提供 Dockerfile,运行镜像基于 debian:stable-slim,并且在容器内使用 app 用户启动服务。
构建镜像:
docker build -t archery-auto-approve:latest .启动容器:
docker run -d \
--name archery-auto-approve \
--restart unless-stopped \
--env-file .env \
-v $(pwd)/config.yaml:/app/config.yaml:ro \
-p 8080:8080 \
archery-auto-approve:latest查看日志:
docker logs -f archery-auto-approve停止容器:
docker stop archery-auto-approve
docker rm archery-auto-approve项目已提供 docker-compose.yml。
启动:
docker compose up -d --build查看日志:
docker compose logs -f停止:
docker compose downdocker-compose.yml 默认会:
- 使用当前目录的 Dockerfile 构建镜像
- 读取当前目录的 .env
- 挂载当前目录的 config.yaml 到容器内
/app/config.yaml - 暴露健康检查端口
8080 - 使用 Docker
json-file日志驱动 - 单个日志文件最大
10m,保留3个滚动文件 - 通过
/healthz自动执行容器健康检查
如果你修改了 .env 或 config.yaml,需要重新执行:
docker compose up -d --build{"level":"info","ts":"2026-03-20T21:45:00+08:00","msg":"polling workflows","now":"2026-03-20T21:45:00+08:00","auto_approve":true}
{"level":"info","ts":"2026-03-20T21:45:01+08:00","msg":"pending workflows loaded","count":3}
{"level":"info","ts":"2026-03-20T21:45:02+08:00","msg":"workflow approved","workflow_id":123,"name":"上线工单-订单表DDL","remark":"系统自动审批(非工作时间)"}[Unit]
Description=Archery Auto Approve
After=network.target
[Service]
WorkingDirectory=/opt/archery-auto-approve
ExecStart=/opt/archery-auto-approve/archery-auto-approve
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target修改完 config.yaml 或 .env 后,需要重启进程或容器让配置生效。
本服务默认暴露 GET /healthz,返回 200 OK 时表示进程正常运行。
本地检查:
curl http://127.0.0.1:8080/healthz查看 docker compose 健康状态:
docker compose ps如果容器状态显示为 healthy,说明 /healthz 探针通过。
如果希望以周期任务运行,也可以将镜像部署为 CronJob。但如果需要持续暴露 /healthz 和保持 Token/连接复用,推荐直接使用 Deployment 或 systemd 常驻运行。
go test ./...