Class: CapistranoMulticonfigParallel::TerminalTable
- Inherits:
-
Object
- Object
- CapistranoMulticonfigParallel::TerminalTable
show all
- Includes:
- ApplicationHelper, 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, job, count, last_job_id) ⇒ Object
-
#get_worker_details(job_id, job, 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
-
#show_worker_percent(worker, tasks, current_task, processed_job) ⇒ Object
-
#terminal_clear ⇒ Object
-
#worker_progress(processed_job, worker) ⇒ Object
-
#worker_state(worker, job) ⇒ Object
parse_task_string, strip_characters_from_string
check_stage_path, checks_paths, fetch_stages, fetch_stages_paths, sorted_paths, stages_paths
Methods included from CoreHelper
app_configuration, app_debug_enabled?, app_logger, ask_confirm, check_terminal_tty, debug_websocket?, execute_with_rescue, find_config_type, find_loaded_gem, find_worker_log, force_confirmation, format_error, log_error, log_to_file, rescue_error, rescue_interrupt, show_warning, websocket_config, websocket_server_config
config_file, custom_commands, default_internal_config, detect_root, enable_main_log_file, find_env_multi_cap_root, internal_config_directory, internal_config_file, log_directory, main_log_file, root, try_detect_capfile
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, job, count, last_job_id) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 90
def add_job_to_table(table, job_id, job, count, last_job_id)
return unless @manager.alive?
worker = @manager.get_worker_for_job(job_id)
details = get_worker_details(job_id, job, 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']}" }
]
table.add_row(row)
table.add_separator if last_job_id != job_id.to_i
end
|
#get_worker_details(job_id, job, worker) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 76
def get_worker_details(job_id, job, worker)
{
'job_id' => job_id,
'app_name' => job.app,
'env_name' => job.stage,
'full_stage' => job.job_stage,
'action_name' => job.capistrano_action,
'env_options' => job.setup_command_line_standard.join("\n"),
'task_arguments' => job.task_arguments,
'state' => worker_state(worker, job),
'processed_job' => job
}
end
|
#message_valid?(message) ⇒ Boolean
52
53
54
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 52
def message_valid?(message)
message[:type].present? && message[:type] == 'output' || message[: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, message)
return unless topic == CapistranoMulticonfigParallel::TerminalTable.topic
default_headings = ['Job ID', 'Job UUID', 'App/Stage', 'Action', 'ENV Variables', 'Current Task']
table = Terminal::Table.new(title: 'Deployment Status Table', headings: default_headings)
if @manager.alive? && @manager.jobs.present? && message_valid?(message)
count = 0
last_job_id = @manager.jobs.keys.last.to_i
@manager.jobs.each do |job_id, job|
count += 1
add_job_to_table(table, job_id, job, count, last_job_id)
end
end
show_terminal_screen(table)
rescue => ex
log_to_file("Terminal Table client disconnected due to error #{ex.inspect}")
log_to_file(ex.backtrace)
terminate
end
|
#percent_of(index, total) ⇒ Object
138
139
140
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 138
def percent_of(index, total)
index.to_f / total.to_f * 100.0
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(message, default)
exclusive do
ask_confirm(message, 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"
puts table
puts "\n"
sleep(1)
@job_manager.condition.signal('completed') if @manager.all_workers_finished?
end
|
#show_worker_percent(worker, tasks, current_task, processed_job) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 126
def show_worker_percent(worker, tasks, current_task, processed_job)
total_tasks = worker.alive? ? tasks.size : nil
task_index = worker.alive? ? tasks.index(current_task.to_s).to_i + 1 : 0
percent = percent_of(task_index, total_tasks)
result = "Progress [#{format('%.2f', percent)}%] (executed #{task_index} of #{total_tasks})"
if worker.alive?
@manager.job_crashed?(processed_job) ? result.red : result.green
else
worker_state(worker)
end
end
|
#terminal_clear ⇒ Object
115
116
117
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 115
def terminal_clear
system('cls') || system('clear') || puts("\e[H\e[2J")
end
|
#worker_progress(processed_job, worker) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 119
def worker_progress(processed_job, worker)
return worker_state(worker) unless worker.alive?
tasks = worker.alive? ? worker.invocation_chain : []
current_task = worker.alive? ? worker.machine.state.to_s : ''
show_worker_percent(worker, tasks, current_task, processed_job)
end
|
#worker_state(worker, job) ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'lib/capistrano_multiconfig_parallel/celluloid/terminal_table.rb', line 67
def worker_state(worker, job)
if worker.alive?
state = worker.machine.state.to_s
@manager.job_crashed?(job) ? state.red : state.green
else
'dead'.upcase.red
end
end
|