class HammerCLIForemanTasks::TaskProgress

Attributes

interval[RW]
task[RW]

Public Class Methods

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

Public Instance Methods

render() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 12
def render
  update_task
  render_progress
end

Private Instance Methods

progress_bar() { |bar| ... } click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 54
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 }
  yield bar
ensure
  bar.close
  render_result
end
progress_message() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 37
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 19
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 41
def render_result
  puts @task['humanized']['output'] unless @task['humanized']['output'].to_s.empty?
  STDERR.puts @task['humanized']['errors'].join("\n") unless @task['humanized']['errors'].to_s.empty?
end
task_pending?() click to toggle source
# File lib/hammer_cli_foreman_tasks/task_progress.rb, line 50
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 46
def update_task
  @task = @update_block.call(@task_id)
end