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.7'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Worker

Returns a new instance of Worker.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

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



23
24
25
26
27
28
29
30
31
32
33
34
# 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_beanstalkObject



42
43
44
45
46
47
48
49
# 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_loggerObject



36
37
38
39
40
# 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

#reconnectObject



51
52
53
54
55
56
57
# 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)) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



82
83
84
# File 'lib/beanstalk-worker/worker.rb', line 82

def work(job)
  return true
end