File: reloader.rb

package info (click to toggle)
redmine 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 27,268 kB
  • ctags: 23,995
  • sloc: ruby: 177,441; sh: 506; perl: 232; sql: 96; makefile: 31
file content (54 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'thread'

module ActionController
  class Reloader
    @@default_lock = Mutex.new
    cattr_accessor :default_lock

    class BodyWrapper
      def initialize(body, lock)
        @body = body
        @lock = lock
      end

      def close
        @body.close if @body.respond_to?(:close)
      ensure
        Dispatcher.cleanup_application
        @lock.unlock
      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

    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
    end
  end
end