Skip to content

Commit 4a18ca8

Browse files
committed
Implement PR feedback
1 parent c5a557f commit 4a18ca8

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

lib/litestream/commands.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ def executable(exe_path: DEFAULT_DIR)
9494
exe_file
9595
end
9696

97+
# Replicate can be run either as a fork or in the same process, depending on the context.
98+
# Puma will start replication as a forked process, while running replication from a rake
99+
# tasks won't.
97100
def replicate(async: false, **argv)
98101
cmd = prepare("replicate", argv)
99-
run_async(cmd, async: async)
102+
run_replicate(cmd, async: async)
100103
rescue
101104
raise CommandFailedException, "Failed to execute `#{cmd.join(" ")}`"
102105
end
@@ -164,31 +167,16 @@ def run(cmd, tabled_output:)
164167
rows.map { keys.zip(_1).to_h }
165168
end
166169

167-
def run_async(cmd, async:)
170+
def run_replicate(cmd, async:)
168171
if async
169172
exec(*cmd) if fork.nil?
170173
else
174+
# When running in-process, we capture output continuously and write to stdout.
171175
IO.popen(cmd, err: [:child, :out]) do |io|
172176
io.each_line { |line| puts line }
173177
end
174178
end
175179
end
176-
177-
module Output
178-
class << self
179-
def format(data)
180-
headers = data.first.keys.map(&:to_s)
181-
widths = headers.map.with_index { |h, i|
182-
[h.length, data.map { |r| r[data.first.keys[i]].to_s.length }.max].max
183-
}
184-
185-
format_str = widths.map { |w| "%-#{w}s" }.join(" ")
186-
([headers] + data.map(&:values)).map { |row|
187-
sprintf(format_str, *row.map(&:to_s))
188-
}.join("\n")
189-
end
190-
end
191-
end
192180
end
193181
end
194182
end

lib/tasks/litestream_tasks.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ namespace :litestream do
6363
if (separator_index = ARGV.index("--"))
6464
ARGV.slice(separator_index + 1, ARGV.length)
6565
.map { |pair| pair.split("=") }
66-
.each { |opt| options[opt[0].to_sym] = opt[1] || nil }
66+
.each { |opt| options[opt[0]] = opt[1] || nil }
6767
end
68-
options.symbolize_keys
68+
options.symbolize_keys!
6969
end
7070
end

test/litestream/test_commands.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_replicate_with_no_options
2727
assert_equal "--config", argv[0]
2828
assert_match Regexp.new("dummy/config/litestream.yml"), argv[1]
2929
end
30-
Litestream::Commands.stub :run_async, stub do
30+
Litestream::Commands.stub :run_replicate, stub do
3131
Litestream::Commands.replicate
3232
end
3333
end
@@ -42,7 +42,7 @@ def test_replicate_with_boolean_option
4242
assert_match Regexp.new("dummy/config/litestream.yml"), argv[1]
4343
assert_equal "--no-expand-env", argv[2]
4444
end
45-
Litestream::Commands.stub :run_async, stub do
45+
Litestream::Commands.stub :run_replicate, stub do
4646
Litestream::Commands.replicate("--no-expand-env" => nil)
4747
end
4848
end
@@ -58,7 +58,7 @@ def test_replicate_with_string_option
5858
assert_equal "--exec", argv[2]
5959
assert_equal "command", argv[3]
6060
end
61-
Litestream::Commands.stub :run_async, stub do
61+
Litestream::Commands.stub :run_replicate, stub do
6262
Litestream::Commands.replicate("--exec" => "command")
6363
end
6464
end
@@ -74,7 +74,7 @@ def test_replicate_with_symbol_option
7474
assert_equal "--exec", argv[2]
7575
assert_equal "command", argv[3]
7676
end
77-
Litestream::Commands.stub :run_async, stub do
77+
Litestream::Commands.stub :run_replicate, stub do
7878
Litestream::Commands.replicate("--exec": "command")
7979
end
8080
end
@@ -88,15 +88,15 @@ def test_replicate_with_config_option
8888
assert_equal "--config", argv[0]
8989
assert_equal "CONFIG", argv[1]
9090
end
91-
Litestream::Commands.stub :run_async, stub do
91+
Litestream::Commands.stub :run_replicate, stub do
9292
Litestream::Commands.replicate("--config" => "CONFIG")
9393
end
9494
end
9595

9696
def test_replicate_sets_replica_bucket_env_var_from_config_when_env_var_not_set
9797
Litestream.replica_bucket = "mybkt"
9898

99-
Litestream::Commands.stub :run_async, nil do
99+
Litestream::Commands.stub :run_replicate, nil do
100100
Litestream::Commands.replicate
101101
end
102102

@@ -108,7 +108,7 @@ def test_replicate_sets_replica_bucket_env_var_from_config_when_env_var_not_set
108108
def test_replicate_sets_replica_key_id_env_var_from_config_when_env_var_not_set
109109
Litestream.replica_key_id = "mykey"
110110

111-
Litestream::Commands.stub :run_async, nil do
111+
Litestream::Commands.stub :run_replicate, nil do
112112
Litestream::Commands.replicate
113113
end
114114

@@ -120,7 +120,7 @@ def test_replicate_sets_replica_key_id_env_var_from_config_when_env_var_not_set
120120
def test_replicate_sets_replica_access_key_env_var_from_config_when_env_var_not_set
121121
Litestream.replica_access_key = "access"
122122

123-
Litestream::Commands.stub :run_async, nil do
123+
Litestream::Commands.stub :run_replicate, nil do
124124
Litestream::Commands.replicate
125125
end
126126

@@ -134,7 +134,7 @@ def test_replicate_sets_all_env_vars_from_config_when_env_vars_not_set
134134
Litestream.replica_key_id = "mykey"
135135
Litestream.replica_access_key = "access"
136136

137-
Litestream::Commands.stub :run_async, nil do
137+
Litestream::Commands.stub :run_replicate, nil do
138138
Litestream::Commands.replicate
139139
end
140140

@@ -152,7 +152,7 @@ def test_replicate_does_not_set_env_var_from_config_when_env_vars_already_set
152152
Litestream.replica_key_id = "mykey"
153153
Litestream.replica_access_key = "access"
154154

155-
Litestream::Commands.stub :run_async, nil do
155+
Litestream::Commands.stub :run_replicate, nil do
156156
Litestream::Commands.replicate
157157
end
158158

0 commit comments

Comments
 (0)