#Best way to implement cron functions?

9 messages · Page 1 of 1 (latest)

dusty adder
#

We're looking to have real time events going on the platform with time-sensitive functions such as opening and closing an event for user interactions and more.

So we need to run cron functions and I'm just wondering what's the best way to approach this in Payload?

I can run my own cron functions inside onInit in server.ts, is there a better approach people have had or? Trying to get a sanity check on this, many thanks!

valid trellis
main shore
#

I implemented that. That particular library is really easy to work with 👍

terse dust
#

I have a followup question for Payload 3.0. Since the server.ts file no longer exists, where schuld I write/start my cron function? My first guess would be in the payload.config.ts file, but it did not work for me. I also could not find the "entry" point of the payload app.

prime plume
terse dust
#

That looks promising... I will try it out in a bit and give feedback 🫶

plush tusk
#

@terse dust
onInit should work but only if not on a serverless env like Vercel, if there - you should use a cron service like this https://vercel.com/docs/cron-jobs

another one if you want to ensure about "entry point" is to use a custom server
https://github.com/vercel/next.js/blob/canary/examples/custom-server/server.ts
with this approach you are completely free

GitHub

The React Framework. Contribute to vercel/next.js development by creating an account on GitHub.

terse dust
#

Thanks! The onInit function in the config does the job for me perfectly. But thanks a lot for the other options!