Class: BeanStalk::Worker
- Inherits:
-
Object
- Object
- BeanStalk::Worker
- 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
Constant Summary collapse
- VERSION =
'0.1.4'
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#log ⇒ Object
Returns the value of attribute log.
-
#stats ⇒ Object
Returns the value of attribute stats.
Instance Method Summary collapse
- #beanstalk ⇒ Object
-
#initialize(config = {}) ⇒ Worker
constructor
A new instance of Worker.
- #initialize_beanstalk ⇒ Object
- #reconnect ⇒ Object
- #start(received = -1)) ⇒ Object
- #work(job) ⇒ Object
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 22 23 24 25 26 |
# 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], config.environment else @config.from_file @config[:config_file] end end @config.merge!(config || {}) @logger = BeanStalk::Worker::Log @logger.reset! @logger.info("Logging started") @stats = { 'received' => 0 } initialize_beanstalk end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/beanstalk-worker/worker.rb', line 4 def config @config end |
#connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/beanstalk-worker/worker.rb', line 4 def connection @connection end |
#log ⇒ Object
Returns the value of attribute log.
4 5 6 |
# File 'lib/beanstalk-worker/worker.rb', line 4 def log @log end |
#stats ⇒ Object
Returns the value of attribute stats.
4 5 6 |
# File 'lib/beanstalk-worker/worker.rb', line 4 def stats @stats end |
Instance Method Details
#beanstalk ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/beanstalk-worker/worker.rb', line 28 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_beanstalk ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/beanstalk-worker/worker.rb', line 41 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 |
#reconnect ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/beanstalk-worker/worker.rb', line 50 def reconnect @connection = nil @logger.error("Sleeping 30 seconds") sleep(30) @logger.error("Attempting to reconnect") start end |
#start(received = -1)) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/beanstalk-worker/worker.rb', line 58 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
79 80 81 |
# File 'lib/beanstalk-worker/worker.rb', line 79 def work(job) return true end |