I want to add some admin utilities to a little Web app, such as “Backup Database”. The user will click on a button and the HTTP response will return immediately, although the potentially long-running process has been started in the background.
In Java this would probably be implemented by spawning an independent thread, in Scala by using an Actor. But what’s an appropriate idiom in node.js? (code snippet appreciated)
I’m now re-reading the docs, this really does seem a node 101 question but that’s pretty much where I am on this…anyhow, to clarify this is the basic scenario :
function onRequest(request, response) {
doSomething();
response.writeHead(202, headers);
response.end("doing something");
}
function doSomething(){
// long-running operation
}
I want the response to return immediately, leaving doSomething() running in the background.
Ok, given the single-thread model of node that doesn’t seem possible without spawning another OS-level ChildProcess. My misunderstanding.
In my code what I need for backup is mostly I/O based, so node should handle that in a nice async fashion. What I think I’ll do is shift the doSomething to after the response.end, see how that behaves.
I don’t see the problem. All you need to do is have
doSomething()start an asynchronous operation. It’ll return immediately, youronRequestwill write the response back, and the client will get their “OK, I started” message.doSomethingwon’t just sit there until the database connection is established, or wait while you tell it to back up. It’ll return right away, having registered a callback that will run later. Behind the scenes, your database library is probably creating some threads for you, to make the async work the way it should, but your code doesn’t need to worry about it; you just return right away, send the response to the client right away, and the asynchronous code keeps running asynchronously.(It’s actually more work to make this run synchronously — you would have to pass your
responseobject intodoSomething, and havedoSomethingdo theresponse.endcall inside the innermost callback, after the backup is done. Of course, that’s not what you want to do here; you want to return immediately, which is exactly what your code will do.)