switch_gear 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module SwitchGear
6
6
  failure = JSON.parse(json)
7
7
  error = Object.const_get(failure["error"])
8
8
  error = error.new(failure["message"])
9
- new(error, Time.parse(failure["timestamp"]))
9
+ new(error, Time.at(failure["timestamp"]).utc)
10
10
  end
11
11
 
12
12
  def initialize(error, recorded = Time.now.utc)
@@ -26,7 +26,7 @@ module SwitchGear
26
26
  JSON.generate({
27
27
  error: error.class,
28
28
  message: error.message,
29
- timestamp: timestamp.to_s
29
+ timestamp: timestamp.to_i
30
30
  })
31
31
  end
32
32
 
@@ -3,17 +3,17 @@ module SwitchGear
3
3
  class Memory
4
4
  include CircuitBreaker
5
5
 
6
- # The main runner, must respond to #call
6
+ # The main runner, must respond to #call.
7
7
  # @return [Proc/Lambda] the runner
8
8
  attr_accessor :circuit
9
9
  # The count of failures
10
- # @return [Integer] the amount of failures to permit. Defaults to 10 seconds
10
+ # @return [Integer] the amount of failures to permit. Defaults to 10 seconds.
11
11
  attr_accessor :failure_limit
12
- # The amount of time in seconds before a breaker should reset if currently open. Defaults to 5
12
+ # The amount of time in seconds before a breaker should reset if currently open. Defaults to 5.
13
13
  # @return [Integer]
14
14
  attr_accessor :reset_timeout
15
15
  # The current logger
16
- # @return [Object] - The logger sent in at initialization. Defaults to ruby's std Logger class
16
+ # @return [Object] - The logger sent in at initialization. Defaults to ruby's std Logger class.
17
17
  attr_accessor :logger
18
18
  # The current state
19
19
  # @return [Symbol] should always return either :open, :closed or :half-open.
@@ -68,23 +68,33 @@ module SwitchGear
68
68
  client.set(state_namespace, state.to_s)
69
69
  end
70
70
 
71
+ def failure_count
72
+ client.llen(fail_namespace)
73
+ end
74
+
75
+ def most_recent_failure
76
+ Failure.from_json(client.lindex(fail_namespace, -1))
77
+ end
78
+
71
79
  def failures
72
- redis_fails = client.smembers(fail_namespace)
73
- return redis_fails.map { |f| Failure.from_json(f) } if redis_fails
80
+ if failure_count > 0
81
+ redis_fails = client.lrange(fail_namespace, 0, -1)
82
+ return redis_fails.map { |f| Failure.from_json(f) }
83
+ end
74
84
  # if there are no failures in redis, set it to empty
75
85
  self.failures = []
76
86
  []
77
87
  end
78
88
 
79
89
  def add_failure(failure)
80
- client.sadd(fail_namespace, failure.to_json)
90
+ client.rpush(fail_namespace, failure.to_json)
81
91
  end
82
92
 
83
93
  # failures= requires we replace what is currently in redis
84
94
  # with the new value so we delete all entries first then add
85
95
  def failures=(failures)
86
96
  client.del(fail_namespace)
87
- failures.each { |f| client.sadd(fail_namespace, f.to_json) }
97
+ failures.each { |f| client.rpush(fail_namespace, f.to_json) }
88
98
  end
89
99
 
90
100
  private
@@ -100,7 +110,7 @@ module SwitchGear
100
110
  def run_validations
101
111
  # call super to ensure module has what it needs
102
112
  super
103
- redis_commands = [:smembers, :get, :set, :sadd, :del]
113
+ redis_commands = [:rpush, :lindex, :llen, :del, :set, :get]
104
114
  if !redis_commands.all? { |c| client.respond_to?(c) }
105
115
  raise NotImplementedError.new("Missing Methods. Your client must implement: #{redis_commands}")
106
116
  end
@@ -1,3 +1,3 @@
1
1
  module SwitchGear
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_gear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Ross
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-14 00:00:00.000000000 Z
11
+ date: 2017-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,6 +140,27 @@ files:
140
140
  - Rakefile
141
141
  - bin/console
142
142
  - bin/setup
143
+ - docs/SwitchGear.html
144
+ - docs/SwitchGear/CircuitBreaker.html
145
+ - docs/SwitchGear/CircuitBreaker/Failure.html
146
+ - docs/SwitchGear/CircuitBreaker/Memory.html
147
+ - docs/SwitchGear/CircuitBreaker/OpenError.html
148
+ - docs/SwitchGear/CircuitBreaker/Redis.html
149
+ - docs/_config.yml
150
+ - docs/_index.html
151
+ - docs/class_list.html
152
+ - docs/css/common.css
153
+ - docs/css/full_list.css
154
+ - docs/css/style.css
155
+ - docs/file.README.html
156
+ - docs/file_list.html
157
+ - docs/frames.html
158
+ - docs/index.html
159
+ - docs/js/app.js
160
+ - docs/js/full_list.js
161
+ - docs/js/jquery.js
162
+ - docs/method_list.html
163
+ - docs/top-level-namespace.html
143
164
  - examples/example.rb
144
165
  - examples/example_redis.rb
145
166
  - lib/switch_gear.rb