Class: VSM::Capsule

Inherits:
Object
  • Object
show all
Defined in:
lib/vsm/capsule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, roles:, children: {}) ⇒ Capsule

Returns a new instance of Capsule.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vsm/capsule.rb', line 7

def initialize(name:, roles:, children: {})
  @name     = name.to_sym
  @roles    = roles
  @children = children
  ctx = { operations_children: children.transform_keys(&:to_s) }
  @bus = AsyncChannel.new(context: ctx)
  @homeostat = Homeostat.new
  # Inject bus into children that accept it, to enable richer observability
  @children.each_value { |c| c.bus = @bus if c.respond_to?(:bus=) }
  wire_observers!
end

Instance Attribute Details

#busObject (readonly)

Returns the value of attribute bus.



5
6
7
# File 'lib/vsm/capsule.rb', line 5

def bus
  @bus
end

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/vsm/capsule.rb', line 5

def children
  @children
end

#homeostatObject (readonly)

Returns the value of attribute homeostat.



5
6
7
# File 'lib/vsm/capsule.rb', line 5

def homeostat
  @homeostat
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/vsm/capsule.rb', line 5

def name
  @name
end

#rolesObject (readonly)

Returns the value of attribute roles.



5
6
7
# File 'lib/vsm/capsule.rb', line 5

def roles
  @roles
end

Instance Method Details

#dispatch(message) ⇒ Object



29
30
31
32
# File 'lib/vsm/capsule.rb', line 29

def dispatch(message)
  return roles[:identity].alert(message) if homeostat.alarm?(message)
  roles[:governance].enforce(message) { route(_1) }
end

#route(message) ⇒ Object



34
35
36
37
38
# File 'lib/vsm/capsule.rb', line 34

def route(message)
  roles[:operations].handle(message, bus: @bus, children: @children) ||
  roles[:intelligence].handle(message, bus: @bus) ||
  roles[:identity].handle(message, bus: @bus)
end

#runObject



19
20
21
22
23
24
25
26
27
# File 'lib/vsm/capsule.rb', line 19

def run
  Async do
    loop do
      message = @bus.pop
      roles[:coordination].stage(message)
      roles[:coordination].drain(@bus) { |m| dispatch(m) }
    end
  end
end