N
Nuxt4mo ago
Rejox

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
isakwang
isakwang4mo ago
Set up a cron job with your provider. Where are you hosting?
Single
Single4mo ago
You can also setup a cronjob in your nuxt application using a cron library. The provider doesn't matter in that case.
Orbis
Orbis4mo ago
I think you can use nitro Tasks. They ca be scheduled
Single
Single4mo ago
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
Rejox
RejoxOP4mo ago
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
IsraelOrtuno
IsraelOrtuno4mo ago
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.
IsraelOrtuno
IsraelOrtuno4mo ago
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
Single
Single4mo ago
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!).
Orbis
Orbis3mo ago
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'] } } })
Rejox
RejoxOP3mo ago
U missed the:
I know I can use like a scheduler to run every second, but I dont think thats the best practice, or is it?
I know I can use like a scheduler to run every second, but I dont think thats the best practice, or is it?
part, but no worries 😄 Looks like its the best option to go with
Cue
Cue3mo ago
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.
Rejox
RejoxOP3mo ago
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
Want results from more Discord servers?
Add your server