lib/bluepill/process.rb in bluepill-0.0.60 vs lib/bluepill/process.rb in bluepill-0.0.61
- old
+ new
@@ -3,14 +3,17 @@
# fixes problem with loading on systems with rubyist-aasm installed
gem "state_machine"
require "state_machine"
require "daemons"
+require "bluepill/system"
+require "bluepill/process_journal"
module Bluepill
class Process
CONFIGURABLE_ATTRIBUTES = [
+ :pre_start_command,
:start_command,
:stop_command,
:restart_command,
:stdout,
@@ -261,15 +264,23 @@
self.clear_pid unless @process_running
@process_running
end
def start_process
+ ProcessJournal.kill_all_from_journal(name) # be sure nothing else is running from previous runs
+ pre_start_process
logger.warning "Executing start command: #{start_command}"
-
if self.daemonize?
- System.daemonize(start_command, self.system_command_options)
-
+ daemon_id = System.daemonize(start_command, self.system_command_options)
+ if daemon_id > 0
+ ProcessJournal.append_pid_to_journal(name, daemon_id)
+ children.each {|child|
+ child_pid = child.actual_id
+ ProcessJournal.append_pid_to_journal(name, child_id)
+ } if self.monitor_children?
+ end
+ daemon_id
else
# This is a self-daemonizing process
with_timeout(start_grace_time) do
result = System.execute_blocking(start_command, self.system_command_options)
@@ -281,11 +292,27 @@
end
self.skip_ticks_for(start_grace_time)
end
+ def pre_start_process
+ return unless pre_start_command
+ logger.warning "Executing pre start command: #{pre_start_command}"
+ result = System.execute_blocking(pre_start_command, self.system_command_options)
+ unless result[:exit_code].zero?
+ logger.warning "Pre start command execution returned non-zero exit code:"
+ logger.warning result.inspect
+ end
+ end
+
def stop_process
+ if monitor_children
+ System.get_children(self.actual_pid).each do |child_pid|
+ ProcessJournal.append_pid_to_journal(name, child_pid)
+ end
+ end
+
if stop_command
cmd = self.prepare_command(stop_command)
logger.warning "Executing stop command: #{cmd}"
with_timeout(stop_grace_time) do
@@ -323,10 +350,11 @@
end
else
logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}"
signal_process("TERM")
end
+ ProcessJournal.kill_all_from_journal(name) # finish cleanup
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
@@ -402,20 +430,20 @@
pid = %x{#{pid_command}}.strip
(pid =~ /\A\d+\z/) ? pid.to_i : nil
end
def actual_pid=(pid)
+ ProcessJournal.append_pid_to_journal(name, pid) # be sure to always log the pid
@actual_pid = pid
end
def clear_pid
@actual_pid = nil
end
def unlink_pid
- File.unlink(pid_file) if pid_file && File.exists?(pid_file)
- rescue Errno::ENOENT
+ System.delete_if_exists(pid_file)
end
# Internal State Methods
def skip_ticks_for(seconds)
# TODO: should this be addative or longest wins?
@@ -438,9 +466,10 @@
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|
+ ProcessJournal.append_pid_to_journal(name, child_pid)
name = "<child(pid:#{child_pid})>"
logger = self.logger.prefix_with(name)
child = self.child_process_factory.create_child_process(name, child_pid, logger)
@children << child