Executing events at specific time
Hey there, does anybody know how I can execute events at a specific dateTime at the serverside? I know I can use like a scheduler to run every second, but I dont think thats the best practice, or is it?
13 Replies
Set up a cron job with your provider. Where are you hosting?
You can also setup a cronjob in your nuxt application using a cron library. The provider doesn't matter in that case.
I think you can use nitro Tasks. They ca be scheduled
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
As far as I could see its about scheduling it x amount of time, instead of executing it at an exact datetime.
The usecase here is to start a task, and to give the task and enddate, when it reaches the enddate, it need to be validated. So I need to execute a validation function at the set endDate
With this, I have to check every second if theres an enddate reached which looks like bad for performance for this usecase
Are you looking for a queues service? Check https://developers.cloudflare.com/queues/configuration/batching-retries/#delay-on-send
Cloudflare Docs
Batching, Retries and Delays · Cloudflare Queues
When configuring a consumer Worker for a queue, you can also define how messages are batched as they are delivered.
it would also depend on your queue workload, you can't never know that it's going to be executed exactly at that delayed time if your queue is flooded with tasks. Just be aware of how you manage your queues
I guess your best shot is still cron (using nitro). Run a cron every second and execute the tasks that have not expired yet (cache those!).
You can use cron with Tasks. Or am i missing some thing?
export default defineNuxtConfig({
nitro: {
scheduledTasks: {
// Run
cms:update
task every minute
'* * * * *': ['cms:update']
}
}
})U missed the:
part, but no worries 😄 Looks like its the best option to go with
I think what people are trying to portray here is that, in JavaScript, it doesn’t know what date or time it is beyond execution time, unless it’s asked to check usually through setInterval or setTimeout. Once it’s checked, it needs to either keep checking (usually every second) until the date/time specified is reached, or run after X seconds between now and the desired date/time. Whichever you choose is entirely up to you, and what’s suitable for your use case.
There isn’t really a best practice at play here. Just remember that if your scheduler runs any demanding tasks, it will hog resources.
I have chosen to use a cronjob
Im using supabase, and they have Edge Functions that I can run every minute, so thats what I went for