0

I try to develop a little browser game based on NodeJS and Angular 4. I have an API server running on NodeJS which is connected to a MongoDB and a second server running Angular 4.

I want to execute recurring standard functions (like every 15 minutes) in the background.

Do I need a third server which runs that functions? Or can I run that functions independently on my API server - no matter which route is open?

2
  • you mean calling the certain api every 15 minutes Commented Apr 13, 2017 at 15:39
  • I mean, getting data from the DB and do something with it and save the update; e.g.: check all 15 mins if a player has a mine, then increase his/her resources by 1 Commented Apr 13, 2017 at 15:46

2 Answers 2

1

You might want to have a look to this library node-cron. You can set it up to work with your services. You will need to initialise the job right after your sever is initialised. An example:

var CronJob = require('cron').CronJob;
var job = new CronJob({
  cronTime: '00 30 11 * * 1-5',
  onTick: function() {
    /*
     * Runs every weekday (Monday through Friday)
     * at 11:30:00 AM. It does not run on Saturday
     * or Sunday.
     */
  },
  start: false,
  timeZone: 'America/Los_Angeles'
});
job.start();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping out. It help that you told me where to place the code. I go with setInterval, but CronJob looks like a good tool too.
0

You can use setTimeout() and setInterval() in Node just like in the browser:

setInterval(() => {
  // this runs every 15 minutes
}, 15 * 60 * 1000);

1 Comment

sure, thanks for your comment! but I worry about the function when the API is on "/whatever". Is it still working?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.