Shutdown hook for Rails

人盡茶涼 提交于 2019-12-01 03:09:43

Probably should just use the Ruby exit handler, which is a Kernel method:

$ irb
>> at_exit do
?>   puts 'bye...'
>> end
=> #<Proc:0xb79a87e4@(irb):1>
>> exit
bye...
$ 

Within the context of a Rails Application, the best place to put such a file is in config/initializers. In my app, I needed to Flush the Redis/Sidekiq queue whenever the development or test environments shut down. This works perfectly.

config/initializers/at_exit.rb

at_exit do
  begin
    puts 'Flushing Redis...'
    Redis.new.flushall
  rescue => e
    puts "There was an #{e.to_s} while flushing redis..."
  ensure
    puts 'Done Flushing Redis!'
  end
end unless Rails.env.production?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!