Class: AethernalAgent::OperationPool

Inherits:
Object
  • Object
show all
Defined in:
lib/aethernal_agent/operation_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ OperationPool

Returns a new instance of OperationPool.



4
5
6
7
8
# File 'lib/aethernal_agent/operation_pool.rb', line 4

def initialize(size)
  @size = size
  @jobs = Queue.new
  self.operations = {}
end

Instance Attribute Details

#operationsObject

Returns the value of attribute operations.



2
3
4
# File 'lib/aethernal_agent/operation_pool.rb', line 2

def operations
  @operations
end

Instance Method Details

#add_operation(op) ⇒ Object



33
34
35
36
# File 'lib/aethernal_agent/operation_pool.rb', line 33

def add_operation(op)
  @jobs << op
  self.operations[op.uuid] = op
end

#get_operation(uuid) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/aethernal_agent/operation_pool.rb', line 38

def get_operation(uuid)
  op = self.operations[uuid]
  if !op.nil? && op.is_completed?
    self.operations.delete(uuid)
  end

  return op
end

#operation_sizeObject



29
30
31
# File 'lib/aethernal_agent/operation_pool.rb', line 29

def operation_size
  self.operations.keys.size
end

#queue_sizeObject



25
26
27
# File 'lib/aethernal_agent/operation_pool.rb', line 25

def queue_size
  @jobs.size
end

#startObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aethernal_agent/operation_pool.rb', line 10

def start
  @pool = Array.new(@size) do |i|
    Thread.new do
      Thread.current[:id] = i
      catch(:exit) do
        loop do
          op = @jobs.pop
          AethernalAgent.logger.debug("Worker #{i} - Picking up job #{op.uuid}")
          op.run
        end
      end
    end
  end
end