Class: Celluloid::Supervision::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/celluloid/supervision/constants.rb,
lib/celluloid/supervision/validation.rb,
lib/celluloid/supervision/configuration.rb,
lib/celluloid/supervision/configuration/instance.rb,
lib/celluloid/supervision/configuration/injections.rb

Defined Under Namespace

Modules: Error Classes: Injection, Instance

Constant Summary collapse

REMOVE_AT_EXPORT =

These are applied inside Supervision::Member ################

%i[
  configuration
  behavior
].freeze
INJECTIONS =
%i[
  configuration
  before_initialization
  after_initialization
  before_start
  before_restart
].freeze
@@parameters =

Using class variable so that parameters can be added by plugins.

{
   mandatory: [:type],
   optional: %i[
    as
    args
    block
  ],
   # TODO: Move these into Behaviors.
  plugins: [
    # de :size,        # Supervision::Pool
    # de :routers,     # Supervision::Coordinator
    # de :supervises   # Supervision::Tree
  ],
   meta: %i[
    registry
    branch
    method
  ]
}
@@arity =
{
  type: :args
}
@@aliases =
{
  name: :as,
  kind: :type,
  # de :pool => :size,   # TODO: Move into Behaviors.
  # de :supervise => :supervises
}
@@defaults =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/celluloid/supervision/configuration.rb', line 28

def initialize(options = {})
  @instances = [Instance.new]
  @branch = :services
  @i = 0 # incrementer of instances in this branch
  resync_accessors
  @configuration = options

  if options.is_a? Hash
    options[:configuration] ||= Container::Behavior.configure(options)
    @configuration = instance_eval(&options[:configuration])
    @supervisor ||= @configuration.fetch(:supervisor, :"Celluloid.services")
  end
  @supervisor ||= :"Celluloid.services"

  define(@configuration) if (@configuration.is_a?(Hash) || @configuration.is_a?(Array)) && @configuration.any?
end

Instance Attribute Details

#instancesObject

Returns the value of attribute instances.



26
27
28
# File 'lib/celluloid/supervision/configuration.rb', line 26

def instances
  @instances
end

Class Method Details

.alias!(aliased, original) ⇒ Object



94
95
96
# File 'lib/celluloid/supervision/constants.rb', line 94

def alias!(aliased, original)
  @@aliases[aliased] = original
end

.aliasesObject



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

def aliases
  @@aliases
end

.arityObject



82
83
84
# File 'lib/celluloid/supervision/constants.rb', line 82

def arity
  @@arity
end

.arity!(key, value) ⇒ Object



86
87
88
# File 'lib/celluloid/supervision/constants.rb', line 86

def arity!(key, value)
  @@arity[key] = value
end

.define(options = {}) ⇒ Object



9
10
11
# File 'lib/celluloid/supervision/configuration.rb', line 9

def define(options = {})
  new(options)
end

.deploy(options = {}) ⇒ Object



5
6
7
# File 'lib/celluloid/supervision/configuration.rb', line 5

def deploy(options = {})
  define(options).deploy
end

.options(config = {}, options = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/celluloid/supervision/validation.rb', line 30

def options(config = {}, options = {})
  configuration = config.merge(options)
  return configuration if configuration.is_a? Configuration
  configuration[:configuration] = Container::Behavior.configure(configuration)
  valid?(configuration, true)
  configuration
end

.parameter!(key, value) ⇒ Object



78
79
80
# File 'lib/celluloid/supervision/constants.rb', line 78

def parameter!(key, value)
  @@parameters[key] << value unless @@parameters[key].include? value
end

.parameters(*args) ⇒ Object



74
75
76
# File 'lib/celluloid/supervision/constants.rb', line 74

def parameters(*args)
  args.inject([]) { |parameters, p| parameters += @@parameters[p]; parameters }
end

.resync_parametersObject



68
69
70
71
72
# File 'lib/celluloid/supervision/constants.rb', line 68

def resync_parameters
  @@parameters = @@defaults[:parameters].each_with_object({}) { |(k, v), p| p[k] = v.dup; }
  @@aliases = @@defaults[:aliases].dup
  @@arity = @@defaults[:arity].dup
end

.save_defaultsObject



60
61
62
63
64
65
66
# File 'lib/celluloid/supervision/constants.rb', line 60

def save_defaults
  @@defaults = {
    parameters: @@parameters.each_with_object({}) { |(k, v), p| p[k] = v.dup; },
    aliases: @@aliases.dup,
    arity: @@arity.dup
  }
end

.valid?(configuration, fails = false) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/celluloid/supervision/validation.rb', line 5

def valid?(configuration, fails = false)
  parameters(:mandatory).each do |k|
    unless configuration.key? k
      if fails
        raise Error::Incomplete, "Missing `:#{k}` in supervision configuration."
      else
        return false
      end
    end
  end
  arity.each do |klass, args|
    next if configuration[args].is_a? Proc
    __a = configuration[args] && configuration[args].count || 0
    __arity = configuration[klass].allocate.method(:initialize).arity
    unless (__arity < 0 && __a >= __arity.abs - 1) || __a == __arity.abs
      if fails
        raise ArgumentError, "#{__a} vs. #{__arity}"
      else
        return false
      end
    end
  end
  true
end

Instance Method Details

#add(options) ⇒ Object



149
150
151
152
# File 'lib/celluloid/supervision/configuration.rb', line 149

def add(options)
  define(options)
  provider.supervise options if Configuration.valid? options
end

#countObject



68
69
70
# File 'lib/celluloid/supervision/configuration.rb', line 68

def count
  @instances.count
end

#define(configuration, fail = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/celluloid/supervision/configuration.rb', line 128

def define(configuration, fail = false)
  if configuration.is_a? Array
    configuration.each { |c| define(c, fail) }
  else
    unless include? configuration[:as]
      begin
        current_instance.define(configuration, fail)
      rescue Error::AlreadyDefined
        increment
        retry
      end
    end
  end
  self
end

#deploy(options = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/celluloid/supervision/configuration.rb', line 60

def deploy(options = {})
  define(options) if options.any?
  @instances.each do |instance|
    provider.add instance.merge(branch: @branch)
  end
  provider
end

#each(&block) ⇒ Object



72
73
74
# File 'lib/celluloid/supervision/configuration.rb', line 72

def each(&block)
  @instances.each(&block)
end

#exportObject



119
120
121
122
# File 'lib/celluloid/supervision/configuration.rb', line 119

def export
  return current_instance.to_hash if @i == 0
  @instances.map(&:export)
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/celluloid/supervision/configuration.rb', line 124

def include?(name)
  @instances.map(&:name).include? name
end

#incrementObject Also known as: another



144
145
146
# File 'lib/celluloid/supervision/configuration.rb', line 144

def increment
  @i += 1
end

#merge(values) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/celluloid/supervision/configuration.rb', line 111

def merge(values)
  if values.is_a?(Configuration) || values.is_a?(Hash)
    current_instance.merge(values)
  else
    raise Error::Invalid
  end
end

#merge!(values) ⇒ Object



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

def merge!(values)
  if values.is_a?(Configuration) || values.is_a?(Hash)
    current_instance.merge!(values)
  else
    raise Error::Invalid
  end
end

#providerObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/celluloid/supervision/configuration.rb', line 45

def provider
  @provider ||= if @supervisor.is_a? Hash
                  @supervisor[:type].run!(@supervisor)
                elsif @supervisor.is_a? Symbol
                  @supervisor = Object.module_eval(@supervisor.to_s)
                  provider
                elsif @supervisor.is_a? Class
                  @supervisor.run!
                elsif @supervisor.respond_to? :supervise
                  @supervisor
                else
                  raise Error::InvalidSupervisor
                end
end

#resync_accessorsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/celluloid/supervision/configuration.rb', line 76

def resync_accessors
  # methods for setting and getting the usual defaults
  Configuration.parameters(:mandatory, :optional, :plugins, :meta).each do |key|
    [:"#{key}!", :"#{key}="].each do |m|
      self.class.instance_eval do
        remove_method :"#{m}" rescue nil # avoid warnings in tests
        define_method(m) { |p| current_instance.send(m, p) }
      end
    end
    [:"#{key}?", :"#{key}"].each do |m|
      self.class.instance_eval do
        remove_method :"#{m}" rescue nil # avoid warnings in tests
        define_method(m) { current_instance.send(m) }
      end
    end
  end

  Configuration.aliases.each do |_alias, _original|
    ["!", :"=", :"?", :""]. each do |m|
      self.class.instance_eval do
        remove_method :"#{_alias}#{m}" rescue nil # avoid warnings in tests
        alias_method :"#{_alias}#{m}", :"#{_original}#{m}"
      end
    end
  end
end

#shutdownObject



154
155
156
# File 'lib/celluloid/supervision/configuration.rb', line 154

def shutdown
  @provider.shutdown
end