Class: Spring::Application
- Inherits:
-
Object
- Object
- Spring::Application
- Defined in:
- lib/spring/application.rb
Instance Attribute Summary collapse
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#spring_env ⇒ Object
readonly
Returns the value of attribute spring_env.
-
#watcher ⇒ Object
readonly
Returns the value of attribute watcher.
Instance Method Summary collapse
- #connect_database ⇒ Object
- #disconnect_database ⇒ Object
- #exiting? ⇒ Boolean
-
#initialize(manager, watcher = Spring.watcher) ⇒ Application
constructor
A new instance of Application.
- #invoke_after_fork_callbacks ⇒ Object
- #loaded_application_features ⇒ Object
- #log(message) ⇒ Object
- #preload ⇒ Object
- #preloaded? ⇒ Boolean
- #run ⇒ Object
- #serve(client) ⇒ Object
-
#setup(command) ⇒ Object
The command might need to require some files in the main process so that they are cached.
-
#shush_backtraces ⇒ Object
This feels very naughty.
- #try_exit ⇒ Object
Constructor Details
#initialize(manager, watcher = Spring.watcher) ⇒ Application
Returns a new instance of Application.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/spring/application.rb', line 9 def initialize(manager, watcher = Spring.watcher) @manager = manager @watcher = watcher @setup = Set.new @spring_env = Env.new @preloaded = false @mutex = Mutex.new @waiting = 0 @exiting = false # Workaround for GC bug in Ruby 2 which causes segfaults if watcher.to_io # instances get dereffed. @fds = [manager, watcher.to_io] end |
Instance Attribute Details
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
7 8 9 |
# File 'lib/spring/application.rb', line 7 def manager @manager end |
#spring_env ⇒ Object (readonly)
Returns the value of attribute spring_env.
7 8 9 |
# File 'lib/spring/application.rb', line 7 def spring_env @spring_env end |
#watcher ⇒ Object (readonly)
Returns the value of attribute watcher.
7 8 9 |
# File 'lib/spring/application.rb', line 7 def watcher @watcher end |
Instance Method Details
#connect_database ⇒ Object
193 194 195 |
# File 'lib/spring/application.rb', line 193 def connect_database ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) end |
#disconnect_database ⇒ Object
189 190 191 |
# File 'lib/spring/application.rb', line 189 def disconnect_database ActiveRecord::Base.remove_connection if defined?(ActiveRecord::Base) end |
#exiting? ⇒ Boolean
32 33 34 |
# File 'lib/spring/application.rb', line 32 def exiting? @exiting end |
#invoke_after_fork_callbacks ⇒ Object
179 180 181 182 183 |
# File 'lib/spring/application.rb', line 179 def invoke_after_fork_callbacks Spring.after_fork_callbacks.each do |callback| callback.call end end |
#loaded_application_features ⇒ Object
185 186 187 |
# File 'lib/spring/application.rb', line 185 def loaded_application_features $LOADED_FEATURES.select { |f| f.start_with?(File.realpath(Rails.root)) } end |
#log(message) ⇒ Object
24 25 26 |
# File 'lib/spring/application.rb', line 24 def log() spring_env.log "[application:#{ENV['RAILS_ENV']}] #{}" end |
#preload ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/spring/application.rb', line 36 def preload log "preloading app" require Spring.application_root_path.join("config", "application") # config/environments/test.rb will have config.cache_classes = true. However # we want it to be false so that we can reload files. This is a hack to # override the effect of config.cache_classes = true. We can then actually # set config.cache_classes = false after loading the environment. Rails::Application.initializer :initialize_dependency_mechanism, group: :all do ActiveSupport::Dependencies.mechanism = :load end require Spring.application_root_path.join("config", "environment") Rails.application.config.cache_classes = false disconnect_database watcher.add loaded_application_features watcher.add Spring.gemfile, "#{Spring.gemfile}.lock" watcher.add Rails.application.paths["config/initializers"] @preloaded = true end |
#preloaded? ⇒ Boolean
28 29 30 |
# File 'lib/spring/application.rb', line 28 def preloaded? @preloaded end |
#run ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spring/application.rb', line 61 def run log "running" watcher.start loop do IO.select(@fds) if watcher.stale? log "watcher stale; exiting" manager.close @exiting = true try_exit sleep else serve manager.recv_io(UNIXSocket) end end end |
#serve(client) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/spring/application.rb', line 86 def serve(client) log "got client" manager.puts streams = 3.times.map { client.recv_io } [STDOUT, STDERR].zip(streams).each { |a, b| a.reopen(b) } preload unless preloaded? args, env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env") command = Spring.command(args.shift) connect_database setup command if Rails.application.reloaders.any?(&:updated?) ActionDispatch::Reloader.cleanup! ActionDispatch::Reloader.prepare! end pid = fork { Process.setsid STDIN.reopen(streams.last) IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") } ARGV.replace(args) # Delete all env vars which are unchanged from before spring started Spring.original_env.each { |k, v| ENV.delete k if ENV[k] == v } # Load in the current env vars, except those which *were* changed when spring started env.each { |k, v| ENV[k] ||= v } connect_database srand invoke_after_fork_callbacks shush_backtraces if command.respond_to?(:call) command.call else exec_name = command.exec_name gem_name = command.gem_name if command.respond_to?(:gem_name) exec = Gem.bin_path(gem_name || exec_name, exec_name) $0 = exec load exec end } disconnect_database [STDOUT, STDERR].each { |stream| stream.reopen(spring_env.log_file) } log "forked #{pid}" manager.puts pid # Wait in a separate thread so we can run multiple commands at once Thread.new { @mutex.synchronize { @waiting += 1 } _, status = Process.wait2 pid log "#{pid} exited with #{status.exitstatus}" streams.each(&:close) client.puts(status.exitstatus) client.close @mutex.synchronize { @waiting -= 1 } try_exit } rescue => e log "exception: #{e}" streams.each(&:close) if streams client.puts(1) client.close raise end |
#setup(command) ⇒ Object
The command might need to require some files in the main process so that they are cached. For example a test command wants to load the helper file once and have it cached.
169 170 171 172 173 174 175 176 177 |
# File 'lib/spring/application.rb', line 169 def setup(command) return if @setup.include?(command.class) @setup << command.class if command.respond_to?(:setup) command.setup watcher.add loaded_application_features # loaded features may have changed end end |
#shush_backtraces ⇒ Object
This feels very naughty
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/spring/application.rb', line 198 def shush_backtraces Kernel.module_eval do old_raise = Kernel.method(:raise) remove_method :raise define_method :raise do |*args| begin old_raise.call(*args) ensure if $! lib = File.("..", __FILE__) $!.backtrace.reject! { |line| line.start_with?(lib) } end end end end end |
#try_exit ⇒ Object
80 81 82 83 84 |
# File 'lib/spring/application.rb', line 80 def try_exit @mutex.synchronize { exit if exiting? && @waiting == 0 } end |