Search code examples
rubypuma

Puma main process on exit callback


How to execute ruby code just before Puma main process shuts down? Now I am gracefully shutting down Puma from the action with this:

Process.kill("INT", Process.ppid)

and when all workers are down I need to execute ruby code.

at_exit do
  puts 'bye...'
end

Will be executed after each process exit and I need callback after all workers are exited.


Solution

  • Just a small hacky way to do it...

    Add a global constant to your config.ru (or setup) file:

    ROOT_PROCESS_ID = Process.pid
    

    Than you can do something like:

    at_exit do
      if(Process.pid == ROOT_PROCESS_ID)
        puts 'bye...'
      end
    end
    

    This isn't Puma specific, so it should work even when you change servers (if you ever switch).