Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow timeout and keep-alive to be specified #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions lib/redis-copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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}")
Expand Down
16 changes: 16 additions & 0 deletions lib/redis-copy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CLI
DEFAULTS = {
ui: :command_line,
verify: 0,
timeout: 5.0,
keepalive: 0,
pipeline: :true,
fail_fast: false,
prompt: true,
Expand Down Expand Up @@ -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
Expand Down