Class: CapistranoMulticonfigParallel::ChildProcess

Inherits:
Object
  • Object
show all
Includes:
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

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def actor
  @actor
end

#exit_statusObject

Returns the value of attribute exit_status.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def exit_status
  @exit_status
end

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def filename
  @filename
end

#pidObject

Returns the value of attribute pid.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def pid
  @pid
end

#processObject

Returns the value of attribute process.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def process
  @process
end

#worker_logObject

Returns the value of attribute worker_log.



7
8
9
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 7

def worker_log
  @worker_log
end

Instance Method Details

#async_exception_handler(*data) ⇒ Object



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

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

#check_exit_statusObject



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

def check_exit_status
  return unless @exit_status.present?
  @timer.cancel
  EM.stop
  if @options[:dry_run]
    debug("worker #{@actor.job_id} starts execute deploy") if @actor.debug_enabled?
    @actor.async.execute_deploy
  else
    debug("worker #{@actor.job_id} startsnotify finished") if @actor.debug_enabled?
    @actor.notify_finished(@exit_status)
  end
end

#io_callback(io, data) ⇒ Object



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

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

#on_exit(status) ⇒ Object



82
83
84
85
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 82

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

#on_input_stdin(data) ⇒ Object



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

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

#on_pid(pid) ⇒ Object



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

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

#on_read_stderr(data) ⇒ Object



78
79
80
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 78

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

#on_read_stdout(data) ⇒ Object



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

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

#set_worker_logObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 28

def set_worker_log
  FileUtils.mkdir_p(CapistranoMulticonfigParallel.log_directory) unless File.directory?(CapistranoMulticonfigParallel.log_directory)
  @filename = File.join(CapistranoMulticonfigParallel.log_directory, "worker_#{@actor.job_id}.log")
  FileUtils.rm_rf(@filename) if File.file?(@filename) && !@actor.crashed? && (@options[:dry_run] || @actor.executed_dry_run != true)
  @worker_log = ::Logger.new(@filename)
  @worker_log.level = ::Logger::Severity::DEBUG
  @worker_log.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

#start_async_deploy(cmd, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 53

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



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

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

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 9

def work(cmd, options = {})
  @options = options
  @actor = @options.fetch(:actor, nil)
  set_worker_log
  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|
    puts "Error during event loop for worker #{@actor.job_id}: #{e.inspect}" if @actor.debug_enabled?
    puts e.backtrace if @actor.debug_enabled?
    EM.stop
  end
end