crontap-supabase-edge-function-cron
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSchedule a Supabase Edge Function
调度 Supabase Edge Function
Use Supabase for SQL-only database maintenance. Use this pattern when
the recurring unit is an HTTP Edge Function and an external scheduler is
desired.
pg_cron对于仅涉及 SQL 的数据库维护,使用 Supabase 。当重复执行单元是 HTTP Edge Function 且需要使用外部调度器时,采用此模式。
pg_cronPrerequisites
前置条件
Before using the required tools, check whether they are available. If any are
missing, stop and offer the user exactly two choices:
- Connect Crontap MCP at . MCP access works on every Crontap tier.
https://mcp.crontap.com/mcp - Configure and
$CRONTAP_CLIENT_IDfor the raw API. Raw API access requires Ultra.$CRONTAP_API_KEY
Do not silently choose a path or continue until the user selects one.
- Deploy the function before creating its schedule.
- Make the function idempotent for one logical period.
- Protect it with a dedicated server-side cron secret.
- Never send a Supabase service-role key to Crontap.
在使用所需工具前,检查它们是否可用。如果有任何缺失,请停止操作,并向用户提供恰好两个选项:
- 在 连接 Crontap MCP。所有 Crontap 套餐均支持 MCP 访问。
https://mcp.crontap.com/mcp - 配置 和
$CRONTAP_CLIENT_ID以使用原生 API。原生 API 访问需要 Ultra 套餐。$CRONTAP_API_KEY
在用户选择之前,不要擅自选择路径或继续操作。
- 创建调度之前,先部署函数。
- 使函数在同一个逻辑周期内具备幂等性。
- 使用专用的服务端 cron secret 进行保护。
- 绝不向 Crontap 发送 Supabase service-role key。
Function security pattern
函数安全模式
Read a dedicated header or bearer value and compare it with a server-only
environment variable before doing work. If Supabase gateway JWT verification
is disabled for the function, this application-layer check is mandatory.
Keep the function response bounded. For long work, durably enqueue the job and
return success only after the queue accepts it.
在执行任务之前,读取专用的 header 或 bearer 值,并将其与服务端专用环境变量进行比较。如果该函数未启用 Supabase gateway JWT 验证,则此应用层检查是强制性的。
限制函数响应的大小。对于耗时较长的任务,将作业持久化入队,并且仅在队列接受该作业后返回成功。
Workflow
工作流
- Deploy the Edge Function and configure its cron secret.
- Call it manually from a controlled environment with and without the secret.
- Confirm expected status codes and idempotency behavior.
- Create the Crontap schedule with the deployed HTTPS URL.
- Verify the resolved cadence and first retained run.
- 部署 Edge Function 并配置其 cron secret。
- 在受控环境中,分别携带和不携带 secret 手动调用该函数。
- 确认预期的状态码和幂等行为。
- 使用已部署的 HTTPS URL 创建 Crontap 调度。
- 验证最终解析出的执行频率和首个保留的运行记录。
MCP example
MCP 示例
json
{
"tool": "create_schedule",
"arguments": {
"url": "https://project-ref.supabase.co/functions/v1/nightly-cleanup",
"text": "every day at 02:00",
"timezone": "UTC",
"method": "POST",
"headers": {
"Authorization": "Bearer <cron-secret-from-secure-store>",
"Content-Type": "application/json"
},
"body": {
"source": "crontap"
},
"label": "Supabase nightly cleanup"
}
}Use the real project reference from the Supabase project URL. Do not place a
secret in the function URL or JSON body.
json
{
"tool": "create_schedule",
"arguments": {
"url": "https://project-ref.supabase.co/functions/v1/nightly-cleanup",
"text": "every day at 02:00",
"timezone": "UTC",
"method": "POST",
"headers": {
"Authorization": "Bearer <cron-secret-from-secure-store>",
"Content-Type": "application/json"
},
"body": {
"source": "crontap"
},
"label": "Supabase nightly cleanup"
}
}使用 Supabase 项目 URL 中真实的 project reference。不要将 secret 放置在函数 URL 或 JSON body 中。
REST fallback
REST 回退方案
REST requires Ultra API access:
bash
curl --fail-with-body https://api.crontap.com/v1/schedule \
-X POST \
-H "ClientId: $CRONTAP_CLIENT_ID" \
-H "ApiKey: $CRONTAP_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"url": "https://project-ref.supabase.co/functions/v1/nightly-cleanup",
"interval": "0 2 * * *",
"timezone": "UTC",
"verb": "POST",
"headers": {
"Authorization": "Bearer <cron-secret-from-secure-store>",
"Content-Type": "application/json"
},
"data": {"source": "crontap"},
"label": "Supabase nightly cleanup"
}'Prefer MCP when available.
REST 需要 Ultra API 访问权限:
bash
curl --fail-with-body https://api.crontap.com/v1/schedule \
-X POST \
-H "ClientId: $CRONTAP_CLIENT_ID" \
-H "ApiKey: $CRONTAP_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"url": "https://project-ref.supabase.co/functions/v1/nightly-cleanup",
"interval": "0 2 * * *",
"timezone": "UTC",
"verb": "POST",
"headers": {
"Authorization": "Bearer <cron-secret-from-secure-store>",
"Content-Type": "application/json"
},
"data": {"source": "crontap"},
"label": "Supabase nightly cleanup"
}'可用时优先使用 MCP。
Verification
验证
- A request without the cron secret returns 401 before side effects.
- A request with the secret returns the intended success status.
- Two requests for the same logical period create one durable side effect.
- The Crontap history outcome matches Supabase function logs at the same time.
- 未携带 cron secret 的请求会在产生副作用之前返回 401。
- 携带 secret 的请求会返回预期的成功状态。
- 同一逻辑周期内的两次请求只会产生一次持久化副作用。
- Crontap 历史记录中的结果与同一时间的 Supabase 函数日志一致。
Safety and plan limits
安全与套餐限制
- Do not use as the scheduler credential.
SUPABASE_SERVICE_ROLE_KEY - Scope the function to the narrow recurring operation.
- If the Supabase project can pause, remember that an external clock exposes the failure but cannot make an unavailable project execute.
- If Crontap rejects the cadence or capacity, present the returned four-option ladder and smallest viable option.
- 不要将 用作调度器凭据。
SUPABASE_SERVICE_ROLE_KEY - 将函数的作用范围限定在特定的周期性操作内。
- 如果 Supabase 项目可能暂停,请记住,外部时钟能够暴露故障,但无法让不可用的项目执行任务。
- 如果 Crontap 拒绝所选的执行频率或容量,请展示返回的四选项分级方案,并给出最小可行选项。
Troubleshooting
故障排查
- Supabase 401 means gateway or function authorization rejected the request.
- Supabase 404 usually means the project reference, function slug, or deploy environment is wrong.
- A 2xx with missing work requires application logs and an idempotency-record check.
- Timeouts are better fixed with durable queueing than aggressive retries.
- Supabase 401 意味着 gateway 或函数授权拒绝了请求。
- Supabase 404 通常意味着 project reference、function slug 或部署环境有误。
- 返回 2xx 但未执行任务时,需要检查应用日志和幂等记录。
- 与频繁重试相比,使用持久化队列是解决超时问题的更好方式。