aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/reloader.rb
blob: 579b5d8a02ec4a5f48ae685fbf20d1c7123f7796 (plain) (blame)
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
55
56
57
58
59
60
61
62
63
64
65
module ActionDispatch
  # ActionDispatch::Reloader provides to_prepare and to_cleanup callbacks.
  # These are analogs of ActionDispatch::Callback's before and after
  # callbacks, with the difference that to_cleanup is not called until the
  # request is fully complete -- that is, after #close has been called on
  # the request body. This is important for streaming responses such as the
  # following:
  #
  #     self.response_body = lambda { |response, output|
  #       # code here which refers to application models
  #     }
  #
  # Cleanup callbacks will not be called until after the response_body lambda
  # is evaluated, ensuring that it can refer to application models and other
  # classes before they are unloaded.
  #
  # By default, ActionDispatch::Reloader is included in the middleware stack
  # only in the development environment.
  #
  class Reloader
    include ActiveSupport::Callbacks

    define_callbacks :prepare, :scope => :name
    define_callbacks :cleanup, :scope => :name

    # Add a preparation callback. Preparation callbacks are run before each
    # request.
    def self.to_prepare(*args, &block)
      set_callback(:prepare, *args, &block)
    end

    # Add a cleanup callback. Cleanup callbacks are run after each request is
    # complete (after #close is called on the response body).
    def self.to_cleanup(*args, &block)
      set_callback(:cleanup, *args, &block)
    end

    def self.prepare!
      new(nil).send(:_run_prepare_callbacks)
    end

    def self.cleanup!
      new(nil).send(:_run_cleanup_callbacks)
    end

    def initialize(app)
      @app = app
    end

    module CleanupOnClose
      def close
        super if defined?(super)
      ensure
        ActionDispatch::Reloader.cleanup!
      end
    end

    def call(env)
      _run_prepare_callbacks
      response = @app.call(env)
      response[2].extend(CleanupOnClose)
      response
    end
  end
end