Class: Spring::ApplicationManager
- Inherits:
-
Object
- Object
- Spring::ApplicationManager
- Defined in:
- lib/spring/application_manager.rb
Instance Attribute Summary collapse
-
#app_env ⇒ Object
readonly
Returns the value of attribute app_env.
-
#child ⇒ Object
readonly
Returns the value of attribute child.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#spawn_env ⇒ Object
readonly
Returns the value of attribute spawn_env.
-
#spring_env ⇒ Object
readonly
Returns the value of attribute spring_env.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(app_env, spawn_env, spring_env) ⇒ ApplicationManager
constructor
A new instance of ApplicationManager.
- #log(message) ⇒ Object
- #restart ⇒ Object
-
#run(client) ⇒ Object
Returns the pid of the process running the command, or nil if the application process died.
- #start ⇒ Object
- #stop ⇒ Object
-
#synchronize ⇒ Object
We’re not using @mutex.synchronize to avoid the weird “<internal:prelude>:10” line which messes with backtraces in e.g.
- #with_child ⇒ Object
Constructor Details
#initialize(app_env, spawn_env, spring_env) ⇒ ApplicationManager
Returns a new instance of ApplicationManager.
5 6 7 8 9 10 11 12 |
# File 'lib/spring/application_manager.rb', line 5 def initialize(app_env, spawn_env, spring_env) @app_env = app_env @spawn_env = spawn_env @spring_env = spring_env @mutex = Mutex.new @state = :running @pid = nil end |
Instance Attribute Details
#app_env ⇒ Object (readonly)
Returns the value of attribute app_env.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def app_env @app_env end |
#child ⇒ Object (readonly)
Returns the value of attribute child.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def child @child end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def pid @pid end |
#spawn_env ⇒ Object (readonly)
Returns the value of attribute spawn_env.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def spawn_env @spawn_env end |
#spring_env ⇒ Object (readonly)
Returns the value of attribute spring_env.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def spring_env @spring_env end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
3 4 5 |
# File 'lib/spring/application_manager.rb', line 3 def status @status end |
Instance Method Details
#alive? ⇒ Boolean
36 37 38 |
# File 'lib/spring/application_manager.rb', line 36 def alive? @pid end |
#log(message) ⇒ Object
14 15 16 |
# File 'lib/spring/application_manager.rb', line 14 def log() spring_env.log "[application_manager:#{app_env}] #{}" end |
#restart ⇒ Object
31 32 33 34 |
# File 'lib/spring/application_manager.rb', line 31 def restart return if @state == :stopping start_child(true) end |
#run(client) ⇒ Object
Returns the pid of the process running the command, or nil if the application process died.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spring/application_manager.rb', line 61 def run(client) with_child do child.send_io client child.gets or raise Errno::EPIPE end pid = child.gets.to_i unless pid.zero? log "got worker pid #{pid}" pid end rescue Errno::ECONNRESET, Errno::EPIPE => e log "#{e} while reading from child; returning no pid" nil ensure client.close end |
#start ⇒ Object
27 28 29 |
# File 'lib/spring/application_manager.rb', line 27 def start start_child end |
#stop ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/spring/application_manager.rb', line 80 def stop log "stopping" @state = :stopping if pid Process.kill('TERM', pid) Process.wait(pid) end rescue Errno::ESRCH, Errno::ECHILD # Don't care end |
#synchronize ⇒ Object
We’re not using @mutex.synchronize to avoid the weird “<internal:prelude>:10” line which messes with backtraces in e.g. rspec
20 21 22 23 24 25 |
# File 'lib/spring/application_manager.rb', line 20 def synchronize @mutex.lock yield ensure @mutex.unlock end |
#with_child ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/spring/application_manager.rb', line 40 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. log "child dead; starting" start yield end else log "child not running; starting" start yield end end end |