Friday, October 9, 2009

Capistrano task to confirm stop of JBoss


This is very common in the domain of automatic deployment to restart the application server and obviously the application server takes sometimes to stop. If we need to make sure the application server is completely stopped before starting it again we need to write some shell script to check that. Capistrano users can use following script to ensure that the application server (in this case JBoss) is completely stopped. The following script will execute the stop script and then check if the server is still running. If it is running then it will sleep for 6 seconds and then check it again. It will do it for 15 times and even then if it is running it will issue kill -9 to kill the process. You can change any of these values. Note that I have assumed the server is running at port 8080.


task :ensure_jboss_stopped do
  set :use_sudo, true
  set :user, prompt("User to run stop.sh ")
  set :is_jboss_running, true
  counter = 0


  run "/opt/jboss-4.2.3.GA/bin/stop.sh"


  while is_jboss_running
    counter = counter + 1
    run "netstat -an | grep -i listen | grep 8080|wc -l" do |ch, stream, data| 
      if data.to_i > 0
        set :is_jboss_running, true
      else
        set :is_jboss_running, false
      end
    end
    if is_jboss_running and counter > 15      
      system "echo JBoss is still running, finding the process id"
      run "ps -eaf |grep frengine |grep -v grep |awk '{print $2}'|xargs -I{} echo kill -9 {};"
      system "echo Now KILLING JBOSS"
      run "ps -eaf |grep frengine |grep -v grep |awk '{print $2}'|xargs -I{} kill -9 {} ;"
      sleep 6
    elsif is_jboss_running
      system "echo JBoss is still running, waiting for 6 seconds"
      sleep 6
    end
  end
  system "echo JBoss stopped successfully"
end 


No comments: