Class: Spring::ApplicationManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_env) ⇒ ApplicationManager

Returns a new instance of ApplicationManager.



11
12
13
14
15
16
# File 'lib/spring/application_manager.rb', line 11

def initialize(app_env)
  super()

  @app_env    = app_env
  @spring_env = Env.new
end

Instance Attribute Details

#app_envObject (readonly)

Returns the value of attribute app_env.



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

def app_env
  @app_env
end

#childObject (readonly)

Returns the value of attribute child.



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

def child
  @child
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

#spring_envObject (readonly)

Returns the value of attribute spring_env.



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

def spring_env
  @spring_env
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


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

def alive?
  @pid
end

#restartObject



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

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.



35
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_manager.rb', line 35

def run(client)
  @client = client

  synchronize do
    if alive?
      begin
        child.send_io @client
      rescue Errno::EPIPE
        # EPIPE indicates child has died but has not been collected by the wait thread yet
        start
        child.send_io @client
      end
    else
      start
      child.send_io @client
    end
  end

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

#startObject



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

def start
  start_child
  start_wait_thread
end

#stopObject



61
62
63
# File 'lib/spring/application_manager.rb', line 61

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