elected 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/elected.gemspec +1 -0
- data/lib/elected.rb +2 -0
- data/lib/elected/core_ext.rb +46 -0
- data/lib/elected/senado.rb +22 -7
- data/lib/elected/stats.rb +46 -0
- data/lib/elected/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e44506af127e794e43d83b9b84725d455da6d66a
|
4
|
+
data.tar.gz: 456a3e9970c362014b05bd10abefcf8d48142840
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3089d6d14ca943641136c04e313bfaf2e5404211fd847670313c3c57efd80379678d07299303d5407e9a17b44191093ad87d477fd9f13c93f73c6191b5b283da
|
7
|
+
data.tar.gz: f27b522321802aa0fe9568d751e923d626c8622bdc47fe8debca93615d1993e6505a26050ed06951f8b86327b43f3d396d3bb1ef2a09e43b5a706de2e51ac334
|
data/elected.gemspec
CHANGED
data/lib/elected.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Try taken from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# Return a list of methods defined locally for a particular object. Useful
|
5
|
+
# for seeing what it does whilst losing all the guff that's implemented
|
6
|
+
# by its parents (eg Object).
|
7
|
+
def local_methods(obj = self)
|
8
|
+
(obj.methods - obj.class.superclass.instance_methods).sort
|
9
|
+
end unless Object.respond_to?(:local_methods)
|
10
|
+
|
11
|
+
def try(*a, &b)
|
12
|
+
try!(*a, &b) if a.empty? || respond_to?(a.first)
|
13
|
+
end unless Object.respond_to?(:try)
|
14
|
+
|
15
|
+
def try!(*a, &b)
|
16
|
+
if a.empty? && block_given?
|
17
|
+
if b.arity.zero?
|
18
|
+
instance_eval(&b)
|
19
|
+
else
|
20
|
+
yield self
|
21
|
+
end
|
22
|
+
else
|
23
|
+
public_send(*a, &b)
|
24
|
+
end
|
25
|
+
end unless Object.respond_to?(:try!)
|
26
|
+
end
|
27
|
+
|
28
|
+
class NilClass
|
29
|
+
def try(*args)
|
30
|
+
nil
|
31
|
+
end unless NilClass.respond_to?(:try)
|
32
|
+
|
33
|
+
def try!(*args)
|
34
|
+
nil
|
35
|
+
end unless NilClass.respond_to?(:try!)
|
36
|
+
end
|
37
|
+
|
38
|
+
class String
|
39
|
+
def underscore
|
40
|
+
self.gsub(/::/, '/').
|
41
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
42
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
43
|
+
tr('-', '_').
|
44
|
+
downcase
|
45
|
+
end unless ''.respond_to?(:underscore)
|
46
|
+
end
|
data/lib/elected/senado.rb
CHANGED
@@ -5,10 +5,13 @@ module Elected
|
|
5
5
|
|
6
6
|
include Logging
|
7
7
|
|
8
|
-
|
8
|
+
attr_writer :key, :timeout
|
9
|
+
attr_reader :stats
|
9
10
|
|
10
11
|
def initialize(key = nil, timeout = nil)
|
11
|
-
@key
|
12
|
+
@key = key
|
13
|
+
@timeout = timeout
|
14
|
+
@stats = Stats.new :elected, :rejected, :missing, :released
|
12
15
|
end
|
13
16
|
|
14
17
|
def key
|
@@ -31,7 +34,11 @@ module Elected
|
|
31
34
|
def release
|
32
35
|
return false unless @leader
|
33
36
|
|
34
|
-
|
37
|
+
if @leader.current?
|
38
|
+
Elected.electorado.unlock @leader.info
|
39
|
+
@stats.increment :released
|
40
|
+
end
|
41
|
+
|
35
42
|
@leader = false
|
36
43
|
end
|
37
44
|
|
@@ -48,7 +55,11 @@ module Elected
|
|
48
55
|
end
|
49
56
|
|
50
57
|
def get_leader
|
51
|
-
|
58
|
+
unless @leader
|
59
|
+
@stats.increment :missing
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
|
52
63
|
return @leader if @leader.current?
|
53
64
|
|
54
65
|
release
|
@@ -56,9 +67,13 @@ module Elected
|
|
56
67
|
|
57
68
|
def set_leader
|
58
69
|
found = Elected.electorado.lock key, timeout
|
59
|
-
|
60
|
-
|
61
|
-
|
70
|
+
if found
|
71
|
+
@stats.increment :elected
|
72
|
+
@leader = Lider.new found, timeout
|
73
|
+
else
|
74
|
+
@stats.increment :rejected
|
75
|
+
false
|
76
|
+
end
|
62
77
|
end
|
63
78
|
|
64
79
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'concurrent'
|
2
|
+
require 'concurrent/thread_safe/util/adder'
|
3
|
+
|
4
|
+
module Elected
|
5
|
+
class Stats
|
6
|
+
|
7
|
+
attr_reader :counters
|
8
|
+
|
9
|
+
def initialize(*names)
|
10
|
+
@counters = Concurrent::Hash.new
|
11
|
+
names.map { |x| get_or_set_counter x }
|
12
|
+
end
|
13
|
+
|
14
|
+
def count(name)
|
15
|
+
get_or_set_counter(name).sum
|
16
|
+
end
|
17
|
+
|
18
|
+
def increment(name, value = 1)
|
19
|
+
get_or_set_counter(name).add value
|
20
|
+
count name
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
@counters.keys.sort.each_with_object({}) { |k, h| h[k] = count k }
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
%{#<#{self.class.name} #{to_hash.map { |k, v| "#{k}=#{v}" }.join(' ')}>}
|
29
|
+
end
|
30
|
+
|
31
|
+
alias :inspect :to_s
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def get_or_set_counter(name, initial_value = 0)
|
36
|
+
safe_name = name.to_s.strip.underscore.to_sym
|
37
|
+
found = @counters[safe_name]
|
38
|
+
return found if found
|
39
|
+
|
40
|
+
@counters[safe_name] = Concurrent::ThreadSafe::Util::Adder.new.tap do |c|
|
41
|
+
c.add(initial_value) unless initial_value == 0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/lib/elected/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elected
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Madrid
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redlock
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.1.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concurrent-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0.pre4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0.pre4
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: pry
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,9 +127,11 @@ files:
|
|
113
127
|
- bin/setup
|
114
128
|
- elected.gemspec
|
115
129
|
- lib/elected.rb
|
130
|
+
- lib/elected/core_ext.rb
|
116
131
|
- lib/elected/lider.rb
|
117
132
|
- lib/elected/logging.rb
|
118
133
|
- lib/elected/senado.rb
|
134
|
+
- lib/elected/stats.rb
|
119
135
|
- lib/elected/version.rb
|
120
136
|
homepage: https://github.com/simple-finance/elected
|
121
137
|
licenses:
|