class BeanStalk::Worker

The configuration object for the gemindexer worker.

Constants

VERSION

Attributes

config[RW]
connection[RW]
log[RW]
stats[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/beanstalk-worker/worker.rb, line 6
def initialize(config = {})
  @config = BeanStalk::Worker::Config
  if File.exists? @config[:config_file]
    if config[:environment]
      @config.from_file @config[:config_file],
        :environment => config[:environment]
    else
      @config.from_file @config[:config_file]
    end
  end
  @config.merge!(config || {})
  
  @stats = {
    'received' => 0
  }
end

Public Instance Methods

beanstalk() click to toggle source
# File lib/beanstalk-worker/worker.rb, line 23
def beanstalk
  if @connection.nil?
    @connection = Beanstalk::Pool.new([
      BeanStalk::Worker::Config.beanstalk_uri
    ])
    @log.info("Connected to beanstalk")
  end
  @connection
rescue
  @log.error("Could not connect to beanstalk.")
  reconnect
end
initialize_beanstalk() click to toggle source
# File lib/beanstalk-worker/worker.rb, line 42
def initialize_beanstalk    
  beanstalk.watch(@config[:beanstalk][:tube])
  beanstalk.use(@config[:beanstalk][:tube])
  beanstalk.ignore('default')
rescue
  @log.error("Could not connect to beanstalk.")
  reconnect
end
initialize_logger() click to toggle source
# File lib/beanstalk-worker/worker.rb, line 36
def initialize_logger
  @log = Logger.new(@config.log_location)
  @log.level = Logger.const_get(@config.log_level.upcase)
  @log.error("Logging started")
end
reconnect() click to toggle source
# File lib/beanstalk-worker/worker.rb, line 51
def reconnect
  @connection = nil
  @log.error("Sleeping 30 seconds")
  sleep(30)
  @log.error("Attempting to reconnect")
  start
end
start(received = -1) click to toggle source
# File lib/beanstalk-worker/worker.rb, line 59
def start(received = -1)
  initialize_logger
  initialize_beanstalk
  while (received == -1) || (@stats['received'] < received)
    begin
      job = beanstalk.reserve
      
      @log.debug("job #{job.inspect}")
      @log.debug("job #{job.body.inspect}")
      
      @stats['received'] += 1
      
      job.delete if work(job)
    rescue Beanstalk::NotConnected => e
      @log.error("Beanstalk disconnected")
      reconnect
    rescue Exception => e
      @log.error("Caught exception #{e.to_s}")
      exit
    end
  end
end
work(job) click to toggle source
# File lib/beanstalk-worker/worker.rb, line 82
def work(job)
  return true
end