Class: Tobox::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tobox/configuration.rb

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  environment: ENV.fetch("APP_ENV", "development"),
  logger: nil,
  log_level: nil,
  database_uri: nil,
  database_options: nil,
  table: :outbox,
  created_at_column: nil,
  batch_size: 1,
  max_attempts: 10,
  exponential_retry_factor: 2,
  wait_for_events_delay: 5,
  shutdown_timeout: 10,
  grace_shutdown_timeout: 5,
  concurrency: 4, # TODO: CPU count
  worker: :thread
}.freeze
LOG_FORMAT_PATTERN =
"%s, [%s #%d (th: %s)] %5s -- %s: %s\n"
DEFAULT_LOG_FORMATTER =
Class.new(Logger::Formatter) do
  def call(severity, time, progname, msg)
    format(LOG_FORMAT_PATTERN, severity[0, 1], format_datetime(time), Process.pid,
           Thread.current.name || Thread.current.object_id, severity, progname, msg2str(msg))
  end
end.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &block) ⇒ Configuration

Returns a new instance of Configuration.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tobox/configuration.rb', line 41

def initialize(name = nil, &block)
  @name = name
  @config = DEFAULT_CONFIGURATION.dup

  @lifecycle_events = {}
  @handlers = {}
  @message_to_arguments = nil
  @plugins = []
  @fetcher_class = Class.new(Fetcher)

  if block
    case block.arity
    when 0
      instance_exec(&block)
    when 1
      yield(self)
    else
      raise Error, "configuration does not support blocks with more than one variable"
    end
  end

  env = @config[:environment]
  @default_logger = @config[:logger] || Logger.new(STDERR, formatter: DEFAULT_LOG_FORMATTER) # rubocop:disable Style/GlobalStdStream
  @default_logger.level = @config[:log_level] || (env == "production" ? Logger::INFO : Logger::DEBUG)

  @database = if @config[:database_uri]
                database_opts = @config[:database_options] || {}
                database_opts[:max_connections] ||= (@config[:concurrency] if @config[:worker] == :thread)
                db = Sequel.connect(@config[:database_uri].to_s, database_opts)
                Array(@lifecycle_events[:database_connect]).each { |cb| cb.call(db) }
                db
              else
                Sequel::DATABASES.first
              end
  raise Error, "no database found" unless @database

  if @database.frozen?
    raise "#{@database} must have the :date_arithmetic extension loaded" unless Sequel.respond_to?(:date_add)
  else
    @database.extension :date_arithmetic
    @database.loggers << @default_logger unless @config[:environment] == "production"
  end

  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



164
165
166
167
168
169
170
171
172
# File 'lib/tobox/configuration.rb', line 164

def method_missing(meth, *args, &block)
  if @config.key?(meth) && args.size == 1
    @config[meth] = args.first
  elsif /\Aon_(.*)\z/.match(meth) && args.empty?
    on(Regexp.last_match(1).to_sym, &block)
  else
    super
  end
end

Instance Attribute Details

#arguments_handlerObject (readonly)

Returns the value of attribute arguments_handler.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def arguments_handler
  @arguments_handler
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def config
  @config
end

#databaseObject (readonly)

Returns the value of attribute database.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def database
  @database
end

#default_loggerObject (readonly)

Returns the value of attribute default_logger.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def default_logger
  @default_logger
end

#fetcher_classObject (readonly)

Returns the value of attribute fetcher_class.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def fetcher_class
  @fetcher_class
end

#handlersObject (readonly)

Returns the value of attribute handlers.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def handlers
  @handlers
end

#lifecycle_eventsObject (readonly)

Returns the value of attribute lifecycle_events.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def lifecycle_events
  @lifecycle_events
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



10
11
12
# File 'lib/tobox/configuration.rb', line 10

def plugins
  @plugins
end

Instance Method Details

#freezeObject



152
153
154
155
156
157
158
159
160
# File 'lib/tobox/configuration.rb', line 152

def freeze
  @name.freeze
  @config.each_value(&:freeze).freeze
  @handlers.each_value(&:freeze).freeze
  @lifecycle_events.each_value(&:freeze).freeze
  @plugins.freeze
  @database.freeze
  super
end

#message_to_arguments(&callback) ⇒ Object



130
131
132
133
# File 'lib/tobox/configuration.rb', line 130

def message_to_arguments(&callback)
  @arguments_handler = callback
  self
end

#on(*event_types, &callback) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/tobox/configuration.rb', line 87

def on(*event_types, &callback)
  callback_events = (@handlers[callback] ||= [])
  event_types.each do |event_type|
    callback_events << event_type.to_sym
  end
  self
end

#on_after_event(&callback) ⇒ Object



110
111
112
113
# File 'lib/tobox/configuration.rb', line 110

def on_after_event(&callback)
  (@lifecycle_events[:after_event] ||= []) << callback
  self
end

#on_before_event(&callback) ⇒ Object



105
106
107
108
# File 'lib/tobox/configuration.rb', line 105

def on_before_event(&callback)
  (@lifecycle_events[:before_event] ||= []) << callback
  self
end

#on_database_connect(&callback) ⇒ Object



125
126
127
128
# File 'lib/tobox/configuration.rb', line 125

def on_database_connect(&callback)
  (@lifecycle_events[:database_connect] ||= []) << callback
  self
end

#on_error_event(&callback) ⇒ Object



115
116
117
118
# File 'lib/tobox/configuration.rb', line 115

def on_error_event(&callback)
  (@lifecycle_events[:error_event] ||= []) << callback
  self
end

#on_error_worker(&callback) ⇒ Object



120
121
122
123
# File 'lib/tobox/configuration.rb', line 120

def on_error_worker(&callback)
  (@lifecycle_events[:error_worker] ||= []) << callback
  self
end

#on_start(&callback) ⇒ Object



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

def on_start(&callback)
  (@lifecycle_events[:on_start] ||= []) << callback
  self
end

#on_stop(&callback) ⇒ Object



100
101
102
103
# File 'lib/tobox/configuration.rb', line 100

def on_stop(&callback)
  (@lifecycle_events[:on_stop] ||= []) << callback
  self
end

#plugin(plugin, **options, &block) ⇒ Object

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tobox/configuration.rb', line 135

def plugin(plugin, **options, &block)
  raise Error, "Cannot add a plugin to a frozen config" if frozen?

  plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)

  return if @plugins.include?(plugin)

  @plugins << plugin
  plugin.load_dependencies(self, **options, &block) if plugin.respond_to?(:load_dependencies)

  extend(plugin::ConfigurationMethods) if defined?(plugin::ConfigurationMethods)

  @fetcher_class.__send__(:include, plugin::FetcherMethods) if defined?(plugin::FetcherMethods)

  plugin.configure(self, **options, &block) if plugin.respond_to?(:configure)
end