class BeanstalkFarmer::Runner

Maps tube names to job handlers, and manages the run loop.

@example

runner = BeanstalkFarmer::Runner.new

runner.register_handlers do
  tube 'my_tube', lambda { |args| puts args {
end

runner.run!

Attributes

handler_pool[R]

Public Class Methods

new() click to toggle source
# File lib/beanstalk_farmer/runner.rb, line 16
def initialize
  @handler_pool = Job.handler_pool
end

Public Instance Methods

close_connection() click to toggle source

Closes the connection to the Beanstalk queue

# File lib/beanstalk_farmer/runner.rb, line 44
def close_connection
  service.close
end
prep_tubes() click to toggle source

Prepares tubes for watching

# File lib/beanstalk_farmer/runner.rb, line 26
def prep_tubes
  service.prep(handler_pool.keys)
end
register_handlers(&block) click to toggle source

@yield block A DSL to define jobs for your queue

@example

runner = Farmer::Runner.new

runner.register_handlers do
  tube 'sms',   proc { |args| puts args }
  tube 'mine',  Proc.new { |args| puts args }
end
# File lib/beanstalk_farmer/runner.rb, line 57
def register_handlers(&block)
  dsl = DSL.new(handler_pool)
  dsl.eval(&block)
  dsl
end
reserve_and_work_job(timeout=nil) click to toggle source

Reserves a job, and works it

@param [Integer] timeout (nil) The optional timeout to use when

reserving a job
# File lib/beanstalk_farmer/runner.rb, line 34
def reserve_and_work_job(timeout=nil)
  service.reserve_and_work_job(timeout)
end
run!() click to toggle source

Looks for jobs to reserve, and applies handlers to them

# File lib/beanstalk_farmer/runner.rb, line 64
def run!
  prep_tubes
  work_jobs
end
service() click to toggle source

@return [Farmer::BeanstalkService] a connection to the Beanstalk queue

# File lib/beanstalk_farmer/runner.rb, line 21
def service
  @service ||= Service.new
end
work_jobs() click to toggle source

Runs a loop looking for jobs to reserve and run

# File lib/beanstalk_farmer/runner.rb, line 39
def work_jobs
  service.run_loop { reserve_and_work_job }
end