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

Instance Method Details

#async_exception_handler(*data) ⇒ Object



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

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



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

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

#finalizeObject Also known as: terminate



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

def finalize
  EM.stop
  @timer.cancel
  super
  true
end

#io_callback(_io, data) ⇒ Object



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

def io_callback(_io, data)
  File.open(@filename, 'a') { |f| f << data }
end

#on_exit(status) ⇒ Object



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

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



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

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

#on_pid(pid) ⇒ Object



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

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

#on_read_stderr(data) ⇒ Object



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

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

#on_read_stdout(data) ⇒ Object



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

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

#set_worker_logObject



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

def set_worker_log
  FileUtils.mkdir_p(CapistranoMulticonfigParallel.log_directory)
  @filename = File.join(CapistranoMulticonfigParallel.log_directory, "worker_#{@actor.job_id}.log")
  FileUtils.rm_rf(@filename) if @options[:dry_run] || @actor.executed_dry_run != true
end

#start_async_deploy(cmd, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 56

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



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

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

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/capistrano_multiconfig_parallel/celluloid/child_process.rb', line 18

def work(cmd, options = {})
  @options = options
  @actor = @options.fetch(:actor, nil)
  EM.run do
    EM.next_tick do
      set_worker_log
      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