Class: Celluloid::Supervisor

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/vendor/celluloid/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

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Celluloid

#abort, actor?, #after, #alive?, #async, current, current_actor, #current_actor, included, #link, #linked_to?, #links, #method_missing, #notify_link, #notify_unlink, #receive, receive, #signal, #sleep, sleep, #tasks, tasks, #terminate, #unlink, version, #wait, #wrapped_object

Constructor Details

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

Returns a new instance of Supervisor.



19
20
21
22
23
24
# File 'lib/vendor/celluloid/lib/celluloid/supervisor.rb', line 19

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

Instance Attribute Details

#actorObject (readonly)

Retrieve the actor this supervisor is supervising



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

def actor
  @actor
end

Class Method Details

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



11
12
13
# File 'lib/vendor/celluloid/lib/celluloid/supervisor.rb', line 11

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

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



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

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

Instance Method Details

#inspectObject



51
52
53
54
55
# File 'lib/vendor/celluloid/lib/celluloid/supervisor.rb', line 51

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



47
48
49
# File 'lib/vendor/celluloid/lib/celluloid/supervisor.rb', line 47

def restart_actor(actor, reason)
  start_actor if @started
end

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vendor/celluloid/lib/celluloid/supervisor.rb', line 26

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

  begin
    @actor = @klass.new_link(*@args, &@block)
  rescue => ex
    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