Class: CapistranoMulticonfigParallel::Job

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/capistrano_multiconfig_parallel/classes/job.rb

Overview

class used for defining the job class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

action_confirmed?, fetch_parsed_string, fetch_remaining_arguments, find_remaining_args, get_question_details, message_from_bundler?, message_is_about_a_task?, message_is_for_stdout?, msg_for_stdin?, parse_json, parse_task_string, percent_of, regex_last_match, setup_command_line_standard, truncate, wrap_string

Methods included from CapistranoHelper

env_key_format, env_prefix, filtered_env_keys_format, setup_flags_for_job, trace_flag

Methods included from GemHelper

fetch_gem_version, find_loaded_gem, find_loaded_gem_property, format_gem_version, get_parsed_version, verify_gem_version

Methods included from StagesHelper

app_names_from_stages, check_stage_path, checks_paths, fetch_stages_app, fetch_stages_from_file, fetch_stages_paths, independent_deploy?, multi_apps?, sorted_paths, stages, stages_paths, stages_root

Methods included from ParseHelper

check_hash_set, check_numeric, strip_characters_from_string, value_is_array?, verify_array_of_strings, verify_empty_options, warn_array_without_strings

Methods included from CoreHelper

app_debug_enabled?, ask_confirm, ask_stdout_confirmation, check_terminal_tty, debug_websocket?, development_debug?, error_filtered?, execute_with_rescue, find_worker_log, force_confirmation, format_error, log_error, log_output_error, log_to_file, multi_fetch_argv, print_to_log_file, rescue_error, rescue_interrupt, setup_filename_logger, setup_logger_formatter, show_warning, terminal_actor, terminal_errors?, websocket_config, websocket_server_config

Methods included from InternalHelper

arg_is_in_default_config?, check_file, create_log_file, custom_commands, default_config_keys, default_internal_config, default_internal_configuration_params, detect_root, enable_main_log_file, fail_capfile_not_found, fetch_default_internal_config, find_config_type, find_env_multi_cap_root, find_file_by_names, find_file_in_directory, get_current_gem_name, internal_config_directory, internal_config_file, log_directory, main_log_file, multi_level_prop, pathname_is_root?, pwd_directory, pwd_parent_dir, root, setup_default_configuration_types, sliced_default_config, try_detect_file

Constructor Details

#initialize(application, options) ⇒ Job

Returns a new instance of Job.



14
15
16
17
18
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 14

def initialize(application, options)
  @options = options.stringify_keys
  @application = application
  @manager = @application.manager
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



189
190
191
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 189

def method_missing(sym, *args, &block)
  command.public_send(sym, *args, &block)
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 8

def application
  @application
end

#bundler_check_statusObject

Returns the value of attribute bundler_check_status.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 8

def bundler_check_status
  @bundler_check_status
end

#bundler_statusObject

Returns the value of attribute bundler_status.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 8

def bundler_status
  @bundler_status
end

#exit_status=(value) ⇒ Object (writeonly)

Sets the attribute exit_status

Parameters:

  • value

    the value to set the attribute exit_status to.



9
10
11
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 9

def exit_status=(value)
  @exit_status = value
end

#managerObject (readonly)

Returns the value of attribute manager.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 8

def manager
  @manager
end

#new_jobs_dispatched=(value) ⇒ Object (writeonly)

Sets the attribute new_jobs_dispatched

Parameters:

  • value

    the value to set the attribute new_jobs_dispatched to.



9
10
11
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 9

def new_jobs_dispatched=(value)
  @new_jobs_dispatched = value
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 8

def options
  @options
end

#status=(value) ⇒ Object (writeonly)

Sets the attribute status

Parameters:

  • value

    the value to set the attribute status to.



9
10
11
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 9

def status=(value)
  @status = value
end

#will_dispatch_new_job=(value) ⇒ Object (writeonly)

Sets the attribute will_dispatch_new_job

Parameters:

  • value

    the value to set the attribute will_dispatch_new_job to.



9
10
11
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 9

def will_dispatch_new_job=(value)
  @will_dispatch_new_job = value
end

Instance Method Details

#bundler_check_terminal_rowObject



61
62
63
64
65
66
67
68
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 61

def bundler_check_terminal_row
  [
    { value: id.to_s },
    { value: wrap_string(File.basename(job.job_path)) },
    { value: "bundle check || bundle install" },
    { value: bundler_check_status.to_s }
  ]
end

#bundler_terminal_rowObject



70
71
72
73
74
75
76
77
78
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 70

def bundler_terminal_row
  [
    { value: id.to_s },
    { value: wrap_string(job_stage_for_terminal) },
    { value: "Preparing app...setting up gems" },
    { value: terminal_env_variables.map { |str| wrap_string(str) }.join("\n") },
    { value: wrap_string(status.to_s.green) }
  ]
end

#commandObject



37
38
39
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 37

def command
  @command ||= CapistranoMulticonfigParallel::JobCommand.new(self)
end

#crashed?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 157

def crashed?
  worker_died? || failed? || exit_status.present?
end

#dead?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 161

def dead?
  status.present? && status.to_s.downcase == 'dead'
end

#env_variableObject



33
34
35
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 33

def env_variable
  CapistranoMulticonfigParallel.env_job_key_id
end

#failed?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 133

def failed?
  ['deploy:failed'].include?(status)
end

#finished?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 129

def finished?
  status == 'finished'
end

#gitflowObject



20
21
22
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 20

def gitflow
  @gitflow ||= command.gitflow_enabled?
end

#idObject



98
99
100
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 98

def id
  @id ||= @options.fetch('id', SecureRandom.uuid)
end

#inspectObject



173
174
175
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 173

def inspect
  to_s
end

#mark_for_dispatching_new_jobObject



141
142
143
144
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 141

def mark_for_dispatching_new_job
  return if rolling_back?
  self.will_dispatch_new_job = new_jobs_dispatched.size + 1
end

#marked_for_dispatching_new_job?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 146

def marked_for_dispatching_new_job?
  will_dispatch_new_job.to_i != new_jobs_dispatched.size
end

#new_jobs_dispatched_finished?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 150

def new_jobs_dispatched_finished?
  if marked_for_dispatching_new_job?
    sleep(0.1) until will_dispatch_new_job.to_i == new_jobs_dispatched.size
  end
  true
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 193

def respond_to_missing?(method_name, include_private = false)
  command.public_methods.include?(method_name) || super
end

#rolling_back?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 137

def rolling_back?
  ['deploy:rollback'].include?(action)
end

#row_sizeObject



80
81
82
83
84
85
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 80

def row_size
  longest_hash = terminal_row.max_by do |hash|
    hash[:value].size
  end
  (longest_hash[:value].size.to_f / 80.0).ceil
end

#save_stderr_error(data) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 24

def save_stderr_error(data)
  return unless development_debug?
  return unless @manager.alive?
  stderr_buffer.rewind
  old_data = stderr_buffer.read.dup
  new_data = old_data.to_s + data
  stderr_buffer.write(new_data) if ['aborted!', 'Terminating', 'Error'].any? { |word| new_data.include?(word) }
end

#setup_additional_env_variables(value) ⇒ Object



125
126
127
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 125

def setup_additional_env_variables(value)
  value["#{env_variable}"] = id
end

#terminal_env_variablesObject



41
42
43
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 41

def terminal_env_variables
  setup_command_line(filtered_keys: [env_variable])
end

#terminal_rowObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 45

def terminal_row
  if bundler_check_status
    bundler_check_terminal_row
  elsif bundler_status
    bundler_terminal_row
  else
    [
      { value: id.to_s },
      { value: wrap_string(job_stage_for_terminal) },
      { value: wrap_string(capistrano_action) },
      { value: terminal_env_variables.map { |str| wrap_string(str) }.join("\n") },
      { value: wrap_string(worker_state) }
    ]
  end
end

#to_jsonObject



181
182
183
184
185
186
187
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 181

def to_json
  hash = {}
  %w(id app stage action task_arguments env_options status exit_status bundler_status will_dispatch_new_job new_jobs_dispatched).each do |key|
    hash[key] = send(key).inspect
  end
  hash
end

#to_sObject



177
178
179
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 177

def to_s
  JSON.generate(to_json)
end

#work_done?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 169

def work_done?
  new_jobs_dispatched_finished? && (finished? || crashed?)
end

#workerObject



87
88
89
90
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 87

def worker
  return unless @manager.alive?
  @manager.get_worker_for_job(id)
end

#worker_died?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 165

def worker_died?
  dead? || worker == nil || worker.dead?
end

#worker_stateObject



92
93
94
95
96
# File 'lib/capistrano_multiconfig_parallel/classes/job.rb', line 92

def worker_state
  worker_obj = worker
  default = status.to_s.upcase.red
  worker_died? ? default : worker_obj.worker_state
end