views:

142

answers:

1

Hi all, I'd like to have some cleanup code run when Rails is shutting down - is that possible? My situation is that I have a few threads in the background (I'm using jruby and calling into java) that live for the life of the process and I need to let them know to shut themselves down

Thanks!

+3  A: 

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...
$
DigitalRoss
Awesome, thanks!
Stuart
Where would you put this code though?
Ryan Bigg
As long as JRuby itself has a lifecycle corresponding to the underlying application server, then you can put it anywhere it will get run at least once. A good place would be to register the exit handler in whatever piece of Ruby code fires off those background threads in the first place.
DigitalRoss
(If it does get called more than once the exit handlers will run more than once, however, so don't call it 30,000 times. :-)
DigitalRoss
@Radar: My guess would be boot.rb. It's the first file read.
EmFi