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

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/supervision/container/instance.rb,
lib/celluloid/supervision/container/behavior/pool.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

#poolObject

Returns the value of attribute pool


34
35
36
# File 'lib/celluloid/supervision/container/behavior/pool.rb', line 34

def pool
  @pool
end

#pool_sizeObject

Returns the value of attribute pool_size


34
35
36
# File 'lib/celluloid/supervision/container/behavior/pool.rb', line 34

def pool_size
  @pool_size
end

Instance Method Details

#cleanupObject


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

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

#restartObject


46
47
48
49
50
51
52
53
# File 'lib/celluloid/supervision/container/instance.rb', line 46

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
# 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(?)
rescue => ex
  Internals::Logger.error("Error ( #{ex.class} ) at start of supervised instance of #{@type}")
  raise ex
end

#terminateObject


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

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