Class: Concurrent::Actor::Behaviour::Termination

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

Overview

Note:

Actor rejects envelopes when terminated.

Handles actor termination.

Instance Attribute Summary collapse

Attributes inherited from Abstract

#core, #subsequent

Instance Method Summary collapse

Methods inherited from Abstract

#broadcast, #on_event, #pass, #reject_envelope

Methods included from InternalDelegations

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

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) ⇒ Termination

Returns a new instance of Termination.



13
14
15
16
# File 'lib/concurrent/actor/behaviour/termination.rb', line 13

def initialize(core, subsequent)
  super core, subsequent
  @terminated = Event.new
end

Instance Attribute Details

#terminatedObject (readonly)

Returns the value of attribute terminated.



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

def terminated
  @terminated
end

Instance Method Details

#on_envelope(envelope) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/concurrent/actor/behaviour/termination.rb', line 24

def on_envelope(envelope)
  case envelope.message
  when :terminated?
    terminated?
  when :terminate!
    terminate!
  when :terminated_event
    terminated
  else
    if terminated?
      reject_envelope envelope
      MESSAGE_PROCESSED
    else
      pass envelope
    end
  end
end

#terminate!Object

Terminates the actor. Any Envelope received after termination is rejected. Terminates all its children, does not wait until they are terminated.



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

def terminate!
  return true if terminated?
  terminated.set
  broadcast(:terminated) # TODO do not end up in Dead Letter Router
  parent << :remove_child if parent
  true
end

#terminated?true, false

Note:

Actor rejects envelopes when terminated.

Returns if actor is terminated.

Returns:

  • (true, false)

    if actor is terminated



20
21
22
# File 'lib/concurrent/actor/behaviour/termination.rb', line 20

def terminated?
  @terminated.set?
end