Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add stop_signal and stop_timeout options; fix stop string command not…

… working
  • Loading branch information...
commit 6f66cf847bcfa7c84564e1d3b1efe944173fceff 1 parent b567578
Tom Preston-Werner mojombo authored
6 History.txt
View
@@ -1,3 +1,9 @@
+==
+ * Minor Enhancements
+ * Add stop_timeout and stop_signal options to Watch
+ * Bug Fixes
+ * Stop command string was being ignored
+
== 0.9.0 / 2010-04-03
* Minor Enhancements
* Allow kqueue for OpenBSD and NetBSD
2  lib/god.rb
View
@@ -157,6 +157,8 @@ module God
DRB_ALLOW_DEFAULT = ['127.0.0.1']
LOG_LEVEL_DEFAULT = :info
TERMINATE_TIMEOUT_DEFAULT = 10
+ STOP_TIMEOUT_DEFAULT = 10
+ STOP_SIGNAL_DEFAULT = 'TERM'
class << self
# user configurable
23 lib/god/process.rb
View
@@ -2,8 +2,9 @@ module God
class Process
WRITES_PID = [:start, :restart]
- attr_accessor :name, :uid, :gid, :log, :log_cmd, :err_log, :err_log_cmd, :start, :stop, :restart,
- :unix_socket, :chroot, :env, :dir
+ attr_accessor :name, :uid, :gid, :log, :log_cmd, :err_log, :err_log_cmd,
+ :start, :stop, :restart, :unix_socket, :chroot, :env, :dir,
+ :stop_timeout, :stop_signal
def initialize
self.log = '/dev/null'
@@ -14,6 +15,8 @@ def initialize
@pid = nil
@unix_socket = nil
@log_cmd = nil
+ @stop_timeout = God::STOP_TIMEOUT_DEFAULT
+ @stop_signal = God::STOP_SIGNAL_DEFAULT
end
def alive?
@@ -203,11 +206,11 @@ def call_action(action)
command = lambda do
applog(self, :info, "#{self.name} stop: default lambda killer")
- ::Process.kill('TERM', pid) rescue nil
- applog(self, :info, "#{self.name} sent SIGTERM")
+ ::Process.kill(@stop_signal, pid) rescue nil
+ applog(self, :info, "#{self.name} sent SIG#{@stop_signal}")
# Poll to see if it's dead
- 5.times do
+ @stop_timeout.times do
begin
::Process.kill(0, pid)
rescue Errno::ESRCH
@@ -220,14 +223,14 @@ def call_action(action)
end
::Process.kill('KILL', pid) rescue nil
- applog(self, :info, "#{self.name} still alive; sent SIGKILL")
+ applog(self, :warn, "#{self.name} still alive after #{@stop_timeout}s; sent SIGKILL")
end
end
if command.kind_of?(String)
pid = nil
- if @tracking_pid
+ if [:start, :restart].include?(action) && @tracking_pid
# double fork god-daemonized processes
# we don't want to wait for them to finish
r, w = IO.pipe
@@ -326,13 +329,15 @@ def spawn(command)
#
# Returns nothing
def ensure_stop
+ applog(self, :warn, "#{self.name} ensuring stop...")
+
unless self.pid
applog(self, :warn, "#{self.name} stop called but pid is uknown")
return
end
# Poll to see if it's dead
- 10.times do
+ @stop_timeout.times do
begin
::Process.kill(0, self.pid)
rescue Errno::ESRCH
@@ -345,7 +350,7 @@ def ensure_stop
# last resort
::Process.kill('KILL', self.pid) rescue nil
- applog(self, :warn, "#{self.name} process still running 10 seconds after stop command returned. Force killing.")
+ applog(self, :warn, "#{self.name} still alive after #{@stop_timeout}s; sent SIGKILL")
end
private
11 lib/god/watch.rb
View
@@ -12,10 +12,13 @@ class Watch < Task
extend Forwardable
def_delegators :@process, :name, :uid, :gid, :start, :stop, :restart, :dir,
- :name=, :uid=, :gid=, :start=, :stop=, :restart=, :dir=,
- :pid_file, :pid_file=, :log, :log=, :log_cmd, :log_cmd=,
- :err_log, :err_log=, :err_log_cmd, :err_log_cmd=, :alive?, :pid,
- :unix_socket, :unix_socket=, :chroot, :chroot=, :env, :env=, :signal
+ :name=, :uid=, :gid=, :start=, :stop=, :restart=,
+ :dir=, :pid_file, :pid_file=, :log, :log=,
+ :log_cmd, :log_cmd=, :err_log, :err_log=,
+ :err_log_cmd, :err_log_cmd=, :alive?, :pid,
+ :unix_socket, :unix_socket=, :chroot, :chroot=,
+ :env, :env=, :signal, :stop_timeout=,
+ :stop_signal=
#
def initialize
super
12 test/configs/stop_options/simple_server.rb
View
@@ -0,0 +1,12 @@
+#! /usr/bin/env ruby
+
+trap :USR1 do
+
+end
+
+loop do
+ STDOUT.puts('server');
+ STDOUT.flush;
+
+ sleep 10
+end
39 test/configs/stop_options/stop_options.god
View
@@ -0,0 +1,39 @@
+God.watch do |w|
+ w.name = 'stop-options'
+ w.start = File.join(GOD_ROOT, *%w[test configs stop_options simple_server.rb])
+ w.stop_signal = 'USR1'
+ w.stop_timeout = 5
+ w.interval = 5
+ w.grace = 2
+
+ w.start_if do |start|
+ start.condition(:process_running) do |c|
+ c.running = false
+ end
+ end
+
+ w.restart_if do |restart|
+ restart.condition(:cpu_usage) do |c|
+ c.above = 30.percent
+ c.times = [3, 5]
+ end
+
+ restart.condition(:memory_usage) do |c|
+ c.above = 10.megabytes
+ c.times = [3, 5]
+ end
+ end
+
+ # lifecycle
+ w.lifecycle do |on|
+ on.condition(:flapping) do |c|
+ c.to_state = [:start, :restart]
+ c.times = 3
+ c.within = 60.seconds
+ c.transition = :unmonitored
+ c.retry_in = 10.seconds
+ c.retry_times = 2
+ c.retry_within = 5.minutes
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.