mperham-politics 0.2.0 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.rdoc +19 -1
- data/README.rdoc +6 -1
- data/examples/queue_worker_example.rb +37 -0
- data/examples/token_worker_example.rb +4 -4
- data/lib/politics/static_queue_worker.rb +6 -7
- data/lib/politics/token_worker.rb +5 -7
- data/lib/politics/version.rb +1 -1
- data/lib/politics.rb +6 -3
- metadata +2 -2
data/History.rdoc
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
= Changelog
|
|
2
2
|
|
|
3
|
-
== 0.2.
|
|
3
|
+
== 0.2.4 (2009-01-28)
|
|
4
|
+
|
|
5
|
+
* Reduce leader token expiration time to discourage a get/set race condition. (Brian Dainton)
|
|
6
|
+
|
|
7
|
+
== 0.2.3 (2009-01-12)
|
|
8
|
+
|
|
9
|
+
* Fix invalid result check in previous change. (Brian Dainton)
|
|
10
|
+
|
|
11
|
+
== 0.2.2 (2009-01-07)
|
|
12
|
+
|
|
13
|
+
* Fix invalid leader? logic in TokenWorker which could allow
|
|
14
|
+
two workers to become leader at the same time. (Brian Dainton)
|
|
15
|
+
|
|
16
|
+
== 0.2.1 (2008-11-04)
|
|
17
|
+
|
|
18
|
+
* Cleanup and prepare for public release for RubyConf 2008.
|
|
19
|
+
* Election Day. Politics. Get it? Hee hee.
|
|
20
|
+
|
|
21
|
+
== 0.2.0 (2008-10-24)
|
|
4
22
|
|
|
5
23
|
* Remove BucketWorker based on initial feedback. Add StaticQueueWorker as a more reliable replacement.
|
|
6
24
|
|
data/README.rdoc
CHANGED
|
@@ -21,7 +21,11 @@ toes? There are several answers:
|
|
|
21
21
|
1. Elect a leader for a short period of time. The leader is the process which performs the
|
|
22
22
|
actual processing. After a length of time, a new leader is elected from the group. This
|
|
23
23
|
is fault tolerant but not as scalable, as only one process is performing the task at a given
|
|
24
|
-
point in time. See the +TokenWorker+
|
|
24
|
+
point in time. See the +TokenWorker+ mixin.
|
|
25
|
+
|
|
26
|
+
== Installation
|
|
27
|
+
|
|
28
|
+
sudo gem install mperham-politics -s http://gems.github.com
|
|
25
29
|
|
|
26
30
|
== Dependencies
|
|
27
31
|
|
|
@@ -39,6 +43,7 @@ TokenWorker mixin
|
|
|
39
43
|
Name:: Mike Perham
|
|
40
44
|
Email:: mailto:mperham@gmail.com
|
|
41
45
|
Twitter:: http://twitter.com/mperham
|
|
46
|
+
Homepage:: http://mikeperham.com/
|
|
42
47
|
|
|
43
48
|
This software is free for you to use as you'd like. If you find it useful, please consider giving
|
|
44
49
|
me a recommendation at {Working with Rails}[http://workingwithrails.com/person/10797-mike-perham].
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#gem 'mperham-politics'
|
|
2
|
+
require 'politics'
|
|
3
|
+
require 'politics/static_queue_worker'
|
|
4
|
+
|
|
5
|
+
# Test this example by starting memcached locally and then in two irb sessions, run this:
|
|
6
|
+
#
|
|
7
|
+
=begin
|
|
8
|
+
require 'queue_worker_example'
|
|
9
|
+
p = Politics::QueueWorkerExample.new
|
|
10
|
+
p.start
|
|
11
|
+
=end
|
|
12
|
+
#
|
|
13
|
+
# You can then watch as one of them is elected leader. You can kill the leader and verify
|
|
14
|
+
# the backup process is elected after approximately iteration_length seconds.
|
|
15
|
+
#
|
|
16
|
+
module Politics
|
|
17
|
+
class QueueWorkerExample
|
|
18
|
+
include Politics::StaticQueueWorker
|
|
19
|
+
TOTAL_BUCKETS = 20
|
|
20
|
+
|
|
21
|
+
def initialize
|
|
22
|
+
register_worker 'queue-example', TOTAL_BUCKETS, :iteration_length => 60, :servers => memcached_servers
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def start
|
|
26
|
+
process_bucket do |bucket|
|
|
27
|
+
puts "PID #{$$} processing bucket #{bucket}/#{TOTAL_BUCKETS} at #{Time.now}..."
|
|
28
|
+
sleep 1.5
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def memcached_servers
|
|
33
|
+
['127.0.0.1:11211']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -4,15 +4,15 @@ require 'politics/token_worker'
|
|
|
4
4
|
|
|
5
5
|
# Test this example by starting memcached locally and then in two irb sessions, run this:
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
# You can then watch as one of them is elected leader. You can kill the leader and verify
|
|
9
|
-
# the backup process is elected after approximately iteration_length seconds.
|
|
10
|
-
#
|
|
11
7
|
=begin
|
|
12
8
|
require 'token_worker_example'
|
|
13
9
|
p = Politics::TokenWorkerExample.new
|
|
14
10
|
p.start
|
|
15
11
|
=end
|
|
12
|
+
#
|
|
13
|
+
# You can then watch as one of them is elected leader. You can kill the leader and verify
|
|
14
|
+
# the backup process is elected after approximately iteration_length seconds.
|
|
15
|
+
#
|
|
16
16
|
module Politics
|
|
17
17
|
class TokenWorkerExample
|
|
18
18
|
include Politics::TokenWorker
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
puts 'hello'
|
|
2
1
|
require 'socket'
|
|
3
2
|
require 'ipaddr'
|
|
4
3
|
require 'uri'
|
|
@@ -235,12 +234,12 @@ module Politics
|
|
|
235
234
|
handle = Net::DNS::MDNSSD.register("#{self.group_name}-#{local_ip}-#{$$}",
|
|
236
235
|
"_#{group_name}._tcp", 'local', @port)
|
|
237
236
|
|
|
238
|
-
['INT', 'TERM'].each { |signal|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
237
|
+
# ['INT', 'TERM'].each { |signal|
|
|
238
|
+
# trap(signal) do
|
|
239
|
+
# handle.stop
|
|
240
|
+
# server.stop_service
|
|
241
|
+
# end
|
|
242
|
+
# }
|
|
244
243
|
end
|
|
245
244
|
|
|
246
245
|
def bonjour_scan
|
|
@@ -142,17 +142,15 @@ module Politics
|
|
|
142
142
|
|
|
143
143
|
# Nominate ourself as leader by contacting the memcached server
|
|
144
144
|
# and attempting to add the token with our name attached.
|
|
145
|
+
# The result will tell us if memcached stored our value and therefore
|
|
146
|
+
# if we are now leader.
|
|
145
147
|
def nominate
|
|
146
|
-
memcache_client.add(token, worker_name, iteration_length)
|
|
148
|
+
result = memcache_client.add(token, worker_name, (iteration_length * 0.9).to_i)
|
|
149
|
+
@leader = (result =~ /\ASTORED/)
|
|
147
150
|
end
|
|
148
151
|
|
|
149
|
-
# Check to see if we are leader by looking at the process name
|
|
150
|
-
# associated with the token.
|
|
151
152
|
def leader?
|
|
152
|
-
@leader
|
|
153
|
-
master_worker = memcache_client.get(token)
|
|
154
|
-
worker_name == master_worker
|
|
155
|
-
end
|
|
153
|
+
@leader
|
|
156
154
|
end
|
|
157
155
|
|
|
158
156
|
# Easy to mock or monkey-patch if another MemCache client is preferred.
|
data/lib/politics/version.rb
CHANGED
data/lib/politics.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'logger'
|
|
2
|
-
|
|
3
1
|
module Politics
|
|
4
2
|
|
|
5
3
|
def self.log=(value)
|
|
@@ -7,6 +5,11 @@ module Politics
|
|
|
7
5
|
end
|
|
8
6
|
|
|
9
7
|
def self.log
|
|
10
|
-
@log ||=
|
|
8
|
+
@log ||= if defined?(RAILS_DEFAULT_LOGGER)
|
|
9
|
+
RAILS_DEFAULT_LOGGER
|
|
10
|
+
else
|
|
11
|
+
require 'logger'
|
|
12
|
+
Logger.new(STDOUT)
|
|
13
|
+
end
|
|
11
14
|
end
|
|
12
15
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mperham-politics
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike Perham
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date:
|
|
12
|
+
date: 2009-01-11 00:00:00 -08:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|