Articles

Vercel cron timing out? Keep the schedule, move the work

Keep Next.js and its cron schedule on Vercel, hand off a slow CSV export to Gregale, and verify duplicate triggers produce one completed file.

Updated

If a Vercel cron route times out while building a report, let it submit a job and return. Keep Next.js and the cron expression on Vercel. A separate worker generates the report and records where to find it. A 202 Accepted with a run ID is a receipt, not an export.

Gregale publishes this guide. The example moves one nightly CSV export, not the rest of the application.

Check what timed out

Vercel Cron Jobs invoke a function on the production deployment. The function's configured duration still applies; if the work is short and the timeout was simply set too low, configure the duration first. For new all-Vercel projects, compare Vercel Schedules, Queues and Workflows before adding another host. This pattern is for existing apps that want to move only the execution of a slow or portable job.

What stays where

WhereWhat it does
VercelCalls GET /api/cron at 02:00 UTC. The route checks CRON_SECRET, computes yesterday's UTC date, and asks for that export.
Gregale APICommits an export row with a unique report window and returns the run ID. Repeated submissions return the same ID.
Separate Gregale workerPicks up the accepted row, renders the CSV, and records the result and SHA-256. A status endpoint reports when it is actually done.

The trigger should wait only for durable acceptance. If the request fails or returns an unexpected receipt, surface the failure rather than reporting success. If the HTTP response is lost after the database commit, a repeat for the same window should return the existing run ID. Alert on missed windows or jobs that stay accepted too long, not just failures in the cron route.

Make one export per scheduled window

Use a unique database key, not an in-memory lock. This example has one report, so window_date is unique. For several reports or tenants, scope the key to the report and tenant as well. The example runs daily and uses the previous UTC day. For a replay after midnight or a historical backfill, pass the intended window through a separate authenticated operation; recomputing yesterday would select another export.

The included worker claims one queued row and commits the tiny synthetic file and completion marker together. It stores the file in PostgreSQL only to keep the demo small. For real reports, use object storage, a deterministic object key and a recoverable lease. Do not keep a large export inside an open SQL transaction.

Run the example

Download the source. Copy the route and cron configuration into your Next.js app. Keep the Gregale invocation credential and export key in server-only environment variables. The backend deploys separately with managed PostgreSQL. The README shows the local dry run and a Gregale command cron for executing the worker outside the request. Command crons are a preview and need app tasks; verify availability for your app.

Trigger the route twice for the same window. Check that both receipts name one run ID and the file is unavailable at first. Run the worker, then download the CSV and compare its checksum to the completed run.

What we verified

The Next.js route built locally. With real local PostgreSQL, two handoffs returned one run ID; the worker produced the expected file and a second run found no pending export.

The same handoff code called a private Gregale Scale app over HTTPS. Both requests returned the same ID and the download was unavailable before the work ran. A manually fired Gregale command cron completed the export; we downloaded the exact three CSV lines and matched their SHA-256 to the completion record. The test cron was disabled afterwards. We did not register a Vercel production schedule.

Questions

Why does my Vercel cron job time out?

A Vercel Cron Job invokes a Vercel Function, so the configured function duration applies. A long export can be submitted to a worker instead of waiting for it inside the scheduled request.

How do I make sure two cron invocations produce one export?

Use the intended schedule window as a database deduplication key, with report and tenant scope if needed. Return the existing run ID for repeated submissions and track completion separately.