/
connection.rb
52 lines (44 loc) · 1.29 KB
/
connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'delegate'
module Backburner
class Connection < SimpleDelegator
class BadURL < RuntimeError; end
attr_accessor :url, :beanstalk
# Constructs a backburner connection
# `url` can be a string i.e 'localhost:3001' or an array of addresses.
def initialize(url)
@url = url
@beanstalk = nil
connect!
end
# Sets the delegator object to the underlying beaneater pool
# self.put(...)
def __getobj__
__setobj__(@beanstalk)
super
end
protected
# Connects to a beanstalk queue
def connect!
@beanstalk ||= Beaneater::Pool.new(beanstalk_addresses)
end
# Returns the beanstalk queue addresses
#
# @example
# beanstalk_addresses => ["localhost:11300"]
#
def beanstalk_addresses
uris = self.url.is_a?(Array) ? self.url : self.url.split(/[\s,]+/)
uris.map { |uri| beanstalk_host_and_port(uri) }
end
# Returns a host and port based on the uri_string given
#
# @example
# beanstalk_host_and_port("beanstalk://localhost") => "localhost:11300"
#
def beanstalk_host_and_port(uri_string)
uri = URI.parse(uri_string)
raise(BadURL, uri_string) if uri.scheme != 'beanstalk'
"#{uri.host}:#{uri.port || 11300}"
end
end # Connection
end # Backburner