Class: QueueWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/task_queue/queue_worker.rb

Constant Summary collapse

PAUSE_DURATION =
1

Instance Method Summary collapse

Constructor Details

#initialize(queue, stats_thread) ⇒ QueueWorker

Returns a new instance of QueueWorker.



3
4
5
6
7
# File 'lib/yodel/task_queue/queue_worker.rb', line 3

def initialize(queue, stats_thread)
  @stats_thread = stats_thread
  @queue = queue
  run
end

Instance Method Details

#joinObject



29
30
31
# File 'lib/yodel/task_queue/queue_worker.rb', line 29

def join
  @thread.join
end

#killObject



25
26
27
# File 'lib/yodel/task_queue/queue_worker.rb', line 25

def kill
  @thread.kill
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yodel/task_queue/queue_worker.rb', line 9

def run
  @running = true
  @thread = Thread.new do
    while @running
      task = @queue.pop
      sleep(PAUSE_DURATION) and next if task.nil?
      task.execute
      @stats_thread.processed_task
    end
  end
end

#stopObject



21
22
23
# File 'lib/yodel/task_queue/queue_worker.rb', line 21

def stop
  @running = false
end