Hi, I am attempting to create a cron job. The cron job I created correctly shows up on my Vercel dashboard. But the cron job does not run at the scheduled time. Instead, it only runs once I start building a new build. That probably sounds weird, but I’ve tried four times and every time the cron job does not run until I start a new build.
This is my vercel.json file:
{
“crons”: [{
“path”: “/api/cron”,
“schedule”: “35 4 *”
}]
}
@amyegan I had already used the crontab guru to validate the time. Also, I know the cron job can run within a one hour window of the time given. I’ve waited for the whole hour and the cron job does not run, until I start a new build.
@amyegan Oh I just realized my schedule was missing “* *”, which is why you thought there was a problem with the cron schedule. I think I accidentally deleted part of the code after I pasted it into the forum. But my actual code is “35 4 * * *”
That schedule makes more sense! If you try to run the /api/cron function directly and then try to click the Run button in project settings to manually run the cron job. What happens in each of those scenarios?
When I run /api/cron in Postman, nothing happens. And when I click the Run button, the log shows status=200. But the code in /api/cron still did not run. So then I tried starting a new build, and then the /api/cron finally ran.
It seems like there’s a problem with the function itself if nothing happens when you run it with Postman or the Run button. I recommend looking into what’s going wrong with the function.
The Deployment Summary will tell you more about the functions that were deployed. And Runtime Logs can help you get more details about what happens when the function is called. You can also check the deployment output
The /api/cron function does correctly show up in the Deployment Summary. The Runtime Logs show that the GET request to /api/cron went through successfully for BOTH the call from Postman and the call from the Run button, even though the code in /api/cron did not run. What else should I try? Thanks!
Since the functions at least attempt to do something, I suspect there’s a silent error or a flaw in the return data. Using try/catch and console.log() is probably the simplest way to gather more info about what’s happening inside the function.
Most likely the logs won’t show up since the function you’re executing through your cronjob is cached. The cron job will always run according to it’s schedule, but if it hits an endpoint that’s cached then it won’t show. Cron jobs don’t have the ability to completely bypass the platform/code caching and instead are just treated as a normal request.
The underlying function is generated during build, so the Cron Job cannot bypass the settings defined (or not, so default caching behavior) when executing the function.
This caching behaviour can be prevented by adding the following to your API route /api/cron (see docs): export const dynamic = 'force-dynamic'
Can you give this a try and let us know if it works?