Skip to content

Commit

Permalink
add stop_signal and stop_timeout options; fix stop string command not…
Browse files Browse the repository at this point in the history
… working
  • Loading branch information
mojombo committed May 18, 2010
1 parent b567578 commit 6f66cf8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
6 changes: 6 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions lib/god/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions lib/god/watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/configs/stop_options/simple_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env ruby

trap :USR1 do

end

loop do
STDOUT.puts('server');
STDOUT.flush;

sleep 10
end
39 changes: 39 additions & 0 deletions test/configs/stop_options/stop_options.god
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6f66cf8

Please sign in to comment.