Cron job not working?

Ive ben using this code for a cron schedule however it wont run on the render server, but it do work on my localhost computer.

"
cron.schedule(‘2 5 * * *’, () => {
// Adjust the timezone to Europe/Stockholm (Swedish timezone)
const swedishTimezone = ‘Europe/Stockholm’;
const swedishNow = new Date().toLocaleString(‘en-US’, { timeZone: swedishTimezone });
removeItemsThreeMonth();
});
"

I also used a more simple cron schedule without the timezone.I am using Free version however the server is up when the cron job should be run, so i tested it.

Cant i use cron schedules in my server code?

I also tried this set timeout code but it only works on localhost but not on render server?

"async function customScheduler() {
const targetTime = new Date();
targetTime.setHours(20, 42, 0, 0);

let timeUntilExecution = targetTime - new Date(); // Use let to allow reassignment

if (timeUntilExecution < 0) {
// If the target time has already passed for today, schedule it for the next day
targetTime.setDate(targetTime.getDate() + 1);
timeUntilExecution = targetTime - new Date();
}

setTimeout(async () => {
try {
await removeItemsThreeMonth();
} catch (error) {
console.error(‘An error occurred while removing items:’, error);
}

// Schedule the next execution (e.g., for the next day)
customScheduler();

}, timeUntilExecution);
}

// Start the custom scheduler
customScheduler();
"

Hi there,

Free instances go to sleep after 15 minutes of no http activity so you can’t use this method because the service might not be running - you either need to use a paid instance type or use our cron-job feature,

Regards,

John B
Render Support, UTC+1 :uk:

Thanks but as i said in OP i tried the timeout and cron schedule before 15 minuets, meaning i start server adn put the time like 5 min after server start to test the code.

It stil wont seem to run since, it doesnt console log as it does on my localhost and the code wont seem to run at all?

Any idea why this could be?

HTTP activity more specifically means new requests to the app from the outside world. Operations your service is currently processing are not considered when keeping a Free service awake.

It’s very likely that the service was sleeping at the time that it failed to process the request.

Isnt the server supposed to stay online for 15 minuets after start?

Wont cron or timeout code run if its up during those 15 mins?

During those first 15 minutes yes. But the static time specification you’ve mentioned in these examples seem unlikely to be during that 15 minute window.

Depending on how that cron is set up and stored, it might be lost when the service shuts down (sleeps).

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