module4
20 分钟

定时任务与自动化

学习进度

当前模块0/6 课
总进度0/36 课
学习时长0 分钟

学习目标

  • 使用 Codex 配置和管理 Cron 定时任务
  • 实现日报、周报、月报的自动生成
  • 搭建定时代码质量检查系统
  • 构建异常检测与告警机制

304 定时任务与自动化

使用 Codex 配置和管理定时任务,实现自动化报告生成、代码质量巡检和异常告警

学习目标

一、Cron 任务配置与管理

创建定时任务

# 创建定时任务
codex cron --create "每日数据备份" \
  --schedule "0 2 * * *" \
  --command "codex run backup --type daily"

# 列出所有定时任务
codex cron --list

# 测试运行定时任务
codex cron --test "每日数据备份"

常用调度模式

场景Cron 表达式说明
每小时`0 * * * *`整点执行
每日凌晨`0 2 * * *`凌晨 2 点
每周一`0 9 * * 1`周一早 9 点
每月 1 号`0 8 1 * *`月初早 8 点
每 15 分钟`*/15 * * * *`高频监控
**💡 提示:** 使用 `codex cron --validate` 检查 Cron 表达式语法的正确性,避免调度错误。

二、自动化报告生成

日报/周报/月报

# 生成日报(基于今日 Git 活动)
codex report --type daily

# 生成周报
codex report --type weekly --output reports/weekly-$(date +%Y-W%V).md

# 生成月报(含代码统计)
codex report --type monthly --with-stats

报告内容配置

`.codex/reports.yaml`:

reports:
  daily:
    sections:
      - git-activity        # 代码提交摘要
      - review-summary      # 审查活动
      - test-results        # 测试结果
    output: reports/daily/
  weekly:
    sections:
      - git-activity
      - review-summary
      - test-results
      - coverage-trend      # 覆盖率趋势
      - velocity-metrics    # 开发速率
    output: reports/weekly/
    delivery:
      - email: team@company.com
      - slack: "#dev-daily"

三、定时代码质量巡检

质量监控任务

#!/bin/bash
# scripts/quality-check.sh — 每日代码质量巡检

echo "📊 开始每日代码质量巡检..."

# 1. 全量审查
codex review --output reports/quality/$(date +%Y-%m-%d)-review.md

# 2. 安全扫描
codex review --security --output reports/quality/$(date +%Y-%m-%d)-security.md

# 3. 测试覆盖率检查
codex test --coverage --output reports/quality/$(date +%Y-%m-%d)-coverage.md

# 4. 依赖安全检查
codex deps --audit --output reports/quality/$(date +%Y-%m-%d)-deps.md

# 5. 生成汇总
codex report --type daily --output reports/quality/$(date +%Y-%m-%d)-summary.md

echo "✅ 巡检完成!"

配置定时执行

# 每天早上 9 点执行质量巡检
codex cron --create "每日质量巡检" \
  --schedule "0 9 * * 1-5" \
  --command "bash scripts/quality-check.sh"

四、异常检测与告警

异常监控规则

# 创建异常检测任务
codex monitor --create "测试覆盖率下降" \
  --metric "test-coverage" \
  --condition "value < 80" \
  --action "notify"

# 配置告警渠道
codex monitor --alert-channel slack "#dev-alerts"
codex monitor --alert-channel email "oncall@company.com"

监控指标示例

监控指标告警条件严重级别
测试覆盖率低于 80%Warning
构建时间超过 10 分钟Warning
Critical 审查问题数大于 0Critical
依赖漏洞数Critical 级别 > 0Critical
API 响应时间P95 > 500msWarning

五、监控仪表盘搭建

快速搭建

# 生成监控仪表盘
codex dashboard --create \
  --metrics "coverage,build-time,review-issues,api-latency" \
  --output dashboards/dev-health.html

# 启动本地仪表盘服务
codex dashboard --serve --port 3000

仪表盘配置

# .codex/dashboard.yaml
dashboard:
  title: "开发健康度仪表盘"
  refresh: 60  # 秒
  panels:
    - title: "测试覆盖率"
      type: gauge
      metric: test-coverage
      thresholds: [80, 90]
    - title: "审查问题趋势"
      type: line
      metric: review-issues
      range: 30d
    - title: "构建时间"
      type: bar
      metric: build-time
**💡 提示:** 将仪表盘链接加入团队 Wiki 首页,让代码质量对全员透明可见。

六、本课总结

**核心要点:**

  • `codex cron` 管理定时任务,支持标准 Cron 表达式
  • `codex report` 自动生成日报、周报、月报
  • 定时质量巡检脚本确保代码质量不退化
  • `codex monitor` 配置异常检测规则和告警渠道
  • 监控仪表盘让质量趋势一目了然

**下一步:** 学习知识库管理 → [305 知识库管理](module4-305.md)

上一课
下一课