class Celluloid::Supervision::Container::Instance

Attributes

actor[R]
name[R]

Public Class Methods

new(configuration = {}) click to toggle source

@option options [#call, Object] :args ([]) arguments array for the

actor's constructor (lazy evaluation if it responds to #call)
# File lib/celluloid/supervision/container/instance.rb, line 10
def initialize(configuration = {})
  @type = configuration.delete(:type)
  @registry = configuration.delete(:registry)
  @branch = configuration.delete(:branch) || :services
  @configuration = configuration

  # allows injections inside initialize, start, and restart
  @injections = configuration.delete(:injections) || {}
  invoke_injection(:before_initialize)

  # Stringify keys :/
  # de @configuration = configuration.each_with_object({}) { |(k,v), h| h[k.to_s] = v }

  @name = @configuration[:as]
  @block = @configuration[:block]
  @args = prepare_args(@configuration[:args])
  @method = @configuration[:method] || "new_link"
  add_accessors
  invoke_injection(:after_initialize)
  start
end

Public Instance Methods

cleanup() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 67
def cleanup
  @registry.delete(@name) if @name
end
restart() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 52
def restart
  # no need to reset @actor, as this is called in an `exclusive {}` block
  # @actor = nil
  # cleanup
  invoke_injection(:before_restart)
  start
  invoke_injection(:after_restart)
end
start() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 32
def start
  invoke_injection(:before_start)
  @actor = @type.send(@method, *@args, &@block)
  @registry.add(@name, @actor, @branch) if @name
  invoke_injection(:after_start)
rescue Celluloid::TaskTimeout => ex
  Internals::Logger.error("TaskTimeout at start of supervised instance of #{@type}")
  raise ex
  #           TODO: Implement timeout/retry.
  #           unless ( @retry += 1 ) <= INSTANCE_RETRY_LIMIT
  #             raise ex
  #           end
  #           Internals::Logger.warn("TaskTimeout at start of supervised actor. Retrying in #{INSTANCE_RETRY_WAIT} seconds. ( Attempt #{@retry} of #{INSTANCE_RETRY_LIMIT} )")
  #           sleep INSTANCE_RETRY_WAIT
  #           retry
rescue => ex
  Internals::Logger.error("Error ( #{ex.class} ) at start of supervised instance of #{@type}")
  raise ex
end
terminate() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 61
def terminate
  @actor.terminate if @actor
  cleanup
rescue DeadActorError
end

Private Instance Methods

add_accessors() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 73
def add_accessors
  remove_accessors
  if @configuration[:accessors].is_a? Array
    # TODO: Decide which level to keep, and only keep that.
    #       Do we provide access by Celluloid.accessor
    #       Do we provide access by Celluloid.actor_system.accessor
    @configuration[:accessors].each do |name|
      Celluloid.instance_exec(@configuration[:as], name) do |actor, where|
        define_method(name) do
          Celluloid.actor_system[actor]
        end
      end
      Celluloid::Actor::System.instance_exec(@configuration[:as], name) do |actor, where|
        define_method(name) do
          Celluloid.actor_system[actor]
        end
      end
    end
  end
end
invoke_injection(name) click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 107
def invoke_injection(name)
  return unless @injections
  block = @injections[name]
  instance_eval(&block) if block.is_a? Proc
end
prepare_args(args) click to toggle source

Executes args if it has the method call, and converts the return value to an Array. Otherwise, it just converts it to an Array.

# File lib/celluloid/supervision/container/instance.rb, line 115
def prepare_args(args)
  args = args.call if args.respond_to?(:call)
  Array(args)
end
remove_accessors() click to toggle source
# File lib/celluloid/supervision/container/instance.rb, line 94
def remove_accessors
  if @configuration[:accessors].is_a? Array
    @configuration[:accessors].each do |name|
      Celluloid.instance_eval do
        remove_method(name) rescue nil # avoid warnings in tests
      end
      Celluloid::Actor::System.instance_eval do
        remove_method(name) rescue nil # avoid warnings in tests
      end
    end
  end
end