Class: Backburner::Connection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Backburner::Connection
- Defined in:
- lib/backburner/connection.rb
Defined Under Namespace
Classes: BadURL
Instance Attribute Summary collapse
-
#beanstalk ⇒ Object
Returns the value of attribute beanstalk.
-
#on_reconnect ⇒ Object
If a proc is provided, it will be called (and given this connection as an argument) whenever the connection is reconnected.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection, if it exists.
-
#connected? ⇒ Boolean
Determines if the connection to Beanstalk is currently open.
-
#initialize(url, &on_reconnect) ⇒ Connection
constructor
Constructs a backburner connection ‘url` can be a string i.e ’127.0.0.1:3001’ or an array of addresses (however, only the first element in the array will be used).
-
#reconnect! ⇒ Object
Attempt to reconnect to Beanstalk.
-
#retryable(options = {}, &block) ⇒ Object
Yield to a block that will be retried several times if the connection to beanstalk goes down and is able to be re-established.
Constructor Details
#initialize(url, &on_reconnect) ⇒ Connection
Constructs a backburner connection ‘url` can be a string i.e ’127.0.0.1:3001’ or an array of addresses (however, only the first element in the array will be used)
19 20 21 22 23 24 |
# File 'lib/backburner/connection.rb', line 19 def initialize(url, &on_reconnect) @url = url @beanstalk = nil @on_reconnect = on_reconnect connect! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object (protected)
Attempt to ensure we’re connected to Beanstalk if the missing method is present in the delegate and we haven’t shut down the connection on purpose
88 89 90 91 |
# File 'lib/backburner/connection.rb', line 88 def method_missing(m, *args, &block) ensure_connected! if respond_to_missing?(m, false) super end |
Instance Attribute Details
#beanstalk ⇒ Object
Returns the value of attribute beanstalk.
7 8 9 |
# File 'lib/backburner/connection.rb', line 7 def beanstalk @beanstalk end |
#on_reconnect ⇒ Object
If a proc is provided, it will be called (and given this connection as an argument) whenever the connection is reconnected.
13 14 15 |
# File 'lib/backburner/connection.rb', line 13 def on_reconnect @on_reconnect end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/backburner/connection.rb', line 7 def url @url end |
Instance Method Details
#close ⇒ Object
Close the connection, if it exists
27 28 29 30 31 |
# File 'lib/backburner/connection.rb', line 27 def close @beanstalk.close if @beanstalk @beanstalk = nil __setobj__(@beanstalk) end |
#connected? ⇒ Boolean
Determines if the connection to Beanstalk is currently open
34 35 36 37 38 39 40 |
# File 'lib/backburner/connection.rb', line 34 def connected? begin !!(@beanstalk && @beanstalk.connection && @beanstalk.connection.connection && !@beanstalk.connection.connection.closed?) # Would be nice if beaneater provided a connected? method rescue false end end |
#reconnect! ⇒ Object
Attempt to reconnect to Beanstalk. Note: the connection will not be watching or using the tubes it was before it was reconnected (as it’s actually a completely new connection)
46 47 48 49 50 |
# File 'lib/backburner/connection.rb', line 46 def reconnect! close connect! @on_reconnect.call(self) if @on_reconnect.respond_to?(:call) end |
#retryable(options = {}, &block) ⇒ Object
Yield to a block that will be retried several times if the connection to beanstalk goes down and is able to be re-established.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/backburner/connection.rb', line 63 def retryable( = {}, &block) = {:max_retries => 4, :on_retry => nil, :retry_delay => 1.0}.merge!() retry_count = [:max_retries] begin yield rescue Beaneater::NotConnected if retry_count > 0 reconnect! retry_count -= 1 sleep [:retry_delay] [:on_retry].call if [:on_retry].respond_to?(:call) retry else # stop retrying raise e end end end |