Class: Celluloid::SupervisionGroup

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

Overview

Supervise collections of actors as a group

Defined Under Namespace

Classes: Member

Constant Summary

Constants included from Celluloid

BARE_OBJECT_WARNING_MESSAGE, DeadActorError, Error, LINKING_TIMEOUT, NotActorError, OWNER_IVAR, TIMER_QUANTUM, TimeoutError, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Celluloid

#abort, actor?, #after, #async, boot, #call_chain_id, cores, #current_actor, #defer, detect_recursion, #every, exception_handler, #exclusive, #exclusive?, #future, included, init, #link, #linked_to?, #links, mailbox, #monitor, #monitoring?, #receive, register_shutdown, running?, shutdown, #signal, #sleep, stack_dump, start, suspend, #tasks, #terminate, #timeout, #unlink, #unmonitor, uuid, version, #wait

Constructor Details

#initialize(registry = nil) {|current_actor| ... } ⇒ SupervisionGroup

Start the group

Yields:


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

def initialize(registry = nil)
  @members = []
  @registry = registry || Celluloid.actor_system.registry

  yield current_actor if block_given?
end

Class Method Details

.blocksObject

Actors or sub-applications to be supervised


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

def blocks
  @blocks ||= []
end

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

Register a pool of actors to be launched on group startup


49
50
51
52
53
# File 'lib/celluloid/supervision_group.rb', line 49

def pool(klass, *args, &block)
  blocks << lambda do |group|
    group.pool(klass, prepare_options(args, :block => block))
  end
end

.prepare_options(args, options = {}) ⇒ Object


55
56
57
# File 'lib/celluloid/supervision_group.rb', line 55

def prepare_options(args, options = {})
  ( ( args.length == 1 and args[0].is_a? Hash ) ? args[0] : { :args => args } ).merge( options )
end

.run(registry = nil) ⇒ Object

Run the application in the foreground with a simple watchdog


24
25
26
27
28
29
30
31
32
33
# File 'lib/celluloid/supervision_group.rb', line 24

def run(registry = nil)
  loop do
    supervisor = run!(registry)

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

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

.run!(registry = nil) ⇒ Object

Start this application (and watch it with a supervisor)


14
15
16
17
18
19
20
21
# File 'lib/celluloid/supervision_group.rb', line 14

def run!(registry = nil)
  group = new(registry) do |_group|
    blocks.each do |block|
      block.call(_group)
    end
  end
  group
end

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

Register an actor class or a sub-group to be launched and supervised


36
37
38
39
40
# File 'lib/celluloid/supervision_group.rb', line 36

def supervise(klass, *args, &block)
  blocks << lambda do |group|
    group.add(klass, prepare_options(args, :block => block))
  end
end

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


42
43
44
45
46
# File 'lib/celluloid/supervision_group.rb', line 42

def supervise_as(name, klass, *args, &block)
  blocks << lambda do |group|
    group.add(klass, prepare_options(args, :block => block, :as => name))
  end
end

Instance Method Details

#[](actor_name) ⇒ Object


95
96
97
# File 'lib/celluloid/supervision_group.rb', line 95

def [](actor_name)
  @registry[actor_name]
end

#actorsObject


91
92
93
# File 'lib/celluloid/supervision_group.rb', line 91

def actors
  @members.map(&:actor)
end

#add(klass, options) ⇒ Object


85
86
87
88
89
# File 'lib/celluloid/supervision_group.rb', line 85

def add(klass, options)
  member = Member.new(@registry, klass, options)
  @members << member
  member.actor
end

#pool(klass, options = {}) ⇒ Object


80
81
82
83
# File 'lib/celluloid/supervision_group.rb', line 80

def pool(klass, options = {})
  options[:method] = 'pool_link'
  add(klass, options)
end

#restart_actor(actor, reason) ⇒ Object

Restart a crashed actor


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/celluloid/supervision_group.rb', line 100

def restart_actor(actor, reason)
  member = @members.find do |_member|
    _member.actor == actor
  end
  raise "a group member went missing. This shouldn't be!" unless member

  if reason
    member.restart
  else
    member.cleanup
    @members.delete(member)
  end
end

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


72
73
74
# File 'lib/celluloid/supervision_group.rb', line 72

def supervise(klass, *args, &block)
  add(klass, self.class.prepare_options(args, :block => block))
end

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


76
77
78
# File 'lib/celluloid/supervision_group.rb', line 76

def supervise_as(name, klass, *args, &block)
  add(klass, self.class.prepare_options(args, :block => block, :as => name))
end