title: How do I run, test, and terminate an HTTP server using Jenkins?
body: <p>I'm working on a team that is building a RESTful HTTP service. We're having trouble with setting up a Jenkins CI job which will build the service, run it in the background, execute some tests, and then terminate the servers.</p>
  
  <h3>Specifics</h3>
  
  <ul>
  <li>The server is built in Node.js using the hapi framework and has some unit tests written in mocha.</li>
  <li>The tests are written in Java using Maven. (Why not node.js-based tests? Because our testing dept. has invested time in creating a Java-based REST-testing framework.)</li>
  <li>The build should fail if the node-based unit tests fail or if the java tests fail.</li>
  <li>Our Jenkins box is run by a support team elsewhere in the company; our builds execute on a Linux slave.</li>
  </ul>
  
  <h3>Current Attempt</h3>
  
  <p>We've got something that kind-of works right now, but it's unreliable. We use 3 build steps:</p>
  
  <p>The first build step is an <code>Execute Shell</code> step with the following commands:</p>
  
  <pre><code>npm install
  npm test
  node server.js ./test-config.json &amp;
  </code></pre>
  
  <p>Second we do a <code>Invoke Maven 3</code> step that points to the test <code>pom.xml</code>.</p>
  
  <p>And third we run <code>Invoke Standalone Sonar Analysis</code> to do static code analysis.</p>
  
  <p>This mostly works, but we depend on Jenkins' <a href="https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller">ProcessTreeKiller</a> to stop the services once the job completes. We always get the warnings stating: <code>Process leaked file descriptors. See
  http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+buildfor
  more information</code></p>
  
  <p>Unfortunately, we've had cases where the service is terminated too soon (before the tests complete) or where the service doesn't get terminated at all (causing subsequent builds to fail because the port is already in use).</p>
  
  <p>So we need something more reliable.</p>
  
  <h3>Failed Attempt</h3>
  
  <p>We tried setting up a single shell script which handled starting the service, running maven, killing the service, then outputting an exit code. But this didn't work out because the <code>mvn</code> command wasn't available on the command-line. Our Jenkins has multiple maven versions available (and jdks too) and I don't know where they live on the slaves or how to get at them without using the <code>Invoke Maven 3</code> build step.</p>
  
  <h3>Ideas</h3>
  
  <p>We've toyed around with some ideas to solve this problem, but are hoping to get some guidance from others that may have solved similar problems with Jenkins.</p>
  
  <ol>
  <li>Have the service self-terminate after some period of time. Problem is figuring out how long to let them run.</li>
  <li>Add a build step to kill the services after we're done. Problem is that if the maven execution fails, subsequent steps won't run. (And if we tell maven to ignore test failures, then the build doesn't show as broken if they fail.)</li>
  <li>Try killing any existing service process as the first and last steps of the build. Problem is that other teams also use these Jenkins slaves so we need to make sure that the service is terminated when we're done with our build.</li>
  <li>Start and stop the node.js services via Maven doing something like <a href="http://avianey.blogspot.com/2012/12/maven-it-case-background-process.html">this blog suggests</a>. Problem is that we don't know if Jenkins will identify the spawned background task as a "leaked file descriptor" and kill it before we're done testing.</li>
  </ol>
  
  <p>It would be nice if Jenkins had a "Post-build action" that let you run a clean-up script. Or if it had a "Execute background process" build step which would kill the background items at the end of the build. But I can't find anything like that.</p>
  
  <p>Has anyone managed to get Jenkins to do anything remotely like this?</p>