Class: CapistranoMulticonfigParallel::ChildProcess

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper, Celluloid, Celluloid::Logger
Defined in:
lib/capistrano_multiconfig_parallel/celluloid/child_process.rb

Overview

class that is used to execute the capistrano tasks and it is invoked by the celluloid worker

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, msg_for_stdin?, msg_for_task?, parse_task_string, percent_of, regex_last_match, setup_command_line_standard, 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, 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?, 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_in_directory, 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

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def actor
  @actor
end

#exit_statusObject

Returns the value of attribute exit_status.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def exit_status
  @exit_status
end

#jobObject

Returns the value of attribute job.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def job
  @job
end

#job_idObject

Returns the value of attribute job_id.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def job_id
  @job_id
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def options
  @options
end

#pidObject

Returns the value of attribute pid.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def pid
  @pid
end

#processObject

Returns the value of attribute process.



10
11
12
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 10

def process
  @process
end

Instance Method Details

#async_exception_handler(*data) ⇒ Object



115
116
117
118
119
120
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 115

def async_exception_handler(*data)
  log_to_file "Child process for worker #{@job_id} async_exception_handler  disconnected due to error #{data.inspect}"
  io_callback('stderr', data)
  @exit_status = 1
  check_exit_status
end

#check_exit_statusObject



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

def check_exit_status
  return if @exit_status.blank?
  @timer.cancel
  @job.exit_status = @exit_status
  print_error_if_exist
  log_to_file("worker #{@job_id} startsnotify finished with exit status #{@exit_status.inspect}")
  @actor.async.notify_finished(@exit_status)
end

#io_callback(io, data) ⇒ Object



127
128
129
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 127

def io_callback(io, data)
  log_to_file("#{io.upcase} ---- #{data}", job_id: @job_id)
end

#on_exit(status) ⇒ Object



109
110
111
112
113
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 109

def on_exit(status)
  log_to_file "Child process for worker #{@job_id} on_exit  disconnected due to error #{status.inspect}"
  @exit_status = status.exitstatus
  check_exit_status
end

#on_input_stdin(data) ⇒ Object



96
97
98
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 96

def on_input_stdin(data)
  io_callback('stdin', data)
end

#on_pid(pid) ⇒ Object



92
93
94
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 92

def on_pid(pid)
  @pid ||= pid
end

#on_read_stderr(data) ⇒ Object



104
105
106
107
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 104

def on_read_stderr(data)
  @job.save_stderr_error(data)
  io_callback('stderr', data)
end

#on_read_stdout(data) ⇒ Object



100
101
102
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 100

def on_read_stdout(data)
  io_callback('stdout', data)
end


70
71
72
73
74
75
76
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 70

def print_error_if_exist
  [@job.stderr_buffer].each do |buffer|
    buffer.rewind
    data = buffer.read
    log_output_error(nil, 'stderr', "Child process for worker #{@job_id} died for reason: #{data}") if data.present?
  end
end

#process_finalizerObject



56
57
58
59
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 56

def process_finalizer
  EM.stop if EM.reactor_running?
  terminate
end

#run_event_machineObject



40
41
42
43
44
45
46
47
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 40

def run_event_machine
  EM.run do
    EM.next_tick do
      start_async_deploy
    end
    setup_periodic_timer
  end
end

#setup_attributesObject



27
28
29
30
31
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 27

def setup_attributes
  @actor = @options.fetch(:actor, nil)
  @job_id = @job.id
  @exit_status = nil
end

#setup_em_error_handlerObject



33
34
35
36
37
38
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 33

def setup_em_error_handler
  EM.error_handler do|exception|
    log_error(exception, job_id: @job_id, output: 'stderr')
    EM.stop
  end
end

#setup_periodic_timerObject



49
50
51
52
53
54
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 49

def setup_periodic_timer
  @timer = EM::PeriodicTimer.new(0.1) do
    check_exit_status
    @timer.cancel if @exit_status.present?
  end
end

#start_async_deployObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 78

def start_async_deploy
  RightScale::RightPopen.popen3_async(
    @cmd,
    target: self,
    environment: @options.fetch(:environment, nil),
    pid_handler: :on_pid,
    input: :on_input_stdin,
    stdout_handler: :on_read_stdout,
    stderr_handler: :on_read_stderr,
    watch_handler: :watch_handler,
    async_exception_handler: :async_exception_handler,
    exit_handler: :on_exit)
end

#start_runningObject



21
22
23
24
25
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 21

def start_running
  setup_attributes
  run_event_machine
  setup_em_error_handler
end

#watch_handler(process) ⇒ Object



122
123
124
125
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 122

def watch_handler(process)
  @process ||= process
  check_exit_status
end

#work(job, cmd, options = {}) ⇒ Object



14
15
16
17
18
19
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 14

def work(job, cmd, options = {})
  @options = options
  @job = job
  @cmd = cmd
  start_running
end