lib/queue_bus/config.rb in queue-bus-0.6.0 vs lib/queue_bus/config.rb in queue-bus-0.7.0

- old
+ new

@@ -1,102 +1,70 @@ +# frozen_string_literal: true + +require 'socket' +require 'logger' + module QueueBus + # This class contains all the configuration for a running queue bus application. class Config - def adapter=val - raise "Adapter already set to #{@adapter_instance.class.name}" if @adapter_instance - if val.is_a?(Class) - @adapter_instance = name_or_klass.new - elsif val.is_a?(::QueueBus::Adapters::Base) - @adapter_instance = val - else - class_name = ::QueueBus::Util.classify(val) - @adapter_instance = ::QueueBus::Util.constantize("::QueueBus::Adapters::#{class_name}").new - end - @adapter_instance - end + attr_accessor :default_queue, :local_mode, :hostname, :incoming_queue, :logger - def adapter - return @adapter_instance if @adapter_instance - raise "no adapter has been set" - end + attr_reader :worker_middleware_stack - def redis(&block) - # TODO: could allow setting for non-redis adapters - adapter.redis(&block) + def initialize + @worker_middleware_stack = QueueBus::Middleware::Stack.new + @incoming_queue = 'bus_incoming' + @hostname = Socket.gethostname end - def default_app_key=val - @default_app_key = Application.normalize(val) - end + def adapter=(val) + raise "Adapter already set to #{@adapter_instance.class.name}" if has_adapter? - def default_app_key - @default_app_key + @adapter_instance = + if val.is_a?(Class) + val.new + elsif val.is_a?(::QueueBus::Adapters::Base) + val + else + class_name = ::QueueBus::Util.classify(val) + ::QueueBus::Util.constantize("::QueueBus::Adapters::#{class_name}").new + end end - def default_queue=val - @default_queue = val - end + def adapter + return @adapter_instance if has_adapter? - def default_queue - @default_queue + raise 'no adapter has been set' end - def local_mode=value - @local_mode = value + # Checks whether an adapter is set and returns true if it is. + def has_adapter? # rubocop:disable Naming/PredicateName + !@adapter_instance.nil? end - def local_mode - @local_mode + def redis(&block) + # TODO: could allow setting for non-redis adapters + adapter.redis(&block) end - def incoming_queue=val - @incoming_queue = val + attr_reader :default_app_key + def default_app_key=(val) + @default_app_key = Application.normalize(val) end - def incoming_queue - @incoming_queue ||= "bus_incoming" + def before_publish=(callback) + @before_publish_callback = callback end - def worker_middleware_stack - @worker_middleware_stack ||= QueueBus::Middleware::Stack.new - end - - def hostname - @hostname ||= `hostname 2>&1`.strip.sub(/.local/,'') - end - - def hostname=val - @hostname = val - end - - def before_publish=(proc) - @before_publish_callback = proc - end - def before_publish_callback(attributes) - if @before_publish_callback - @before_publish_callback.call(attributes) - end + @before_publish_callback&.call(attributes) end - def logger - @logger - end - - def logger=val - @logger = val - end - def log_application(message) - if logger - time = Time.now.strftime('%H:%M:%S %Y-%m-%d') - logger.info("** [#{time}] #$$: QueueBus #{message}") - end + logger&.info(message) end def log_worker(message) - if ENV['LOGGING'] || ENV['VERBOSE'] || ENV['VVERBOSE'] - time = Time.now.strftime('%H:%M:%S %Y-%m-%d') - puts "** [#{time}] #$$: #{message}" - end + logger&.debug(message) end end end