Class: Concurrent::Actor::AbstractContext Abstract
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/concurrent-ruby-edge/concurrent/actor/context.rb |
Overview
implement #on_message and #behaviour_definition
New actor is defined by subclassing RestartingContext
, Context
and defining its abstract methods. AbstractContext
can be subclassed directly to implement more specific behaviour see Root
implementation.
-
> Basic Context of an
Actor
. It supports only linking and it simply terminates on error. Uses Behaviour.basic_behaviour_definition: -
>
Context
of anActor
for robust systems. It supports supervision, linking, pauses on error. Uses Behaviour.restarting_behaviour_definition
Example of ac actor definition:
Message = {Struct.new} :action, :value class AnActor < {Concurrent::Actor::RestartingContext} def initialize(init) @counter = init end # override #on_message to define actor's behaviour on message received def ( ) case .action when :add @counter = @counter + .value when :subtract @counter = @counter - .value when :value @counter else pass end end # set counter to zero when there is an error def on_event(event) if event == :reset @counter = 0 # ignore initial value end end end an_actor = {AnActor.spawn} name: 'an_actor', args: 10 an_actor << {Message.new}(:add, 1) << {Message.new}(:subtract, 2) an_actor.ask!(Message.new(:value, nil)) # => 9 an_actor << :boo << {Message.new}(:add, 1) an_actor.ask!(Message.new(:value, nil)) # => 1 an_actor << :terminate! # => #<Concurrent::Actor::Reference:0x7fbedc137688 /an_actor (AnActor)>
See methods of AbstractContext
what else can be tweaked, e.g #default_reference_class
Constant Summary
Concern::Logging
- Included
Class Method Summary
-
.spawn(name_or_opts, *args, &block)
Behaves as spawn but
:class
is auto-inserted based on receiver so it can be omitted. -
.spawn!(name_or_opts, *args, &block)
behaves as spawn! but
:class
is auto-inserted based on receiver so it can be omitted. - .to_spawn_options(name_or_opts, *args) private
Instance Attribute Summary
Instance Method Summary
-
#<<(message)
Alias for #tell.
- #ask(message) (also: #ask!)
-
#ask!(message)
Alias for #ask.
- #behaviour_definition ⇒ Array<Array(Behavior::Abstract, Array<Object>)>
-
#dead_letter_routing ⇒ Reference
Defines an actor responsible for dead letters.
-
#default_executor ⇒ Executor
override to se different default executor, e.g.
-
#default_reference_class ⇒ CLass
override if different class for reference is needed.
- #envelope ⇒ Envelope
-
#on_event(event)
override to add custom code invocation on internal events like
:terminated
,:resumed
,anError
. - #on_message(message) ⇒ Object abstract
-
#pass
if you want to pass the message to next behaviour, usually
Behaviour::ErrorsOnUnknownMessage
-
#tell(message)
(also: #<<)
tell self a message.
- #initialize_core(core) private
- #on_envelope(envelope) Internal use only Internal use only
InternalDelegations
- Included
#behaviour | see Core#behaviour |
#behaviour! | see Core#behaviour! |
#children, #context, #dead_letter_routing, | |
#log | delegates to core.log. |
#redirect, #terminate! |
Concern::Logging
- Included
#log | Logs through Concurrent.global_logger, it can be overridden by setting @logger. |
PublicDelegations
- Included
#actor_class | Alias for PublicDelegations#context_class. |
#context_class, #executor, #name, #parent, #path, | |
#ref | Alias for PublicDelegations#reference. |
#reference |
TypeCheck
- Included
Class Method Details
.spawn(name_or_opts, *args, &block)
Behaves as Concurrent::Actor.spawn but :class
is auto-inserted based on receiver so it can be omitted.
.spawn!(name_or_opts, *args, &block)
behaves as Concurrent::Actor.spawn! but :class
is auto-inserted based on receiver so it can be omitted.
.to_spawn_options(name_or_opts, *args) (private)
[ GitHub ]# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 132
def self. (name_or_opts, *args) if name_or_opts.is_a? ::Hash if name_or_opts.key?(:class) && name_or_opts[:class] != self raise ArgumentError, ':class option is ignored when calling on context class, use Actor.spawn instead' end name_or_opts.merge class: self else { class: self, name: name_or_opts, args: args } end end
Instance Attribute Details
#core (readonly)
[ GitHub ]# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 30
attr_reader :core
Instance Method Details
#<<(message)
Alias for #tell.
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 102
alias_method :<<, :tell
#ask(message) Also known as: #ask!
[ GitHub ]# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 98
def ask( ) raise 'actor cannot ask itself' end
#ask!(message)
Alias for #ask.
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 103
alias_method :ask!, :ask
#behaviour_definition ⇒ Array<Array(Behavior::Abstract
, Array<Object>)>
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 72
def behaviour_definition raise NotImplementedError end
#dead_letter_routing ⇒ Reference
Defines an actor responsible for dead letters. Any rejected message send with Reference#tell is sent there, a message with future is considered already monitored for failures. Default behaviour is to use #dead_letter_routing
of the parent, so if no #dead_letter_routing
method is overridden in parent-chain the message ends up in Actor.root.dead_letter_routing
agent which will log warning.
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 67
def dead_letter_routing parent.dead_letter_routing end
#default_executor ⇒ Executor
override to se different default executor, e.g. to change it to global_fast_executor
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 89
def default_executor Concurrent.global_io_executor end
#default_reference_class ⇒ CLass
override if different class for reference is needed
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 83
def default_reference_class Reference end
#envelope ⇒ Envelope
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 77
def envelope @envelope or raise 'envelope not set' end
#initialize_core(core) (private)
[ GitHub ]#on_envelope(envelope)
#on_event(event)
override to add custom code invocation on internal events like :terminated
, :resumed
, anError
.
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 42
def on_event(event) end
#on_message(message) ⇒ Object
override to define Actor’s behaviour
self should not be returned (or sent to other actors), #reference
should be used instead
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 37
def ( ) raise NotImplementedError end
#pass
if you want to pass the message to next behaviour, usually Behaviour::ErrorsOnUnknownMessage
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 55
def pass core.behaviour!(Behaviour::ExecutesContext).pass envelope end
#tell(message) Also known as: #<<
tell self a message
# File 'lib/concurrent-ruby-edge/concurrent/actor/context.rb', line 94
def tell( ) reference.tell end