translated (english). $stderr -> output (default $stderr). #run / #loop dummies (#loop will be called by #run in a loop, if #run not overwritten. SystemExit will be reraised.
This commit is contained in:
parent
fc48ded7ba
commit
9551fbe35d
1 changed files with 24 additions and 11 deletions
|
@ -104,19 +104,19 @@ end
|
||||||
# Subclasses should implements *#run*, which will be your main-worker.
|
# Subclasses should implements *#run*, which will be your main-worker.
|
||||||
# For initializing, you can override **#initialize**, but doen't forget to call **super**.
|
# For initializing, you can override **#initialize**, but doen't forget to call **super**.
|
||||||
class RobustServer
|
class RobustServer
|
||||||
attr_reader :signals
|
attr_reader :signals, :output
|
||||||
|
|
||||||
def self.main *argv
|
def self.main *argv
|
||||||
self.new( *argv).main
|
self.new( *argv).main
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize *p
|
def initialize *_
|
||||||
sh = method :signal_handler
|
sh = method :signal_handler
|
||||||
@sigs = {
|
@sigs = {
|
||||||
Signal[:INT] => sh, Signal[:HUP] => nil, Signal[:TERM] => sh,
|
Signal[:INT] => sh, Signal[:HUP] => nil, Signal[:TERM] => sh,
|
||||||
Signal[:KILL] => sh, Signal[:USR1] => nil, Signal[:USR2] => nil
|
Signal[:KILL] => sh, Signal[:USR1] => nil, Signal[:USR2] => nil
|
||||||
}
|
}
|
||||||
@signals = []
|
@signals, @output = [], $stderr
|
||||||
end
|
end
|
||||||
|
|
||||||
def trapping
|
def trapping
|
||||||
|
@ -124,7 +124,7 @@ class RobustServer
|
||||||
end
|
end
|
||||||
|
|
||||||
def signal_handler s
|
def signal_handler s
|
||||||
$stderr.puts [:signal, s, Signal[s]].inspect
|
output.puts [:signal, s, Signal[s]].inspect
|
||||||
s = s
|
s = s
|
||||||
@signals.push s unless @signals.include? s
|
@signals.push s unless @signals.include? s
|
||||||
end
|
end
|
||||||
|
@ -132,20 +132,33 @@ class RobustServer
|
||||||
def main max = nil, range = nil
|
def main max = nil, range = nil
|
||||||
retries = Retries.new max, range
|
retries = Retries.new max, range
|
||||||
trapping
|
trapping
|
||||||
$stderr.puts "Arbeit wird nun aufgenommen..."
|
output.puts "Running...."
|
||||||
begin
|
begin
|
||||||
self.run
|
self.run
|
||||||
rescue SystemExit, Interrupt, SignalException
|
rescue SystemExit
|
||||||
$stderr.puts "Das Beenden des Programms wurde angefordert. #{$!}"
|
output.puts "Server interrupted by signal: #$!"
|
||||||
|
raise
|
||||||
|
rescue Interrupt, SignalException
|
||||||
|
output.puts "Server interrupted by signal: #$!"
|
||||||
|
exit 0
|
||||||
rescue Object
|
rescue Object
|
||||||
$stderr.puts [:rescue, $!, $!.class, $!.backtrace].inspect
|
output.puts [:rescue, $!, $!.class, $!.backtrace].inspect
|
||||||
retry if retries.retry?
|
retry if retries.retry?
|
||||||
$stderr.print "Zuviele Fehler in zu kurzer Zeit. Ich gebe auf und "
|
output.print "Too many errors in too short time. Give up: "
|
||||||
end
|
end
|
||||||
$stderr.puts "Unbeachtete Signale: #{@signals.map(&Signal.method(:[])).join( ', ')}"
|
ensure
|
||||||
|
output.puts "Disregarded signals: #{@signals.map(&Signal.method(:[])).join( ', ')}"
|
||||||
trapping
|
trapping
|
||||||
self.at_exit
|
self.at_exit
|
||||||
$stderr.puts "Beende mich selbst."
|
output.puts "Shutdown."
|
||||||
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
Kernel.loop &method( :loop)
|
||||||
|
end
|
||||||
|
|
||||||
|
def loop
|
||||||
|
raise Exception, "You must implement #{self.class.name}#run or #{self.class.name}#loop."
|
||||||
end
|
end
|
||||||
|
|
||||||
def at_exit
|
def at_exit
|
||||||
|
|
Loading…
Add table
Reference in a new issue