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
Public Class Methods
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
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
@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
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
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
@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
@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
@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
@yield A block to execute in the run loop
# File lib/beanstalk_farmer/service.rb, line 103 def run_loop(&block) loop &block end
@return [String] a formatted URI for a Beanstalk queue
# File lib/beanstalk_farmer/service.rb, line 35 def uri build_uri(host, port) end
@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
# File lib/beanstalk_farmer/service.rb, line 109 def build_uri(host, port) [host, port].join(':') end
# File lib/beanstalk_farmer/service.rb, line 113 def delete_connection @connection = nil end