0

I have a node.js server accepting requests from a web page. Upon receiving a "START" request, I want to be able to spawn a timer that executes a task in the background every n seconds endlessly. Is this doable? and what would be a good example? A simple psudo-code is as below:

app.get("myindependenttasks/starttask/:userid"){
   //start a simple timer to handle to userid
   //continue to run the timers endlessly while 
   //while this call returns a "Task running" status
   //to the user.
   //Other users should be able to run their own tasks.
}

Are there any downsides if the number of user requests are around 1000 running tasks.

Ram Iyer
  • 1,404
  • 2
  • 20
  • 27
  • 1
    timers don't "execute in the background" they are just deferred until the event round when it is time to execute, at which point it blocks. Can you describe what type of work needs to be done? – Chad Mar 01 '13 at 20:16
  • The task is to call another web-service (REST calls) in background to read data and write updates. – Ram Iyer Mar 01 '13 at 20:50
  • That sounds asynchronous, just do the calls; as long as they are async you should be fine. Show me some codes. – Chad Mar 01 '13 at 21:34
  • I ended up using simple async timeout that are called every n mins. I needed a random wait every time the timer is run so had to use setTimeout instead of setInterval. – Ram Iyer Mar 04 '13 at 17:26

1 Answers1

2

You can use setInterval for this (http://nodejs.org/api/timers.html), but I'd be hesitant to do this for 1000s of these.

Instead, you should run one setInterval, and run the tasks for all connected users in this task.

So, when a user hits "myindependenttasks/starttask/:userid", it gets added to the list of users to process, and in your periodic task you go through that list one by one.

Pascal Belloncle
  • 11,184
  • 3
  • 56
  • 56