Class: BeanStalk::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/beanstalk-worker/config.rb,
lib/beanstalk-worker.rb,
lib/beanstalk-worker/worker.rb,
lib/beanstalk-worker/version_class.rb

Overview

The configuration object for the gemindexer worker.

Defined Under Namespace

Modules: Config, Log, Version

Constant Summary collapse

VERSION =
'0.1.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Worker



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/beanstalk-worker/worker.rb', line 6

def initialize(config = {})
  @config = BeanStalk::Worker::Config
  if File.exists? @config[:config_file]
    @config.from_file @config[:config_file]
  end
  @config.merge!(config || {})

  @logger = BeanStalk::Worker::Log
  @logger.reset!
  @logger.info("Logging started")
  
  @stats = {
    'received' => 0
  }
  
  initialize_beanstalk
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/beanstalk-worker/worker.rb', line 4

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/beanstalk-worker/worker.rb', line 4

def connection
  @connection
end

#logObject

Returns the value of attribute log.



4
5
6
# File 'lib/beanstalk-worker/worker.rb', line 4

def log
  @log
end

#statsObject

Returns the value of attribute stats.



4
5
6
# File 'lib/beanstalk-worker/worker.rb', line 4

def stats
  @stats
end

Instance Method Details

#beanstalkObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/beanstalk-worker/worker.rb', line 24

def beanstalk
  if @connection.nil?
    @connection = Beanstalk::Pool.new([
      BeanStalk::Worker::Config.beanstalk_uri
    ])
    @logger.info("Connected to beanstalk")
  end
  @connection
rescue
  @logger.error("Could not connect to beanstalk.")
  reconnect
end

#initialize_beanstalkObject



37
38
39
40
41
42
43
44
# File 'lib/beanstalk-worker/worker.rb', line 37

def initialize_beanstalk    
  beanstalk.watch(@config[:beanstalk][:tube])
  beanstalk.use(@config[:beanstalk][:tube])
  beanstalk.ignore('default')
rescue
  @logger.error("Could not connect to beanstalk.")
  reconnect
end

#reconnectObject



46
47
48
49
50
51
52
# File 'lib/beanstalk-worker/worker.rb', line 46

def reconnect
  @connection = nil
  @logger.error("Sleeping 30 seconds")
  sleep(30)
  @logger.error("Attempting to reconnect")
  start
end

#start(received = -1)) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/beanstalk-worker/worker.rb', line 54

def start(received = -1)
  while (received == -1) || (@stats['received'] < received)
    begin
      job = beanstalk.reserve
      
      @logger.debug("job #{job.inspect}")
      @logger.debug("job #{job.body.inspect}")
      
      @stats['received'] += 1
      
      job.delete if work(job)
    rescue Beanstalk::NotConnected => e
      @logger.error("Beanstalk disconnected")
      reconnect
    rescue Exception => e
      @logger.error("Caught exception #{e.to_s}")
      exit
    end
  end
end

#work(job) ⇒ Object



75
76
77
# File 'lib/beanstalk-worker/worker.rb', line 75

def work(job)
  return true
end