Cron Job Not Running

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 *”
}]
}

Hi @for-ry-in-range. It looks like there might be a problem with your cron schedule.

You can use a tool crontab guru to validate your cron expression.

@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.

What else do you recommend I try?

@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 * * *” :smiley:

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.

import { inngest } from "../../../../inngest/client";

export async function GET() {
    try {
        await inngest.send({
          name: "myfunc/send-summary"
        });
        console.log("Cron worked")
        return new Response("Successful!", { status: 200 });
    } catch (error) {
        console.error("Error:", error)
        return new Response("Error occured", {status: 500})
    }
}

This is my code at /api/cron. I have a console.log() and console.error() but nothing is printing. It would print to the Logs tab, right?

Hi, @amyegan are you still there?

Yes, you should see it in the Logs tab for the deployment. Do you see any errors, even ones that seem unrelated, in the Logs tab?

What happens if you put a console.log() before await inngest... or even before the try/catch statement?


@amyegan I put the console.log() before the try/catch statement and I still didn’t get anything in the Logs tab. This is what I see in the Logs tab.

Please check your middleware. It’s possible that something there is preventing the function from working as you expect.

Note that you can click the log row to expand a panel with more details, which would include the console.log() output if available.

@amyegan I don’t see anything printing when I expand the panel:

This is my middleware.ts:

import { clerkMiddleware } from "@clerk/nextjs/server";

export default clerkMiddleware();

export const config = {
  matcher: ['/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
 '/(api|trpc)(.*)',
 '/app/api/get_inboxes(.*)',
 '/app/api/cron(.*)'],
};

Is there anything wrong with it?

Hey @for-ry-in-range ,

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?

3 Likes

It works now after adding export const dynamic = 'force-dynamic'

Thank you so much for your help!

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.