Class: ThreadedInMemoryQueue::Worker

Inherits:
Object
  • Object
show all
Includes:
Timeout
Defined in:
lib/threaded_in_memory_queue/worker.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

seconds, 1 minute

60
POISON =
"poison"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Timeout

#timeout

Constructor Details

#initialize(queue, options = {}) ⇒ Worker

Returns a new instance of Worker.



8
9
10
11
12
# File 'lib/threaded_in_memory_queue/worker.rb', line 8

def initialize(queue, options = {})
  @queue   = queue
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
  @logger  = options[:logger]  || ThreadedInMemoryQueue.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/threaded_in_memory_queue/worker.rb', line 6

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



6
7
8
# File 'lib/threaded_in_memory_queue/worker.rb', line 6

def queue
  @queue
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


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

def alive?
  return false unless @thread
  thread.alive?
end

#joinObject



33
34
35
36
# File 'lib/threaded_in_memory_queue/worker.rb', line 33

def join
  return false unless @thread
  thread.join
end

#poison(times = 1) ⇒ Object



24
25
26
# File 'lib/threaded_in_memory_queue/worker.rb', line 24

def poison(times = 1)
  @queue.enq(POISON)
end

#startObject



19
20
21
22
# File 'lib/threaded_in_memory_queue/worker.rb', line 19

def start
  @thread ||= create_thread
  return self
end

#threadObject

Raises:



14
15
16
17
# File 'lib/threaded_in_memory_queue/worker.rb', line 14

def thread
  raise WorkerNotStarted, "Must start worker before using" unless @thread
  @thread
end