Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Best way to implement cron functions?

default discord avatar
paulpopus3 years ago
9

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!

  • default discord avatar
    60pfennig3 years ago

    hi, have a look a the payload demo example. they are using nodecron there 🙂


    https://github.com/payloadcms/public-demo/blob/master/src/server.ts
  • default discord avatar
    paulpopus3 years ago

    Brilliant, thanks mate!

  • discord user avatar
    denolfe
    3 years ago

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

  • default discord avatar
    freaky_chrissi2 years ago

    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.

  • default discord avatar
    freaky_chrissi2 years ago

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

  • default discord avatar
    ritsu04552 years ago
    @407577392769400832


    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

  • default discord avatar
    freaky_chrissi2 years ago

    Thanks! The

    onInit

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

  • default discord avatar
    mobeigi2 years ago

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.