Class: Celluloid::Supervision::Container

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

Direct Known Subclasses

Service::Public, Service::Root

Defined Under Namespace

Modules: Behavior Classes: Injection, Instance, Pool, Tree

Constant Summary

Constants included from Celluloid

BARE_OBJECT_WARNING_MESSAGE, LINKING_TIMEOUT, OWNER_IVAR, VERSION

Instance Attribute Summary collapse

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?, public_registry, publish, #receive, register_shutdown, running?, shutdown, #signal, #sleep, stack_dump, stack_summary, start, suspend, #tasks, #terminate, #timeout, #unlink, #unmonitor, uuid, version, #wait

Constructor Details

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

Start the container.

Yields:


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

.pool(klass, config, &block) ⇒ Object

Register a pool of actors to be launched on group startup


39
40
41
42
43
# File 'lib/celluloid/supervision/container/behavior/pool.rb', line 39

def pool(klass, config, &block)
  blocks << lambda do |container|
    container.pool(klass, config, &block)
  end
end

.run(options = {}) ⇒ 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!(options = {}) ⇒ 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(config, &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

.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


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

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

#actorsObject


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

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


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

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

#pool(klass, config = {}, &block) ⇒ Object


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

def pool(klass, config = {}, &block)
  _ = supervise(pooling_options(config, block: block, actors: klass))
  _.actors.last
end

#remove(actor) ⇒ Object


93
94
95
96
97
# File 'lib/celluloid/supervision/container.rb', line 93

def remove(actor)
  actor = Celluloid::Actor[actor] if actor.is_a? Symbol
  instance = find(actor)
  instance.terminate if instance
end

#remove_accessorsObject


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

def remove_accessors; end

#restart_actor(actor, reason) ⇒ Object

Restart a crashed actor


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/celluloid/supervision/container.rb', line 114

def restart_actor(actor, reason)
  return if @state == :shutdown
  instance = find(actor)
  raise "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


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

def shutdown
  @state = :shutdown
  finalize
end

#supervise(config, &block) ⇒ Object


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

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