Class: Spring::ApplicationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/application_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, app_env) ⇒ ApplicationManager

Returns a new instance of ApplicationManager.



9
10
11
12
13
14
# File 'lib/spring/application_manager.rb', line 9

def initialize(server, app_env)
  @server     = server
  @app_env    = app_env
  @spring_env = Env.new
  @mutex      = Mutex.new
end

Instance Attribute Details

#app_envObject (readonly)

Returns the value of attribute app_env.



7
8
9
# File 'lib/spring/application_manager.rb', line 7

def app_env
  @app_env
end

#childObject (readonly)

Returns the value of attribute child.



7
8
9
# File 'lib/spring/application_manager.rb', line 7

def child
  @child
end

#pidObject (readonly)

Returns the value of attribute pid.



7
8
9
# File 'lib/spring/application_manager.rb', line 7

def pid
  @pid
end

#serverObject (readonly)

Returns the value of attribute server.



7
8
9
# File 'lib/spring/application_manager.rb', line 7

def server
  @server
end

#spring_envObject (readonly)

Returns the value of attribute spring_env.



7
8
9
# File 'lib/spring/application_manager.rb', line 7

def spring_env
  @spring_env
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/spring/application_manager.rb', line 37

def alive?
  @pid
end

#restartObject



30
31
32
33
34
35
# File 'lib/spring/application_manager.rb', line 30

def restart
  # Restarting is a background operation. If it fails, we don't want
  # any terminal output. The user will see the output when they next
  # try to run a command.
  start_child(true)
end

#run(client) ⇒ Object

Returns the pid of the process running the command, or nil if the application process died.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spring/application_manager.rb', line 60

def run(client)
  with_child do
    child.send_io client
    child.gets
  end

  child.gets.chomp.to_i # get the pid
rescue Errno::ECONNRESET, Errno::EPIPE
  nil
ensure
  client.close
end

#startObject



25
26
27
28
# File 'lib/spring/application_manager.rb', line 25

def start
  start_child
  start_wait_thread
end

#stopObject



73
74
75
# File 'lib/spring/application_manager.rb', line 73

def stop
  Process.kill('TERM', pid) if pid
end

#synchronizeObject

We’re not using @mutex.synchronize to avoid the weird “<internal:prelude>:10” line which messes with backtraces in e.g. rspec



18
19
20
21
22
23
# File 'lib/spring/application_manager.rb', line 18

def synchronize
  @mutex.lock
  yield
ensure
  @mutex.unlock
end

#with_childObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spring/application_manager.rb', line 41

def with_child
  synchronize do
    if alive?
      begin
        yield
      rescue Errno::ECONNRESET, Errno::EPIPE
        # The child has died but has not been collected by the wait thread yet,
        # so start a new child and try again.
        start
        yield
      end
    else
      start
      yield
    end
  end
end