K
K
Kiryushka Tsisar2018-08-28 18:50:28
Node.js
Kiryushka Tsisar, 2018-08-28 18:50:28

NodeJs how to make a script run automatically on time?

Good day.
- There is a nodejs server, I run the server.js input file on the server through forever start
- There is a script that parses information from the donor site
I wanted to make the parsing happen every half an hour, I used the 'node-schedule' package
But I realized that I did it wrong by putting the code in server.js i.e. each user who came to the site ran the parsing script.
The question is - how to do it right?
Do I understand correctly that you need to run separate processes with parsing and with the site. If possible, please share an example.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton fon Faust, 2018-08-29
@bubandos

Feng shui: set up cron and run node parser.js
If there is nothing to do: setInterval(function(){/*parse*/}, 30*60*1000)

I
Ilya Gerasimov, 2018-08-30
@Omashu

For example like this:

function parser() {
  // request get
  return Promise.delay(500).then(() => ({ results: [] }));
}

function* sequence() {
  while (true) {
    yield parser();
    // other throwns
  }
}

(async () => {
  for (let parserPromise of sequence()) {
    const parserResults = await parserPromise;
    await Promise.delay(30 * 60 * 1000);
  }
})();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question