Class: CapistranoMulticonfigParallel::ProcessRunner

Inherits:
Object
  • Object
show all
Includes:
BaseActorHelper
Defined in:
lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb

Instance Method Summary collapse

Methods included from BaseActorHelper

included

Instance Method Details

#check_exit_statusObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 59

def check_exit_status
  exit_status = @runner_status.exit_status
  return if exit_status.blank?
  @timer.cancel
  if @actor.present? && @actor.respond_to?(:notify_finished)
    log_to_file("#{@actor.class} #{@job_id} startsnotify finished with exit status #{exit_status.inspect}")
    if @actor.respond_to?(:async) && @synchronicity == :async
      @actor.async.notify_finished(exit_status, @runner_status)
    else
      @actor.notify_finished(exit_status, @runner_status)
    end
  end
end

#do_right_popen3(synchronicity, command, popen3_options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 115

def do_right_popen3(synchronicity, command, popen3_options)
  popen3_options = {
    :target                  => @runner_status,
    :environment             => @options.fetch(:environment, nil),
    :input                   => :on_input_stdin,
    :stdout_handler          => :on_read_stdout,
    :stderr_handler          => :on_read_stderr,
    :watch_handler           => :watch_handler,
    :pid_handler             => :on_pid,
    :timeout_handler         => :on_timeout,
    :size_limit_handler      => :on_size_limit,
    :exit_handler            => :on_exit,
    :async_exception_handler => :async_exception_handler
  }.merge(popen3_options)
  case synchronicity
  when :sync
    result = ::RightScale::RightPopen.popen3_sync(command, popen3_options)
  when :async
    result = ::RightScale::RightPopen.popen3_async(command, popen3_options)
  else
    raise "Uknown synchronicity = #{synchronicity.inspect}"
  end
  result == true
end

#do_right_popen3_async(command, popen3_options) ⇒ Object



144
145
146
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 144

def do_right_popen3_async( command, popen3_options)
  do_right_popen3(:async, command, popen3_options)
end

#do_right_popen3_sync(command, popen3_options) ⇒ Object



140
141
142
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 140

def do_right_popen3_sync(command, popen3_options)
  do_right_popen3(:sync, command, popen3_options)
end

#process_finalizerObject



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

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

#run_right_popen3Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 78

def run_right_popen3
  popen3_options = {
    #  :timeout_seconds  => @options.has_key?(:timeout) ? @options[:timeout] : 2,
    :size_limit_bytes => @options[:size_limit_bytes],
    :watch_directory  => @options[:watch_directory],
    :user             => @options[:user],
    :group            => @options[:group],
  }
  command = @runner_status.command
  case @synchronicity
  when :sync
    run_right_popen3_sync(command, popen3_options)
  when :async
    run_right_popen3_async(command, popen3_options)
  else
    raise "unknown synchronicity = #{synchronicity.inspect}"
  end
end

#run_right_popen3_async(command, popen3_options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 101

def run_right_popen3_async(command, popen3_options)
  EM.run do
    EM.defer do
      begin
        do_right_popen3_async(command, popen3_options)
      rescue Exception => e
        log_error(exception, job_id: @job_id, output: 'stderr')
        EM.stop
      end
    end
    setup_periodic_timer
  end
end

#run_right_popen3_sync(command, popen3_options) ⇒ Object



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

def run_right_popen3_sync(command, popen3_options)
  do_right_popen3_sync(command, popen3_options)
end

#setup_attributesObject



40
41
42
43
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 40

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

#setup_em_error_handlerObject



45
46
47
48
49
50
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 45

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



52
53
54
55
56
57
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 52

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

#start_runningObject



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

def start_running
  setup_attributes
  run_right_popen3
  setup_em_error_handler
end

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/capistrano_multiconfig_parallel/celluloid/process_runner.rb', line 23

def work(job, cmd, options = {})
  @options = options.is_a?(Hash) ? options.symbolize_keys : {}
  @job = job
  @cmd = cmd

  @runner_status_klass = @options[:runner_status_klass].present? ? @options[:runner_status_klass] : RunnerStatus
  @runner_status = @runner_status_klass.new(Actor.current, job, cmd,  @options)
  @synchronicity = @options[:process_sync]
  start_running
end