mirror of
https://github.com/jimeh/bunnyrun.git
synced 2026-02-19 07:56:40 +00:00
Expose options to Consumer, fix bunny logger, misc cleanup
This commit is contained in:
@@ -103,28 +103,35 @@ module BunnyRun
|
|||||||
parser.opt :url, 'Connection string ' \
|
parser.opt :url, 'Connection string ' \
|
||||||
'(example: "amqp://guest:guest@127.0.0.1:5672/vhost")',
|
'(example: "amqp://guest:guest@127.0.0.1:5672/vhost")',
|
||||||
short: 'U', type: :string,
|
short: 'U', type: :string,
|
||||||
default: ENV['RABBITMQ_URL'] || nil
|
default: ENV.fetch('RABBITMQ_URL', nil)
|
||||||
|
|
||||||
parser.opt :host, 'Host',
|
parser.opt :host, 'Host',
|
||||||
short: 'H', type: :string,
|
short: 'H', type: :string,
|
||||||
default: ENV['RABBITMQ_HOST'] || '127.0.0.1'
|
default: ENV.fetch('RABBITMQ_HOST', '127.0.0.1')
|
||||||
|
|
||||||
parser.opt :port, 'Port',
|
parser.opt :port, 'Port',
|
||||||
short: 'P', type: :int,
|
short: 'P', type: :int,
|
||||||
default: (ENV['RABBITMQ_PORT'] || 5672).to_i
|
default: ENV.fetch('RABBITMQ_PORT', '5672').to_i
|
||||||
|
|
||||||
parser.opt :ssl, 'Connect using SSL',
|
parser.opt :ssl, 'Connect using SSL',
|
||||||
short: 's', type: :bool,
|
short: 's', type: :bool,
|
||||||
default: trufy?(ENV.fetch('RABBITMQ_SSL', false))
|
default: trufy?(ENV.fetch('RABBITMQ_SSL', false))
|
||||||
|
|
||||||
parser.opt :vhost, 'Virtual host',
|
parser.opt :vhost, 'Virtual host',
|
||||||
short: 'V', type: :string,
|
short: 'V', type: :string,
|
||||||
default: ENV['RABBITMQ_VHOST'] || '/'
|
default: ENV.fetch('RABBITMQ_VHOST', '/')
|
||||||
|
|
||||||
parser.opt :user, 'Username',
|
parser.opt :user, 'Username',
|
||||||
short: 'u', type: :string,
|
short: 'u', type: :string,
|
||||||
default: ENV['RABBITMQ_USER'] || 'guest'
|
default: ENV.fetch('RABBITMQ_USER', 'guest')
|
||||||
|
|
||||||
parser.opt :pass, 'Password',
|
parser.opt :pass, 'Password',
|
||||||
short: 'p', type: :string,
|
short: 'p', type: :string,
|
||||||
default: ENV['RABBITMQ_PASS'] || 'guest'
|
default: ENV.fetch('RABBITMQ_PASS', 'guest')
|
||||||
|
|
||||||
parser.opt :prefetch, 'Default prefetch count',
|
parser.opt :prefetch, 'Default prefetch count',
|
||||||
short: :none, type: :int,
|
short: :none, type: :int,
|
||||||
default: (ENV['RABBITMQ_PREFETCH'] || 1).to_i
|
default: ENV.fetch('RABBITMQ_PREFETCH', 1).to_i
|
||||||
|
|
||||||
parser.conflicts :url, :host
|
parser.conflicts :url, :host
|
||||||
parser.conflicts :url, :port
|
parser.conflicts :url, :port
|
||||||
@@ -139,16 +146,19 @@ module BunnyRun
|
|||||||
|
|
||||||
parser.opt :log_target, 'Log target, file path or STDOUT',
|
parser.opt :log_target, 'Log target, file path or STDOUT',
|
||||||
short: 't', type: :string,
|
short: 't', type: :string,
|
||||||
default: ENV['LOG_TARGET'] || 'STDOUT'
|
default: ENV.fetch('LOG_TARGET', 'STDOUT')
|
||||||
|
|
||||||
parser.opt :log_level, 'Log level (debug, info, warn, error, fatal)',
|
parser.opt :log_level, 'Log level (debug, info, warn, error, fatal)',
|
||||||
short: 'l', type: :string,
|
short: 'l', type: :string,
|
||||||
default: ENV['LOG_LEVEL'] || 'info'
|
default: ENV.fetch('LOG_LEVEL', 'info')
|
||||||
|
|
||||||
parser.opt :bunny_log_target, 'Log target used by Bunny',
|
parser.opt :bunny_log_target, 'Log target used by Bunny',
|
||||||
short: :none, type: :string,
|
short: :none, type: :string,
|
||||||
default: ENV['BUNNY_LOG_TARGET'] || 'STDOUT'
|
default: ENV.fetch('BUNNY_LOG_TARGET', 'STDOUT')
|
||||||
|
|
||||||
parser.opt :bunny_log_level, 'Log level used by Bunny',
|
parser.opt :bunny_log_level, 'Log level used by Bunny',
|
||||||
short: :none, type: :string,
|
short: :none, type: :string,
|
||||||
default: ENV['BUNNY_LOG_LEVEL'] || 'warn'
|
default: ENV.fetch('BUNNY_LOG_LEVEL', 'warn')
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_application_options(parser)
|
def define_application_options(parser)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ module BunnyRun
|
|||||||
attr_reader :connection
|
attr_reader :connection
|
||||||
attr_reader :publish_channel
|
attr_reader :publish_channel
|
||||||
attr_reader :default_prefetch
|
attr_reader :default_prefetch
|
||||||
|
attr_reader :options
|
||||||
attr_reader :logger
|
attr_reader :logger
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
@@ -52,6 +53,7 @@ module BunnyRun
|
|||||||
@connection = opts[:connection]
|
@connection = opts[:connection]
|
||||||
@publish_channel = opts[:publish_channel]
|
@publish_channel = opts[:publish_channel]
|
||||||
@default_prefetch = opts[:default_prefetch]
|
@default_prefetch = opts[:default_prefetch]
|
||||||
|
@options = opts[:options]
|
||||||
@logger = opts[:logger]
|
@logger = opts[:logger]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -34,11 +34,14 @@ module BunnyRun
|
|||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
@logger ||= begin
|
@logger ||= create_logger(options.log_target, options.log_level)
|
||||||
logger = Logger.new(log_target)
|
end
|
||||||
logger.level = log_level
|
|
||||||
logger
|
def bunny_logger
|
||||||
end
|
@bunny_logger ||= create_logger(
|
||||||
|
options.bunny_log_target,
|
||||||
|
options.bunny_log_level
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@@ -52,34 +55,39 @@ module BunnyRun
|
|||||||
connection: connection,
|
connection: connection,
|
||||||
publish_channel: publish_channel,
|
publish_channel: publish_channel,
|
||||||
default_prefetch: options.prefetch,
|
default_prefetch: options.prefetch,
|
||||||
|
options: options,
|
||||||
logger: logger
|
logger: logger
|
||||||
)
|
)
|
||||||
|
|
||||||
consumer.start
|
consumer.start
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_opts
|
def connection_opts
|
||||||
return options[:url] if options[:url]
|
return options.url if options.url
|
||||||
|
|
||||||
{
|
{
|
||||||
host: options[:host],
|
host: options.host,
|
||||||
port: options[:port],
|
port: options.port,
|
||||||
ssl: options[:ssl],
|
ssl: options.ssl,
|
||||||
vhost: options[:vhost],
|
vhost: options.vhost,
|
||||||
user: options[:user],
|
user: options.user,
|
||||||
pass: options[:pass]
|
pass: options.pass,
|
||||||
|
logger: bunny_logger
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_target
|
def create_logger(target, level)
|
||||||
if options[:log_target].casecmp('stdout').zero?
|
Logger.new(normalize_log_target(target)).tap do |l|
|
||||||
STDOUT
|
l.level = Kernel.const_get("::Logger::#{level.upcase}")
|
||||||
else
|
|
||||||
options[:log_target]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_level
|
def normalize_log_target(input)
|
||||||
Kernel.const_get("::Logger::#{options[:log_level].upcase}")
|
if input.casecmp('stdout').zero?
|
||||||
|
STDOUT
|
||||||
|
else
|
||||||
|
input
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user