class BeanstalkFarmer::Service

Provides an abstraction against our Beanstalk client to buffer us against changes in their API.

Set the host and port through the configuration interface.

@todo Abstract all 3rd party errors @todo Catch all 3rd party errors, and re-raise our own

@private

Constants

DEFAULT_DELAY
DEFAULT_PRIORITY
DEFAULT_TTR

Attributes

host[RW]
port[RW]

Public Class Methods

new() click to toggle source

Sets the host and port of your Beanstalk queue.

@param [String] host (DEFAULT_HOST) the host name to of your Beanstalk queue

@param [String] port (DEFAULT_PORT) the port that your Beanstalk queue is on

# File lib/beanstalk_farmer/service.rb, line 29
def initialize
  self.host = Config.host
  self.port = Config.port
end

Public Instance Methods

close() click to toggle source

Closes the Beanstalk connection

@return [nil] Nothing. Absolutely nothing.

# File lib/beanstalk_farmer/service.rb, line 49
def close
  logger.warn "Closing connection to Beanstalk"
  connection.close
  delete_connection
end
connection() click to toggle source

@return [Beanstalk::Pool] a connection to Beanstalk

# File lib/beanstalk_farmer/service.rb, line 40
def connection
  @connection ||= Beanstalk::Pool.new([uri])
rescue Beanstalk::NotConnected
  raise NotConnectedError
end
enqueue(tube_name, args={}) click to toggle source

Helper method. Used when testing to see if our queue works

# File lib/beanstalk_farmer/service.rb, line 78
def enqueue(tube_name, args={})
  encoded_message = MultiJson.encode([tube_name, args])

  connection.on_tube(tube_name) do |conn|
    conn.put(encoded_message, DEFAULT_PRIORITY, DEFAULT_DELAY, DEFAULT_TTR)
  end
end
ignore_unwatched_tubes(watched_tube_names) click to toggle source

Ignores any tubes that aren’t of interest, excluding the default tube.

# File lib/beanstalk_farmer/service.rb, line 68
def ignore_unwatched_tubes(watched_tube_names)
  connection.list_tubes_watched.values.flatten.each do |tube_name|
    unless watched_tube_names.include?(tube_name)
      logger.info "Ignoring tube: #{tube_name}"
      connection.ignore(tube_name)
    end
  end
end
prep(tube_names) click to toggle source

@param [Array<String>] tube_names The tube names to be watched

# File lib/beanstalk_farmer/service.rb, line 56
def prep(tube_names)
  watch_tubes(tube_names)
  ignore_unwatched_tubes(tube_names)
end
reserve(timeout=nil) click to toggle source

@param [Integer, nil] timeout When nil, a job is reserved indefinitely,

otherwise for the number of seconds provided

@return [Farmer::Job] a Farmer Job to work

# File lib/beanstalk_farmer/service.rb, line 90
def reserve(timeout=nil)
  Job.new(connection.reserve(timeout))
rescue Beanstalk::TimedOut => e
  raise TimedOut, e.message
end
reserve_and_work_job(timeout=nil) click to toggle source

@see BeanstalkFarmer::Service#reserve

# File lib/beanstalk_farmer/service.rb, line 97
def reserve_and_work_job(timeout=nil)
  job = reserve(timeout)
  job.work
end
run_loop(&block) click to toggle source

@yield A block to execute in the run loop

# File lib/beanstalk_farmer/service.rb, line 103
def run_loop(&block)
  loop &block
end
uri() click to toggle source

@return [String] a formatted URI for a Beanstalk queue

# File lib/beanstalk_farmer/service.rb, line 35
def uri
  build_uri(host, port)
end
watch_tubes(tube_names) click to toggle source

@param [Array<String>] tube_names The tube names to be watched

# File lib/beanstalk_farmer/service.rb, line 62
def watch_tubes(tube_names)
  logger.info "Watching tubes: #{tube_names.inspect}"
  tube_names.each { |tube_name| connection.watch(tube_name) }
end

Private Instance Methods

build_uri(host, port) click to toggle source
# File lib/beanstalk_farmer/service.rb, line 109
def build_uri(host, port)
  [host, port].join(':')
end
delete_connection() click to toggle source
# File lib/beanstalk_farmer/service.rb, line 113
def delete_connection
  @connection = nil
end