Shutdown Hook?

899 views
Skip to first unread message

kitplummer

unread,
Mar 8, 2009, 3:18:55 PM3/8/09
to sinatrarb
Is there a shutdown hook for Sinatra? I'd only seen one mention of it
prior, in this group. It mentioned an 'at_exit do' block...but, that
didn't work.

I'm running Sinatra from a Gumstix, and need to turn off an LED is
Sinatra gets shutdown. I can light the LED in the configure do block
- no prob.

Any help?

Kit

Harry Vangberg

unread,
Mar 8, 2009, 3:25:30 PM3/8/09
to sina...@googlegroups.com
What happens when you create a classic top level block is that Sinatra adds an at_exit-hook which runs the app at exit:

== lib/sinatra/main.rb L44
at_exit do
  raise $! if $!
  Sinatra::Application.run! if Sinatra::Application.run?
end
==

To my knowledge Sinatra doesn't have a shutdown hook, but a solution could be to create a sub class app:

==
require 'sinatra/base'
class MyApp < Sinatra::Default
  configure do
    puts "STARTING!"
  end

  get '/foo' do
    "bar"
  end
end

MyApp.run!

at_exit do
  puts "SHUTTING DOWN!"
end
==

And the actual output:

$ ruby -rubygems my_app.rb
STARTING!
== Sinatra/0.9.1 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.0.0 codename That's What She Said)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
^C>> Stopping ...

== Sinatra has ended his set (crowd applauds)
SHUTTING DOWN!
$

2009/3/8 kitplummer <kitpl...@gmail.com>

candlerb

unread,
Mar 9, 2009, 6:15:24 AM3/9/09
to sinatrarb
> To my knowledge Sinatra doesn't have a shutdown hook, but a solution could
> be to create a sub class app:

I think this is right, but I suggest you use a rackup file rather than
MyApp.run!

This is because you have to do your own command-line parsing to pass
appropriate options to MyApp.run!(...) to be able to select the port,
server and environment (or else hard-code them).

Regards,

Brian.
Reply all
Reply to author
Forward
0 new messages