i want to run a function on a future date/time - once | nodejs - backend task?

Viewed 192

i have a event web app on react.js | when logged-in user set an event on event page - assume four days later from today and there is another dropdown input filed of setReminder with values 4 hours ago / 3 hours ago and so on and on submit i'm calling or hiting an

route/api/endpoint/postRequest post->api->userSchema->mongoDB->req.body -

json { setReminderTime: currentDateTime - req.body.data.setReminderValue } etc. etc. and saving other more data and so now i want to my code to run a function in there i write some code i want that code to exicute on that event date/time - {minus} that reminder date/time (4 hours or 3 or 2 hours ago ) so in reminder i send a notification or a smg or want to do other things more and i don't want to hit my databse each second and i also don't want to do use setTimeout staf beacuse my server refresh again again due to puch->updates

1 Answers

I'm not sure if i had understand your problem well, but you can try to use a reminder on the server (backend side) and you have to run the reminder every hour by using cron service like that :

const schedulerFactory = function() {
 return {
  start: function() {
  // this action will be executed every day at 12:00 u can choose what you wan't
  new CronJob('00 00 12 * * *', function() {

     //CronJob('0 */1 * * * *') // this for execute each one minute 
     console.log('Running Send Notifications Worker for ' + moment().format());
       notificationsWorker.run(); // this should be the function to call and it will search for all instance where some condition are true and than execute them
    }, null, true, '');
  },
 };
};

on the database you can stock some informations to know if you have to execute this action or no (for example last connection or something like that to know when to send notification)

Good luck Bro.

Related