Class: Concurrent::Actress::Core Private
- Inherits:
-
Object
- Object
- Concurrent::Actress::Core
- Defined in:
- lib/concurrent/actress/core.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
devel: core should not block on anything, e.g. it cannot wait on children to terminate that would eat up all threads in task pool and deadlock
Core of the actor
Instance Attribute Summary collapse
- #executor ⇒ Object readonly private
- #name ⇒ Object readonly private
- #path ⇒ Object readonly private
- #reference ⇒ Object readonly private
- #terminated ⇒ Object readonly private
Instance Method Summary collapse
- #add_child(child) ⇒ Object private
-
#children ⇒ Array<Reference>
private
Of children actors.
-
#guard! ⇒ Object
private
ensures that we are inside of the executor.
-
#initialize(opts = {}, &block) ⇒ Core
constructor
private
A new instance of Core.
-
#on_envelope(envelope) ⇒ Object
private
is executed by Reference scheduling processing of new messages can be called from other alternative Reference implementations.
-
#parent ⇒ Reference?
private
Of parent actor.
- #remove_child(child) ⇒ Object private
-
#terminate! ⇒ Object
private
Terminates the actor.
-
#terminated? ⇒ true, false
private
If actor is terminated.
Methods included from TypeCheck
#Child!, #Child?, #Match!, #Match?, #Type!, #Type?
Constructor Details
#initialize(opts = {}, &block) ⇒ Core
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Core.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/concurrent/actress/core.rb', line 25 def initialize(opts = {}, &block) @mailbox = Array.new @one_by_one = OneByOne.new # noinspection RubyArgCount @terminated = Event.new @executor = Type! opts.fetch(:executor, Concurrent.configuration.global_task_pool), Executor @children = Set.new @reference = Reference.new self @name = (Type! opts.fetch(:name), String, Symbol).to_s parent = opts[:parent] @parent_core = (Type! parent, Reference, NilClass) && parent.send(:core) if @parent_core.nil? && @name != '/' raise 'only root has no parent' end @path = @parent_core ? File.join(@parent_core.path, @name) : @name @logger = opts[:logger] @parent_core.add_child reference if @parent_core @actress_class = actress_class = Child! opts.fetch(:class), Context args = opts.fetch(:args, []) initialized = Type! opts[:initialized], IVar, NilClass schedule_execution do begin @actress = actress_class.new *args, &block @actress.send :initialize_core, self initialized.set true if initialized rescue => ex log ERROR, ex terminate! initialized.fail ex if initialized end end end |
Instance Attribute Details
#executor ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/concurrent/actress/core.rb', line 14 def executor @executor end |
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/concurrent/actress/core.rb', line 14 def name @name end |
#path ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/concurrent/actress/core.rb', line 14 def path @path end |
#reference ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/concurrent/actress/core.rb', line 14 def reference @reference end |
#terminated ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/concurrent/actress/core.rb', line 14 def terminated @terminated end |
Instance Method Details
#add_child(child) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
75 76 77 78 79 80 |
# File 'lib/concurrent/actress/core.rb', line 75 def add_child(child) guard! Type! child, Reference @children.add child nil end |
#children ⇒ Array<Reference>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns of children actors.
69 70 71 72 |
# File 'lib/concurrent/actress/core.rb', line 69 def children guard! @children.to_a end |
#guard! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
ensures that we are inside of the executor
134 135 136 137 138 |
# File 'lib/concurrent/actress/core.rb', line 134 def guard! unless Actress.current == reference raise "can be called only inside actor #{reference} but was #{Actress.current}" end end |
#on_envelope(envelope) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
is executed by Reference scheduling processing of new messages can be called from other alternative Reference implementations
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/concurrent/actress/core.rb', line 94 def on_envelope(envelope) schedule_execution do if terminated? reject_envelope envelope else @mailbox.push envelope end process_envelopes? end nil end |
#parent ⇒ Reference?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns of parent actor.
64 65 66 |
# File 'lib/concurrent/actress/core.rb', line 64 def parent @parent_core && @parent_core.reference end |
#remove_child(child) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 86 87 88 89 |
# File 'lib/concurrent/actress/core.rb', line 83 def remove_child(child) schedule_execution do Type! child, Reference @children.delete child end nil end |
#terminate! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Terminates the actor. Any Envelope received after termination is rejected. Terminates all its children, does not wait until they are terminated.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/concurrent/actress/core.rb', line 114 def terminate! guard! @children.each do |ch| ch.send(:core).tap { |core| core.send(:schedule_execution) { core.terminate! } } end @terminated.set @parent_core.remove_child reference if @parent_core @mailbox.each do |envelope| reject_envelope envelope log DEBUG, "rejected #{envelope.message} from #{envelope.sender_path}" end @mailbox.clear nil end |
#terminated? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Actor rejects envelopes when terminated.
Returns if actor is terminated.
108 109 110 |
# File 'lib/concurrent/actress/core.rb', line 108 def terminated? @terminated.set? end |