diff --git a/.gitignore b/.gitignore deleted file mode 100644 index cf08465..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.*.swp -pkg diff --git a/AUTHORS b/AUTHOR similarity index 100% rename from AUTHORS rename to AUTHOR diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 1934d03..0000000 --- a/Gemfile +++ /dev/null @@ -1,12 +0,0 @@ -source "http://rubygems.org" - -# Add dependencies to develop your gem here. -# Include everything needed to run rake, tests, features, etc. -group :development do - gem "shoulda" - gem "yard" - gem "rdoc" - gem "bundler" - gem "jeweler" - gem "simplecov" -end diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 4f0c120..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,46 +0,0 @@ -GEM - remote: http://rubygems.org/ - specs: - activesupport (3.2.13) - i18n (= 0.6.1) - multi_json (~> 1.0) - bourne (1.4.0) - mocha (~> 0.13.2) - git (1.2.5) - i18n (0.6.1) - jeweler (1.8.4) - bundler (~> 1.0) - git (>= 1.2.5) - rake - rdoc - json (1.7.7) - metaclass (0.0.1) - mocha (0.13.3) - metaclass (~> 0.0.1) - multi_json (1.7.2) - rake (10.0.4) - rdoc (4.0.1) - json (~> 1.4) - shoulda (3.4.0) - shoulda-context (~> 1.0, >= 1.0.1) - shoulda-matchers (~> 1.0, >= 1.4.1) - shoulda-context (1.1.0) - shoulda-matchers (1.5.6) - activesupport (>= 3.0.0) - bourne (~> 1.3) - simplecov (0.7.1) - multi_json (~> 1.0) - simplecov-html (~> 0.7.1) - simplecov-html (0.7.1) - yard (0.8.5.2) - -PLATFORMS - ruby - -DEPENDENCIES - bundler - jeweler - rdoc - shoulda - simplecov - yard diff --git a/Rakefile b/Rakefile index 06cfb4f..00f3418 100644 --- a/Rakefile +++ b/Rakefile @@ -1,49 +1,57 @@ -# encoding: utf-8 - require 'rubygems' -require 'bundler' -begin - Bundler.setup(:default, :development) -rescue Bundler::BundlerError => e - $stderr.puts e.message - $stderr.puts "Run `bundle install` to install missing gems" - exit e.status_code -end require 'rake' -require 'jeweler' -Jeweler::Tasks.new do |gem| - # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options - gem.name = "robustserver" - gem.summary = %Q{Robust Server} - gem.description = %Q{Protects your Server against SIGS and rescues all exceptions.} - gem.email = "Denis.Knauf@gmail.com" - gem.homepage = "http://github.com/DenisKnauf/robustserver" - gem.authors = ["Denis Knauf"] - gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb] - gem.require_paths = %w[lib] - # dependencies defined in Gemfile +begin + require 'jeweler' + Jeweler::Tasks.new do |gem| + gem.name = "robustserver" + gem.summary = %Q{Robust Server} + gem.description = %Q{Protects your Server against SIGS and unplaned exceptions} + gem.email = "Denis.Knauf@gmail.com" + gem.homepage = "http://github.com/DenisKnauf/robustserver" + gem.authors = ["Denis Knauf"] + gem.files = ["AUTHORS", "README.md", "VERSION", "lib/**/*.rb", "test/**/*.rb"] + gem.require_paths = ["lib"] + end + Jeweler::GemcutterTasks.new +rescue LoadError + puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" end -Jeweler::RubygemsDotOrgTasks.new require 'rake/testtask' Rake::TestTask.new(:test) do |test| - test.libs << 'lib' << 'test' - test.pattern = 'test/**/test_*.rb' + test.libs << 'lib' << 'test' << 'ext' + test.pattern = 'test/**/*_test.rb' test.verbose = true end -=begin -require 'rcov/rcovtask' -Rcov::RcovTask.new do |test| - test.libs << 'test' - test.pattern = 'test/**/test_*.rb' - test.verbose = true - test.rcov_opts << '--exclude "gems/*"' +begin + require 'rcov/rcovtask' + Rcov::RcovTask.new do |test| + test.libs << 'test' + test.pattern = 'test/**/*_test.rb' + test.verbose = true + end +rescue LoadError + task :rcov do + abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" + end end -=end + +task :test => :check_dependencies task :default => :test -require 'yard' -YARD::Rake::YardocTask.new +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + if File.exist?('VERSION') + version = File.read('VERSION') + else + version = "" + end + + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "sbdb #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/VERSION b/VERSION index 6812f81..77d6f4c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.3 \ No newline at end of file +0.0.0 diff --git a/lib/robustserver.rb b/lib/robustserver.rb index 91fc1d9..8a63a4d 100644 --- a/lib/robustserver.rb +++ b/lib/robustserver.rb @@ -4,7 +4,6 @@ def Signal.signame s when String then s when Symbol then s.to_s when Fixnum then list.invert[s] - else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}" end end @@ -13,7 +12,6 @@ def Signal.sig s when Fixnum then s when String then list[s] when Symbol then list[s.to_s] - else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}" end end @@ -27,65 +25,25 @@ def Signal.[] s when String then list[s] when Symbol then list[s.to_s] when Fixnum then list.invert[s] - else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}" + else raise ArgumentError end end -# Description -# =========== -# -# Counts retries ot something. If the retries are to often in a short time, -# you shouldn't retry again. -# -# Examples -# ======== -# -# Strings aren't Integers and 2*"Text" will raise TypeError. -# -# retries = Retry.new 5, 1 -# begin -# array_of_ints_and_some_strings.each do |i| -# puts 2*i -# end -# rescue TypeError -# retries.retry? and retry -# raise $! -# end -# -# Retry.new( 10, 30).run( ConnectionLost) do -# try_to_connect_to_db -# try_query -# end class Retries attr_accessor :max, :range attr_reader :count, :last - # max: How many retries in range-time are allowed maximal. - # range: In which time-range are these retries are allowed - def initialize max = nil, range = nil - @max, @range, @count, @last = max || 10, range || 10, 0, Time.now + def initialize max = 10, range = 10 + @max, @range, @count, @last = max, range, 0, Time.now end - # Counts retries on every call. - # If these retries are to often - max times in range - it will return false - # else true. - # Now you can say: "I give up, to many retries, it seems it doesn't work." def retry? @count = @last + @range > Time.now ? @count + 1 : 1 @last = Time.now @count < @max end - # Automatical retrieing on raised exceptions in block. - # ex: Your expected Esception you will rescue. Default: Object, so realy everything. - # - # Example: - # Retries.new( 10, 30).run ArgumentError do something_do_which_could_raise_exception ArgumentError end - # - # This will retry maximal 10 times in 30 seconds to Call this block. But only rescues ArgumentError! - # Every other Error it will ignore and throws Exception. No retry. - def run ex = nil, &e - ex ||= Object + def run ex, &e begin e.call *args rescue ex retries.retry? and retry @@ -93,33 +51,17 @@ class Retries end end -# Easy problem-handler for your Server. -# -# Problem: Your process should not crash, it should be available for anytime. -# If an exception will be raised, you have to handle it, or it will be shutdown abnormaly. -# Or if a signal tries to "kill" your program, your program will shutdown abnormaly, too. -# -# RobustServer handles any exception / kill, logs it and prevents the server to shutting down. -# -# For implementation a server, create a subclass of RobustServer and implement the *#run*-method. -# Anytime this *#run*-method returns or an exception will be raised, it will be rescued and run recalled. -# For initialization, you can override **#initialize**, but don't forget to call **super**. class RobustServer - class UnimplementedRun sh, Signal[:HUP] => nil, Signal[:TERM] => sh, Signal[:KILL] => sh, Signal[:USR1] => nil, Signal[:USR2] => nil } - @signals, @output = [], $stderr end def trapping @@ -127,42 +69,23 @@ class RobustServer end def signal_handler s - output.puts [:signal, s, Signal[s]].inspect - @signals.push s unless @signals.include? s + @signal = s end def main max = nil, range = nil retries = Retries.new max, range trapping - output.puts "Running...." + $stderr.puts "Arbeit wird nun aufgenommen..." begin self.run - rescue UnimplementedRun - output.puts $!.message - exit 1 - rescue SystemExit - output.puts "Server interrupted by signal: #$!" - raise - rescue Interrupt, SignalException - output.puts "Server interrupted by signal: #$!" - exit 0 + rescue SystemExit, Interrupt, SignalException + $stderr.puts "Das Beenden des Programms wurde angefordert. #{$!}" rescue Object - output.puts [:rescue, $!, $!.class, $!.backtrace].inspect + $stderr.puts [:rescue, $!, $!.class, $!.backtrace].inspect retry if retries.retry? - output.print "Too many errors in too short time. Give up: " - exit 2 + $stderr.print "Zuviele Fehler in zu kurzer Zeit. Ich gebe auf und " end - ensure - output.puts "Disregarded signals: #{@signals.map(&Signal.method(:[])).join( ', ')}" unless @signals.empty? trapping - self.at_exit - output.puts "Shutdown." - end - - def run - raise UnimplementedRun, "Unimplemented #{self.class.name}#run." - end - - def at_exit + $stderr.puts "Beende mich selbst." end end diff --git a/robustserver.gemspec b/robustserver.gemspec index 7ffa2ee..e6892d2 100644 --- a/robustserver.gemspec +++ b/robustserver.gemspec @@ -1,57 +1,40 @@ # Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command # -*- encoding: utf-8 -*- Gem::Specification.new do |s| - s.name = "robustserver" - s.version = "0.0.3" + s.name = %q{robustserver} + s.version = "0.0.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Denis Knauf"] - s.date = "2013-04-15" - s.description = "Protects your Server against SIGS and rescues all exceptions." - s.email = "Denis.Knauf@gmail.com" + s.date = %q{2010-03-02} + s.description = %q{Protects your Server against SIGS and unplaned exceptions} + s.email = %q{Denis.Knauf@gmail.com} s.extra_rdoc_files = [ "LICENSE", - "README.md" + "README.md" ] s.files = [ - "AUTHORS", "README.md", - "VERSION", - "lib/robustserver.rb" + "VERSION", + "lib/robustserver.rb" ] - s.homepage = "http://github.com/DenisKnauf/robustserver" + s.homepage = %q{http://github.com/DenisKnauf/robustserver} + s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubygems_version = "1.8.23" - s.summary = "Robust Server" + s.rubygems_version = %q{1.3.5} + s.summary = %q{Robust Server} if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 - if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then else - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) end else - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) end end