File: supervising.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (39 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Concurrent
  module Actor
    module Behaviour

      # Handles supervised actors. Handle configures what to do with failed child: :terminate!, :resume!, :reset!,
      # or :restart!. Strategy sets :one_for_one (restarts just failed actor) or :one_for_all (restarts all child actors).
      # @note TODO missing example
      # @note this will change in next version to support supervision trees better
      class Supervising < Abstract
        def initialize(core, subsequent, core_options, handle, strategy)
          super core, subsequent, core_options
          @handle   = Match! handle, :terminate!, :resume!, :reset!, :restart!
          @strategy = case @handle
                      when :terminate!
                        Match! strategy, nil
                      when :resume!
                        Match! strategy, :one_for_one
                      when :reset!, :restart!
                        Match! strategy, :one_for_one, :one_for_all
                      end
        end

        def on_envelope(envelope)
          case envelope.message
          when Exception, :paused
            receivers = if @strategy == :one_for_all
                          children
                        else
                          [envelope.sender]
                        end
            receivers.each { |ch| ch << @handle }
          else
            pass envelope
          end
        end
      end
    end
  end
end