Class: Contender::Pool::PoolWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/contender/pool/pool_worker.rb

Overview

Encapsulates a single worker thread in a thread pool

Instance Method Summary collapse

Constructor Details

#initialize(pool, queue, non_block = false) ⇒ undefined

Parameters:



9
10
11
12
13
14
15
16
# File 'lib/contender/pool/pool_worker.rb', line 9

def initialize(pool, queue, non_block = false)
  @pool = pool
  @queue = queue
  @non_block = non_block

  # Only changed in the worker thread
  @state = :inactive
end

Instance Method Details

#executing?Boolean

Returns true if the pool worker is executing a task

Returns:

  • (Boolean)


53
54
55
# File 'lib/contender/pool/pool_worker.rb', line 53

def executing?
  :executing == @state
end

#inactive?Boolean

Returns true if the pool worker is inactive

Returns:

  • (Boolean)


41
42
43
# File 'lib/contender/pool/pool_worker.rb', line 41

def inactive?
  :inactive == @state
end

#interruptundefined

Returns:

  • (undefined)


34
35
36
37
# File 'lib/contender/pool/pool_worker.rb', line 34

def interrupt
  return unless @thread
  @thread.raise OperationInterrupted
end

#joinundefined

Returns:

  • (undefined)


28
29
30
31
# File 'lib/contender/pool/pool_worker.rb', line 28

def join
  return unless @thread
  @thread.join
end

#startundefined

Returns:

  • (undefined)


19
20
21
22
23
24
25
# File 'lib/contender/pool/pool_worker.rb', line 19

def start
  return unless inactive?

  @thread = Thread.new do
    start_worker_loop
  end
end

#waiting?Boolean

Returns true if the pool worker is waiting for tasks

Returns:

  • (Boolean)


47
48
49
# File 'lib/contender/pool/pool_worker.rb', line 47

def waiting?
  :waiting == @state
end