Class: RobustServer
- Inherits:
-
Object
- Object
- RobustServer
- Defined in:
- lib/robustserver.rb
Overview
Easy problem-handler for your Server.
A Server should never crash. If an Exception raised, which is not rescued, your program will shutdown abnormaly. Or if a signal tries to “kill” your program, your program will shutdown abnormaly too.
With RobustServer these errors will be a more unimportant problem and It’ll be easier to handle.
Subclasses should implements #run, which will be your main-worker. For initializing, you can override #initialize, but doen’t forget to call super.
Instance Attribute Summary collapse
-
#signals ⇒ Object
readonly
Returns the value of attribute signals.
Class Method Summary collapse
Instance Method Summary collapse
- #at_exit ⇒ Object
-
#initialize(*p) ⇒ RobustServer
constructor
A new instance of RobustServer.
- #main(max = nil, range = nil) ⇒ Object
- #signal_handler(s) ⇒ Object
- #trapping ⇒ Object
Constructor Details
#initialize(*p) ⇒ RobustServer
Returns a new instance of RobustServer.
113 114 115 116 117 118 119 120 |
# File 'lib/robustserver.rb', line 113 def initialize *p sh = method :signal_handler @sigs = { Signal[:INT] => sh, Signal[:HUP] => nil, Signal[:TERM] => sh, Signal[:KILL] => sh, Signal[:USR1] => nil, Signal[:USR2] => nil } @signals = [] end |
Instance Attribute Details
#signals ⇒ Object (readonly)
Returns the value of attribute signals.
107 108 109 |
# File 'lib/robustserver.rb', line 107 def signals @signals end |
Class Method Details
.main(*argv) ⇒ Object
109 110 111 |
# File 'lib/robustserver.rb', line 109 def self.main *argv self.new( *argv).main end |
Instance Method Details
#at_exit ⇒ Object
151 152 |
# File 'lib/robustserver.rb', line 151 def at_exit end |
#main(max = nil, range = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/robustserver.rb', line 132 def main max = nil, range = nil retries = Retries.new max, range trapping $stderr.puts "Arbeit wird nun aufgenommen..." begin self.run rescue SystemExit, Interrupt, SignalException $stderr.puts "Das Beenden des Programms wurde angefordert. #{$!}" rescue Object $stderr.puts [:rescue, $!, $!.class, $!.backtrace].inspect retry if retries.retry? $stderr.print "Zuviele Fehler in zu kurzer Zeit. Ich gebe auf und " end $stderr.puts "Unbeachtete Signale: #{@signals.map(&Signal.method(:[])).join( ', ')}" trapping self.at_exit $stderr.puts "Beende mich selbst." end |
#signal_handler(s) ⇒ Object
126 127 128 129 130 |
# File 'lib/robustserver.rb', line 126 def signal_handler s $stderr.puts [:signal, s, Signal[s]].inspect s = s @signals.push s unless @signals.include? s end |
#trapping ⇒ Object
122 123 124 |
# File 'lib/robustserver.rb', line 122 def trapping @sigs.each { |s, p| @sigs[s] = trap s, p } if @sigs end |