1

I need to get a stock price from an API and save it to the database every 15min whats the best way to do this?

My first thought was to use node-fetch with a setInterval but I'm not sure this is best practice. Or maybe a cron job with node-cron?

1
  • setInterval() and the request-promise library for making the http request is one option. Commented Nov 18, 2019 at 4:03

2 Answers 2

3

node-cron can work, but you will lose the scheduled tasks if the server goes down. I have used schedulers like agenda extensively in the past and benefits from having a persistence layer.

For example:

agenda.define('getStockPrice', async job => { const data = await stockeService.getPrice(); ... }); agenda.every('15 minutes', 'getStockPrice'); 

Checkout the docs for more info.

Sign up to request clarification or add additional context in comments.

9 Comments

Why use an external package in place of a simple 15 minute timer?
@jfriend00 "will lose the scheduled tasks if the server goes down."
So, you're saying that agenda somehow automatically runs when your server restarts? You could also just have a setInterval() in the startup code when your server restarts too. I'm just trying to understand what's the advantage of using an external package for such a simple problem. This isn't dozens of scheduled tasks at all various times. It's just one recurring polling operation.
I guess if you wanted the job to run every 6 hours but the server restarted every 5 hours then the job would never run. Or if want a job to run every Monday at 9am GMT then agenda is the way to go
if the server was to restart after 14min would the task never run, leaving the data out of sync for 30min?
|
0

Use cron job https://www.npmjs.com/package/node-cron

It's a best way to run the api or any function every time which we want to.

The node-cron module is tiny task scheduler in pure JavaScript for node.js based on GNU crontab. This module allows you to schedule task in node.js using full crontab syntax.

1 Comment

But why is it better than using pure setInterval()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.