GCP Cloud Scheduler 核心语法与实战示例速查手册
GCP Cloud Scheduler 核心语法与实战示例速查手册
本文适合收藏备查,直接进入核心内容,不绕弯。
核心概念
| 概念 | 说明 | 示例 |
|---|---|---|
| Job | 一个定时任务 | projects/PROJECT_ID/locations/LOCATION_ID/jobs/JOB_NAME |
| Trigger | 触发器,定义任务执行时间 | 0 * * * * (每小时执行一次) |
| Target | 任务的目标,可以是 HTTP 请求、Pub/Sub 消息 | https://example.com/endpoint |
| Location | 任务的地理位置 | us-central1 |
创建 Job
bash
# 创建一个每小时执行一次的 Job,目标是一个 HTTP POST 请求
gcloud scheduler jobs create http my-job --schedule="0 * * * *" --uri="https://example.com/endpoint" --http-method=POST --oauth-service-account-email="service-account@example.com"
--schedule: 定义任务的执行时间,使用 Cron 表达式--uri: 目标 URL--http-method: HTTP 请求方法(GET, POST, PUT, DELETE 等)--oauth-service-account-email: 用于身份验证的服务账户邮箱
更新 Job
bash
# 更新现有 Job 的目标 URL
gcloud scheduler jobs update http my-job --uri="https://example.com/new-endpoint"
--uri: 新的目标 URL
删除 Job
bash
# 删除指定的 Job
gcloud scheduler jobs delete my-job
列出所有 Job
bash
# 列出指定位置的所有 Job
gcloud scheduler jobs list --location=us-central1
暂停 Job
bash
# 暂停指定的 Job
gcloud scheduler jobs pause my-job
恢复 Job
bash
# 恢复指定的 Job
gcloud scheduler jobs resume my-job
Pub/Sub Target
bash
# 创建一个每分钟发送 Pub/Sub 消息的 Job
gcloud scheduler jobs create pubsub my-pubsub-job --schedule="*/1 * * * *" --topic=my-topic --message-body="Hello, Pub/Sub!"
--topic: Pub/Sub 主题名称--message-body: 消息内容
重试配置
bash
# 创建一个带有重试配置的 Job
gcloud scheduler jobs create http my-retry-job --schedule="0 * * * *" --uri="https://example.com/endpoint" --retry-count=5 --min-backoff=10s --max-backoff=300s
--retry-count: 重试次数--min-backoff: 最小重试间隔--max-backoff: 最大重试间隔
日志查看
bash
# 查看 Job 的执行日志
gcloud scheduler jobs describe my-job --location=us-central1
时区设置
bash
# 创建一个指定时区的 Job
gcloud scheduler jobs create http my-timezone-job --schedule="0 9 * * *" --uri="https://example.com/endpoint" --time-zone="Asia/Shanghai"
--time-zone: 时区名称
环境变量
bash
# 创建一个带有环境变量的 Job
gcloud scheduler jobs create http my-env-job --schedule="0 * * * *" --uri="https://example.com/endpoint" --headers="Authorization: Bearer $(gcloud auth print-access-token)"
--headers: HTTP 请求头,可以包含环境变量
任务执行记录
bash
# 查看 Job 的执行记录
gcloud scheduler jobs describe my-job --location=us-central1 --format="json(executions)"
高级配置
| 配置 | 说明 | 示例 |
|---|---|---|
--description |
描述信息 | gcloud scheduler jobs create http my-job --description="My custom job" |
--max-attempts |
最大尝试次数 | gcloud scheduler jobs create http my-job --max-attempts=10 |
--schedule-timezone |
设置任务的时区 | gcloud scheduler jobs create http my-job --schedule-timezone="America/New_York" |
--retry-configuration |
重试策略配置 | gcloud scheduler jobs create http my-job --retry-configuration="max-retry-attempts=5, min-backoff-duration=10s, max-backoff-duration=300s" |
常见问题
-
如何查看 Job 的状态?
bashgcloud scheduler jobs describe my-job --location=us-central1 -
如何设置 Job 的执行日志?
bashgcloud scheduler jobs update http my-job --log-ttl=7d -
如何暂停所有 Job?
bashgcloud scheduler jobs pause all --location=us-central1
实战示例
示例 1: 每天备份数据库
bash
# 创建一个每天凌晨 2 点执行备份任务的 Job
gcloud scheduler jobs create http daily-backup --schedule="0 2 * * *" --uri="https://example.com/backup" --http-method=POST --oauth-service-account-email="service-account@example.com" --time-zone="Asia/Shanghai"
--schedule: 每天凌晨 2 点--uri: 执行备份的 API 端点--http-method: POST 请求--oauth-service-account-email: 用于身份验证的服务账户邮箱--time-zone: 时区设置为上海
示例 2: 每 5 分钟检查服务状态
bash
# 创建一个每 5 分钟检查服务状态的 Job
gcloud scheduler jobs create http service-check --schedule="*/5 * * * *" --uri="https://example.com/health-check" --http-method=GET --oauth-service-account-email="service-account@example.com"
--schedule: 每 5 分钟--uri: 检查服务状态的 API 端点--http-method: GET 请求--oauth-service-account-email: 用于身份验证的服务账户邮箱
示例 3: 每周发送电子邮件报告
bash
# 创建一个每周五下午 5 点发送电子邮件报告的 Job
gcloud scheduler jobs create http email-report --schedule="0 17 * * 5" --uri="https://example.com/send-email" --http-method=POST --oauth-service-account-email="service-account@example.com" --description="Weekly Email Report"
--schedule: 每周五下午 5 点--uri: 发送电子邮件的 API 端点--http-method: POST 请求--oauth-service-account-email: 用于身份验证的服务账户邮箱--description: 描述信息
示例 4: 每小时清理日志
bash
# 创建一个每小时清理日志的 Job
gcloud scheduler jobs create http log-cleanup --schedule="0 * * * *" --uri="https://example.com/cleanup-logs" --http-method=POST --oauth-service-account-email="service-account@example.com" --retry-count=3 --min-backoff=10s --max-backoff=300s
--schedule: 每小时--uri: 清理日志的 API 端点--http-method: POST 请求--oauth-service-account-email: 用于身份验证的服务账户邮箱--retry-count: 重试次数--min-backoff: 最小重试间隔--max-backoff: 最大重试间隔
结尾推荐
在使用 GCP Cloud Scheduler 时,Cron 表达式的生成和验证是常见的痛点。推荐使用 Hey Cron,一个免费在线工具网站,提供以下功能:
- Cron 表达式生成器:输入中文描述,秒转 Cron 表达式
- 正则表达式生成器:轻松生成和测试正则表达式
- 中英互译:方便的中英文翻译工具
- JSON 格式化:格式化和验证 JSON 数据
- Base64 编码解码:快速进行 Base64 编码和解码
- 时间戳转换:时间戳与日期时间的互相转换
- JWT 解析:解析 JWT 令牌,查看其内容
希望这些工具能帮助你更高效地管理和调试定时任务。