Class: Moleculer::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/moleculer/configuration.rb

Overview

Handles Moleculer configuration

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



84
85
86
87
88
89
90
91
92
93
# File 'lib/moleculer/configuration.rb', line 84

def initialize(options = {})
  options.each do |option, value|
    send("#{option}=".to_sym, value)
  end
  @rescue_handlers = {}

  rescue_from(StandardError) do |e|
    logger.error(e)
  end
end

Class Attribute Details

.accessorsObject (readonly)

Returns the value of attribute accessors.



45
46
47
# File 'lib/moleculer/configuration.rb', line 45

def accessors
  @accessors
end

Instance Attribute Details

#brokerObject

Returns the value of attribute broker.



82
83
84
# File 'lib/moleculer/configuration.rb', line 82

def broker
  @broker
end

Instance Method Details

#handle_error(error, parent = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/moleculer/configuration.rb', line 134

def handle_error(error, parent = nil)
  handler = select_rescue_handler_for(parent || error.class)
  raise error unless handler

  begin
    handler.call(error)
  rescue StandardError => e
    # if the error was re-raised, and a new err was not raised then call the handler for the parent of the original
    # error, otherwise, restart the chain
    return handle_error(error, parent&.superclass || error.class.superclass) if error == e

    handle_error(error)
  end
end

#rescue_from(err, &block) ⇒ Object

Add a rescue handler for a specific error. This allows libraries such as airbrake to hook into the error handling flow. When a rescue handler raises, it will look for a rescue handler for the parent class of the thrown error recursively until it reaches StandardError. If the block does not itself raise an error, the error may be swallowed.

Parameters:

  • err (Class)

    the error class to handle

  • block (Proc)

    the block to execute when the error is captured

Raises:

  • (ArgumentError)


125
126
127
128
129
130
# File 'lib/moleculer/configuration.rb', line 125

def rescue_from(err, &block)
  raise ArgumentError, "block required" unless block_given?
  raise ArgumentError, "error must be a standard error" unless err.ancestors.include?(StandardError)

  @rescue_handlers[err] = block
end

#servicesObject



95
96
97
# File 'lib/moleculer/configuration.rb', line 95

def services
  @services ||= ServiceList.new(self)
end

#services=(array) ⇒ Object



99
100
101
102
# File 'lib/moleculer/configuration.rb', line 99

def services=(array)
  @services = ServiceList.new(self)
  array.each { |s| @services << s }
end

#to_hObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/moleculer/configuration.rb', line 104

def to_h
  {
    log_file:           log_file,
    log_level:          log_level,
    heartbeat_interval: heartbeat_interval,
    timeout:            timeout,
    transporter:        transporter,
    serializer:         serializer,
    node_id:            node_id,
    service_prefix:     service_prefix,
  }
end