rhod 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e38a0b14c5bc5b47d92df1a87fba15f554276ce
4
- data.tar.gz: 86bbfc6da898d50d9fe574cd893d112412ce7ac3
3
+ metadata.gz: 4fdc88634e5041f5eb95de18515709647bd9edd2
4
+ data.tar.gz: fa5cdadac5ca8fcaaef6066862e5b09552b39aee
5
5
  SHA512:
6
- metadata.gz: 6f796a4dbdb6170a7b688f3828a89cadf8be3bc3239e57f3fb2e43656d63522abac99cd7dfb703995d5014540b33c2ddfdee43ce188d8b7d8f9cf2303b783909
7
- data.tar.gz: 62dd2d51886b9b78e1f61a60ef5c732562f6064298651a850d4e968c58aac0d3eca3c08174d05ea707214fc10c07e381bed62962ff1e77a30c1f7bdd4a48d94e
6
+ metadata.gz: bb25f0ef2719d5141211eab7f9981938efd5be0dc1fb1a64362280ff844624da0899458e714682e38e6a5021bc7ca7ecc70a48eca151d3b71b260fd7a5733947
7
+ data.tar.gz: a9d86ea5c9e59a1ea39df024f69bac790adb79c4e87990af471cedff1cc341b7174d9a5bd092661746139f516b2ad0c3caa5725bdd82d15f315cd3d1a8cdd2bb
data/.pryrc CHANGED
@@ -1 +1,2 @@
1
- require './lib/rhod'
1
+ require 'benchmark'
2
+ require './lib/rhod'
data/Changes.md CHANGED
@@ -1,3 +1,10 @@
1
+ # v0.1.2
2
+ Added logging as a feature - dleung - PR #8
3
+
4
+ # v0.1.0 & v0.1.1
5
+ Changed backoffs to be threadsafe, requiring an API change.
6
+ Added support for profiles so commonly used Rhod settings could be located in one place.
7
+
1
8
  # v0.0.3
2
9
  Add support for connection pooling to be attached to a Rhod command.
3
10
 
data/README.md CHANGED
@@ -63,6 +63,7 @@ Or install it yourself as:
63
63
 
64
64
  ## Configuration
65
65
 
66
+ ### Creating or Editing a New Profile
66
67
  To configure Rhod's defaults, just overwrite the default profile with any changes you'd like to make. If you're on Rails, a good place for your profiles is `config/initializers/rhod.rb`
67
68
 
68
69
  ```ruby
@@ -93,7 +94,9 @@ Rhod.create_profile(:redis,
93
94
  retries: 10,
94
95
  backoffs: :^,
95
96
  pool: ConnectionPool.new(size: 3, timeout: 10) { Redis.new },
96
- exceptions: [Redis::BaseError])
97
+ exceptions: [Redis::BaseError],
98
+ logger: Logger.new("log.txt")
99
+ )
97
100
 
98
101
  Rhod.with_redis("1") {|r, a| r.set('test',a)}
99
102
  # => "OK"
@@ -1,4 +1,5 @@
1
1
  require 'connection_pool'
2
+ require 'logger'
2
3
  require_relative "rhod/version"
3
4
  require_relative "rhod/backoffs"
4
5
  require_relative "rhod/command"
@@ -3,25 +3,24 @@ class Rhod::Command
3
3
  EXCEPTIONS = [Exception, StandardError]
4
4
 
5
5
  def initialize(*args, &block)
6
- opts = args[-1].kind_of?(Hash) ? args.pop : {}
7
- @args = args
8
- @args ||= []
6
+ opts = args[-1].kind_of?(Hash) ? args.pop : {}
7
+ @args = args || []
9
8
 
10
- @request = block
9
+ @request = block
11
10
 
12
- @retries = opts[:retries]
13
- @retries ||= 0
14
- @attempts = 0
11
+ @retries = opts[:retries] || 0
12
+ @attempts = 0
15
13
 
16
- @backoffs = Rhod::Backoffs.backoff_sugar_to_enumerator(opts[:backoffs])
17
- @backoffs ||= Rhod::Backoffs::Logarithmic.new(1.3)
14
+ @logger = opts[:logger]
18
15
 
19
- @fallback = opts[:fallback]
16
+ @backoffs = Rhod::Backoffs.backoff_sugar_to_enumerator(opts[:backoffs])
17
+ @backoffs ||= Rhod::Backoffs::Logarithmic.new(1.3)
20
18
 
21
- @pool = opts[:pool]
19
+ @fallback = opts[:fallback]
22
20
 
23
- @exceptions = opts[:exceptions]
24
- @exceptions ||= EXCEPTIONS
21
+ @pool = opts[:pool]
22
+
23
+ @exceptions = opts[:exceptions] || EXCEPTIONS
25
24
  end
26
25
 
27
26
  ### Class methods
@@ -44,10 +43,12 @@ class Rhod::Command
44
43
  else
45
44
  @request.call(*@args)
46
45
  end
47
- rescue *@exceptions
46
+ rescue *@exceptions => e
48
47
  @attempts += 1
48
+ @next_attempt = @backoffs.next
49
49
  if @attempts <= @retries
50
- sleep(@backoffs.next)
50
+ @logger.warn("Rhod - Caught an exception: #{e.message}. Attempt #{@attempts} in #{sprintf("%.2f", @next_attempt)} secs") if @logger && @logger.respond_to?(:warn)
51
+ sleep(@next_attempt)
51
52
  retry
52
53
  else
53
54
  return @fallback.call(*@args) if @fallback
@@ -55,5 +56,4 @@ class Rhod::Command
55
56
  end
56
57
  end
57
58
  end
58
-
59
59
  end
@@ -0,0 +1,10 @@
1
+ class Rhod::Middleware
2
+ def use(middleware, *args, &block)
3
+ self.stack << [middleware, args, block]
4
+ end
5
+
6
+ protected
7
+ def stack
8
+ @stack ||= []
9
+ end
10
+ end
@@ -2,7 +2,7 @@ class Rhod::Profile < Hash
2
2
  @@profiles = {}
3
3
 
4
4
  def initialize(name, options={})
5
- # When creating new profiles, copy from the global default, incase it was customized.
5
+ # When creating new profiles, copy from the global default, in case it was customized.
6
6
  if @@profiles[:default]
7
7
  default = @@profiles[:default].dup
8
8
  else
@@ -29,9 +29,7 @@ class Rhod::Profile < Hash
29
29
  end
30
30
 
31
31
  Rhod::Profile.new(:default,
32
- retries: 0,
32
+ retries: 5,
33
33
  backoffs: Rhod::Backoffs::Logarithmic.new(1.3),
34
- fallback: nil,
35
- pool: nil,
36
34
  exceptions: [Exception, StandardError],
37
35
  )
@@ -1,3 +1,3 @@
1
1
  module Rhod
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'minitest/autorun'
2
+ require 'stringio'
2
3
  require File.expand_path(File.dirname(__FILE__) + '/helper')
3
4
 
4
5
  describe Rhod::Command do
@@ -46,6 +47,17 @@ describe Rhod::Command do
46
47
  backoff.verify
47
48
  end
48
49
 
50
+ describe "with logging" do
51
+ it "logs failures" do
52
+ test_log = Logger.new(output = StringIO.new)
53
+ begin
54
+ Rhod::Command.new(logger: test_log, retries: 1) {raise StandardError}.execute
55
+ rescue
56
+ end
57
+ output.string.must_match(/^W.*Rhod - Caught an exception.*Attempt \d in \d\.\d\d secs$/)
58
+ end
59
+ end
60
+
49
61
  end
50
62
 
51
63
  describe "it uses fallbacks" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bergeron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-04 00:00:00.000000000 Z
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - lib/rhod/backoffs/logarithmic.rb
117
117
  - lib/rhod/backoffs/random.rb
118
118
  - lib/rhod/command.rb
119
+ - lib/rhod/middleware.rb
119
120
  - lib/rhod/profile.rb
120
121
  - lib/rhod/version.rb
121
122
  - rhod.gemspec