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

parse_task_string, strip_characters_from_string

Methods included from StagesHelper

check_stage_path, checks_paths, fetch_stages, fetch_stages_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

Methods included from InternalHelper

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

Instance Attribute Details

#actor ⇒ Object

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_status ⇒ Object

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

#job_id ⇒ Object

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

#pid ⇒ Object

Returns the value of attribute pid.



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

def pid
  @pid
end

#process ⇒ Object

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



79
80
81
82
83
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 79

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
end

#check_exit_status ⇒ Object



38
39
40
41
42
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 38

def check_exit_status
  return if @exit_status.blank? || !@actor.worker_finshed?
  debug("worker #{@actor.job_id} startsnotify finished") if @debug_enabled
  @actor.notify_finished(@exit_status)
end

#io_callback(io, data) ⇒ Object



89
90
91
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 89

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

#on_exit(status) ⇒ Object



74
75
76
77
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 74

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

#on_input_stdin(data) ⇒ Object



62
63
64
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 62

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

#on_pid(pid) ⇒ Object



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

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

#on_read_stderr(data) ⇒ Object



70
71
72
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 70

def on_read_stderr(data)
  io_callback('stderr', data)
end

#on_read_stdout(data) ⇒ Object



66
67
68
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 66

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

#process_finalizer ⇒ Object



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

def process_finalizer
  @timer.cancel
  EM.stop if EM.reactor_running?
end

#start_async_deploy(cmd, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 44

def start_async_deploy(cmd, options)
  RightScale::RightPopen.popen3_async(
    cmd,
    target: self,
    environment: options[:environment].present? ? options[: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

#watch_handler(process) ⇒ Object



85
86
87
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 85

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

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 14

def work(cmd, options = {})
  @options = options
  @actor = @options.fetch(:actor, nil)
  @job_id = @actor.job_id
  EM.run do
    EM.next_tick do
      start_async_deploy(cmd, options)
    end
    @timer = EM::PeriodicTimer.new(0.1) do
      check_exit_status
    end
  end
  EM.error_handler do|e|
    log_to_file("Error during event loop for worker #{@job_id}: #{e.inspect}", @job_id)
    log_to_file(e.backtrace, @job_id)
    EM.stop
  end
end