infopark-politics 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,8 +74,8 @@ module Politics
74
74
 
75
75
  self.group_name = name
76
76
  self.iteration_length = options[:iteration_length]
77
- @nominated_at = Time.now - self.iteration_length
78
77
  @memcache_client = client_for(Array(options[:servers]))
78
+ @nominated_at = Time.now
79
79
  # FIXME: Tests
80
80
  @domain = options[:domain]
81
81
 
@@ -96,6 +96,11 @@ module Politics
96
96
  raise ArgumentError, "You must call register_worker before processing!" unless @memcache_client
97
97
 
98
98
  begin
99
+ begin
100
+ raise "self is not alive via drb" unless DRbObject.new(nil, uri).alive?
101
+ rescue Exception => e
102
+ raise "cannot reach self via drb: #{e.message}"
103
+ end
99
104
  begin
100
105
  nominate
101
106
  if leader?
@@ -149,6 +154,10 @@ module Politics
149
154
  left > 0 ? left : 0
150
155
  end
151
156
 
157
+ def alive?
158
+ true
159
+ end
160
+
152
161
  private
153
162
 
154
163
  def bucket_process(bucket, sleep_time)
@@ -182,25 +191,7 @@ module Politics
182
191
  end
183
192
 
184
193
  def leader
185
- log.debug "trying to get the leader (#{leader_uri})"
186
- name = leader_uri
187
- repl = nil
188
- log.debug "replicas: #{replicas}"
189
- loops = 0
190
- while loops < 10 and (replicas.empty? or repl == nil)
191
- repl = replicas.detect { |replica| replica.__drburi == name }
192
- log.debug "repl: #{repl && repl.__drburi}"
193
- unless repl
194
- log.debug "scan bonjour for other nodes (replicas)"
195
- relax 1
196
- bonjour_scan do |replica|
197
- log.debug "found replica #{replica.__drburi}"
198
- replicas << replica
199
- end
200
- end
201
- loops += 1
202
- end
203
- repl || raise(DRb::DRbError.new("Could not contact leader #{leader_uri}"))
194
+ DRbObject.new(nil, leader_uri)
204
195
  end
205
196
 
206
197
  def loop?
@@ -234,8 +225,8 @@ module Politics
234
225
  # and attempting to add the token with our name attached.
235
226
  def nominate
236
227
  log.debug("try to nominate")
237
- @memcache_client.add(token, @uri, iteration_length)
238
228
  @nominated_at = Time.now
229
+ @memcache_client.add(token, @uri, iteration_length)
239
230
  @leader_uri = nil
240
231
  end
241
232
 
@@ -283,17 +274,6 @@ module Politics
283
274
  # }
284
275
  end
285
276
 
286
- def bonjour_scan
287
- Net::DNS::MDNSSD.browse("_#{group_name}._tcp") do |b|
288
- Net::DNS::MDNSSD.resolve(b.name, b.type, b.domain) do |r|
289
- yield DRbObject.new(nil, "druby://#{r.target}:#{r.port}")
290
- unless !@domain || r.target =~ /\.#{@domain}$/
291
- yield DRbObject.new(nil, "druby://#{r.target}.#{@domain}:#{r.port}")
292
- end
293
- end
294
- end
295
- end
296
-
297
277
  # http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
298
278
  def local_ip
299
279
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
@@ -305,6 +285,5 @@ module Politics
305
285
  ensure
306
286
  Socket.do_not_reverse_lookup = orig
307
287
  end
308
-
309
288
  end
310
289
  end
@@ -27,47 +27,66 @@ describe Worker do
27
27
  @worker.until_next_iteration
28
28
  end
29
29
 
30
- it "should return zero for 'until_next_iteration' if nominate was not completed" do
31
- @worker.until_next_iteration.should == 0
30
+ it "should return time to next iteration even if nominate was not completed" do
31
+ @worker.until_next_iteration.should > 0
32
+ @worker.until_next_iteration.should <= 10
32
33
  end
33
34
 
34
35
  it "should give access to the uri" do
35
36
  @worker.uri.should =~ %r(^druby://)
36
37
  end
37
38
 
39
+ it "should be alive" do
40
+ @worker.should be_alive
41
+ end
42
+
38
43
  describe Worker, "when processing bucket" do
39
44
  before do
40
- @worker.stub!(:until_next_iteration).and_return 666
41
- @worker.stub!(:nominate)
42
- @worker.should_receive(:loop?).and_return true, true, true, false
45
+ DRbObject.stub!(:new).with(nil, @worker.uri).
46
+ and_return(@worker_drb = mock('drb', :alive? => true))
43
47
  end
44
48
 
45
- it "should relax until next iteration on MemCache errors during nomination" do
46
- @worker.should_receive(:nominate).at_least(1).and_raise MemCache::MemCacheError.new("Buh!")
47
- @worker.should_receive(:relax).with(666).exactly(4).times
48
-
49
- @worker.start
49
+ it "should raise an error if it is not alive via Drb" do
50
+ @worker_drb.stub!(:alive?).and_raise("drb error")
51
+ lambda {@worker.start}.should raise_error(/cannot reach self/)
52
+ @worker_drb.stub!(:alive?).and_return(false)
53
+ lambda {@worker.start}.should raise_error(/not alive/)
50
54
  end
51
55
 
52
- it "should relax until next iteration on MemCache errors during request for leader" do
53
- @worker.should_receive(:leader_uri).at_least(1).and_raise(MemCache::MemCacheError.new("Buh!"))
54
- @worker.should_receive(:relax).with(666).exactly(4).times
56
+ describe "" do
57
+ before do
58
+ @worker.stub!(:until_next_iteration).and_return 666
59
+ @worker.stub!(:nominate)
60
+ @worker.should_receive(:loop?).and_return true, true, true, false
61
+ end
55
62
 
56
- @worker.start
57
- end
63
+ it "should relax until next iteration on MemCache errors during nomination" do
64
+ @worker.should_receive(:nominate).exactly(4).and_raise MemCache::MemCacheError.new("Buh!")
65
+ @worker.should_receive(:relax).with(666).exactly(4).times
58
66
 
59
- describe "as follower" do
60
- before do
61
- @worker.stub!(:leader?).and_return false
62
- @worker.stub!(:leader_uri).and_return "the leader"
67
+ @worker.start
63
68
  end
64
69
 
65
- it "should get the bucket to process from the leader at every iteration" do
66
- @worker.stub!(:uri).and_return("my uri")
67
- @worker.should_receive(:leader).exactly(4).times.and_return(leader = mock('leader'))
68
- leader.should_receive(:bucket_request).with("my uri").exactly(4).times.and_return([1, 2])
70
+ it "should relax until next iteration on MemCache errors during request for leader" do
71
+ @worker.should_receive(:leader_uri).exactly(4).and_raise(MemCache::MemCacheError.new("Buh"))
72
+ @worker.should_receive(:relax).with(666).exactly(4).times
73
+
69
74
  @worker.start
70
75
  end
76
+
77
+ describe "as follower" do
78
+ before do
79
+ @worker.stub!(:leader?).and_return false
80
+ @worker.stub!(:leader_uri).and_return "the leader"
81
+ end
82
+
83
+ it "should get the bucket to process from the leader at every iteration" do
84
+ @worker.should_receive(:leader).exactly(4).times.and_return(leader = mock('leader'))
85
+ leader.should_receive(:bucket_request).with(@worker.uri).exactly(4).times.
86
+ and_return([1, 2])
87
+ @worker.start
88
+ end
89
+ end
71
90
  end
72
91
  end
73
92
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infopark-politics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Perham
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-05-29 00:00:00 -07:00
13
+ date: 2009-05-30 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency