Class: Concurrent::Actor::Behaviour::Supervised

Inherits:
Abstract
  • Object
show all
Defined in:
lib/concurrent/actor/behaviour/supervised.rb

Overview

Sets and holds the supervisor of the actor if any. There is at most one supervisor for each actor. Each supervisor is automatically linked. Messages: ‘:pause!, :resume!, :reset!, :restart!` are accepted only from supervisor.

Instance Attribute Summary collapse

Attributes inherited from Abstract

#core, #subsequent

Instance Method Summary collapse

Methods inherited from Abstract

#broadcast, #pass, #reject_envelope

Methods included from InternalDelegations

#behaviour, #behaviour!, #children, #context, #dead_letter_routing, #log, #redirect, #terminate!

Methods included from PublicDelegations

#context_class, #executor, #name, #parent, #path, #reference

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?

Constructor Details

#initialize(core, subsequent) ⇒ Supervised

Returns a new instance of Supervised.



11
12
13
14
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 11

def initialize(core, subsequent)
  super core, subsequent
  @supervisor = nil
end

Instance Attribute Details

#supervisorObject (readonly)

Returns the value of attribute supervisor.



9
10
11
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 9

def supervisor
  @supervisor
end

Instance Method Details

#on_envelope(envelope) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 16

def on_envelope(envelope)
  case envelope.message
  when :supervise
    supervise envelope.sender
  when :supervisor
    supervisor
  when :un_supervise
    un_supervise envelope.sender
  when :pause!, :resume!, :reset!, :restart!
    # allow only supervisor to control the actor
    if @supervisor == envelope.sender
      pass envelope
    else
      false
    end
  else
    pass envelope
  end
end

#on_event(event) ⇒ Object



52
53
54
55
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 52

def on_event(event)
  @supervisor = nil if event == :terminated
  super event
end

#supervise(ref) ⇒ Object



36
37
38
39
40
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 36

def supervise(ref)
  @supervisor = ref
  behaviour!(Linking).link ref
  true
end

#un_supervise(ref) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/concurrent/actor/behaviour/supervised.rb', line 42

def un_supervise(ref)
  if @supervisor == ref
    behaviour!(Linking).unlink ref
    @supervisor = nil
    true
  else
    false
  end
end