robustserver 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/AUTHORS +1 -0
  2. data/VERSION +1 -1
  3. data/lib/robustserver.rb +58 -2
  4. metadata +4 -3
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Denis Knauf <denis dot knauf at gmail dot com>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/robustserver.rb CHANGED
@@ -4,6 +4,7 @@ def Signal.signame s
4
4
  when String then s
5
5
  when Symbol then s.to_s
6
6
  when Fixnum then list.invert[s]
7
+ else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}"
7
8
  end
8
9
  end
9
10
 
@@ -12,6 +13,7 @@ def Signal.sig s
12
13
  when Fixnum then s
13
14
  when String then list[s]
14
15
  when Symbol then list[s.to_s]
16
+ else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}"
15
17
  end
16
18
  end
17
19
 
@@ -25,25 +27,65 @@ def Signal.[] s
25
27
  when String then list[s]
26
28
  when Symbol then list[s.to_s]
27
29
  when Fixnum then list.invert[s]
28
- else raise ArgumentError
30
+ else raise ArgumentError, "String, Symbol or Fixnum expected, not #{s.class}"
29
31
  end
30
32
  end
31
33
 
34
+ # Description
35
+ # ===========
36
+ #
37
+ # Counts retries ot something. If the retries are to often in a short time,
38
+ # you shouldn't retry again.
39
+ #
40
+ # Examples
41
+ # ========
42
+ #
43
+ # Strings aren't Integers and 2*"Text" will raise TypeError.
44
+ #
45
+ # retries = Retry.new 5, 1
46
+ # begin
47
+ # array_of_ints_and_some_strings.each do |i|
48
+ # puts 2*i
49
+ # end
50
+ # rescue TypeError
51
+ # retries.retry? and retry
52
+ # raise $!
53
+ # end
54
+ #
55
+ # Retry.new( 10, 30).run( ConnectionLost) do
56
+ # try_to_connect_to_db
57
+ # try_query
58
+ # end
32
59
  class Retries
33
60
  attr_accessor :max, :range
34
61
  attr_reader :count, :last
35
62
 
63
+ # max: How many retries in range-time are allowed maximal.
64
+ # range: In which time-range are these retries are allowed
36
65
  def initialize max = nil, range = nil
37
66
  @max, @range, @count, @last = max || 10, range || 10, 0, Time.now
38
67
  end
39
68
 
69
+ # Counts retries on every call.
70
+ # If these retries are to often - max times in range - it will return false
71
+ # else true.
72
+ # Now you can say: "I give up, to many retries, it seems it doesn't work."
40
73
  def retry?
41
74
  @count = @last + @range > Time.now ? @count + 1 : 1
42
75
  @last = Time.now
43
76
  @count < @max
44
77
  end
45
78
 
46
- def run ex, &e
79
+ # Automatical retrieing on raised exceptions in block.
80
+ # ex: Your expected Esception you will rescue. Default: Object, so realy everything.
81
+ #
82
+ # Example:
83
+ # Retries.new( 10, 30).run ArgumentError do something_do_which_could_raise_exception ArgumentError end
84
+ #
85
+ # This will retry maximal 10 times in 30 seconds to Call this block. But only rescues ArgumentError!
86
+ # Every other Error it will ignore and throws Exception. No retry.
87
+ def run ex = nil, &e
88
+ ex ||= Object
47
89
  begin e.call *args
48
90
  rescue ex
49
91
  retries.retry? and retry
@@ -51,6 +93,16 @@ class Retries
51
93
  end
52
94
  end
53
95
 
96
+ # Easy problem-handler for your Server.
97
+ #
98
+ # A Server should never crash.
99
+ # If an Exception raised, which is not rescued, your program will shutdown abnormaly.
100
+ # Or if a signal tries to "kill" your program, your program will shutdown abnormaly too.
101
+ #
102
+ # With RobustServer these errors will be a more unimportant problem and It'll be easier to handle.
103
+ #
104
+ # Subclasses should implements *#run*, which will be your main-worker.
105
+ # For initializing, you can override **#initialize**, but doen't forget to call **super**.
54
106
  class RobustServer
55
107
  attr_reader :signals
56
108
 
@@ -92,6 +144,10 @@ class RobustServer
92
144
  end
93
145
  $stderr.puts "Unbeachtete Signale: #{@signals.map(&Signal.method(:[])).join( ', ')}"
94
146
  trapping
147
+ self.at_exit
95
148
  $stderr.puts "Beende mich selbst."
96
149
  end
150
+
151
+ def at_exit
152
+ end
97
153
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Denis Knauf
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +01:00
17
+ date: 2010-03-21 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,6 +28,7 @@ extra_rdoc_files:
28
28
  - LICENSE
29
29
  - README.md
30
30
  files:
31
+ - AUTHORS
31
32
  - README.md
32
33
  - VERSION
33
34
  - lib/robustserver.rb