Cron Job Function not working
derrick-feehi
HOBBYOP

4 months ago

Hello Railway Support,

I've deployed a Bun function and I'm trying to get it to run on a schedule.

What I've done:

  1. I've written the source code file, defined teh entry point

    export async function cron(req: CronRequest): Promise<Response> {
  2. I've gone to the function's Settings tab and set the "Cron Schedule" to */5 * * * * (to run every 5 minutes).

  3. I've deployed the service.

I expected to see the console.log("Cron job \"* * * * *\" triggered...") message from my function appear in the logs after the start of the next run.

What actually happened: The function deploys successfully, and the Deploy Logs show the build process (e.g., "Starting Container", "Analyzing index.tsx"). However, even after waiting several minutes, I cannot find my console.log messages anywhere.

I'm not sure if my cron job is failing to trigger or if I am simply looking in the wrong place for the logs. Could you please confirm if my service is receiving the cron triggers?

Thank you!

$10 Bounty

7 Replies

Railway
BOT

4 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


mikaoelitiana
HOBBY

4 months ago

Hi @derrick-feehi, would you mind sharing how your entrypoint file looks like and how it calls the cron function.

On your app deployment, do you see the Cron Run tab ?

Attachments


mikaoelitiana

Hi @derrick-feehi, would you mind sharing how your entrypoint file looks like and how it calls the cron function.On your app deployment, do you see the Cron Run tab ?

derrick-feehi
HOBBYOP

4 months ago

Short snippet of the source code, this ideally should be the entry point for the Function, i believe:

export async function cron(req: CronRequest): Promise<Response> {

// Log a message to your Railway function logs

console.log(

Cron job "${req.cron}" triggered at ${new Date().toISOString()}

);

const ticket_id = Bun.env.TICKET_ID || ("" as string);

if (!ticket_id) {

return Response.json(

{ success: false, error: "Environment variable TICKET_ID is missing" },

{ status: 400 }

);

}

const result = await sendTicketConfirmation(ticket_id);

// Railway will log this response, but nothing "receives" it.

// This is fine.

return Response.json(result);

}


mikaoelitiana
HOBBY

4 months ago

From what I see, the cron calls index.tsx file and I guess cron function is inside this file, right?

Somewhere in that file, you should have an explicit call to the cron function, something like

// ...
cron(req)
  .then(...)
  .catch(...)

mikaoelitiana

From what I see, the cron calls index.tsx file and I guess cron function is inside this file, right? Somewhere in that file, you should have an explicit call to the cron function, something like// ... cron(req) .then(...) .catch(...)

derrick-feehi
HOBBYOP

4 months ago

Not sure I need that, bare in mind this is Railway Function-Bun tasks.


mikaoelitiana
HOBBY

4 months ago

If you did not define the cron function, I guess your code would be executed directly. But since the function is defined, it won't be executed unless explicitly called I think


z0xm
FREE

3 months ago

I am facing the same issue,

async function main() {
  if (result.success) {
    process.exit(0);
  } else {
    process.exit(1);
  }
}

// Run the script
main().catch((error) => {
  process.exit(1);
});

this is a sample script that i am running.
It runs perfectly if deployed as a normal service without cron.
Does not run with cron or even with 'run now'


Loading...