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.
-
#watcher ⇒ Object
readonly
Returns the value of attribute watcher.
Instance Method Summary collapse
-
#initialize(manager, watcher = Spring.watcher) ⇒ Application
constructor
A new instance of Application.
- #invoke_after_fork_callbacks ⇒ Object
- #loaded_application_features ⇒ Object
- #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.
- #start ⇒ Object
Constructor Details
#initialize(manager, watcher = Spring.watcher) ⇒ Application
Returns a new instance of Application.
10 11 12 13 14 15 16 17 18 |
# File 'lib/spring/application.rb', line 10 def initialize(manager, watcher = Spring.watcher) @manager = manager @watcher = watcher @setup = Set.new # 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.
8 9 10 |
# File 'lib/spring/application.rb', line 8 def manager @manager end |
#watcher ⇒ Object (readonly)
Returns the value of attribute watcher.
8 9 10 |
# File 'lib/spring/application.rb', line 8 def watcher @watcher end |
Instance Method Details
#invoke_after_fork_callbacks ⇒ Object
125 126 127 128 129 |
# File 'lib/spring/application.rb', line 125 def invoke_after_fork_callbacks Spring.after_fork_callbacks.each do |callback| callback.call end end |
#loaded_application_features ⇒ Object
131 132 133 |
# File 'lib/spring/application.rb', line 131 def loaded_application_features $LOADED_FEATURES.select { |f| f.start_with?(File.realpath(Rails.root)) } end |
#run ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/spring/application.rb', line 46 def run watcher.start loop do IO.select(@fds) if watcher.stale? exit else serve manager.recv_io(UNIXSocket) end end end |
#serve(client) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 |
# File 'lib/spring/application.rb', line 60 def serve(client) manager.puts streams = 3.times.map { client.recv_io } args = JSON.load(client.read(client.gets.to_i)) command = Spring.command(args.shift) setup command ActionDispatch::Reloader.cleanup! ActionDispatch::Reloader.prepare! pid = fork { Process.setsid [STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) } IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") } ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) ARGV.replace(args) srand invoke_after_fork_callbacks 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 } manager.puts pid # Wait in a separate thread so we can run multiple commands at once Thread.new { _, status = Process.wait2 pid streams.each(&:close) client.puts(status.exitstatus) client.close } rescue => 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.
115 116 117 118 119 120 121 122 123 |
# File 'lib/spring/application.rb', line 115 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 |
#start ⇒ Object
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 |
# File 'lib/spring/application.rb', line 20 def start require Spring.application_root_path.join("config", "application") # The test environment has config.cache_classes = true set by default. # However, we don't want this to prevent us from performing class reloading, # so this gets around that. ::Rails::Application.initializer :initialize_dependency_mechanism, group: :all do ActiveSupport::Dependencies.mechanism = :load end # This initializer has issues after we disconnect from the DB, but we don't need it. if defined?(ActiveRecord::Base) ActiveRecord::Railtie.initializers.delete_if { |i| i.name == "active_record.set_reloader_hooks" } end require Spring.application_root_path.join("config", "environment") ActiveRecord::Base.remove_connection if defined?(ActiveRecord::Base) watcher.add loaded_application_features watcher.add Spring.gemfile, "#{Spring.gemfile}.lock" watcher.add Rails.application.paths["config/initializers"] run end |