lib/cloudstack-cli/helper.rb in cloudstack-cli-0.3.2 vs lib/cloudstack-cli/helper.rb in cloudstack-cli-0.3.3

- old
+ new

@@ -15,31 +15,50 @@ ids.map do |id| client.query_job(id)['jobstatus'] end end + ASYNC_STATES = { + 0 => "running", + 1 => "completed", + 2 => "error" + } + def watch_jobs(jobs) chars = %w(| / - \\) - async_state = {0 => "running", 1 => "completed", 2 => "error"} status = get_async_job_status(jobs.map {|job| job[:id]}) call = 0 + opts = {t_start: Time.now} + puts while status.include?(0) do - status = call.modulo(40) == 0 ? get_async_job_status(jobs.map {|job| job[:id]}) : status - print ("\r" + "\e[A\e[K" * (status.size)) if call > 0 - - status.each_with_index do |job_status, i| - puts "#{jobs[i][:name]} : job #{async_state[job_status]} #{chars[0]}" + if call.modulo(40) == 0 + t = Thread.new { status = get_async_job_status(jobs.map {|job| job[:id]}) } + while t.alive? + chars = print_job_status(jobs, status, chars, opts) + end + t.join + else + print ("\r" + "\e[A\e[K" * (status.size + 1)) if call > 0 + chars = print_job_status(jobs, status, chars, + call == 0 ? opts.merge(no_clear: true) : opts + ) + call += 1 end - - sleep 0.1 - chars.push chars.shift - call += 1 end - - print ("\r" + "\e[A\e[K" * (status.size)) + print_job_status(jobs, status, chars, opts) + end + + def print_job_status(jobs, status, spinner, opts = {t_start: Time.now}) + print ("\r" + "\e[A\e[K" * (status.size + 1)) unless opts[:no_clear] status.each_with_index do |job_status, i| - puts "#{jobs[i][:name]} : job #{async_state[job_status]}" + print "#{jobs[i][:name]} : job #{ASYNC_STATES[job_status]} " + puts job_status == 0 ? spinner.first : "" end + t_elapsed = opts[:t_start] ? (Time.now - opts[:t_start]).round(1) : 0 + puts "Runtime: #{t_elapsed}s" + sleep opts[:sleeptime] || 0.1 + spinner.push spinner.shift + spinner end def bootstrap_server(args = {}) if args[:project] && project = client(quiet: true).get_project(args[:project]) project_id = project["id"] \ No newline at end of file