Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
--quiet option silences output as well as preview, fix scheduler_opts…
Browse files Browse the repository at this point in the history
… bug in transform-tree
  • Loading branch information
freels committed Dec 30, 2010
1 parent f035ab6 commit 8424c69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
10 changes: 8 additions & 2 deletions lib/gizzard/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,12 @@ def run
manifest = manager.manifest(forwarding.table_id)
shard = manifest.trees[forwarding]
copy_wrapper = command_options.scheduler_options[:copy_wrapper]
be_quiet = global_options.force && command_options.quiet
transformation = Transformation.new(shard.template, to_template, copy_wrapper)

unless global_options.force && command_options.quiet
command_options.scheduler_options[:quiet] = be_quiet

unless be_quiet
puts transformation.inspect
puts ""
end
Expand All @@ -831,8 +834,11 @@ def run

manifest = manager.manifest(*global_options.tables)
copy_wrapper = command_options.scheduler_options[:copy_wrapper]
be_quiet = global_options.force && command_options.quiet
transformations = {}

command_options.scheduler_options[:quiet] = be_quiet

@argv.each_slice(2) do |(from_template_s, to_template_s)|
from, to = [from_template_s, to_template_s].map {|s| ShardTemplate.parse(s) }
transformation = Transformation.new(from, to, copy_wrapper)
Expand All @@ -844,7 +850,7 @@ def run

base_name = transformations.values.first.values.first.id.table_prefix.split('_').first

unless global_options.force && command_options.quiet
unless be_quiet
transformations.each do |transformation, trees|
puts transformation.inspect
puts "Applied to:"
Expand Down
63 changes: 37 additions & 26 deletions lib/gizzard/transformation_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class Transformation::Scheduler

def initialize(nameserver, base_name, transformations, options = {})
options = DEFAULT_OPTIONS.merge(options)
@nameserver = nameserver
@transformations = transformations
@max_copies = options[:max_copies]
@copies_per_host = options[:copies_per_host]
@poll_interval = options[:poll_interval]
@nameserver = nameserver
@transformations = transformations
@max_copies = options[:max_copies]
@copies_per_host = options[:copies_per_host]
@poll_interval = options[:poll_interval]
@be_quiet = options[:quiet]
@dont_show_progress = options[:no_progress] || @be_quiet

@jobs_in_progress = []
@jobs_finished = []
Expand Down Expand Up @@ -60,9 +62,10 @@ def apply!
break if @jobs_pending.empty? && @jobs_in_progress.empty?

unless nameserver.dryrun?
12.times do
sleep(@poll_interval / 12.0)
put_copy_progress
if @dont_show_progress
sleep(@poll_interval)
else
sleep_with_progress(@poll_interval)
end
end
end
Expand Down Expand Up @@ -153,27 +156,26 @@ def busy_hosts(extra_hosts = [])
copies_count_map.select {|_, count| count >= @copies_per_host }.map {|(host, _)| host }
end

def reset_progress_string
if @progress_string
@progress_string = nil
puts ""
def sleep_with_progress(interval)
start = Time.now
while (Time.now - start) < interval
put_copy_progress
sleep 0.2
end
end

def time_elapsed
s = (Time.now - @start_time).to_i

days = s / (60 * 60 * 24) if s >= 60 * 60 * 24
hours = (s % (60 * 60 * 24)) / (60 * 60) if s >= 60 * 60
minutes = (s % (60 * 60)) / 60 if s >= 60
seconds = s % 60

[days,hours,minutes,seconds].compact.map {|i| "%0.2i" % i }.join(":")
def clear_progress_string
if @progress_string
#print "\r" + (" " * (@progress_string.length + 10)) + "\r"
@progress_string = nil
end
end

def log(*args)
reset_progress_string
puts *args
unless @be_quiet
clear_progress_string
puts *args
end
end

def put_copy_progress
Expand All @@ -192,13 +194,22 @@ def put_copy_progress
"In progress: #{@jobs_in_progress.length}"
end

if @progress_string
print "\r" + (" " * (@progress_string.length + 10)) + "\r"
end
clear_progress_string

@progress_string = "#{spinner} #{in_progress_txt} #{pending_txt} #{finished_txt} #{elapsed_txt}"
print @progress_string; $stdout.flush
end
end

def time_elapsed
s = (Time.now - @start_time).to_i

days = s / (60 * 60 * 24) if s >= 60 * 60 * 24
hours = (s % (60 * 60 * 24)) / (60 * 60) if s >= 60 * 60
minutes = (s % (60 * 60)) / 60 if s >= 60
seconds = s % 60

[days,hours,minutes,seconds].compact.map {|i| "%0.2i" % i }.join(":")
end
end
end
25 changes: 13 additions & 12 deletions lib/gizzmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ def load_config(options, filename)
end
end

def add_scheduler_opts(opts)
hash = {}
def add_scheduler_opts(subcommand_options, opts)
opts.on("--max-copies=COUNT", "Limit max simultaneous copies to COUNT.") do |c|
hash[:max_copies] = c.to_i
(subcommand_options.scheduler_options ||= {})[:max_copies] = c.to_i
end
opts.on("--copies-per-host=COUNT", "Limit max copies per individual destination host to COUNT") do |c|
hash[:copies_per_host] = c.to_i
(subcommand_options.scheduler_options ||= {})[:copies_per_host] = c.to_i
end
opts.on("--poll-interval=SECONDS", "Sleep SECONDS between polling for copy status") do |c|
hash[:poll_interval] = c.to_i
(subcommand_options.scheduler_options ||= {})[:poll_interval] = c.to_i
end
opts.on("--copy-wrapper=TYPE", "wrap copy destination shards with TYPE. default WriteOnlyShard") do |t|
hash[:copy_wrapper] = t
opts.on("--copy-wrapper=TYPE", "Wrap copy destination shards with TYPE. default WriteOnlyShard") do |t|
(subcommand_options.scheduler_options ||= {})[:copy_wrapper] = t
end
opts.on("--no-progress", "Do not show progress bar at bottom.") do
(subcommand_options.scheduler_options ||= {})[:no_progress] = true
end
hash
end

subcommands = {
Expand Down Expand Up @@ -309,19 +310,19 @@ def add_scheduler_opts(opts)
opts.banner = "Usage: #{zero} transform-tree [options] ROOT_SHARD_ID TEMPLATE"
separators(opts, DOC_STRINGS['transform-tree'])

subcommand_options.scheduler_options = add_scheduler_opts(opts)
add_scheduler_opts subcommand_options, opts

opts.on("-q", "--quiet", "Do not display transformation preview (only valid with --force)") do
opts.on("-q", "--quiet", "Do not display transformation info (only valid with --force)") do
subcommand_options.quiet = true
end
end,
'transform' => OptionParser.new do |opts|
opts.banner = "Usage: #{zero} transform [options] FROM_TEMPLATE TO_TEMPLATE"
separators(opts, DOC_STRINGS['transform'])

subcommand_options.scheduler_options = add_scheduler_opts(opts)
add_scheduler_opts subcommand_options, opts

opts.on("-q", "--quiet", "Do not display transformation preview (only valid with --force)") do
opts.on("-q", "--quiet", "Do not display transformation info (only valid with --force)") do
subcommand_options.quiet = true
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/gizzmo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def nameserver
gizzmo "addforwarding 0 1 localhost/s_0_001_replicating"
gizzmo "-f reload"

gizzmo('-f transform-tree --poll-interval=1 "ReplicatingShard(1) -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1))" localhost/s_0_001_replicating').should == <<-EOF
gizzmo('-f transform-tree --no-progress --poll-interval=1 "ReplicatingShard(1) -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1))" localhost/s_0_001_replicating').should == <<-EOF
ReplicatingShard(1) -> TestShard(localhost,1,Int,Int) => ReplicatingShard(1) -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1,1)) :
PREPARE
create_shard(TestShard/127.0.0.1)
Expand All @@ -355,7 +355,7 @@ def nameserver
localhost/s_0_001_a -> 127.0.0.1/s_0_0001
FINISHING:
[0] 1 = localhost/s_0_001_replicating: ReplicatingShard(1) -> TestShard(localhost,1,Int,Int) => ReplicatingShard(1) -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1,1))
All transformations applied. Total time elapsed: 10
All transformations applied. Total time elapsed: 01
EOF

nameserver[:shards].should == [ info("127.0.0.1", "s_0_0001", "TestShard"),
Expand All @@ -379,7 +379,7 @@ def nameserver
end
gizzmo "-f reload"

gizzmo('-f -T0 transform --poll-interval=1 "ReplicatingShard -> TestShard(localhost,1,Int,Int)" "ReplicatingShard -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1))"').should == <<-EOF
gizzmo('-f -T0 transform --no-progress --poll-interval=1 "ReplicatingShard -> TestShard(localhost,1,Int,Int)" "ReplicatingShard -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1))"').should == <<-EOF
ReplicatingShard(1) -> TestShard(localhost,1,Int,Int) => ReplicatingShard(1) -> (TestShard(localhost,1,Int,Int), TestShard(127.0.0.1,1)) :
PREPARE
create_shard(TestShard/127.0.0.1)
Expand Down

0 comments on commit 8424c69

Please sign in to comment.