Class: TengineJobAgent::Run

Inherits:
Object
  • Object
show all
Includes:
CommandUtils
Defined in:
lib/tengine_job_agent/run.rb

Instance Method Summary collapse

Methods included from CommandUtils

included

Constructor Details

#initialize(logger, args, config = {}) ⇒ Run

Returns a new instance of Run.



9
10
11
12
13
14
15
16
17
18
# File 'lib/tengine_job_agent/run.rb', line 9

def initialize(logger, args, config = {})
  @logger = logger
  @pid_output = STDOUT
  @error_output = STDERR
  @args = args
  @config = config
  @pid_path = File.expand_path("pid_for_#{Process.pid}", @config['log_dir'])
  @timeout       = (config[:timeout      ] || ENV["MM_SYSTEM_AGENT_RUN_TIMEOUT"      ] || 600).to_i # seconds
  @timeout_alert = (config[:timeout_alert] || ENV["MM_SYSTEM_AGENT_RUN_TIMEOUT_ALERT"] || 30 ).to_i # seconds
end

Instance Method Details

#processObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tengine_job_agent/run.rb', line 20

def process
  validate_environment
  line = nil
  process_spawned = false
  begin
    timeout(@timeout) do #タイムアウト(秒)
      @logger.info("watchdog process spawning for #{@args.join(' ')}")
      pid = spawn_watchdog # watchdogプロセスをspawnで起動
      @logger.info("watchdog process spawned. PID: #{pid.inspect}")
      File.open(@pid_path, "r") do |f|
        sleep(0.1) until line = f.gets
        process_spawned = true
        @logger.info("watchdog process returned first result: #{line.inspect}")
        if line =~ /\A\d+\n?\Z/ # 数字と改行のみで構成されるならそれはPIDのはず。
          @pid_output.puts(line.strip)
          @logger.info("return PID: #{pid.inspect}")
        else
          f.rewind
          msg = f.read
          @logger.error("error occurred:\n#{msg}")
          @error_output.puts(msg)
          return false
        end
      end
    end
  rescue Timeout::Error => e
    @error_output.puts("[#{e.class.name}] #{e.message}")
    raise e # raiseしたものはTengineJobAgent::Run.processでloggerに出力されるので、ここでは何もしません
  end
end

#spawn_watchdogObject

引数に@pid_pathを渡してwatchdogを起動します。戻り値は起動したwatchdogのPIDです



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tengine_job_agent/run.rb', line 52

def spawn_watchdog
  @logger.info("pid file creating: #{@pid_path}")
  File.open(@pid_path, "w"){ } # ファイルをクリア
  @logger.info("pid file created: #{@pid_path}")
  # http://doc.ruby-lang.org/ja/1.9.2/method/Kernel/m/spawn.html を参考にしています
  args = @args # + [{:out => stdout_w}] #, :err => stderr_w}]
  watchdog = File.expand_path("../../bin/tengine_job_agent_watchdog", File.dirname(__FILE__))
  @logger.info("spawning watchdog: #{@pid_path}")
  pid = Process.spawn(RbConfig.ruby, watchdog, @pid_path, *args)
  @logger.info("spawned watchdog: #{pid}")
  return pid
end

#validate_environmentObject

ジョブ実行時に使用されるRubyが1.8系の場合でもtengine_job_agent_runがエラーを起こさない



66
67
68
69
70
71
72
# File 'lib/tengine_job_agent/run.rb', line 66

def validate_environment
  if RUBY_VERSION >= "1.9.2"
    @logger.info("RUBY_VERSION is #{RUBY_VERSION}")
  else
    raise "RUBY_VERSION must be >= 1.9.2 but was #{RUBY_VERSION}"
  end
end