Shutdown WEBrick server used in installed app flow

Starting and stopping WEBrick only controls whether the event loop is
running, it does not start and stop listening on TCP sockets.
Our WEBrick server is starting to listen when it is initialised,
so we should ensure that we shut it down when we are done with it.

Note that shutdown is idempotent.
This commit is contained in:
Ben Barnard 2013-11-20 00:22:28 +01:00
parent f06f1e33fa
commit c428db0f3d
1 changed files with 10 additions and 6 deletions

View File

@ -92,9 +92,10 @@ module Google
:Logger => WEBrick::Log.new(STDOUT, 0), :Logger => WEBrick::Log.new(STDOUT, 0),
:AccessLog => [] :AccessLog => []
) )
trap("INT") { server.shutdown } begin
trap("INT") { server.shutdown }
server.mount_proc '/' do |req, res|
server.mount_proc '/' do |req, res|
auth.code = req.query['code'] auth.code = req.query['code']
if auth.code if auth.code
auth.fetch_access_token! auth.fetch_access_token!
@ -102,10 +103,13 @@ module Google
res.status = WEBrick::HTTPStatus::RC_ACCEPTED res.status = WEBrick::HTTPStatus::RC_ACCEPTED
res.body = RESPONSE_BODY res.body = RESPONSE_BODY
server.stop server.stop
end end
Launchy.open(auth.authorization_uri.to_s) Launchy.open(auth.authorization_uri.to_s)
server.start server.start
ensure
server.shutdown
end
if @authorization.access_token if @authorization.access_token
if storage.respond_to?(:write_credentials) if storage.respond_to?(:write_credentials)
storage.write_credentials(@authorization) storage.write_credentials(@authorization)