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/deprecate/validation.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 ################

[
  :configuration,
  :behavior,
]
INJECTIONS =
[
  :configuration,
  :before_initialization,
  :after_initialization,
  :before_start,
  :before_restart,
]
@@parameters =

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

{
   mandatory: [:type],
   optional: [
    :as,
    :args,
    :block,
  ],
   # TODO: Move these into Behaviors.
  plugins: [
    # de :size,        # Supervision::Pool
    # de :routers,     # Supervision::Coordinator
    # de :supervises   # Supervision::Tree
  ],
   meta: [
    :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
44
45
# 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"

  if (@configuration.is_a?(Hash) || @configuration.is_a?(Array)) && @configuration.any?
    define(@configuration)
  end
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(args, options = {}) ⇒ Object



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

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

.parse(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/celluloid/supervision/deprecate/validation.rb', line 12

def parse(args)
  return args if args.is_a? Configuration::Instance
  options = {args: []}
  return {type: args} if args.is_a? Class
  if args.is_a? Hash
    return args
  elsif args.is_a? Array
    if args.length == 1
      return args[0] if args.first.is_a? Configuration::Instance
      return {type: args.first} if args.first.is_a? Class
      if args.first.is_a?(Hash) && args = args.pop
        return args
      end
      options[:args] = args if args.any?
    elsif args.length > 1
      # TODO: don't use each
      options.merge! args.pop if args.last.is_a? Hash
      options[:type] = args.shift if args.first.is_a? Class
      options[:args] += args if args.any?
    end
  end
  options
end

.resync_parametersObject



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

def resync_parameters
  @@parameters = @@defaults[:parameters].inject({}) { |p, (k, v)| p[k] = v.dup; p }
  @@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.inject({}) { |p, (k, v)| p[k] = v.dup; p },
    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
29
# File 'lib/celluloid/supervision/validation.rb', line 5

def valid?(configuration, fails=false)
  parameters(:mandatory).each do |k|
    unless configuration.key? k
      if fails
        fail Error::Incomplete, "Missing `:#{k}` in supervision configuration."
      else
        return false
      end
    end
  end
  arity.each do |klass, args|
    unless 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
          fail ArgumentError.new("#{__a} vs. #{__arity}")
        else
          return false
        end
      end
    end
  end
  true
end

Instance Method Details

#add(options) ⇒ Object



151
152
153
154
# File 'lib/celluloid/supervision/configuration.rb', line 151

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

#countObject



70
71
72
# File 'lib/celluloid/supervision/configuration.rb', line 70

def count
  @instances.count
end

#define(configuration, fail = false) ⇒ Object



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

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



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

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

#each(&block) ⇒ Object



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

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

#exportObject



121
122
123
124
# File 'lib/celluloid/supervision/configuration.rb', line 121

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

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/celluloid/supervision/configuration.rb', line 126

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

#incrementObject Also known as: another



146
147
148
# File 'lib/celluloid/supervision/configuration.rb', line 146

def increment
  @i += 1
end

#merge(values) ⇒ Object



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

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

#merge!(values) ⇒ Object



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

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

#providerObject



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

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
                  fail Error::InvalidSupervisor
                end
end

#resync_accessorsObject



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

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



156
157
158
# File 'lib/celluloid/supervision/configuration.rb', line 156

def shutdown
  @provider.shutdown
end