Class: CapistranoMulticonfigParallel::TerminalTable
- Inherits:
-
Object
- Object
- CapistranoMulticonfigParallel::TerminalTable
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #add_job_to_table(table, job_id, count) ⇒ Object
- #get_worker_details(job_id, worker) ⇒ Object
-
#initialize(manager, job_manager) ⇒ TerminalTable
constructor
A new instance of TerminalTable.
- #message_valid?(message) ⇒ Boolean
- #notify_time_change(topic, message) ⇒ Object
- #percent_of(index, total) ⇒ Object
- #run ⇒ Object
- #show_confirmation(message, default) ⇒ Object
- #show_terminal_screen(table) ⇒ Object
- #terminal_clear ⇒ Object
- #worker_action(processed_job) ⇒ Object
- #worker_dry_running?(worker) ⇒ Boolean
- #worker_env_options(processed_job) ⇒ Object
- #worker_progress(_details, worker) ⇒ Object
- #worker_stage(processed_job) ⇒ Object
- #worker_state(worker) ⇒ Object
Constructor Details
#initialize(manager, job_manager) ⇒ TerminalTable
Returns a new instance of TerminalTable.
14 15 16 17 18 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 14 def initialize(manager, job_manager) @manager = manager @job_manager = job_manager async.run end |
Class Method Details
.topic ⇒ Object
10 11 12 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 10 def self.topic 'sshkit_terminal' end |
Instance Method Details
#add_job_to_table(table, job_id, count) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 108 def add_job_to_table(table, job_id, count) worker = @manager.get_worker_for_job(job_id) details = get_worker_details(job_id, worker) row = [{ value: count.to_s }, { value: job_id.to_s }, { value: details['full_stage'] }, { value: details['action_name'] }, { value: details['env_options'] }, { value: "#{details['state']}" } ] # if CapistranoMulticonfigParallel.show_task_progress # if worker.alive? # row << { value: worker.rake_tasks.size } # row << { value: worker_progress(details, worker) } # else # row << { value: 0 } # row << { value: worker_state(worker) } # end # end table.add_row(row) table.add_separator if @manager.jobs.keys.last.to_i != job_id.to_i end |
#get_worker_details(job_id, worker) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 92 def get_worker_details(job_id, worker) job = @manager.jobs[job_id] processed_job = @manager.process_job(job) { 'job_id' => job_id, 'app_name' => processed_job['app_name'], 'env_name' => processed_job['env_name'], 'full_stage' => worker_stage(processed_job), 'action_name' => worker_action(processed_job), 'env_options' => (processed_job), 'task_arguments' => job['task_arguments'], 'state' => worker_state(worker) } end |
#message_valid?(message) ⇒ Boolean
52 53 54 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 52 def () [:type].present? && [:type] == 'output' || [:type] == 'event' end |
#notify_time_change(topic, message) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 24 def notify_time_change(topic, ) return unless topic == CapistranoMulticonfigParallel::TerminalTable.topic default_headings = ['Job ID', 'Job UUID', '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? && () count = 0 @manager.jobs.each do |job_id, _job| count += 1 add_job_to_table(table, job_id, count) end end show_terminal_screen(table) rescue => ex info "Terminal Table client disconnected due to error #{ex.inspect}" info ex.backtrace terminate end |
#percent_of(index, total) ⇒ Object
156 157 158 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 156 def percent_of(index, total) index.to_f / total.to_f * 100.0 end |
#run ⇒ Object
20 21 22 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 20 def run subscribe(CapistranoMulticonfigParallel::TerminalTable.topic, :notify_time_change) end |
#show_confirmation(message, default) ⇒ Object
46 47 48 49 50 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 46 def show_confirmation(, default) exclusive do CapistranoMulticonfigParallel.ask_confirm(, default) end end |
#show_terminal_screen(table) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 56 def show_terminal_screen(table) return unless table.rows.present? terminal_clear puts "\n" # table.style = { width: 20 } puts table puts "\n" sleep(1) @job_manager.condition.signal('completed') if @manager.all_workers_finished? end |
#terminal_clear ⇒ Object
132 133 134 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 132 def terminal_clear system('cls') || system('clear') || puts("\e[H\e[2J") end |
#worker_action(processed_job) ⇒ Object
84 85 86 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 84 def worker_action(processed_job) processed_job['task_arguments'].present? ? "#{processed_job['action_name']}[#{processed_job['task_arguments'].join(',')}]" : processed_job['action_name'] end |
#worker_dry_running?(worker) ⇒ Boolean
136 137 138 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 136 def worker_dry_running?(worker) worker.alive? && worker.dry_running? end |
#worker_env_options(processed_job) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 76 def (processed_job) worker_optons = '' processed_job['job_argv'].each do |elem| worker_optons << "#{elem}\n" end worker_optons end |
#worker_progress(_details, worker) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 140 def worker_progress(_details, worker) return worker_state(worker) unless worker.alive? return if worker.executing_dry_run.nil? tasks = worker.alive? ? worker.dry_run_tasks : [] current_task = worker.alive? ? worker.machine.state.to_s : '' total_tasks = worker_dry_running?(worker) ? nil : tasks.size task_index = worker_dry_running?(worker) ? 0 : tasks.index(current_task.to_s).to_i + 1 percent = percent_of(task_index, total_tasks) result = worker_dry_running?(worker) ? 'Please wait.. building the progress bars' : "Progress [#{format('%.2f', percent)}%] (executed #{task_index} of #{total_tasks})" if worker.alive? worker.crashed? ? result.red : result.green else worker_state(worker) end end |
#worker_stage(processed_job) ⇒ Object
88 89 90 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 88 def worker_stage(processed_job) processed_job['app_name'].present? ? "#{processed_job['app_name']}\n#{processed_job['env_name']}" : "#{processed_job['env_name']}" end |
#worker_state(worker) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 67 def worker_state(worker) if worker.alive? state = worker.machine.state.to_s worker.crashed? ? state.red : state.green else 'dead'.upcase.red end end |