Module: CapistranoMulticonfigParallel::CoreHelper

Included in:
ApplicationHelper
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

Class Method Details

.app_debug_enabled?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 6

def app_debug_enabled?
  configuration.multi_debug.to_s.downcase == 'true'
end

.ask_confirm(message, default) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 25

def ask_confirm(message, default)
  force_confirmation do
    ask_stdout_confirmation(message, default)
  end
rescue
  return nil
end

.ask_stdout_confirmation(message, default) ⇒ Object



19
20
21
22
23
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 19

def ask_stdout_confirmation(message, default)
  result = Ask.input message, default: default
  $stdout.flush
  result
end

.check_terminal_ttyObject



14
15
16
17
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 14

def check_terminal_tty
  $stdin.sync = true if $stdin.isatty
  $stdout.sync = true if $stdout.isatty
end

.debug_websocket?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 98

def debug_websocket?
  websocket_server_config['enable_debug'].to_s == 'true'
end

.error_filtered?(error) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 41

def error_filtered?(error)
  [CapistranoMulticonfigParallel::CelluloidWorker::TaskFailed].find { |class_name| error.is_a?(class_name) }.present?
end

.execute_with_rescue(output = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 111

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



77
78
79
80
81
82
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 77

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



33
34
35
36
37
38
39
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 33

def force_confirmation(&block)
  `stty -raw echo`
  check_terminal_tty
  result = block.call
  `stty -raw echo`
  result
end

.format_error(exception) ⇒ Object



59
60
61
62
63
64
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 59

def format_error(exception)
  message = "\n#{exception.class} (#{exception.respond_to?(:message) ? exception.message : exception.inspect}):\n"
  message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
  message << '  ' << exception.backtrace.join("\n  ") if exception.respond_to?(:backtrace)
  message
end

.log_error(error, output = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 45

def log_error(error, output = nil)
  return if error_filtered?(error)
  message = format_error(error)
  log_output_error(output, message)
  log_to_file(message, log_method: 'fatal')
end

.log_output_error(output, message) ⇒ Object



52
53
54
55
56
57
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 52

def log_output_error(output, message)
  return if output.blank?
  terminal = Celluloid::Actor[:terminal_server]
  terminal.errors.push(message) if terminal.present? && terminal.alive? && !terminal.errors.include?(message)
  puts message
end

.log_to_file(message, options = {}) ⇒ Object



66
67
68
69
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 66

def log_to_file(message, options = {})
  worker_log = options.fetch(:job_id, '').present? ? find_worker_log(options[:job_id]) : logger
  print_to_log_file(worker_log, options.merge(message: message)) if worker_log.present? && app_debug_enabled?
end


71
72
73
74
75
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 71

def print_to_log_file(worker_log, options = {})
  ActiveSupport::Deprecation.silence do
    worker_log.send(options.fetch(:log_method, 'debug'), "#{options.fetch(:message, '')}\n")
  end
end

.rescue_error(error, output = nil) ⇒ Object



119
120
121
122
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 119

def rescue_error(error, output = nil)
  log_error(error, output)
  exit(1)
end

.rescue_interruptObject



124
125
126
127
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 124

def rescue_interrupt
  `stty icanon echo`
  puts "\n Command was cancelled due to an Interrupt error."
end

.setup_filename_logger(filename) ⇒ Object



84
85
86
87
88
89
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 84

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



91
92
93
94
95
96
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 91

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



10
11
12
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 10

def show_warning(message)
  warn message if app_debug_enabled?
end

.websocket_configObject



106
107
108
109
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 106

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?, 'use_redis' => false)
end

.websocket_server_configObject



102
103
104
# File 'lib/capistrano_multiconfig_parallel/helpers/core_helper.rb', line 102

def websocket_server_config
  configuration.fetch(:websocket_server, {}).stringify_keys
end