Class: ActiveSupport::Reloader

Inherits:
ExecutionWrapper show all
Defined in:
activesupport/lib/active_support/reloader.rb

Overview

Active Support Reloader

This class defines several callbacks:

to_prepare -- Run once at application startup, and also from
+to_run+.

to_run -- Run before a work run that is reloading. If
+reload_classes_only_on_change+ is true (the default), the class
unload will have already occurred.

to_complete -- Run after a work run that has reloaded. If
+reload_classes_only_on_change+ is false, the class unload will
have occurred after the work run, but before this callback.

before_class_unload -- Run immediately before the classes are
unloaded.

after_class_unload -- Run immediately after the classes are
unloaded.

Constant Summary

Constants inherited from ExecutionWrapper

ExecutionWrapper::Null

Constants included from Callbacks

Callbacks::CALLBACK_FILTER_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ExecutionWrapper

active?, active_key, #complete, error_reporter, perform, register_hook, #run, to_complete, to_run

Methods included from Callbacks

#run_callbacks

Methods included from Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

#initializeReloader

Returns a new instance of Reloader.



99
100
101
102
# File 'activesupport/lib/active_support/reloader.rb', line 99

def initialize
  super
  @locked = false
end

Class Method Details

.after_class_unload(*args, &block) ⇒ Object

Registers a callback that will run immediately after the classes are unloaded.



44
45
46
# File 'activesupport/lib/active_support/reloader.rb', line 44

def self.after_class_unload(*args, &block)
  set_callback(:class_unload, :after, *args, &block)
end

.before_class_unload(*args, &block) ⇒ Object

Registers a callback that will run immediately before the classes are unloaded.



39
40
41
# File 'activesupport/lib/active_support/reloader.rb', line 39

def self.before_class_unload(*args, &block)
  set_callback(:class_unload, *args, &block)
end

.check!Object

:nodoc:



87
88
89
# File 'activesupport/lib/active_support/reloader.rb', line 87

def self.check! # :nodoc:
  @should_reload ||= check.call
end

.prepare!Object

:nodoc:



95
96
97
# File 'activesupport/lib/active_support/reloader.rb', line 95

def self.prepare! # :nodoc:
  new.run_callbacks(:prepare)
end

.reload!Object

Initiate a manual reload



51
52
53
54
55
56
57
58
59
60
# File 'activesupport/lib/active_support/reloader.rb', line 51

def self.reload!
  executor.wrap do
    new.tap do |instance|
      instance.run!
    ensure
      instance.complete!
    end
  end
  prepare!
end

.reloaded!Object

:nodoc:



91
92
93
# File 'activesupport/lib/active_support/reloader.rb', line 91

def self.reloaded! # :nodoc:
  @should_reload = false
end

.run!(reset: false) ⇒ Object

:nodoc:



62
63
64
65
66
67
68
# File 'activesupport/lib/active_support/reloader.rb', line 62

def self.run!(reset: false) # :nodoc:
  if check!
    super
  else
    Null
  end
end

.to_prepare(*args, &block) ⇒ Object

Registers a callback that will run once at application startup and every time the code is reloaded.



34
35
36
# File 'activesupport/lib/active_support/reloader.rb', line 34

def self.to_prepare(*args, &block)
  set_callback(:prepare, *args, &block)
end

.wrap(**kwargs) ⇒ Object

Run the supplied block as a work unit, reloading code as needed



71
72
73
74
75
76
77
78
79
80
81
82
# File 'activesupport/lib/active_support/reloader.rb', line 71

def self.wrap(**kwargs)
  return yield if active?

  executor.wrap(**kwargs) do
    instance = run!
    begin
      yield
    ensure
      instance.complete!
    end
  end
end

Instance Method Details

#class_unload!(&block) ⇒ Object

:nodoc:



126
127
128
129
# File 'activesupport/lib/active_support/reloader.rb', line 126

def class_unload!(&block) # :nodoc:
  require_unload_lock!
  run_callbacks(:class_unload, &block)
end

#complete!Object

:nodoc:



131
132
133
134
135
136
# File 'activesupport/lib/active_support/reloader.rb', line 131

def complete! # :nodoc:
  super
  self.class.reloaded!
ensure
  release_unload_lock!
end

#release_unload_lock!Object

Release the unload lock if it has been previously obtained



114
115
116
117
118
119
# File 'activesupport/lib/active_support/reloader.rb', line 114

def release_unload_lock!
  if @locked
    @locked = false
    ActiveSupport::Dependencies.interlock.done_unloading
  end
end

#require_unload_lock!Object

Acquire the ActiveSupport::Dependencies::Interlock unload lock, ensuring it will be released automatically



106
107
108
109
110
111
# File 'activesupport/lib/active_support/reloader.rb', line 106

def require_unload_lock!
  unless @locked
    ActiveSupport::Dependencies.interlock.start_unloading
    @locked = true
  end
end

#run!Object

:nodoc:



121
122
123
124
# File 'activesupport/lib/active_support/reloader.rb', line 121

def run! # :nodoc:
  super
  release_unload_lock!
end