Class: Procodile::Instance
- Inherits:
-
Object
- Object
- Procodile::Instance
- Defined in:
- lib/procodile/instance.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#process ⇒ Object
Returns the value of attribute process.
Instance Method Summary collapse
-
#add_respawn ⇒ Object
Increment the counter of respawns for this process.
-
#can_respawn? ⇒ Boolean
Can this process be respawned if needed?.
-
#check ⇒ Object
Check the status of this process and handle as appropriate.
-
#description ⇒ Object
Return a description for this instance.
-
#initialize(process, id) ⇒ Instance
constructor
A new instance of Instance.
-
#log_file_path ⇒ Object
Return the path to this instance’s log file.
-
#on_stop ⇒ Object
A method that will be called when this instance has been stopped and it isn’t going to be started again.
-
#pid_file_path ⇒ Object
Return the path to this instance’s PID file.
-
#pid_from_file ⇒ Object
Return the PID that is in the instances process PID file.
-
#respawns ⇒ Object
Return the number of times this process has been respawned in the last hour.
-
#restart ⇒ Object
Retarts the process using the appropriate method from the process configuraiton.
-
#running?(force_pid = nil) ⇒ Boolean
Is this process running? Pass an option to check the given PID instead of the instance.
-
#start ⇒ Object
Start a new instance of this process.
-
#stop ⇒ Object
Send this signal the signal to stop and mark the instance in a state that tells us that we want it to be stopped.
-
#stopping? ⇒ Boolean
Is this instance supposed to be stopping/be stopped?.
-
#tidy ⇒ Object
Tidy up when this process isn’t needed any more.
-
#to_hash ⇒ Object
Return this instance as a hash.
-
#unmonitor ⇒ Object
Mark this process as dead and tidy up after it.
-
#unmonitored? ⇒ Boolean
Should this instance still be monitored by the supervisor?.
-
#update_pid ⇒ Object
Update the locally cached PID from that stored on the file system.
Constructor Details
#initialize(process, id) ⇒ Instance
Returns a new instance of Instance.
10 11 12 13 14 |
# File 'lib/procodile/instance.rb', line 10 def initialize(process, id) @process = process @id = id @respawns = 0 end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/procodile/instance.rb', line 7 def id @id end |
#pid ⇒ Object
Returns the value of attribute pid.
6 7 8 |
# File 'lib/procodile/instance.rb', line 6 def pid @pid end |
#process ⇒ Object
Returns the value of attribute process.
8 9 10 |
# File 'lib/procodile/instance.rb', line 8 def process @process end |
Instance Method Details
#add_respawn ⇒ Object
Increment the counter of respawns for this process
231 232 233 234 235 236 237 238 |
# File 'lib/procodile/instance.rb', line 231 def add_respawn if @last_respawn && @last_respawn < (Time.now - @process.respawn_window) @respawns = 1 else @last_respawn = Time.now @respawns += 1 end end |
#can_respawn? ⇒ Boolean
Can this process be respawned if needed?
213 214 215 |
# File 'lib/procodile/instance.rb', line 213 def can_respawn? !stopping? && (respawns + 1) <= @process.max_respawns end |
#check ⇒ Object
Check the status of this process and handle as appropriate
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/procodile/instance.rb', line 179 def check # Don't do any checking if we're in the midst of a restart return if @restarting if self.running? # Everything is OK. The process is running. else # If the process isn't running any more, update the PID in our memory from # the file in case the process has changed itself. return check if update_pid if can_respawn? Procodile.log(@process.log_color, description, "Process has stopped. Respawning...") start add_respawn elsif respawns >= @process.max_respawns Procodile.log(@process.log_color, description, "\e[41;37mWarning:\e[0m\e[31m this process has been respawned #{respawns} times and keeps dying.\e[0m") Procodile.log(@process.log_color, description, "\e[31mIt will not be respawned automatically any longer and will no longer be managed.\e[0m") tidy unmonitor end end end |
#description ⇒ Object
Return a description for this instance
19 20 21 |
# File 'lib/procodile/instance.rb', line 19 def description "#{@process.name}.#{@id}" end |
#log_file_path ⇒ Object
Return the path to this instance’s log file
40 41 42 |
# File 'lib/procodile/instance.rb', line 40 def log_file_path File.join(@process.config.log_root, "#{description}.log") end |
#on_stop ⇒ Object
A method that will be called when this instance has been stopped and it isn’t going to be started again
116 117 118 119 |
# File 'lib/procodile/instance.rb', line 116 def on_stop tidy unmonitor end |
#pid_file_path ⇒ Object
Return the path to this instance’s PID file
33 34 35 |
# File 'lib/procodile/instance.rb', line 33 def pid_file_path File.join(@process.config.pid_root, "#{description}.pid") end |
#pid_from_file ⇒ Object
Return the PID that is in the instances process PID file
47 48 49 50 51 52 53 54 |
# File 'lib/procodile/instance.rb', line 47 def pid_from_file if File.exist?(pid_file_path) pid = File.read(pid_file_path) pid.length > 0 ? pid.strip.to_i : nil else nil end end |
#respawns ⇒ Object
Return the number of times this process has been respawned in the last hour
220 221 222 223 224 225 226 |
# File 'lib/procodile/instance.rb', line 220 def respawns if @respawns.nil? || @last_respawn.nil? || @last_respawn < (Time.now - @process.respawn_window) 0 else @respawns end end |
#restart ⇒ Object
Retarts the process using the appropriate method from the process configuraiton
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/procodile/instance.rb', line 132 def restart Procodile.log(@process.log_color, description, "Restarting using #{@process.restart_mode} mode") @restarting = true update_pid case @process.restart_mode when 'usr1', 'usr2' if running? ::Process.kill(@process.restart_mode.upcase, @pid) Procodile.log(@process.log_color, description, "Sent #{@process.restart_mode.upcase} signal to process #{@pid}") else Procodile.log(@process.log_color, description, "Process not running already. Starting it.") start end when 'start-term' old_process_pid = @pid start Procodile.log(@process.log_color, description, "Sent TERM signal to old PID #{old_process_pid} (forgetting now)") ::Process.kill('TERM', old_process_pid) when 'term-start' stop Thread.new do # Wait for this process to stop and when it has, run it. sleep 0.5 while running? start end end ensure @restarting = false end |
#running?(force_pid = nil) ⇒ Boolean
Is this process running? Pass an option to check the given PID instead of the instance
59 60 61 62 63 64 65 66 67 |
# File 'lib/procodile/instance.rb', line 59 def running?(force_pid = nil) if force_pid || @pid ::Process.getpgid(force_pid || @pid) ? true : false else false end rescue Errno::ESRCH false end |
#start ⇒ Object
Start a new instance of this process
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/procodile/instance.rb', line 72 def start @stopping = false existing_pid = self.pid_from_file if running?(existing_pid) # If the PID in the file is already running, we should just just continue # to monitor this process rather than spawning a new one. @pid = existing_pid Procodile.log(@process.log_color, description, "Already running with PID #{@pid}") else log_file = File.open(self.log_file_path, 'a') Dir.chdir(@process.config.root) @pid = ::Process.spawn({'PID_FILE' => pid_file_path}, @process.command, :out => log_file, :err => log_file, :pgroup => true) Procodile.log(@process.log_color, description, "Started with PID #{@pid}") File.open(pid_file_path, 'w') { |f| f.write(@pid.to_s + "\n") } ::Process.detach(@pid) end end |
#stop ⇒ Object
Send this signal the signal to stop and mark the instance in a state that tells us that we want it to be stopped.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/procodile/instance.rb', line 101 def stop @stopping = true update_pid if self.running? Procodile.log(@process.log_color, description, "Sending TERM to #{@pid}") ::Process.kill('TERM', pid) else Procodile.log(@process.log_color, description, "Process already stopped") end end |
#stopping? ⇒ Boolean
Is this instance supposed to be stopping/be stopped?
93 94 95 |
# File 'lib/procodile/instance.rb', line 93 def stopping? @stopping || false end |
#tidy ⇒ Object
Tidy up when this process isn’t needed any more
124 125 126 127 |
# File 'lib/procodile/instance.rb', line 124 def tidy FileUtils.rm_f(self.pid_file_path) Procodile.log(@process.log_color, description, "Removed PID file") end |
#to_hash ⇒ Object
Return this instance as a hash
243 244 245 246 247 248 249 250 |
# File 'lib/procodile/instance.rb', line 243 def to_hash { :description => self.description, :pid => self.pid, :respawns => self.respawns, :running => self.running? } end |
#unmonitor ⇒ Object
Mark this process as dead and tidy up after it
206 207 208 |
# File 'lib/procodile/instance.rb', line 206 def unmonitor @monitored = false end |
#unmonitored? ⇒ Boolean
Should this instance still be monitored by the supervisor?
26 27 28 |
# File 'lib/procodile/instance.rb', line 26 def unmonitored? @monitored == false end |
#update_pid ⇒ Object
Update the locally cached PID from that stored on the file system.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/procodile/instance.rb', line 165 def update_pid pid_from_file = self.pid_from_file if pid_from_file != @pid @pid = pid_from_file Procodile.log(@process.log_color, description, "PID file changed. Updated pid to #{@pid}") true else false end end |