LoginSignup
2
2

More than 5 years have passed since last update.

digdag logを繰り返し実行するプログラム

Last updated at Posted at 2017-02-08

何度も手でdigdag logを打つのは面倒
digdag logは時々「error: Attempted read on closed stream. (io)」エラーになる(0.9.4にて)
digdag UIはログの取りこぼしがある(0.9.4にて)

digdag_log.rb
require 'open3'

STDOUT.sync = true

attempt_id = ARGV[0]

log_line_no_max = 0
attempt_stdout = nil

loop do
  # digdag attempt実行 (エラーなら即終了)
  status = nil
  attempt_stdout = []
  Open3.popen3("digdag attempt " + attempt_id) do |stdin, stdout, stderr, wait_thr|
    stdout.each do |line|
      tmp = line.match(/  status: (.+)/)
      status = tmp[1] unless tmp.nil?
      attempt_stdout << line
    end
    unless wait_thr.value.success?
      stdout.each do |line|        puts line end
      stderr.each do |line| STDERR.puts line end
      abort
    end
  end

  # digdag log実行 (正常終了するまで繰り返す)
  loop do
    log_line_no = 0
    log_success = false
    Open3.popen3("digdag log " + attempt_id) do |stdin, stdout, stderr, wait_thr|
      stdout.each do |line|
        log_line_no += 1
        # 前回出力行以降を出力
        if (log_line_no > log_line_no_max)
          puts line
          log_line_no_max = log_line_no
        end
      end
      log_success = wait_thr.value.success?
    end
    if log_success
      break
    end
  end

  # attemptが終了しているか
  if status == "error" || status == "success"
    break
  end

  # 次の実行までスリープ
  sleep 10
end

puts
puts "this attempt ended with following status"
puts
puts attempt_stdout
ruby digdag_log.rb 6
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2