class HammerCLIForemanTasks::TaskProgress

Attributes

interval[RW]
task[RW]

Public Class Methods

new(task_id, options = {}, &block) click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 6
def initialize(task_id, options = {}, &block)
  @update_block = block
  @task_id      = task_id
  @interval     = 2
  @options      = options
end

Public Instance Methods

render() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 13
def render
  update_task
  render_progress
end
success?() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 18
def success?
  !%w(error warning).include?(@task['result'])
end

Private Instance Methods

appropriate_verbosity?() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 74
def appropriate_verbosity?
  @options[:verbosity] >= HammerCLI::V_VERBOSE
end
progress_bar() { |bar| ... } click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 61
def progress_bar
  bar                                      = PowerBar.new
  @closed = false
  bar.settings.tty.finite.template.main    = '[${<bar>}] [${<percent>%}]'
  bar.settings.tty.finite.template.padchar = ' '
  bar.settings.tty.finite.template.barchar = '.'
  bar.settings.tty.finite.output           = Proc.new { |s| $stderr.print s if appropriate_verbosity? }
  yield bar
ensure
  bar.close
  render_result
end
progress_message() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 42
def progress_message
  "Task #{@task_id} #{task_pending? ? @task['state'] : @task['result']}"
end
render_progress() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 24
def render_progress
  progress_bar do |bar|
    begin
      while true
        bar.show(:msg => progress_message, :done => @task['progress'].to_f, :total => 1)
        if task_pending?
          sleep interval
          update_task
        else
          break
        end
      end
    rescue Interrupt
      # Inerrupting just means we stop rednering the progress bar
    end
  end
end
render_result() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 46
def render_result
  puts @task['humanized']['output'] if !@task['humanized']['output'].to_s.empty? && appropriate_verbosity?
  unless @task['humanized']['errors'].nil? || @task['humanized']['errors'].empty?
    STDERR.puts "Error: #{@task['humanized']['errors'].join("\n")}"
  end
end
task_pending?() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 57
def task_pending?
  !%w[paused stopped].include?(@task['state'])
end
update_task() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 53
def update_task
  @task = @update_block.call(@task_id)
end