Class: Backburner::Connection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/backburner/connection.rb

Defined Under Namespace

Classes: BadURL

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Raises:

  • (Beaneater::NotConnected)

    If beanstalk fails to connect after multiple attempts.



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

#beanstalkObject

Returns the value of attribute beanstalk.



7
8
9
# File 'lib/backburner/connection.rb', line 7

def beanstalk
  @beanstalk
end

#on_reconnectObject

If a proc is provided, it will be called (and given this connection as an argument) whenever the connection is reconnected.

Examples:

connection.on_reconnect = lambda { |conn| puts 'reconnected!' }


13
14
15
# File 'lib/backburner/connection.rb', line 13

def on_reconnect
  @on_reconnect
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/backburner/connection.rb', line 7

def url
  @url
end

Instance Method Details

#closeObject

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

Returns:

  • (Boolean)


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)

Raises:

  • (Beaneater::NotConnected)

    If beanstalk fails to connect



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.

Parameters:

  • options (defaults to: {})

    Hash Options. Valid options are: :max_retries Integer The maximum number of times the block will be yielded to.

    Defaults to 4
    

    :on_retry Proc An optional proc that will be called for each retry. Will be

    called after the connection is re-established and :retry_delay
    has passed but before the block is yielded to again
    

    :retry_delay Float The amount to sleep before retrying. Defaults to 1.0

Raises:

  • Beaneater::NotConnected If a connection is unable 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(options = {}, &block)
  options = {:max_retries => 4, :on_retry => nil, :retry_delay => 1.0}.merge!(options)
  retry_count = options[:max_retries]

  begin
    yield

  rescue Beaneater::NotConnected
    if retry_count > 0
      reconnect!
      retry_count -= 1
      sleep options[:retry_delay]
      options[:on_retry].call if options[:on_retry].respond_to?(:call)
      retry
    else # stop retrying
      raise e
    end
  end
end