Search code examples
rubychef-infrachef-recipechef-solo

Wait until service shutdown complete before moving to next block in chef recipe


I'm running chef recipe block to stop particular service running on port 9991. here is my chef recipe block.

execute "run script" do
    command "./stop.sh"
    cwd "#{node[MY_ENVIRONMENT]['home']}"
    user "centos"
    action :run
    only_if { ::File.exist?("#{node[MY_ENVIRONMENT]['home']}/stop.sh") }
end

According to the my application it might take some time to shutdown the application by breaking the TCP connections which has been established. I cannot tell how long it will take.

What I need is to wait chef-client to run next block until my service get stop completely. I can ensure the service is down by executing below command and if its get echo $?; -ne result from bash.

ss -a | grep -E '9991'  # reason for this is my application can have close_wait, fin_wait status within the shutdown process

Can anyone help me on this?


Solution

  • It would be best to put that in the stop.sh itself probably. Chef will wait until that script ends, so whatever it does is up to you. Otherwise you can use a ruby_block resource, but you'll need a way to get the PID of your program:

    ruby_block 'wait until down' do
      block do
        begin
          loop do
            Process.kill(0, yourpid)
            sleep(1)
           end
        rescue Errno::ESRCH
          # This space left intentionally blank, when the PID is gone, we're done.
        end
      end
    end