user2487076
user2487076

Reputation: 83

nodejs - run a function at a specific time

I'm building a website that some users will enter and after a specific amount of time an algorithm has to run in order to take the input of the users that is stored in the database and create some results for them storing the results also in the database. The problem is that in nodejs i cant figure out where and how should i implement this algorithm in order to run after a specific amount of time and only once(every few minutes or seconds). The app is builded in nodejs-expressjs. For example lets say that i start the application and after 3 minutes the algorithm should run and take some data from the database and after the algorithm has created some output stores it in database again. What are the typical solutions for that (at least one is enough). thank you!

Upvotes: 2

Views: 3175

Answers (3)

Duncan
Duncan

Reputation: 527

From your question it's not clear whether you want to run the algorithm on the web server (perhaps processing input from multiple users) or on the client (processing the input from a particular user).

If the former, then use setTimeout(), or something similar, in your main javascript file that creates the web server listener. Your server can then be handling inputs from users (via the app listener) and in parallel running algorithms that look at the database.

If the latter, then use setTimeout(), or something similar, in the javascript code that is being loaded into the user's browser.

You may actually need some combination of the above: code running on the server to periodically do some processing on a central database, and code running in each user's browser to periodically refresh the user's display with new data pulled down from the server.

You might also want to implement a websocket and json rpc interface between the client and the server. Then, rather than having the client "poll" the server for the results of your algorithm, you can have the client listen for events arriving on the websocket.

Hope that helps!

Upvotes: 0

TamirNahum
TamirNahum

Reputation: 484

If I understand you correctly - I would just send the data to the client-side while rendering the page and store it into some hidden tag (like input type="hidden"). Then I would run a script on the server-side with setTimeout to display the data to the client.

Upvotes: 0

Serhii Kuts
Serhii Kuts

Reputation: 449

Let say you have a user request that saves url to crawl and get listed products

So one of the simplest ways would be to: On user requests create in DB "tasks" table

userId | urlToCrawl | dateAdded | isProcessing | ....

Then in node main site you have some setInterval(findAndProcessNewTasks, 60000) so it will get all tasks that are not currently in work (where isProcessing is false) every 1 min or whatever interval you need

findAndProcessNewTasks will query db and run your algorithm for every record that is not processed yet also it will set isProcessing to true eventually once algorithm is finished it will remove the record from tasks (or mark some another field like "finished" as true)

Depending on load and number of tasks it may make sense to process your algorithm in another node app

Typically you would have a message bus (Kafka, rabbitmq etc.) with main app just sending events and worker node.js apps doing actual job and inserting products into db

this would make main app lightweight and allow scaling worker apps

Upvotes: 1

Related Questions