cron like behavior using server actions
Unanswered
elfensky posted this in #help-forum
elfenskyOP
I would like to offer a restapi in my nextjs website. However, the up-2-date info is coming from outside the application.
I could make a separate node app that fetches new data every X seconds (using node-cron, bree or similar) and updates the database, but ideally I would keep this functionality in my nextjs project.
As I understand it, I can't really use node-cron in nextjs, so here's what I came up with (as I understand it's similar to how Drupal's (PHP) cron works):
Would it be a good idea to, on each api request check the "last_updated" time, and if more than 15 seconds have elapsed, trigger a server action to fetch and store new data?
I could make a separate node app that fetches new data every X seconds (using node-cron, bree or similar) and updates the database, but ideally I would keep this functionality in my nextjs project.
As I understand it, I can't really use node-cron in nextjs, so here's what I came up with (as I understand it's similar to how Drupal's (PHP) cron works):
Would it be a good idea to, on each api request check the "last_updated" time, and if more than 15 seconds have elapsed, trigger a server action to fetch and store new data?
1 Reply
elfenskyOP
Additionally, how could I make sure that server action is atomic? As in, if 2 requests come in at the same time, how do I make sure there's only 1 instance of the server action running? So I don't update the data twice.