Home > database >  Executing a function on a specific date
Executing a function on a specific date

Time:11-23

I've seen a few solutions to this, but none feel quite right.

I am building a full stack app with React and Node.js, and using Supabase as the database. It's for hobby purposes, and I can switch up just about whatever to accommodate this, so I'm open to any suggestions.

Users will provide a date, and the app needs to execute a function on that date. Most of what I have come across suggests using setInterval. I could be wrong, but constantly running setIntervals doesn't seem like the most efficient or scalable way to do things.

Are there any suggestions for better managing this? On the server, database (Postgres), or even the client? Any pointers to docs that might help out are appreciated. Thanks for your time.

CodePudding user response:

The best and simple way for periodic task is using crontab

You can edit using crontab -e

You can set cron expression.

 # ┌────────────── second 
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

This can help to validate

This is node lib to setup crontab

var cron = require('node-cron');

cron.schedule('* * * January,September Sunday', () => {
  console.log('running on Sundays of January and September');
});

CodePudding user response:

you can use schedulers for this task.

choose one that suits your need.i prefer using bull.it is lightweight with additional features.

https://blog.logrocket.com/comparing-best-node-js-schedulers/

  • Related