Class: DatWorkerPool::WorkerPoolSpy

Inherits:
Object
  • Object
show all
Defined in:
lib/dat-worker-pool/worker_pool_spy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_class, options = nil) ⇒ WorkerPoolSpy

Returns a new instance of WorkerPoolSpy.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 12

def initialize(worker_class, options = nil)
  @worker_class = worker_class
  if !@worker_class.kind_of?(Class) || !@worker_class.include?(DatWorkerPool::Worker)
    raise ArgumentError, "worker class must include `#{DatWorkerPool::Worker}`"
  end

  @options      = options || {}
  @num_workers  = (@options[:num_workers] || DEFAULT_NUM_WORKERS).to_i
  if @num_workers && @num_workers < MIN_WORKERS
    raise ArgumentError, "number of workers must be at least #{MIN_WORKERS}"
  end

  @queue = @options[:queue] || begin
    require 'dat-worker-pool/default_queue'
    DatWorkerPool::DefaultQueue.new
  end

  @logger        = @options[:logger]
  @worker_params = @options[:worker_params]

  @available_worker_count = 0
  @worker_available       = false
  @start_called           = false
  @shutdown_called        = false
  @shutdown_timeout       = nil
end

Instance Attribute Details

#available_worker_countObject

Returns the value of attribute available_worker_count.



10
11
12
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 10

def available_worker_count
  @available_worker_count
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 7

def logger
  @logger
end

#num_workersObject (readonly)

Returns the value of attribute num_workers.



8
9
10
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 8

def num_workers
  @num_workers
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 8

def options
  @options
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 7

def queue
  @queue
end

#shutdown_calledObject (readonly)

Returns the value of attribute shutdown_called.



9
10
11
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 9

def shutdown_called
  @shutdown_called
end

#shutdown_timeoutObject (readonly)

Returns the value of attribute shutdown_timeout.



9
10
11
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 9

def shutdown_timeout
  @shutdown_timeout
end

#start_calledObject (readonly)

Returns the value of attribute start_called.



9
10
11
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 9

def start_called
  @start_called
end

#worker_availableObject

Returns the value of attribute worker_available.



10
11
12
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 10

def worker_available
  @worker_available
end

#worker_classObject (readonly)

Returns the value of attribute worker_class.



8
9
10
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 8

def worker_class
  @worker_class
end

#worker_paramsObject (readonly)

Returns the value of attribute worker_params.



8
9
10
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 8

def worker_params
  @worker_params
end

Instance Method Details

#add_work(work_item) ⇒ Object Also known as: push



50
51
52
53
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 50

def add_work(work_item)
  return if work_item.nil?
  @queue.dwp_push(work_item)
end

#shutdown(timeout = nil) ⇒ Object



44
45
46
47
48
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 44

def shutdown(timeout = nil)
  @shutdown_called  = true
  @shutdown_timeout = timeout
  @queue.dwp_shutdown
end

#startObject



39
40
41
42
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 39

def start
  @start_called = true
  @queue.dwp_start
end

#work_itemsObject



56
57
58
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 56

def work_items
  @queue.work_items
end

#worker_available?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dat-worker-pool/worker_pool_spy.rb', line 60

def worker_available?
  !!@worker_available
end