Search code examples
javascriptnode.jslogicserver-side

Run a function every weekend at midnight?


so I'm trying to run a function every weekend, Saturday, at 12 am. I was searching for an answer over here, and I found some people suggesting the use of timeout, but it's not really what I want because I don't think it solves the problem that I'm having

so when the server starts, I should get a date from the database let's say it was Friday 22/5/2020 (this should happen every weekend)

I want the server to do some requests/computations at Saturday 23/5/2020 midnight

I was thinking of doing something like this:

while(true){

if(current_date==next_week){
//do stuff that i want ...

next_week=next_week+ 1 week

}

//update current_date
} 

is it considered a bad idea to run a while loop on the server in general? or it's an okay idea? if it a bad idea, is there any better ways to solve this problem?


Solution

  • You should use on of the node modules for cron job management, cron is good and nice npm module for such a task, also node-schedule do the same thing, using timeout functions is not the right choice here, long running process should be managed using well written module for that use and utilize the OS system calls and APIs.