Skip to content
Richard Wilson edited this page Mar 8, 2014 · 14 revisions

Supervision groups provide a declarative way to supervise many actors at once. You can start or stop a group of actors in one fell swoop, and ensure that if any of them crash they'll be restarted automatically. Groups can also supervise other groups, allowing you to build supervision trees for all the components in your system, even the supervisors themselves!

To define a group, subclass Celluloid::SupervisionGroup:

class MyGroup < Celluloid::SupervisionGroup
  supervise MyActor, as: :my_actor
  supervise AnotherActor, as: :another_actor, args: [{start_working_right_now: true}]
  pool MyWorker, as: :my_worker_pool, size: 5
end

This group, when started, will launch MyActor and AnotherActor and automatically register them as Celluloid::Actor[:my_actor] and Celluloid::Actor[:another_actor]. It will also start a pool of 5 MyWorkers and register them as Celluloid::Actor[:my_worker_pool]. See Pools for more information about working with actor pools.

Use :args to pass arguments to the actor's initialize method, its value should always be an array.
The example above will result in calling something like AnotherActor.new(start_working_right_now: true).

To launch your group, do:

MyGroup.run

This launches your group in the foreground. To launch it in the background, do:

MyGroup.run!

Customized supervision

class Person
  include Celluloid

  def initialize(age, height, weight)
    p(age: age, height: height, weight: weight)
  end

  def bar
    rand(20)
  end
end

supervisor = Celluloid::SupervisionGroup.run!

supervisor.pool(Person, as: :people, args: [1,2,3], size: 3)

p Celluloid::Actor.all.size

10.times do
  # DISCLAIMER: This is new functionality only available on master as of March 8th 2014.
  # Use: p Celluloid::Actor[:people].bar for version 0.15.2
  p supervisor[:people].bar
end

See Also: