2009-09-05 09:01:46 +02:00
|
|
|
require 'thread'
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
module ActionController
|
|
|
|
class Reloader
|
2009-09-05 09:01:46 +02:00
|
|
|
@@default_lock = Mutex.new
|
|
|
|
cattr_accessor :default_lock
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
class BodyWrapper
|
2009-09-05 09:01:46 +02:00
|
|
|
def initialize(body, lock)
|
2009-08-04 17:16:03 +02:00
|
|
|
@body = body
|
2009-09-05 09:01:46 +02:00
|
|
|
@lock = lock
|
2009-08-04 17:16:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@body.close if @body.respond_to?(:close)
|
|
|
|
ensure
|
|
|
|
Dispatcher.cleanup_application
|
2009-09-05 09:01:46 +02:00
|
|
|
@lock.unlock
|
2009-08-04 17:16:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(*args, &block)
|
|
|
|
@body.send(*args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(symbol, include_private = false)
|
|
|
|
symbol == :close || @body.respond_to?(symbol, include_private)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-05 09:01:46 +02:00
|
|
|
def self.run(lock = @@default_lock)
|
|
|
|
lock.lock
|
|
|
|
begin
|
|
|
|
Dispatcher.reload_application
|
|
|
|
status, headers, body = yield
|
|
|
|
# We do not want to call 'cleanup_application' in an ensure block
|
|
|
|
# because the returned Rack response body may lazily generate its data. This
|
|
|
|
# is for example the case if one calls
|
|
|
|
#
|
|
|
|
# render :text => lambda { ... code here which refers to application models ... }
|
|
|
|
#
|
|
|
|
# in an ActionController.
|
|
|
|
#
|
|
|
|
# Instead, we will want to cleanup the application code after the request is
|
|
|
|
# completely finished. So we wrap the body in a BodyWrapper class so that
|
|
|
|
# when the Rack handler calls #close during the end of the request, we get to
|
|
|
|
# run our cleanup code.
|
|
|
|
[status, headers, BodyWrapper.new(body, lock)]
|
|
|
|
rescue Exception
|
|
|
|
lock.unlock
|
|
|
|
raise
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|