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.
-
#respawnable ⇒ Object
Returns the value of attribute respawnable.
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.
-
#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.
11 12 13 14 15 16 |
# File 'lib/procodile/instance.rb', line 11 def initialize(process, id) @process = process @id = id @respawns = 0 @respawnable = true 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 |
#respawnable ⇒ Object
Returns the value of attribute respawnable.
9 10 11 |
# File 'lib/procodile/instance.rb', line 9 def respawnable @respawnable end |
Instance Method Details
#add_respawn ⇒ Object
Increment the counter of respawns for this process
245 246 247 248 249 250 251 252 |
# File 'lib/procodile/instance.rb', line 245 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?
227 228 229 |
# File 'lib/procodile/instance.rb', line 227 def can_respawn? !stopping? && (respawns + 1) <= @process.max_respawns end |
#check ⇒ Object
Check the status of this process and handle as appropriate.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/procodile/instance.rb', line 185 def check # Don't do any checking if we're in the midst of a restart return if @restarting return if unmonitored? if self.running? # Everything is OK. The process is running. true 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 @respawnable 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, "It will not be respawned automatically any longer and will no longer be managed.".color(31)) tidy unmonitor end else Procodile.log(@process.log_color, description, "Process has stopped. Respawning not available.") tidy unmonitor end end end |
#description ⇒ Object
Return a description for this instance
21 22 23 |
# File 'lib/procodile/instance.rb', line 21 def description "#{@process.name}.#{@id}" 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
122 123 124 125 |
# File 'lib/procodile/instance.rb', line 122 def on_stop tidy unmonitor end |
#pid_file_path ⇒ Object
Return the path to this instance’s PID file
35 36 37 |
# File 'lib/procodile/instance.rb', line 35 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
42 43 44 45 46 47 48 49 |
# File 'lib/procodile/instance.rb', line 42 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
234 235 236 237 238 239 240 |
# File 'lib/procodile/instance.rb', line 234 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/procodile/instance.rb', line 138 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 #{@process.term_signal} signal to old PID #{old_process_pid} (forgetting now)") ::Process.kill(@process.term_signal, 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
54 55 56 57 58 59 60 61 62 |
# File 'lib/procodile/instance.rb', line 54 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/procodile/instance.rb', line 67 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}") nil else if self.process.log_path log_destination = File.open(self.process.log_path, 'a') return_value = nil else reader, writer = IO.pipe log_destination = writer return_value = reader end Dir.chdir(@process.config.root) @pid = ::Process.spawn({'PID_FILE' => pid_file_path}, @process.command, :out => log_destination, :err => log_destination, :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) return_value 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.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/procodile/instance.rb', line 107 def stop @stopping = true update_pid if self.running? Procodile.log(@process.log_color, description, "Sending #{@process.term_signal} to #{@pid}") ::Process.kill(@process.term_signal, pid) else Procodile.log(@process.log_color, description, "Process already stopped") end end |
#stopping? ⇒ Boolean
Is this instance supposed to be stopping/be stopped?
99 100 101 |
# File 'lib/procodile/instance.rb', line 99 def stopping? @stopping || false end |
#tidy ⇒ Object
Tidy up when this process isn’t needed any more
130 131 132 133 |
# File 'lib/procodile/instance.rb', line 130 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
257 258 259 260 261 262 263 264 |
# File 'lib/procodile/instance.rb', line 257 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
220 221 222 |
# File 'lib/procodile/instance.rb', line 220 def unmonitor @monitored = false end |
#unmonitored? ⇒ Boolean
Should this instance still be monitored by the supervisor?
28 29 30 |
# File 'lib/procodile/instance.rb', line 28 def unmonitored? @monitored == false end |
#update_pid ⇒ Object
Update the locally cached PID from that stored on the file system.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/procodile/instance.rb', line 171 def update_pid pid_from_file = self.pid_from_file if pid_from_file && 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 |