Module: Fleck::Core::Consumer::Base::ClassMethods

Defined in:
lib/fleck/core/consumer/base.rb

Overview

Defines class methods to import when ‘Autostart` module is imported.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



15
16
17
# File 'lib/fleck/core/consumer/base.rb', line 15

def condition
  @condition
end

#consumersObject

Returns the value of attribute consumers.



15
16
17
# File 'lib/fleck/core/consumer/base.rb', line 15

def consumers
  @consumers
end

#initialize_blockObject

Returns the value of attribute initialize_block.



15
16
17
# File 'lib/fleck/core/consumer/base.rb', line 15

def initialize_block
  @initialize_block
end

#lockObject

Returns the value of attribute lock.



15
16
17
# File 'lib/fleck/core/consumer/base.rb', line 15

def lock
  @lock
end

Instance Method Details

#inherited(subclass) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/fleck/core/consumer/base.rb', line 17

def inherited(subclass)
  super
  return if subclass == Fleck::Consumer

  init_consumer(subclass)
  autostart(subclass)
  Fleck.register_consumer(subclass)
end

#initialize(&block) ⇒ Object



26
27
28
# File 'lib/fleck/core/consumer/base.rb', line 26

def initialize(&block)
  self.initialize_block = block
end

#on_terminate(consumer) ⇒ Object



39
40
41
42
# File 'lib/fleck/core/consumer/base.rb', line 39

def on_terminate(consumer)
  consumers.delete consumer
  terminate if consumers.empty?
end

#start(block: false) ⇒ Object



30
31
32
33
# File 'lib/fleck/core/consumer/base.rb', line 30

def start(block: false)
  consumers.each(&:start)
  wait_termination if block
end

#terminateObject



44
45
46
47
# File 'lib/fleck/core/consumer/base.rb', line 44

def terminate
  consumers.each(&:terminate)
  lock.synchronize { condition.signal }
end

#wait_terminationObject



35
36
37
# File 'lib/fleck/core/consumer/base.rb', line 35

def wait_termination
  lock.synchronize { condition.wait(lock) }
end