0

I'm doing a school project about CMS System to help operate my school website. It use 3 database:

  • MongoDB (Head database, store all information)
  • Redis (Store the menu of website)
  • Elasticsearch (Store posts)

Currently when I insert/edit/delete data, I also insert/edit/delete to a related database. But my mentor want me to write a function that let's system auto sync data between those 3 database at a specific time (user can choose when).

My server uses Node JS to operate, this requirement is new for me, never heard about it before. My new approach is:

  • Add 1 flag to database field
  • Select all rows which contain flag == true.
  • Sync data

But I don't know how to auto run above function at a specific time. I hope you guys can help me to optimize my new flow and solve the sync problem.

Thank you all !!!

Edit 1: Change tittle.

Edit 2: I found a solution from this topic: Running a function everyday midnight, is there anyway to re-run this function if error occur when sync data. Something like this:

function syncDataAtMidNight(){
 var now = new Date();
    var night = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate() + 1, // the next day, ...
        0, 0, 0 // ...at 00:00:00 hours
    );
    var msToMidnight = night.getTime() - now.getTime();

    setTimeout(function() {
    // is this a correct way to loop, in case of error when sync ???
        myFunctionSync(err, resp) {
          if(err){
            myFunctionSync();
          } else {
             changeFlagToFalse();
             syncDataAtMidNight();
          }
        }              
    }, msToMidnight);}
Huy
  • 179
  • 1
  • 1
  • 9
  • Within your node.js program, you can use `setTimeout()` to cause something to run any number of ms from now. Get the current time and calculate how many ms until the time you want your function to run and then issue the `setTimeout()` call to schedule it. If this question is really just about running a function at some future time, please change the question title and main body to be about that. – jfriend00 May 27 '17 at 05:56
  • @jfriend00 thank you, I edited the tittle and add my new solution for this. Could you help me to check it again? – Huy May 27 '17 at 07:37
  • Ummm, the way questions work here, you can clarify your original intent, but you should not change your question into a new question. It appears you are now asking something new about running the function over again if there's an error. That's entirely different than what you first asked. People will avoid spending time on your questions if you keep changing the crux of what you're asking. FYI, your syntax for your first call to `myFunctionSync()` is not legal Javascript for calling a function. I'm not sure what you're trying to do there since you do don't show the `myFunctionSync()` code. – jfriend00 May 27 '17 at 07:48
  • I found a solution for my problem. I rarely post question on stack so I don't know about what you say, thanks for your comment, I will take a note. Anyway, thank you. – Huy May 27 '17 at 09:23
  • Which is why I'm trying to teach you the rules of the site so next time you will better know how to take advantage of the site. – jfriend00 May 27 '17 at 09:42

1 Answers1

0

You should consider to use cron, he have the significant advantage to be at system level, he can get the code error return and mostly launch/relaunch node if necessary.

lp177
  • 83
  • 1
  • 6