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
onInitin server.ts, is there a better approach people have had or? Trying to get a sanity check on this, many thanks!
hi, have a look a the payload demo example. they are using nodecron there 🙂
Brilliant, thanks mate!
I implemented that. That particular library is really easy to work with 👍
I have a followup question for Payload 3.0. Since the
server.tsfile no longer exists, where schuld I write/start my cron function? My first guess would be in the
payload.config.tsfile, but it did not work for me. I also could not find the "entry" point of the payload app.
There is an
https://github.com/payloadcms/payload-3.0-demo/blob/main/payload.config.ts#L98, might this help?
That looks promising... I will try it out in a bit and give feedback 🫶
onInitshould 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
with this approach you are completely free
Thanks! The
onInitfunction in the config does the job for me perfectly. But thanks a lot for the other options!
Hi folks, I am working on a 'collection view counter' feature in my 3.0 project which I will probably later make into a sharable plugin.
If I wanted to implement a cron job (to do periodic DB cleanup) tasks from a plugin environment how would I do this?
There are ways I can 'cheat' like persist a timestamp and then check it on the next read via hooks.
I am looking for something that is contained that can hopefully avoid the user setting up the cron themselves if possible.
Do any of the other existing plugins have any sort of cron job built into them?
Star
Discord
online
Get dedicated engineering support directly from the Payload team.