Class: CapistranoMulticonfigParallel::TerminalTable

Inherits:
Object
  • Object
show all
Includes:
BaseActorHelper
Defined in:
lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb

Overview

class used to display the progress of each worker on terminal screen using a table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseActorHelper

included

Constructor Details

#initialize(manager, job_manager, options = {}) ⇒ TerminalTable

Returns a new instance of TerminalTable.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 20

def initialize(manager, job_manager, options = {})
  @manager = manager
  @position = nil
  @terminal_rows = nil
  @errors = []
  @options = options.is_a?(Hash) ? options.stringify_keys : options
  @job_manager = job_manager
  @screen_erased = false
  async.run
rescue => ex
  rescue_exception(ex)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def errors
  @errors
end

#job_managerObject (readonly)

Returns the value of attribute job_manager.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def job_manager
  @job_manager
end

#managerObject (readonly)

Returns the value of attribute manager.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def manager
  @manager
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def options
  @options
end

#positionObject (readonly)

Returns the value of attribute position.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def position
  @position
end

#screen_erasedObject (readonly)

Returns the value of attribute screen_erased.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def screen_erased
  @screen_erased
end

#terminal_rowsObject (readonly)

Returns the value of attribute terminal_rows.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 8

def terminal_rows
  @terminal_rows
end

Class Method Details

.topicObject



16
17
18
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 16

def self.topic
  'sshkit_terminal'
end

Instance Method Details

#default_heaadingsObject



33
34
35
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 33

def default_heaadings
  ['Job UUID', 'App/Stage', 'Action', 'ENV Variables', 'Current Status']
end

#display_table_on_terminal(table, jobs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 58

def display_table_on_terminal(table, jobs)
  table_size = fetch_table_size(jobs)
  @position, @terminal_rows, @screen_erased = CapistranoMulticonfigParallel::Cursor.display_on_screen(
    "#{table}",
    @options.merge(
      position: @position,
      table_size: table_size,
      screen_erased: @screen_erased
    )
  )
  print_errors
  signal_complete
end

#fetch_table_size(jobs) ⇒ Object



53
54
55
56
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 53

def fetch_table_size(jobs)
  job_rows = jobs.sum { |_job_id, job| job.row_size }
  (job_rows + 2)**2
end

#managers_alive?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 91

def managers_alive?
  @job_manager.alive? && @manager.alive?
end

#notify_time_change(_channel, _message) ⇒ Object



41
42
43
44
45
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 41

def notify_time_change(_channel, _message)
  table = Terminal::Table.new(title: 'Deployment Status Table', headings: default_heaadings)
  jobs = setup_table_jobs(table)
  display_table_on_terminal(table, jobs)
end


72
73
74
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 72

def print_errors
  puts(@errors.join("\n")) if @errors.present? && @options.fetch('clear_screen', false).to_s == 'false'
end

#rescue_exception(ex) ⇒ Object



47
48
49
50
51
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 47

def rescue_exception(ex)
  log_to_file("Terminal Table client disconnected due to error #{ex.inspect}")
  rescue_error(ex, 'stderr')
  terminate
end

#runObject



37
38
39
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 37

def run
  subscribe(CapistranoMulticonfigParallel::TerminalTable.topic, :notify_time_change)
end

#setup_table_jobs(table) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 76

def setup_table_jobs(table)
  jobs = managers_alive? ? @manager.jobs.dup : []
  jobs.each do |job_id, job|
    table.add_row(job.terminal_row)
    table.add_separator if jobs.keys.last != job_id
  end
  jobs
end

#show_confirmation(message, default) ⇒ Object



85
86
87
88
89
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 85

def show_confirmation(message, default)
  exclusive do
    ask_confirm(message, default)
  end
end

#signal_completeObject



95
96
97
98
99
100
101
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 95

def signal_complete
  if managers_alive? && @manager.all_workers_finished? && workers_terminated.instance_variable_get("@waiters").blank?
    condition.signal('completed') if @job_manager.alive? && condition.instance_variable_get("@waiters").present?
  elsif !managers_alive?
    terminate
  end
end