lib/bluepill/process.rb in bluepill-0.0.36 vs lib/bluepill/process.rb in bluepill-0.0.37
- old
+ new
@@ -91,22 +91,20 @@
@watches = []
@triggers = []
@children = []
@statistics = ProcessStatistics.new
- @monitor_children = options[:monitor_children] || false
+ # These defaults are overriden below if it's configured to be something else.
+ @monitor_children = false
+ @start_grace_time = @stop_grace_time = @restart_grace_time = 3
- %w(start_grace_time stop_grace_time restart_grace_time).each do |grace|
- instance_variable_set("@#{grace}", options[grace.to_sym] || 3)
- end
-
CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
self.send("#{attribute_name}=", options[attribute_name]) if options.has_key?(attribute_name)
end
-
+
# Let state_machine do its initialization stuff
- super()
+ super() # no arguments intentional
end
def tick
return if self.skipping_ticks?
self.skip_ticks_until = nil
@@ -116,13 +114,13 @@
# run state machine transitions
super
if self.up?
- run_watches
+ self.run_watches
- if monitor_children?
+ if self.monitor_children?
refresh_children!
children.each {|child| child.tick}
end
end
end
@@ -192,47 +190,46 @@
break if @transitioned
self.dispatch!(event, reason)
end
end
+ def determine_initial_state
+ if self.process_running?(true)
+ self.state = 'up'
+ else
+ # TODO: or "unmonitored" if bluepill was started in no auto-start mode.
+ self.state = 'down'
+ end
+ end
+
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'
+ when "start"
+ if self.process_running?(true)
+ logger.warning("Refusing to re-run start command on an already running process.")
else
- self.state = 'starting'
+ dispatch!(:start, "user initiated")
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, "user initiated")
-
when "stop"
stop_process
dispatch!(:unmonitor, "user initiated")
-
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, "user initiated")
end
end
# System Process Methods
def process_running?(force = false)
- @process_running = nil if force
+ @process_running = nil if force # clear existing state if forced
+
@process_running ||= signal_process(0)
+ # the process isn't running, so we should clear the PID
self.clear_pid unless @process_running
@process_running
end
def start_process
@@ -256,11 +253,11 @@
self.skip_ticks_for(start_grace_time)
end
def stop_process
if stop_command
- cmd = process_command(stop_command)
+ cmd = self.prepare_command(stop_command)
logger.warning "Executing stop command: #{cmd}"
with_timeout(stop_grace_time) do
result = System.execute_blocking(cmd, self.system_command_options)
@@ -279,11 +276,11 @@
self.skip_ticks_for(stop_grace_time)
end
def restart_process
if restart_command
- cmd = process_command(restart_command)
+ cmd = self.prepare_command(restart_command)
logger.warning "Executing restart command: #{cmd}"
with_timeout(restart_grace_time) do
result = System.execute_blocking(cmd, self.system_command_options)
@@ -382,11 +379,11 @@
def deep_copy
Marshal.load(Marshal.dump(self))
end
- def process_command(cmd)
- cmd.to_s.gsub("{{PID}}", actual_pid.to_s)
+ def prepare_command(command)
+ command.to_s.gsub("{{PID}}", actual_pid.to_s)
end
def system_command_options
{
:uid => self.uid,
\ No newline at end of file