class MultiBackgroundJob::Config

Attributes

config_path[RW]

Path to the YAML file with configs

Private Class Methods

attribute_accessor(field, validator: nil, normalizer: nil, default: nil) click to toggle source
# File lib/multi_background_job/config.rb, line 13
def attribute_accessor(field, validator: nil, normalizer: nil, default: nil)
  normalizer ||= :"normalize_#{field}"
  validator ||= :"validate_#{field}"

  define_method(field) do
    unless instance_variable_defined?(:"@#{field}")
      fallback = config_from_yaml[field.to_s] || default
      return if fallback.nil?

      send(:"#{field}=", fallback.respond_to?(:call) ? fallback.call : fallback)
    end
    instance_variable_get(:"@#{field}")
  end

  define_method(:"#{field}=") do |value|
    value = send(normalizer, field, value) if respond_to?(normalizer, true)
    send(validator, field, value) if respond_to?(validator, true)

    instance_variable_set(:"@#{field}", value)
  end
end

Public Instance Methods

config_path=(value) click to toggle source
# File lib/multi_background_job/config.rb, line 90
def config_path=(value)
  @config_from_yaml = nil
  @config_path = value
end
middleware() { |middleware| ... } click to toggle source
# File lib/multi_background_job/config.rb, line 84
def middleware
  @middleware ||= MiddlewareChain.new
  yield @middleware if block_given?
  @middleware
end
redis_pool() click to toggle source
# File lib/multi_background_job/config.rb, line 77
def redis_pool
  {
    size: redis_pool_size,
    timeout: redis_pool_timeout,
  }
end
worker_options(class_name) click to toggle source
# File lib/multi_background_job/config.rb, line 68
def worker_options(class_name)
  class_name = class_name.to_s
  if strict? && !workers.key?(class_name)
    raise NotDefinedWorker.new(class_name)
  end

  workers.fetch(class_name, {})
end

Private Instance Methods

config_from_yaml() click to toggle source
# File lib/multi_background_job/config.rb, line 143
def config_from_yaml
  @config_from_yaml ||= begin
    config_path ? YAML.load_file(config_path) : {}
  rescue Errno::ENOENT, Errno::ESRCH
    {}
  end
end
normalize_redis_config(_attribute, value) click to toggle source
# File lib/multi_background_job/config.rb, line 111
def normalize_redis_config(_attribute, value)
  case value
  when String
    { url: value }
  when Hash
    value.each_with_object({}) { |(k, v), r| r[k.to_sym] = v }
  else
    value
  end
end
normalize_to_int(_attribute, value) click to toggle source
# File lib/multi_background_job/config.rb, line 97
def normalize_to_int(_attribute, value)
  value.to_i
end
normalize_workers(_, value) click to toggle source
# File lib/multi_background_job/config.rb, line 133
def normalize_workers(_, value)
  return unless value.is_a?(Hash)

  hash = {}
  value.each do |class_name, opts|
    hash[class_name.to_s] = MultiJson.load(MultiJson.dump(opts), symbolize_names: true)
  end
  hash
end
validate_greater_than_zero(attribute, value) click to toggle source
# File lib/multi_background_job/config.rb, line 101
def validate_greater_than_zero(attribute, value)
  return if value > 0

  raise InvalidConfig, format(
    'The %<value>p for %<attr>s is not valid. It must be greater than zero',
    value: value,
    attr: attribute,
  )
end
validate_redis_config(attribute, value) click to toggle source
# File lib/multi_background_job/config.rb, line 122
def validate_redis_config(attribute, value)
  return if value.is_a?(Hash)

  raise InvalidConfig, format(
    'The %<value>p for %<attr>i is not valid. It must be a Hash with the redis initialization options. ' +
    'See https://github.com/redis/redis-rb for all available options',
    value: value,
    attr: attribute,
  )
end