diff --git a/README.md b/README.md index 5bbdb71..fb080a7 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ Specific options: -t, --[no-]trace Enable backtrace on failure (default false) --[no-]prompt Prompt for confirmation (default true) --[no-]dry-run Output configuration and exit + -T --timeout Set the Redis connection timeout (default 5.0) + See https://github.com/redis/redis-rb for more info. + -K --keepalive Set the Redis connection keep-alive (default 0) + See https://github.com/redis/redis-rb for more info. ``` ## Example: diff --git a/lib/redis-copy.rb b/lib/redis-copy.rb index cc787d1..2b193e9 100644 --- a/lib/redis-copy.rb +++ b/lib/redis-copy.rb @@ -18,8 +18,8 @@ class << self def copy(source, destination, options = {}) ui = UI.new(options) - source = redis_from(source) - destination = redis_from(destination) + source = redis_from(source, options) + destination = redis_from(destination, options) ui.abort('source cannot equal destination!') if same_redis?(source, destination) @@ -86,7 +86,7 @@ def same_redis?(redis_a, redis_b) redis_a.client.id == redis_b.client.id end - def redis_from(connection_string) + def redis_from(connection_string, options) require 'uri' connection_string = "redis://#{connection_string}" unless connection_string.start_with?("redis://") uri = URI(connection_string) @@ -101,7 +101,14 @@ def redis_from(connection_string) password = uri.password # Connect & Ping to ensure access. - Redis.new(host: host, port: port, db: db, password: password).tap(&:ping) + Redis.new( + host: host, + port: port, + db: db, + password: password, + timeout: options[:timeout], + tcp_keepalive: options[:keepalive] + ).tap(&:ping) rescue Redis::CommandError => e fail(Redis::CommandError, "There was a problem connecting to #{uri.to_s}\n#{e.message}") diff --git a/lib/redis-copy/cli.rb b/lib/redis-copy/cli.rb index 534331f..70070de 100644 --- a/lib/redis-copy/cli.rb +++ b/lib/redis-copy/cli.rb @@ -9,6 +9,8 @@ class CLI DEFAULTS = { ui: :command_line, verify: 0, + timeout: 5.0, + keepalive: 0, pipeline: :true, fail_fast: false, prompt: true, @@ -100,6 +102,20 @@ def initialize(argv = ARGV) options[:dry_run] = true end + opts.on('-T', '--timeout SECONDS', indent_desc.( + "Set the Redis connection timeout (default #{DEFAULTS[:timeout]})\n" + + "See https://github.com/redis/redis-rb for more info.") + ) do |timeout| + options[:timeout] = timeout.to_f + end + + opts.on('-K', '--keep-alive SECONDS', indent_desc.( + "Set the Redis connection keep-alive (default #{DEFAULTS[:keepalive]})\n" + + "See https://github.com/redis/redis-rb for more info.") + ) do |keepalive| + options[:keepalive] = keepalive.to_i + end + begin opts.parse!(argv) unless argv.size == 2