Class: CapistranoMulticonfigParallel::TerminalTable

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger, Celluloid::Notifications
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 rubocop:disable ClassLength

Constant Summary collapse

TOPIC =
'sshkit_terminal'

Instance Method Summary collapse

Constructor Details

#initialize(manager) ⇒ TerminalTable

Returns a new instance of TerminalTable.



11
12
13
14
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 11

def initialize(manager)
  @manager = manager
  async.run
end

Instance Method Details

#add_job_to_table(table, job_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 54

def add_job_to_table(table, job_id)
  worker = @manager.get_worker_for_job(job_id)
  return unless worker.present?
  worker_optons = ''
  worker.env_options.each do |key, value|
    worker_optons << "#{key}=#{value}\n"
  end
  state = worker.machine.state.to_s
  state = worker_crashed?(worker) ? state.red : state.green
  row = [{ value: worker.job_id.to_s },
         { value: "#{worker.app_name}\n#{worker.env_name}" },
         { value: worker.action_name },
         { value: worker_optons },
         { value: state }
        ]
  if CapistranoMulticonfigParallel.show_task_progress
    row << { value: worker.rake_tasks.size }
    row << { value: worker_progress(worker) }
  end
  table.add_row(row)
  table.add_separator if @manager.jobs.keys.last.to_i != job_id.to_i
end

#capture(stream) ⇒ Object

rubocop:disable Lint/Eval



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 85

def capture(stream)
  stream = stream.to_s
  captured_stream = Tempfile.new(stream)
  stream_io = eval("$#{stream}")
  origin_stream = stream_io.dup
  stream_io.reopen(captured_stream)

  yield

  stream_io.rewind
  return captured_stream.read
ensure
  captured_stream.close
  captured_stream.unlink
  stream_io.reopen(origin_stream)
end

#message_valid?(message) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 40

def message_valid?(message)
  message[:type].present? && message[:type] == 'output' || message[:type] == 'event'
end

#notify_time_change(topic, message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 20

def notify_time_change(topic, message)
  return unless topic == CapistranoMulticonfigParallel::TerminalTable::TOPIC
  default_headings = ['Job ID', 'App/Stage', 'Action', 'ENV Variables', 'Current Task']
  if CapistranoMulticonfigParallel.show_task_progress
    default_headings << 'Total'
    default_headings << 'Progress'
  end
  table = Terminal::Table.new(title: 'Deployment Status Table', headings: default_headings)
  if @manager.jobs.present? && message_valid?(message)
    @manager.jobs.each do |job_id, _job|
      add_job_to_table(table, job_id)
    end
  end
  show_terminal_screen(table)
rescue => ex
  info "Terminal Table  client disconnected due to error #{ex.inspect}"
  info ex.backtrace
  terminate
end

#runObject



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

def run
  subscribe(CapistranoMulticonfigParallel::TerminalTable::TOPIC, :notify_time_change)
end

#show_terminal_screen(table) ⇒ Object



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

def show_terminal_screen(table)
  return unless table.rows.present?
  terminal_clear
  puts "\n"
  #  table.style = { width: 20 }
  puts table
  puts "\n"
  sleep(1)
end

#terminal_clearObject



77
78
79
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 77

def terminal_clear
  system('cls') || system('clear') || puts("\e[H\e[2J")
end

#worker_crashed?(worker) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 81

def worker_crashed?(worker)
  worker.crashed?
end

#worker_progress(worker) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 102

def worker_progress(worker)
  tasks = worker.rake_tasks
  current_task = worker.machine.state
  total_tasks = tasks.size
  task_index = tasks.index(current_task)
  progress = Formatador::ProgressBar.new(total_tasks, color: 'green', start: task_index.to_i)
  result = capture(:stdout) do
    progress.increment
  end
  result = result.gsub("\r\n", '')
  result = result.gsub("\n", '')
  result = result.gsub('|', '#')
  result = result.gsub(/\s+/, ' ')
  if worker_crashed?(worker)
    return result.red
  else
    return result.green
  end
end