lib/bluepill/process.rb in bluepill-0.0.19 vs lib/bluepill/process.rb in bluepill-0.0.20

- old
+ new

@@ -229,37 +229,41 @@ def start_process logger.warning "Executing start command: #{start_command}" if self.daemonize? - child_pid = System.daemonize(start_command, self.system_command_options) - File.open(pid_file, "w") {|f| f.write(child_pid)} + System.daemonize(start_command, self.system_command_options) else # This is a self-daemonizing process - result = System.execute_blocking(start_command, self.system_command_options) - - unless result[:exit_code].zero? - logger.warning "Start command execution returned non-zero exit code:" - logger.warning result.inspect + with_timeout(start_grace_time) do + result = System.execute_blocking(start_command, self.system_command_options) + + 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 def stop_process if stop_command cmd = process_command(stop_command) logger.warning "Executing stop command: #{cmd}" - - result = System.execute_blocking(cmd, self.system_command_options) - unless result[:exit_code].zero? - logger.warning "Stop command execution returned non-zero exit code:" - logger.warning result.inspect - end + with_timeout(stop_grace_time) do + result = System.execute_blocking(cmd, self.system_command_options) + + 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? @@ -271,15 +275,17 @@ if restart_command cmd = process_command(restart_command) logger.warning "Executing restart command: #{cmd}" - result = System.execute_blocking(cmd, self.system_command_options) - - unless result[:exit_code].zero? - logger.warning "Restart command execution returned non-zero exit code:" - logger.warning result.inspect + with_timeout(restart_grace_time) do + result = System.execute_blocking(cmd, self.system_command_options) + + 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" @@ -372,10 +378,25 @@ def process_command(cmd) cmd.to_s.gsub("{{PID}}", actual_pid.to_s) end def system_command_options - {:uid => self.uid, :gid => self.gid, :working_dir => self.working_dir, :logger => self.logger} + { + :uid => self.uid, + :gid => self.gid, + :working_dir => self.working_dir, + :pid_file => self.pid_file, + :logger => self.logger + } + end + + 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 end end \ No newline at end of file