Class: Celluloid::Supervision::Container::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/supervision/container/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Instance

Returns a new instance of Instance.

Parameters:

  • options (Hash)

    a customizable set of options



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



6
7
8
# File 'lib/celluloid/supervision/container/instance.rb', line 6

def actor
  @actor
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/celluloid/supervision/container/instance.rb', line 6

def name
  @name
end

Instance Method Details

#cleanupObject



67
68
69
# File 'lib/celluloid/supervision/container/instance.rb', line 67

def cleanup
  @registry.delete(@name) if @name
end

#restartObject



52
53
54
55
56
57
58
59
# 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

#startObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#terminateObject



61
62
63
64
65
# File 'lib/celluloid/supervision/container/instance.rb', line 61

def terminate
  @actor.terminate if @actor
  cleanup
rescue DeadActorError
end