Class: Spring::Application
- Inherits:
-
Object
- Object
- Spring::Application
- Defined in:
- lib/spring/application.rb
Constant Summary collapse
- WATCH_INTERVAL =
0.2
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.application_watcher) ⇒ Application
constructor
A new instance of Application.
- #invoke_after_fork_callbacks ⇒ 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
- #watch_application ⇒ Object
Constructor Details
#initialize(manager, watcher = Spring.application_watcher) ⇒ Application
Returns a new instance of Application.
18 19 20 21 22 |
# File 'lib/spring/application.rb', line 18 def initialize(manager, watcher = Spring.application_watcher) @manager = manager @watcher = watcher @setup = Set.new end |
Instance Attribute Details
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
16 17 18 |
# File 'lib/spring/application.rb', line 16 def manager @manager end |
#watcher ⇒ Object (readonly)
Returns the value of attribute watcher.
16 17 18 |
# File 'lib/spring/application.rb', line 16 def watcher @watcher end |
Instance Method Details
#invoke_after_fork_callbacks ⇒ Object
108 109 110 111 112 |
# File 'lib/spring/application.rb', line 108 def invoke_after_fork_callbacks Spring.after_fork_callbacks.each do |callback| callback.call end end |
#run ⇒ Object
43 44 45 46 47 48 |
# File 'lib/spring/application.rb', line 43 def run loop do watch_application serve manager.recv_io(UNIXSocket) end end |
#serve(client) ⇒ Object
56 57 58 59 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 |
# File 'lib/spring/application.rb', line 56 def serve(client) streams = 3.times.map { client.recv_io } args_length = client.gets.to_i args = args_length.times.map { 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") } invoke_after_fork_callbacks command.call(args) } 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.
FIXME: The watcher.add_files will reset the watcher, which may mean that previous changes to already-loaded files are missed.
98 99 100 101 102 103 104 105 106 |
# File 'lib/spring/application.rb', line 98 def setup(command) return if @setup.include?(command.class) @setup << command.class if command.respond_to?(:setup) command.setup watcher.add_files $LOADED_FEATURES # loaded features may have changed end end |
#start ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/spring/application.rb', line 24 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 require Spring.application_root_path.join("config", "environment") watcher.add_files $LOADED_FEATURES watcher.add_files ["Gemfile", "Gemfile.lock"].map { |f| "#{Rails.root}/#{f}" } watcher.add_globs Rails.application.paths["config/initializers"].map { |p| "#{Rails.root}/#{p}/*.rb" } run end |
#watch_application ⇒ Object
50 51 52 53 54 |
# File 'lib/spring/application.rb', line 50 def watch_application until IO.select([manager], [], [], WATCH_INTERVAL) exit if watcher.stale? end end |