Class: Bluepill::Process
- Inherits:
-
Object
- Object
- Bluepill::Process
- Defined in:
- lib/bluepill/process.rb
Constant Summary collapse
- CONFIGURABLE_ATTRIBUTES =
[ :start_command, :stop_command, :restart_command, :stdout, :stderr, :stdin, :daemonize, :pid_file, :working_dir, :start_grace_time, :stop_grace_time, :restart_grace_time, :uid, :gid, :monitor_children, :child_process_template ]
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
Returns the value of attribute name.
-
#skip_ticks_until ⇒ Object
Returns the value of attribute skip_ticks_until.
-
#triggers ⇒ Object
Returns the value of attribute triggers.
-
#watches ⇒ Object
Returns the value of attribute watches.
Instance Method Summary collapse
- #actual_pid ⇒ Object
- #actual_pid=(pid) ⇒ Object
- #add_trigger(name, options = {}) ⇒ Object
-
#add_watch(name, options = {}) ⇒ Object
Watch related methods.
- #clear_pid ⇒ Object
- #daemonize? ⇒ Boolean
- #deep_copy ⇒ Object
-
#dispatch!(event) ⇒ Object
State machine methods.
- #handle_user_command(cmd) ⇒ Object
-
#initialize(process_name, options = {}) ⇒ Process
constructor
A new instance of Process.
- #monitor_children? ⇒ Boolean
- #notify_triggers(transition) ⇒ Object
- #process_command(cmd) ⇒ Object
-
#process_running?(force = false) ⇒ Boolean
System Process Methods.
- #record_transition(transition) ⇒ Object
- #refresh_children! ⇒ Object
- #restart_process ⇒ Object
- #run_watches ⇒ Object
- #signal_process(code) ⇒ Object
-
#skip_ticks_for(seconds) ⇒ Object
Internal State Methods.
- #skipping_ticks? ⇒ Boolean
- #start_process ⇒ Object
- #stop_process ⇒ Object
- #system_command_options ⇒ Object
- #tick ⇒ Object
- #unlink_pid ⇒ Object
- #with_timeout(secs, &blk) ⇒ Object
Constructor Details
#initialize(process_name, options = {}) ⇒ Process
Returns a new instance of Process.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bluepill/process.rb', line 87 def initialize(process_name, = {}) @name = process_name @event_mutex = Monitor.new @transition_history = Util::RotationalArray.new(10) @watches = [] @triggers = [] @children = [] @monitor_children = [:monitor_children] || false %w(start_grace_time stop_grace_time restart_grace_time).each do |grace| instance_variable_set("@#{grace}", [grace.to_sym] || 3) end CONFIGURABLE_ATTRIBUTES.each do |attribute_name| self.send("#{attribute_name}=", [attribute_name]) if .has_key?(attribute_name) end # Let state_machine do its initialization stuff super() end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
32 33 34 |
# File 'lib/bluepill/process.rb', line 32 def children @children end |
#logger ⇒ Object
Returns the value of attribute logger.
30 31 32 |
# File 'lib/bluepill/process.rb', line 30 def logger @logger end |
#name ⇒ Object
Returns the value of attribute name.
30 31 32 |
# File 'lib/bluepill/process.rb', line 30 def name @name end |
#skip_ticks_until ⇒ Object
Returns the value of attribute skip_ticks_until.
30 31 32 |
# File 'lib/bluepill/process.rb', line 30 def skip_ticks_until @skip_ticks_until end |
#triggers ⇒ Object
Returns the value of attribute triggers.
30 31 32 |
# File 'lib/bluepill/process.rb', line 30 def triggers @triggers end |
#watches ⇒ Object
Returns the value of attribute watches.
30 31 32 |
# File 'lib/bluepill/process.rb', line 30 def watches @watches end |
Instance Method Details
#actual_pid ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/bluepill/process.rb', line 316 def actual_pid @actual_pid ||= begin if pid_file if File.exists?(pid_file) str = File.read(pid_file) str.to_i if str.size > 0 else logger.warning("pid_file #{pid_file} does not exist or cannot be read") nil end end end end |
#actual_pid=(pid) ⇒ Object
330 331 332 |
# File 'lib/bluepill/process.rb', line 330 def actual_pid=(pid) @actual_pid = pid end |
#add_trigger(name, options = {}) ⇒ Object
167 168 169 |
# File 'lib/bluepill/process.rb', line 167 def add_trigger(name, = {}) self.triggers << Trigger[name].new(self, .merge(:logger => self.logger)) end |
#add_watch(name, options = {}) ⇒ Object
Watch related methods
163 164 165 |
# File 'lib/bluepill/process.rb', line 163 def add_watch(name, = {}) self.watches << ConditionWatch.new(name, .merge(:logger => self.logger)) end |
#clear_pid ⇒ Object
334 335 336 |
# File 'lib/bluepill/process.rb', line 334 def clear_pid @actual_pid = nil end |
#daemonize? ⇒ Boolean
301 302 303 |
# File 'lib/bluepill/process.rb', line 301 def daemonize? !!self.daemonize end |
#deep_copy ⇒ Object
379 380 381 |
# File 'lib/bluepill/process.rb', line 379 def deep_copy Marshal.load(Marshal.dump(self)) end |
#dispatch!(event) ⇒ Object
State machine methods
136 137 138 139 140 |
# File 'lib/bluepill/process.rb', line 136 def dispatch!(event) @event_mutex.synchronize do self.send("#{event}") end end |
#handle_user_command(cmd) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/bluepill/process.rb', line 193 def handle_user_command(cmd) case cmd when "boot" # This is only called when bluepill is initially starting up if process_running?(true) # process was running even before bluepill was self.state = 'up' else self.state = 'starting' end when "start" if process_running?(true) && daemonize? logger.warning("Refusing to re-run start command on an automatically daemonized process to preserve currently running process pid file.") return end dispatch!(:start) when "stop" stop_process dispatch!(:unmonitor) when "restart" restart_process when "unmonitor" # When the user issues an unmonitor cmd, reset any triggers so that # scheduled events gets cleared triggers.each {|t| t.reset! } dispatch!(:unmonitor) end end |
#monitor_children? ⇒ Boolean
305 306 307 |
# File 'lib/bluepill/process.rb', line 305 def monitor_children? !!self.monitor_children end |
#notify_triggers(transition) ⇒ Object
158 159 160 |
# File 'lib/bluepill/process.rb', line 158 def notify_triggers(transition) self.triggers.each {|trigger| trigger.notify(transition)} end |
#process_command(cmd) ⇒ Object
383 384 385 |
# File 'lib/bluepill/process.rb', line 383 def process_command(cmd) cmd.to_s.gsub("{{PID}}", actual_pid.to_s) end |
#process_running?(force = false) ⇒ Boolean
System Process Methods
227 228 229 230 231 232 |
# File 'lib/bluepill/process.rb', line 227 def process_running?(force = false) @process_running = nil if force @process_running ||= signal_process(0) self.clear_pid unless @process_running @process_running end |
#record_transition(transition) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bluepill/process.rb', line 142 def record_transition(transition) unless transition.loopback? @transitioned = true # When a process changes state, we should clear the memory of all the watches self.watches.each { |w| w.clear_history! } # Also, when a process changes state, we should re-populate its child list if self.monitor_children? self.logger.warning "Clearing child list" self.children.clear end logger.info "Going from #{transition.from_name} => #{transition.to_name}" end end |
#refresh_children! ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/bluepill/process.rb', line 353 def refresh_children! # First prune the list of dead children @children.delete_if {|child| !child.process_running?(true) } # Add new found children to the list new_children_pids = System.get_children(self.actual_pid) - @children.map {|child| child.actual_pid} unless new_children_pids.empty? logger.info "Existing children: #{@children.collect{|c| c.actual_pid}.join(",")}. Got new children: #{new_children_pids.inspect} for #{actual_pid}" end # Construct a new process wrapper for each new found children new_children_pids.each do |child_pid| child = self.child_process_template.deep_copy child.name = "<child(pid:#{child_pid})>" child.actual_pid = child_pid child.logger = self.logger.prefix_with(child.name) child.initialize_state_machines child.state = "up" @children << child end end |
#restart_process ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/bluepill/process.rb', line 278 def restart_process if restart_command cmd = process_command(restart_command) logger.warning "Executing restart command: #{cmd}" with_timeout(restart_grace_time) do result = System.execute_blocking(cmd, self.) unless result[:exit_code].zero? logger.warning "Restart command execution returned non-zero exit code:" logger.warning result.inspect end end self.skip_ticks_for(restart_grace_time) else logger.warning "No restart_command specified. Must stop and start to restart" self.stop_process # the tick will bring it back. end end |
#run_watches ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/bluepill/process.rb', line 171 def run_watches now = Time.now.to_i threads = self.watches.collect do |watch| [watch, Thread.new { Thread.current[:events] = watch.run(self.actual_pid, now) }] end @transitioned = false threads.inject([]) do |events, (watch, thread)| thread.join if thread[:events].size > 0 logger.info "#{watch.name} dispatched: #{thread[:events].join(',')}" events << thread[:events] end events end.flatten.uniq.each do |event| break if @transitioned self.dispatch!(event) end end |
#signal_process(code) ⇒ Object
309 310 311 312 313 314 |
# File 'lib/bluepill/process.rb', line 309 def signal_process(code) ::Process.kill(code, actual_pid) true rescue false end |
#skip_ticks_for(seconds) ⇒ Object
Internal State Methods
343 344 345 346 347 |
# File 'lib/bluepill/process.rb', line 343 def skip_ticks_for(seconds) # TODO: should this be addative or longest wins? # i.e. if two calls for skip_ticks_for come in for 5 and 10, should it skip for 10 or 15? self.skip_ticks_until = (self.skip_ticks_until || Time.now.to_i) + seconds.to_i end |
#skipping_ticks? ⇒ Boolean
349 350 351 |
# File 'lib/bluepill/process.rb', line 349 def skipping_ticks? self.skip_ticks_until && self.skip_ticks_until > Time.now.to_i end |
#start_process ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bluepill/process.rb', line 234 def start_process logger.warning "Executing start command: #{start_command}" if self.daemonize? System.daemonize(start_command, self.) else # This is a self-daemonizing process with_timeout(start_grace_time) do result = System.execute_blocking(start_command, self.) unless result[:exit_code].zero? logger.warning "Start command execution returned non-zero exit code:" logger.warning result.inspect end end end self.skip_ticks_for(start_grace_time) end |
#stop_process ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/bluepill/process.rb', line 255 def stop_process if stop_command cmd = process_command(stop_command) logger.warning "Executing stop command: #{cmd}" with_timeout(stop_grace_time) do result = System.execute_blocking(cmd, self.) unless result[:exit_code].zero? logger.warning "Stop command execution returned non-zero exit code:" logger.warning result.inspect end end else logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}" signal_process("TERM") end self.unlink_pid # TODO: we only write the pid file if we daemonize, should we only unlink it if we daemonize? self.skip_ticks_for(stop_grace_time) end |
#system_command_options ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/bluepill/process.rb', line 387 def { :uid => self.uid, :gid => self.gid, :working_dir => self.working_dir, :pid_file => self.pid_file, :logger => self.logger, :stdin => self.stdin, :stdout => self.stdout, :stderr => self.stderr } end |
#tick ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bluepill/process.rb', line 109 def tick return if self.skipping_ticks? self.skip_ticks_until = nil # clear the memoization per tick @process_running = nil # run state machine transitions super if self.up? run_watches if monitor_children? refresh_children! children.each {|child| child.tick} end end end |
#unlink_pid ⇒ Object
338 339 340 |
# File 'lib/bluepill/process.rb', line 338 def unlink_pid File.unlink(pid_file) if pid_file && File.exists?(pid_file) end |
#with_timeout(secs, &blk) ⇒ Object
400 401 402 403 404 405 406 407 |
# File 'lib/bluepill/process.rb', line 400 def with_timeout(secs, &blk) Timeout.timeout(secs.to_f, &blk) rescue Timeout::Error logger.err "Execution is taking longer than expected. Unmonitoring." logger.err "Did you forget to tell bluepill to daemonize this process?" self.dispatch!("unmonitor") end |