Skip to content

Commit

Permalink
== Release 1.1.10: June 26, 2013
Browse files Browse the repository at this point in the history
* fix Pid.running? rescuing timeout exceptions (thanks to Geraud Boyer)
* monitor.rb/application.rb/application_group.rb: handle :monitor and :multiple in combination correctly
  (thanks to Prakash Murthy).
  • Loading branch information
thuehlinger committed Jun 26, 2013
1 parent 57ddcfe commit 2dcef4d
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 104 deletions.
8 changes: 4 additions & 4 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Daemons Version 1.1.9
= Daemons Version 1.1.10

(See Releases for release-specific information)

Expand Down Expand Up @@ -181,12 +181,12 @@ The RDoc documentation is also online at http://daemons.rubyforge.org

== Author

Written 2005-2010 by Thomas Uehlinger <mailto:th.uehlinger@gmx.ch>.
Written 2005-2013 by Thomas Uehlinger <mailto:th.uehlinger@gmx.ch>.
Anonymous SVN checkout is available with "svn checkout http://daemons.rubyforge.org/svn/".

== License

Copyright (c) 2005-2012 Thomas Uehlinger
Copyright (c) 2005-2013 Thomas Uehlinger

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand All @@ -211,4 +211,4 @@ OTHER DEALINGS IN THE SOFTWARE.

== Feedback and other resources

At http://rubyforge.org/projects/daemons.
At http://rubyforge.org/projects/daemons .
6 changes: 6 additions & 0 deletions Releases
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
= Daemons Release History

== Release 1.1.10: June 26, 2013

* fix Pid.running? rescuing timeout exceptions (thanks to Geraud Boyer)
* monitor.rb/application.rb/application_group.rb: handle :monitor and :multiple in combination correctly
(thanks to Prakash Murthy).

== Release 1.1.9: August 10, 2012

* daemonize.rb: do srand in the forked child process both in daemonize and call_as_daemon
Expand Down
4 changes: 2 additions & 2 deletions daemons.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Gem::Specification.new do |s|
s.name = %q{daemons}


s.version = "1.1.9"
s.date = %q{2012-08-10}
s.version = "1.1.10"
s.date = %q{2013-06-26}


s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
Expand Down
18 changes: 18 additions & 0 deletions examples/run/ctrl_monitor_multiple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))

if File.exist?(File.join(lib_dir, 'daemons.rb'))
$LOAD_PATH.unshift lib_dir
else
begin; require 'rubygems'; rescue ::Exception; end
end

require 'daemons'


options = {
:multiple => true,
:monitor => true,
:log_output => true,
}

Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options)
4 changes: 2 additions & 2 deletions examples/run/myserver_crashing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

loop do
puts 'ping from myserver.rb!'
puts 'this example server will crash in 3 seconds...'
puts 'this example server will crash in 10 seconds...'

sleep(3)
sleep(10)

puts 'CRASH!'
raise 'CRASH!'
Expand Down
35 changes: 0 additions & 35 deletions examples/run/myserver_crashing.rb.log

This file was deleted.

30 changes: 0 additions & 30 deletions examples/run/myserver_crashing.rb.output

This file was deleted.

4 changes: 1 addition & 3 deletions lib/daemons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
require 'daemons/exceptions'
require 'daemons/monitor'


require 'daemons/application'
require 'daemons/application_group'
require 'daemons/controller'

require 'timeout'

# All functions and classes that Daemons provides reside in this module.
#
Expand Down Expand Up @@ -66,7 +64,7 @@
#
module Daemons

VERSION = "1.1.9"
VERSION = "1.1.10"

require 'daemons/daemonize'

Expand Down
16 changes: 10 additions & 6 deletions lib/daemons/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'daemons/pidmem'
require 'daemons/change_privilege'
require 'daemons/daemonize'
require 'daemons/exceptions'

require 'timeout'

Expand Down Expand Up @@ -280,10 +281,13 @@ def start_proc
end


def start
def start(restart = false)
change_privilege
@group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground

if not restart
@group.create_monitor(self) unless options[:ontop] # we don't monitor applications in the foreground
end

case options[:mode]
when :none
# this is only used to daemonize the currently running process
Expand Down Expand Up @@ -402,12 +406,12 @@ def stop(no_wait = false)
STDOUT.flush

begin
Timeout::timeout(@force_kill_waittime) {
Timeout::timeout(@force_kill_waittime, TimeoutError) {
while Pid.running?(pid)
sleep(0.2)
end
}
rescue Timeout::Error
rescue TimeoutError
puts "#{self.group.app_name}: process with pid #{pid} won't stop, we forcefully kill it..."
STDOUT.flush

Expand All @@ -417,12 +421,12 @@ def stop(no_wait = false)
end

begin
Timeout::timeout(20) {
Timeout::timeout(20, TimeoutError) {
while Pid.running?(pid)
sleep(1)
end
}
rescue Timeout::Error
rescue TimeoutError
puts "#{self.group.app_name}: unable to forcefully kill process with pid #{pid}."
STDOUT.flush
end
Expand Down
14 changes: 10 additions & 4 deletions lib/daemons/application_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ def setup_app(app)
private :setup_app

def create_monitor(an_app)
return if @monitor
if @monitor and options[:monitor]
@monitor.stop
@monitor = nil
end

if options[:monitor]
@monitor = Monitor.new(an_app)

@monitor.start(@applications)
@monitor.start(self)
end
end

Expand All @@ -162,7 +164,11 @@ def start_all
end

def stop_all(no_wait = false)
@monitor.stop if @monitor
if @monitor
@monitor.stop
@monitor = nil
setup
end

threads = []

Expand Down
3 changes: 3 additions & 0 deletions lib/daemons/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ def initialize(msg, system_error)

end

class TimeoutError < Error
end

end
40 changes: 23 additions & 17 deletions lib/daemons/monitor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'daemons/exceptions'


module Daemons

Expand Down Expand Up @@ -38,29 +40,33 @@ def initialize(an_app)
end
end

def watch(applications)
sleep(30)
def watch(application_group)
sleep(5)

loop do
applications.each {|a|
sleep(10)

application_group.applications.each {|a|
unless a.running?
a.zap!

Process.detach(fork { a.start })
sleep(1)

Process.detach(fork { a.start(restart=true) })

sleep(5)

sleep(10)
#application_group.setup
end
}

sleep(30)
#sleep(5)
#application_group.setup
#sleep(30)
end
end
private :watch


def start_with_pidfile(applications)
def start_with_pidfile(application_group)
fork do
Daemonize.daemonize(nil, @app_name)

Expand All @@ -81,7 +87,7 @@ def start_with_pidfile(applications)
# exit
# }

watch(applications)
watch(application_group)
rescue ::Exception => e
begin
File.open(@app.logfile, 'a') {|f|
Expand All @@ -98,19 +104,19 @@ def start_with_pidfile(applications)
end
private :start_with_pidfile

def start_without_pidfile(applications)
Thread.new { watch(applications) }
def start_without_pidfile(application_group)
Thread.new { watch(application_group) }
end
private :start_without_pidfile


def start(applications)
return if applications.empty?
def start(application_group)
return if application_group.applications.empty?

if @pid.kind_of?(PidFile)
start_with_pidfile(applications)
start_with_pidfile(application_group)
else
start_without_pidfile(applications)
start_without_pidfile(application_group)
end
end

Expand All @@ -119,7 +125,7 @@ def stop
begin
pid = @pid.pid
Process.kill(Application::SIGNAL, pid)
Timeout::timeout(5) {
Timeout::timeout(5, TimeoutError) {
while Pid.running?(pid)
sleep(0.1)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/daemons/pid.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'daemons/exceptions'


module Daemons
Expand All @@ -14,6 +15,8 @@ def Pid.running?(pid)
begin
Process.kill(0, pid)
return true
rescue TimeoutError
raise
rescue Errno::ESRCH
return false
rescue ::Exception # for example on EPERM (process exists but does not belong to us)
Expand Down
2 changes: 1 addition & 1 deletion lib/daemons/pidfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def initialize(dir, progname, multiple = false)
@number += 1
end

if @number == 1024
if @number >= 1024
raise RuntimeException('cannot run more than 1024 instances of the application')
end
end
Expand Down

0 comments on commit 2dcef4d

Please sign in to comment.