Class: Actor::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/actor/supervisor.rb

Defined Under Namespace

Modules: Assertions Classes: Actor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSupervisor

Returns a new instance of Supervisor.



6
7
8
# File 'lib/actor/supervisor.rb', line 6

def initialize
  @actors = Set.new
end

Instance Attribute Details

#actorsObject (readonly)

Returns the value of attribute actors.



3
4
5
# File 'lib/actor/supervisor.rb', line 3

def actors
  @actors
end

#exception_notifierObject



67
68
69
# File 'lib/actor/supervisor.rb', line 67

def exception_notifier
  @exception_notifier ||= proc { }
end

Instance Method Details

#add(address, thread) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/actor/supervisor.rb', line 10

def add address, thread
  actor = Actor.new address, thread

  actors << actor

  actor
end

#broadcast(message) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/actor/supervisor.rb', line 24

def broadcast message
  addresses = actors.map &:address

  addresses.each do |address|
    Messaging::Writer.(message, address)
  end
end

#pauseObject



32
33
34
# File 'lib/actor/supervisor.rb', line 32

def pause
  broadcast Message::Pause.new
end

#remove(address) ⇒ Object



18
19
20
21
22
# File 'lib/actor/supervisor.rb', line 18

def remove address
  actors.delete_if do |actor|
    actor.address == address
  end
end

#resumeObject



36
37
38
# File 'lib/actor/supervisor.rb', line 36

def resume
  broadcast Message::Resume.new
end

#start(&supplementary_action) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/actor/supervisor.rb', line 44

def start &supplementary_action
  Signal.trap 'CONT' do resume end
  Signal.trap 'INT' do stop end
  Signal.trap 'TSTP' do pause end

  loop do
    supplementary_action.() if supplementary_action

    actors.delete_if do |actor|
      thread = actor.thread
      thread.join TimeUnit.millisecond
    end

    break if actors.empty?

    Thread.pass
  end

rescue => error
  exception_notifier.(error)
  raise error
end

#stopObject



40
41
42
# File 'lib/actor/supervisor.rb', line 40

def stop
  broadcast Message::Stop.new
end