Class: PartTime::Worker

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

Constant Summary collapse

SHUTDOWN =
"GO HOME!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Worker

Returns a new instance of Worker.



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

def initialize(queue)
  @queue = queue
  @on_the_clock = true

  work
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

Instance Method Details

#joinObject



26
27
28
# File 'lib/part_time/worker.rb', line 26

def join
  @thread.join
end

#on_the_clock?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/part_time/worker.rb', line 30

def on_the_clock?
  @on_the_clock
end

#workObject



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

def work
  @thread ||= Thread.new do
    loop do
      job, *args = queue.pop
      break if job == SHUTDOWN
      job.new.perform(*args)
    end

    @on_the_clock = false
  end
end