Class: Celluloid::Supervision::Container

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/celluloid/supervision/container.rb,
lib/celluloid/supervision/supervise.rb,
lib/celluloid/supervision/container/behavior.rb,
lib/celluloid/supervision/container/instance.rb,
lib/celluloid/supervision/deprecate/supervise.rb,
lib/celluloid/supervision/container/injections.rb,
lib/celluloid/supervision/container/behavior/tree.rb

Direct Known Subclasses

Service::Public, Service::Root

Defined Under Namespace

Modules: Behavior Classes: Injection, Instance, Tree

Constant Summary

Constants included from Celluloid

Celluloid::SupervisionGroup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|current_actor| ... } ⇒ Container

Start the container.

Yields:

  • (current_actor)


60
61
62
63
64
65
66
67
# 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

Instance Attribute Details

#registryObject

Returns the value of attribute registry.



57
58
59
# File 'lib/celluloid/supervision/container.rb', line 57

def registry
  @registry
end

Class Method Details

.blocksObject

Actors or sub-applications to be supervised



28
29
30
# File 'lib/celluloid/supervision/container.rb', line 28

def blocks
  @blocks ||= []
end

.define(options) ⇒ Object



10
11
12
# File 'lib/celluloid/supervision/container.rb', line 10

def define(options)
  Configuration.define(top(options))
end

.deploy(options) ⇒ Object



14
15
16
# File 'lib/celluloid/supervision/container.rb', line 14

def deploy(options)
  Configuration.deploy(top(options))
end

.run(*args) ⇒ Object

Run the application in the foreground with a simple watchdog



43
44
45
46
47
48
49
50
51
52
# 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!(*args) ⇒ Object

Start this application (and watch it with a supervisor)



33
34
35
36
37
38
39
40
# 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) ⇒ Object



23
24
25
26
27
# File 'lib/celluloid/supervision/supervise.rb', line 23

def supervise(config, &block)
  blocks << lambda do |container|
    container.add(config, &block)
  end
end

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



87
88
89
90
91
# 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) ⇒ Object



18
19
20
21
22
23
24
25
# 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

Instance Method Details

#[](actor_name) ⇒ Object



110
111
112
# File 'lib/celluloid/supervision/container.rb', line 110

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

#actorsObject



100
101
102
# File 'lib/celluloid/supervision/container.rb', line 100

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

#add(configuration) ⇒ Object



71
72
73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



104
105
106
107
108
# File 'lib/celluloid/supervision/container.rb', line 104

def find(actor)
  @actors.find do |instance|
    instance.actor == actor
  end
end

#remove(actor) ⇒ Object



94
95
96
97
98
# 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_accessorsObject



91
92
# File 'lib/celluloid/supervision/container.rb', line 91

def remove_accessors
end

#restart_actor(actor, reason) ⇒ Object

Restart a crashed actor



115
116
117
118
119
120
121
122
123
124
125
126
# 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

#shutdownObject



128
129
130
131
# File 'lib/celluloid/supervision/container.rb', line 128

def shutdown
  @state = :shutdown
  finalize
end

#supervise(*args, &block) ⇒ Object



29
30
31
# File 'lib/celluloid/supervision/supervise.rb', line 29

def supervise(config, &block)
  add(Configuration.options(config, block: block))
end

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



100
101
102
# File 'lib/celluloid/supervision/deprecate/supervise.rb', line 100

def supervise_as(name, *args, &block)
  add(Configuration.options(args, block: block, as: name))
end