2

So here is a thing, I have some lawyer app, and he needs to set reminder for some case, that reminder could be tomorrow, next week, next month idk. I was thinking about using node-cron, thing is i dont know what will happen when my app restarts, I assume it will crash all my reminders, and the second thing is how much will I load my server if i got to much reminders, OR do u have some other advice for this solution, maybe something with setInterval and run it every hour or something like that?

Mario Rozic
  • 254
  • 5
  • 12

1 Answers1

2

node-cron is used to run code as you would with normal cron, but in JS :) So even if your code is crushed and restarted it will look at time set by you to execute some code and will not crush reminders.

For example if you run some code with cron condition 0 0 1 * *, which means every month on 1st date at 00:00.

That means that even if your app is crushed at 15th of month and restarted at 20th of month, it will run your job at 1st of next month at 00:00.

Between for automatically restarting your app you can use forever or pm2 packages from NPM

Now about this question.

how much will I load my server if i got to much reminders

I don't think it will load your server. node-cron internally uses setInterval, which is not CPU consuming, so go and run your crons without fear.

Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45
  • Hmm ty for response, I need to run some socket.io code, I mean i need to emit some data, are you sure i will know all sockets connected to my app and broadcast message to all of them, this solution with database could be right – Mario Rozic Aug 23 '17 at 09:54
  • You will run `socket.io` within crons? If you want to know whether you emit data or now you should keep logging your program for sure. I would suggest NoSQL DB or file logging with `bunyan` https://www.npmjs.com/package/bunyan. Anyway your code should not be supposed to crush accidentally. – Aren Hovsepyan Aug 23 '17 at 10:00
  • Yea i need to emit some "reminder" for my client, well my app doesnt crash but u know i cant rely on that what if power went out or idk app restarts for some random reason or its some OS bug idk ..so i need to ensure my reminders will work no matter what :) ty for those links i will try them for sure – Mario Rozic Aug 23 '17 at 10:14
  • What I want to find out is say you have a cron job that runs every 2 hours, and let say when it is 30 minute for the job to run, the server restarted. I want to know will the cron job run in 30 minute time or it will start from afresh with the 2 hours? – Wisdom May 04 '21 at 09:01
  • Since the `node-cron` works inside `node` process, it will be stopped with the process. Therefore, it will run in a 2 hours from fresh state. You can implement more sophisticated version with `redis` or some kind of external state – Aren Hovsepyan May 04 '21 at 14:59
  • I have another a question concerning this and its just to confirm, if an app crashes and restarts, since this is running in a node process, there is no chance of recreating duplicate jobs and running both old and new jobs concurrently, right? – lulliezy Nov 26 '21 at 16:19
  • Bascially `node-cron` uses `setTimeout` internally, so when app crashed all jobs are being crashed as well, thus restarting main node script can't duplicate crons. It's not same `crontab` that operates in OS level. – Aren Hovsepyan Nov 26 '21 at 23:45
  • And how can I access the job instance after node server restarts. I need that instance to stop the scheduler. Is there any workaround for this problem? – Shiv Apr 13 '22 at 11:34