How to trigger function periodically and dynamically?
Unanswered
Pixiebob posted this in #help-forum
PixiebobOP
I'm relatively new to Next and wanna know how can I periodically and dynamically trigger a function (e.g., every 2 days, every x days, or on a specific date) in nextjs in the practical way?
12 Replies
are you using vercel? then I would use vercel cron jobs if not inngest could work as well @Pixiebob
basically you set up an API route check the env var got the cron secret if valid then do something on the server ie your function, then theres a vercel.json that points to the route and sets when it should be triggered. Read those docs and ask me any questions.
@Patrick MacDonald basically you set up an API route check the env var got the cron secret if valid then do something on the server ie your function, then theres a vercel.json that points to the route and sets when it should be triggered. Read those docs and ask me any questions.
PixiebobOP
But cron jobs only for the static interval. What about custom interval or specific datetime?
Cron jobs allow you to select what time and what day to trigger you could do like everyday at 2:00 p.m. or Monday at 6:00 p.m.
Or you could do like every 22nd
@Pixiebob
Like I have a cron job that checks user settings every day at 6 am if user settings and sends out a report of hours clocked in if applicable
@Pixiebob But cron jobs only for the static interval. What about custom interval or specific datetime?
the chart can cover a specific date and time, but if you want something like event happens, trigger function a, then sleep for 2 days, then function b then sleep for 1 week then send welcome email, you would be better served using vercel work flows
Best way to so it is if the time or date is fixed then use cron job, if the time happens after an event use workflows and sleep, I would bet money you could even check a db with work flows and use a custom sleep time inbtween steps
but if you need to do something like every 2 hours, or 6am on the 5th cron jobs are for you