Class: Celluloid::Group

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

Overview

Supervise collections of actors as a group

Defined Under Namespace

Classes: Member

Constant Summary

Constants included from Celluloid

SHUTDOWN_TIMEOUT, VERSION

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, #inspect, #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

#initializeGroup

Start the group



39
40
41
42
43
44
45
46
47
48
# File 'lib/celluloid/group.rb', line 39

def initialize
  @actors = {}

  # This is some serious lolcode, but like... start the supervisors for
  # this group
  self.class.members.each do |member|
    actor = member.start
    @actors[actor] = member
  end
end

Dynamic Method Handling

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

Class Method Details

.membersObject

Actors or sub-applications to be supervised



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

def members
  @members ||= []
end

.runObject

Run the application in the foreground with a simple watchdog



17
18
19
20
21
22
23
24
25
26
# File 'lib/celluloid/group.rb', line 17

def run
  loop do
    supervisor = run!

    # Take five, toplevel supervisor
    sleep 5 while supervisor.alive?

    Logger.error "!!! Celluloid::Group #{self} crashed. Restarting..."
  end
end

.run!Object

Start this application (and watch it with a supervisor)



14
# File 'lib/celluloid/group.rb', line 14

alias_method :run!, :supervise

.supervise(klass, options = {}) ⇒ Object

Register an actor class or a sub-application class to be launched and supervised while this application is running. Available options are:

  • as: register this application in the Celluloid::Actor[] directory

  • args: start the actor with the given arguments



33
34
35
# File 'lib/celluloid/group.rb', line 33

def supervise(klass, options = {})
  members << Member.new(klass, options)
end

Instance Method Details

#finalizeObject

Terminate the group



51
52
53
54
55
56
57
58
# File 'lib/celluloid/group.rb', line 51

def finalize
  @actors.each do |actor, _|
    begin
      actor.terminate
    rescue DeadActorError
    end
  end
end

#restart_actor(actor, reason) ⇒ Object

Restart a crashed actor



61
62
63
64
65
66
67
68
69
70
# File 'lib/celluloid/group.rb', line 61

def restart_actor(actor, reason)
  member = @actors.delete actor
  raise "a group member went missing. This shouldn't be!" unless member

  # Ignore supervisors that shut down cleanly
  return unless reason

  actor = member.start
  @actors[actor] = member
end