Class: Workerholic::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/workerholic/worker.rb

Overview

handles job execution in threads

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue = nil) ⇒ Worker

Returns a new instance of Worker.



7
8
9
10
11
# File 'lib/workerholic/worker.rb', line 7

def initialize(queue=nil)
  @queue = queue
  @alive = true
  @logger = LogManager.new
end

Instance Attribute Details

#aliveObject

Returns the value of attribute alive.



5
6
7
# File 'lib/workerholic/worker.rb', line 5

def alive
  @alive
end

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/workerholic/worker.rb', line 5

def queue
  @queue
end

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#joinObject



29
30
31
# File 'lib/workerholic/worker.rb', line 29

def join
  thread.join if thread
end

#killObject



25
26
27
# File 'lib/workerholic/worker.rb', line 25

def kill
  self.alive = false
end

#workObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/workerholic/worker.rb', line 13

def work
  @thread = Thread.new do
    while alive
      serialized_job = poll
      JobProcessor.new(serialized_job).process if serialized_job
    end
  end
rescue ThreadError => e
  @logger.info(e.message)
  raise Interrupt
end