Module: CapistranoMulticonfigParallel::CoreHelper
- Included in:
- ApplicationHelper, Configuration
- Defined in:
- lib/capistrano_multiconfig_parallel/helpers/core_helper.rb
Overview
class that holds the options that are configurable for this gem
Class Method Summary collapse
- .app_debug_enabled? ⇒ Boolean
- .ask_confirm(message, default) ⇒ Object
- .ask_stdout_confirmation(message, default) ⇒ Object
- .check_terminal_tty ⇒ Object
- .debug_websocket? ⇒ Boolean
- .error_filtered?(error) ⇒ Boolean
- .execute_with_rescue(output = nil) ⇒ Object
- .find_worker_log(job_id) ⇒ Object
- .force_confirmation(&block) ⇒ Object
- .format_error(exception) ⇒ Object
- .log_error(error, options = {}) ⇒ Object
- .log_output_error(error, output, message) ⇒ Object
- .log_to_file(message, options = {}) ⇒ Object
- .multi_fetch_argv(args) ⇒ Object
- .print_to_log_file(worker_log, options = {}) ⇒ Object
- .rescue_error(error, output = nil) ⇒ Object
- .rescue_interrupt ⇒ Object
- .setup_filename_logger(filename) ⇒ Object
- .setup_logger_formatter(logger) ⇒ Object
- .show_warning(message) ⇒ Object
- .terminal_actor ⇒ Object
- .terminal_errors? ⇒ Boolean
- .websocket_config ⇒ Object
- .websocket_server_config ⇒ Object
Class Method Details
.app_debug_enabled? ⇒ Boolean
16 17 18 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 16 def app_debug_enabled? configuration.multi_debug.to_s.downcase == 'true' end |
.ask_confirm(message, default) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 35 def ask_confirm(, default) force_confirmation do ask_stdout_confirmation(, default) end rescue return nil end |
.ask_stdout_confirmation(message, default) ⇒ Object
29 30 31 32 33 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 29 def ask_stdout_confirmation(, default) result = Ask.input , default: default $stdout.flush result end |
.check_terminal_tty ⇒ Object
24 25 26 27 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 24 def check_terminal_tty $stdin.sync = true if $stdin.isatty $stdout.sync = true if $stdout.isatty end |
.debug_websocket? ⇒ Boolean
114 115 116 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 114 def debug_websocket? websocket_server_config['enable_debug'].to_s == 'true' end |
.error_filtered?(error) ⇒ Boolean
51 52 53 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 51 def error_filtered?(error) [CapistranoMulticonfigParallel::CelluloidWorker::TaskFailed, SystemExit].find { |class_name| error.is_a?(class_name) }.present? end |
.execute_with_rescue(output = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 127 def execute_with_rescue(output = nil) yield if block_given? rescue Interrupt rescue_interrupt rescue => error rescue_error(error, output) end |
.find_worker_log(job_id) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 93 def find_worker_log(job_id) return if job_id.blank? FileUtils.mkdir_p(log_directory) unless File.directory?(log_directory) filename = File.join(log_directory, "worker_#{job_id}.log") setup_filename_logger(filename) end |
.force_confirmation(&block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 43 def force_confirmation(&block) `stty -raw echo` check_terminal_tty result = block.call `stty -raw echo` result end |
.format_error(exception) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 75 def format_error(exception) = "\n#{exception.class} (#{exception.respond_to?(:message) ? exception.message : exception.inspect}):\n" << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) << ' ' << exception.backtrace.join("\n ") if exception.respond_to?(:backtrace) end |
.log_error(error, options = {}) ⇒ Object
55 56 57 58 59 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 55 def log_error(error, = {}) = format_error(error) log_output_error(error, .fetch(:output, nil), ) log_to_file(, .merge(log_method: 'fatal')) end |
.log_output_error(error, output, message) ⇒ Object
69 70 71 72 73 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 69 def log_output_error(error, output, ) return if .blank? || error_filtered?(error) puts if output.present? terminal_actor.errors.push() if terminal_errors? end |
.log_to_file(message, options = {}) ⇒ Object
82 83 84 85 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 82 def log_to_file(, = {}) worker_log = .fetch(:job_id, '').present? ? find_worker_log([:job_id]) : logger print_to_log_file(worker_log, .merge(message: )) if worker_log.present? && app_debug_enabled? end |
.multi_fetch_argv(args) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 6 def multi_fetch_argv(args) = {} args.each do |arg| if arg =~ /^(\w+)=(.*)$/m [Regexp.last_match(1)] = Regexp.last_match(2) end end end |
.print_to_log_file(worker_log, options = {}) ⇒ Object
87 88 89 90 91 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 87 def print_to_log_file(worker_log, = {}) ActiveSupport::Deprecation.silence do worker_log.send(.fetch(:log_method, 'debug'), "#{options.fetch(:message, '')}\n") end end |
.rescue_error(error, output = nil) ⇒ Object
135 136 137 138 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 135 def rescue_error(error, output = nil) log_error(error, output: output) exit(1) end |
.rescue_interrupt ⇒ Object
140 141 142 143 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 140 def rescue_interrupt `stty icanon echo` puts "\n Command was cancelled due to an Interrupt error." end |
.setup_filename_logger(filename) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 100 def setup_filename_logger(filename) worker_log = ::Logger.new(filename) worker_log.level = ::Logger::Severity::DEBUG setup_logger_formatter(worker_log) worker_log end |
.setup_logger_formatter(logger) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 107 def setup_logger_formatter(logger) logger.formatter = proc do |severity, datetime, progname, msg| date_format = datetime.strftime('%Y-%m-%d %H:%M:%S') "[#{date_format}] #{severity} (#{progname}): #{msg}\n" end end |
.show_warning(message) ⇒ Object
20 21 22 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 20 def show_warning() warn if app_debug_enabled? end |
.terminal_actor ⇒ Object
61 62 63 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 61 def terminal_actor Celluloid::Actor[:terminal_server] end |
.terminal_errors? ⇒ Boolean
65 66 67 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 65 def terminal_errors? terminal_actor.present? && terminal_actor.alive? && terminal_actor.errors.is_a?(Array) end |
.websocket_config ⇒ Object
122 123 124 125 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 122 def websocket_config create_log_file(websocket_server_config.fetch('log_file_path', nil)) if debug_websocket? websocket_server_config.merge('enable_debug' => debug_websocket?) end |
.websocket_server_config ⇒ Object
118 119 120 |
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 118 def websocket_server_config configuration.fetch(:websocket_server, {}).stringify_keys end |