Skip to content

Commit 1b96989

Browse files
committed
Supervised and linked by default
Pass spawn options to behaviours
1 parent 595f7cd commit 1b96989

File tree

10 files changed

+39
-31
lines changed

10 files changed

+39
-31
lines changed

lib/concurrent/actor/behaviour/abstract.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Abstract
77

88
attr_reader :core, :subsequent
99

10-
def initialize(core, subsequent)
10+
def initialize(core, subsequent, core_options)
1111
@core = Type! core, Core
1212
@subsequent = Type! subsequent, Abstract, NilClass
1313
end

lib/concurrent/actor/behaviour/buffer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Behaviour
88
# and they can be processed before messages arriving into buffer. This allows to
99
# process internal actor messages like (`:link`, `:supervise`) processed first.
1010
class Buffer < Abstract
11-
def initialize(core, subsequent)
12-
super core, subsequent
11+
def initialize(core, subsequent, core_options)
12+
super core, subsequent, core_options
1313
@buffer = []
1414
@receive_envelope_scheduled = false
1515
end

lib/concurrent/actor/behaviour/linking.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ module Behaviour
3838
# got event #<RuntimeError: failed> from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
3939
# got event :reset from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
4040
class Linking < Abstract
41-
def initialize(core, subsequent)
42-
super core, subsequent
41+
def initialize(core, subsequent, core_options)
42+
super core, subsequent, core_options
4343
@linked = Set.new
44+
if core_options[:link] != false || core_options[:supervise] != false
45+
@linked.add Actor.current
46+
end
4447
end
4548

4649
def on_envelope(envelope)

lib/concurrent/actor/behaviour/pausing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Behaviour
88
# Reset also reinitialized context.
99
# @note TODO missing example
1010
class Pausing < Abstract
11-
def initialize(core, subsequent)
12-
super core, subsequent
11+
def initialize(core, subsequent, core_options)
12+
super core, subsequent, core_options
1313
@paused = false
1414
@buffer = []
1515
end

lib/concurrent/actor/behaviour/sets_results.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Behaviour
55
class SetResults < Abstract
66
attr_reader :error_strategy
77

8-
def initialize(core, subsequent, error_strategy)
9-
super core, subsequent
8+
def initialize(core, subsequent, core_options, error_strategy)
9+
super core, subsequent, core_options
1010
@error_strategy = Match! error_strategy, :just_log, :terminate!, :pause!
1111
end
1212

lib/concurrent/actor/behaviour/supervised.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ module Behaviour
2727
class Supervised < Abstract
2828
attr_reader :supervisor
2929

30-
def initialize(core, subsequent)
31-
super core, subsequent
32-
@supervisor = nil
30+
def initialize(core, subsequent, core_options)
31+
super core, subsequent, core_options
32+
33+
@supervisor = if core_options[:supervise] != false
34+
Actor.current
35+
else
36+
nil
37+
end
3338
end
3439

3540
def on_envelope(envelope)

lib/concurrent/actor/behaviour/supervising.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ module Behaviour
55
# Handles supervised actors. Handle configures what to do with failed child: :terminate!, :resume!, :reset!,
66
# or :restart!. Strategy sets :one_for_one (restarts just failed actor) or :one_for_all (restarts all child actors).
77
# @note TODO missing example
8+
# @note this will change in next version to support supervision trees better
89
class Supervising < Abstract
9-
def initialize(core, subsequent, handle, strategy)
10-
super core, subsequent
10+
def initialize(core, subsequent, core_options, handle, strategy)
11+
super core, subsequent, core_options
1112
@handle = Match! handle, :terminate!, :resume!, :reset!, :restart!
1213
@strategy = case @handle
1314
when :terminate!

lib/concurrent/actor/behaviour/termination.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Termination < Abstract
1111
# @return [Event] event which will become set when actor is terminated.
1212
attr_reader :terminated
1313

14-
def initialize(core, subsequent)
15-
super core, subsequent
14+
def initialize(core, subsequent, core_options)
15+
super core, subsequent, core_options
1616
@terminated = Concurrent::Event.new
1717
end
1818

lib/concurrent/actor/core.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class Core
3636
# @option opts [Context] actor_class a class to be instantiated defining Actor's behaviour
3737
# @option opts [Array<Object>] args arguments for actor_class instantiation
3838
# @option opts [Executor] executor, default is `Concurrent.configuration.global_task_pool`
39-
# @option opts [true, false] link, atomically link the actor to its parent
40-
# @option opts [true, false] supervise, atomically supervise the actor by its parent
39+
# @option opts [true, false] link, atomically link the actor to its parent (default: true)
40+
# @option opts [true, false] supervise, atomically supervise the actor by its parent (default: true)
4141
# @option opts [Class] reference a custom descendant of {Reference} to use
4242
# @option opts [Array<Array(Behavior::Abstract, Array<Object>)>] behaviour_definition, array of pairs
4343
# where each pair is behaviour class and its args, see {Behaviour.basic_behaviour_definition}
@@ -78,19 +78,9 @@ def initialize(opts = {}, &block)
7878
@block = block
7979
initialized = Type! opts[:initialized], IVar, NilClass
8080

81-
messages = []
82-
messages << :link if opts[:link]
83-
messages << :supervise if opts[:supervise]
84-
8581
schedule_execution do
8682
begin
8783
build_context
88-
89-
messages.each do |message|
90-
log DEBUG, "preprocessing #{message.inspect} from #{parent}"
91-
process_envelope Envelope.new(message, nil, parent, reference)
92-
end
93-
9484
initialized.set reference if initialized
9585
rescue => ex
9686
log ERROR, ex
@@ -221,7 +211,7 @@ def initialize_behaviours(opts)
221211
end
222212
@behaviours = {}
223213
@first_behaviour = @behaviour_definition.reverse.
224-
reduce(nil) { |last, (behaviour, args)| @behaviours[behaviour] = behaviour.new(self, last, *args) }
214+
reduce(nil) { |last, (behaviour, args)| @behaviours[behaviour] = behaviour.new(self, last, opts, *args) }
225215
end
226216
end
227217
end

spec/spec_helper.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717

1818
require 'concurrent'
1919

20-
logger = Logger.new($stderr)
21-
logger.level = Logger::WARN
20+
logger = Logger.new($stderr)
21+
logger.level = Logger::WARN
22+
23+
logger.formatter = lambda do |severity, datetime, progname, msg|
24+
format "[%s] %5s -- %s: %s\n",
25+
datetime.strftime('%Y-%m-%d %H:%M:%S.%L'),
26+
severity,
27+
progname,
28+
msg
29+
end
30+
2231
Concurrent.configuration.logger = lambda do |level, progname, message = nil, &block|
2332
logger.add level, message, progname, &block
2433
end

0 commit comments

Comments
 (0)