While I'm building my project using Jenkins, I need to start a nodejs server process to host some files, if I were to start this process like the following, the build would hang infinitely
<target name="staticserver" description="Starts nodejs static server">
<exec executable="node">
<arg value="${env.WORKSPACE}staticserver.js"/>
</exec>
</target>
so I switched to the following, and the build would run fine
<target name="staticserver" description="Starts nodejs static server">
<exec executable="cmd.exe">
<arg value="/c"/>
<arg value="start"/>
<arg value="node"/>
<arg value="${env.WORKSPACE}staticserver.js"/>
</exec>
</target>
however when the Jenkins build finishes, the node process is left alive.
I searched around, but it seems like everyone's problem with killing child processes is that Jenkins kills all of them...
How should I start the node process so that Jenkins can properly kill it after the build is finished? Am I approaching this problem from the wrong angle and need to look at it from a different direction?
Thanks.
I found two ways to solve the problem the first is to add a target that kills all node.exe, there won't be a problem if no other node instances needs to be run on the same machine
<target name="stopnode" description="Stops all instances of node">
<exec executable="taskkill">
<arg value="/IM"/>
<arg value="node.exe"/>
</exec>
</target>
the second is to add a timer that gets reset when the server is accessed, and to shutdown the server when it hasn't been accessed in a while.
global.timer = {
count: 5,
reset: function() {
this.count = 5;
}
};
function countdown() {
global.timer.count = global.timer.count - 1;
//console.log(global.timer.count);
if (global.timer.count <= 0) {
clearInterval(cd);
process.exit(0);
}
}
var cd = setInterval(function () { countdown() }, 1000);
a jsfiddle just for fun: http://jsfiddle.net/jeJkm/