class Celluloid::Supervision::Container
Attributes
registry[RW]
Public Class Methods
blocks()
click to toggle source
Actors or sub-applications to be supervised
# File lib/celluloid/supervision/container.rb, line 28 def blocks @blocks ||= [] end
define(options)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 10 def define(options) Configuration.define(top(options)) end
deploy(options)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 14 def deploy(options) Configuration.deploy(top(options)) end
new(options={}) { |current_actor| ... }
click to toggle source
Start the container.
# File lib/celluloid/supervision/container.rb, line 60 def initialize(options={}) options = {registry: options} if options.is_a? Internals::Registry @state = :initializing @actors = [] # instances in the container @registry = options.delete(:registry) || Celluloid.actor_system.registry @branch = options.delete(:branch) || :services yield current_actor if block_given? end
run(options={})
click to toggle source
Run the application in the foreground with a simple watchdog
# File lib/celluloid/supervision/container.rb, line 43 def run(options={}) loop do supervisor = run!(options) # Take five, toplevel supervisor sleep 5 while supervisor.alive? # Why 5? Internals::Logger.error "!!! Celluloid::Supervision::Container #{self} crashed. Restarting..." end end
run!(options={})
click to toggle source
Start this application (and watch it with a supervisor)
# File lib/celluloid/supervision/container.rb, line 33 def run!(options={}) container = new(options) do |g| blocks.each do |block| block.call(g) end end container end
supervise(*args, &block)
click to toggle source
# File lib/celluloid/supervision/deprecate/supervise.rb, line 80 def supervise(*args, &block) blocks << lambda do |container| container.supervise(*args, &block) end end
supervise_as(name, *args, &block)
click to toggle source
# File lib/celluloid/supervision/deprecate/supervise.rb, line 87 def supervise_as(name, *args, &block) blocks << lambda do |container| container.supervise_as(name, *args, &block) end end
top(options)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 18 def top(options) { as: (options.delete(:as)), type: (options.delete(:type) || self), branch: (options.delete(:branch) || :services), supervise: options.delete(:supervise) || [], } end
Public Instance Methods
[](actor_name)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 110 def [](actor_name) @registry[actor_name] end
actors()
click to toggle source
# File lib/celluloid/supervision/container.rb, line 100 def actors @actors.map(&:actor) end
add(configuration)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 71 def add(configuration) Configuration.valid?(configuration, true) @actors << Instance.new(configuration.merge(registry: @registry, branch: @branch)) @state = :running add_accessors configuration Actor.current end
add_accessors(configuration)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 79 def add_accessors(configuration) if configuration[:as] unless methods.include? configuration[:as] self.class.instance_eval do define_method(configuration[:as]) do @registry[configuration[:as]] end end end end end
find(actor)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 104 def find(actor) @actors.find do |instance| instance.actor == actor end end
remove(actor)
click to toggle source
# File lib/celluloid/supervision/container.rb, line 94 def remove(actor) actor = Celluloid::Actor[actor] if actor.is_a? Symbol instance = find(actor) instance.terminate if instance end
remove_accessors()
click to toggle source
# File lib/celluloid/supervision/container.rb, line 91 def remove_accessors end
restart_actor(actor, reason)
click to toggle source
Restart a crashed actor
# File lib/celluloid/supervision/container.rb, line 115 def restart_actor(actor, reason) return if @state == :shutdown instance = find(actor) fail "a container instance went missing. This shouldn't be!" unless instance if reason exclusive { instance.restart } else instance.cleanup @actors.delete(instance) end end
shutdown()
click to toggle source
# File lib/celluloid/supervision/container.rb, line 128 def shutdown @state = :shutdown finalize end
supervise(*args, &block)
click to toggle source
# File lib/celluloid/supervision/deprecate/supervise.rb, line 95 def supervise(*args, &block) add(Configuration.options(args, block: block)) end
supervise_as(name, *args, &block)
click to toggle source
# File lib/celluloid/supervision/deprecate/supervise.rb, line 100 def supervise_as(name, *args, &block) add(Configuration.options(args, block: block, as: name)) end
Private Instance Methods
finalize()
click to toggle source
# File lib/celluloid/supervision/container.rb, line 135 def finalize if @actors @actors.reverse_each do |instance| instance.terminate @actors.delete(instance) end end end