Class: Gorgon::ProgressBarView

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgon/progress_bar_view.rb

Constant Summary collapse

MAX_LENGTH =
200
LOADING_MSG =
"Loading environment and workers..."
RUNNING_MSG =
"Running files:"
LEGEND_MSG =
"Legend:\nF - failure files count\nH - number of hosts that have run files\nW - number of workers running files"
PROGRESS_BAR_REFRESH_RATE =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(job_state) ⇒ ProgressBarView

  • because the resolution of the elapsed time is one second, we use the nyquist frequency of 0.5 seconds

en.wikipedia.org/wiki/Nyquist_frequency



17
18
19
20
21
# File 'lib/gorgon/progress_bar_view.rb', line 17

def initialize job_state
  @job_state = job_state
  @job_state.add_observer(self)
  @timer = EventMachine::PeriodicTimer.new(PROGRESS_BAR_REFRESH_RATE) { update_elapsed_time }
end

Instance Method Details

#create_progress_bar_if_started_job_runningObject



49
50
51
52
53
54
55
56
57
# File 'lib/gorgon/progress_bar_view.rb', line 49

def create_progress_bar_if_started_job_running
  if @progress_bar.nil? && @job_state.state == :running
    print "\r#{' ' * (LOADING_MSG.length)}\r"
    puts LEGEND_MSG
    @progress_bar = ProgressBar.create(:total => @job_state.total_files,
                                       :length => [terminal_length, MAX_LENGTH].min,
                                       :format => format(bar: :green, title: :white));
  end
end

#showObject



23
24
25
# File 'lib/gorgon/progress_bar_view.rb', line 23

def show
  print LOADING_MSG
end

#update(payload = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gorgon/progress_bar_view.rb', line 27

def update payload={}
  output_gorgon_crash_message payload if gorgon_crashed? payload

  create_progress_bar_if_started_job_running

  return if @progress_bar.nil? || @finished

  failed_files_count = @job_state.failed_files_count

  @progress_bar.title=" F: #{failed_files_count} H: #{@job_state.total_running_hosts} W: #{@job_state.total_running_workers}"
  if failed_files_count > 0
    @progress_bar.format(format(bar: :red, title: :default))
  end

  @progress_bar.progress = @job_state.finished_files_count

  if @job_state.is_job_complete? || @job_state.is_job_cancelled?
    @finished = true
    print_summary
  end
end

#update_elapsed_timeObject



59
60
61
62
# File 'lib/gorgon/progress_bar_view.rb', line 59

def update_elapsed_time
  @timer.cancel if @finished
  @progress_bar.refresh if @progress_bar
end