Class: Celluloid::Supervisor

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/celluloid/supervisor.rb

Overview

Supervisors are actors that watch over other actors and restart them if they crash

Constant Summary

Constants included from Celluloid

SHUTDOWN_TIMEOUT, VERSION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Celluloid

#abort, actor?, #after, #alive?, #async, cores, current_actor, #current_actor, #defer, #every, exception_handler, #exclusive, exclusive?, #future, included, #link, #linked_to?, #links, #method_missing, #name, #notify_link, #notify_unlink, #receive, receive, shutdown, #signal, #sleep, sleep, #tasks, #terminate, #unlink, uuid, version, #wait, #wrapped_object

Constructor Details

#initialize(name, klass, *args, &block) ⇒ Supervisor

Returns a new instance of Supervisor.



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

def initialize(name, klass, *args, &block)
  @name, @klass, @args, @block = name, klass, args, block
  @started = false

  start_actor
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Celluloid

Class Attribute Details

.rootObject

Define the root of the supervision tree



13
14
15
# File 'lib/celluloid/supervisor.rb', line 13

def root
  @root
end

Instance Attribute Details

#actorObject (readonly)

Retrieve the actor this supervisor is supervising



9
10
11
# File 'lib/celluloid/supervisor.rb', line 9

def actor
  @actor
end

Class Method Details

.supervise(klass, *args, &block) ⇒ Object



15
16
17
# File 'lib/celluloid/supervisor.rb', line 15

def supervise(klass, *args, &block)
  new(nil, klass, *args, &block)
end

.supervise_as(name, klass, *args, &block) ⇒ Object



19
20
21
# File 'lib/celluloid/supervisor.rb', line 19

def supervise_as(name, klass, *args, &block)
  new(name, klass, *args, &block)
end

Instance Method Details

#finalizeObject



31
32
33
# File 'lib/celluloid/supervisor.rb', line 31

def finalize
  @actor.terminate if @actor and @actor.alive?
end

#inspectObject



63
64
65
66
67
# File 'lib/celluloid/supervisor.rb', line 63

def inspect
  str = "#<#{self.class}(#{@klass}):0x#{object_id.to_s(16)}"
  str << " " << @args.map { |arg| arg.inspect }.join(' ') unless @args.empty?
  str << ">"
end

#restart_actor(actor, reason) ⇒ Object

When actors die, regardless of the reason, restart them



56
57
58
59
60
61
# File 'lib/celluloid/supervisor.rb', line 56

def restart_actor(actor, reason)
  # If the actor we're supervising exited cleanly, exit the supervisor cleanly too
  terminate unless reason

  start_actor if @started
end

#start_actor(start_attempts = 3, sleep_interval = 30) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/celluloid/supervisor.rb', line 35

def start_actor(start_attempts = 3, sleep_interval = 30)
  failures = 0

  begin
    @actor = @klass.new_link(*@args, &@block)
  rescue
    failures += 1
    if failures >= start_attempts
      failures = 0

      Logger.warn("#{@klass} is crashing on initialize too quickly, sleeping for #{sleep_interval} seconds")
      sleep sleep_interval
    end
    retry
  end

  @started = true
  Actor[@name] = @actor if @name
end