Class: Concurrent::Actress::Core Private

Inherits:
Object
  • Object
show all
Includes:
TypeCheck, Logging
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.

Note:

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

Instance Method Summary collapse

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.

Parameters:

  • block (Proc)

    for class instantiation

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • name (String)
  • parent (Reference, nil)

    of an actor spawning this one

  • actress_class (Context)

    a class to be instantiated defining Actor’s behaviour

  • args (Array<Object>)

    arguments for actress_class instantiation

  • executor, (Executor)

    default is ‘Concurrent.configuration.global_task_pool`

  • initialized, (IVar, nil)

    if present it’ll be set or failed after Concurrent::Actress::Context initialization

  • logger (Proc, nil)

    a proc accepting (level, progname, message = nil, &block) params, can be used to hook actor instance to any logging system



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

#executorObject (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

#nameObject (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

#pathObject (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

#referenceObject (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

#terminatedObject (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

#childrenArray<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.

Returns:



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

Parameters:



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

#parentReference?

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.

Returns:



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.

Note:

Actor rejects envelopes when terminated.

Returns if actor is terminated.

Returns:

  • (true, false)

    if actor is terminated



108
109
110
# File 'lib/concurrent/actress/core.rb', line 108

def terminated?
  @terminated.set?
end