# lib/log_before_timeout.rb class LogBeforeTimeout < Struct.new(:app) def call(env) thr = Thread.new(Thread.current) do |request_thread| sleep(14) # set this to Unicorn timeout - 1 unless Thread.current[:done] # instead of Rails.logger you should be able to use env["rack.logger"], # but I never got this to work (was always nil in my Rails app) Rails.logger.warn "about to die from SIGKILL. thread:" Rails.logger.warn "#{request_thread.inspect}" Rails.logger.warn request_thread.backtrace.join("\n").prepend("\n") end end app.call(env) ensure thr[:done] = true thr.run end end