LoginSignup
3
2

More than 5 years have passed since last update.

Digdagサーバの日次処理を指定各日について実行するプログラム

Last updated at Posted at 2017-10-27

Digdag 0.9.20まで動作確認済

each_daily.rb
require 'date'
require 'open3'

STDOUT.sync = true

# 第一引数に-fを指定すると同一日付のセッションが既にあった場合にリトライする
force_retry = false
if ARGV[0] == "-f"
  force_retry = true
  puts "同一日付のセッションが既にある場合にはリトライします。"
  ARGV.shift
end

# 指定各日について日次処理を実行
ARGV.each do |date_str|
  date = Date.parse(date_str)
  puts date.to_s + "の日次処理を実行します。(前日" + (date - 1).to_s + "のデータを処理します)"
  cmd = "digdag start <Digdagプロジェクト名> daily --session " + date.to_s ★ここを書き換える★
  puts cmd

  session_id = nil
  need_retry = false
  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    stdout.each do |line|
      puts line
      tmp = line.match(/  session id: (.+)/)
      session_id = tmp[1] if tmp
    end
    stderr.each do |line|
      STDERR.puts line
      tmp = line.match(/hint: use `(.+)` command to run the session again for the same session_time/)
      if tmp && force_retry
        cmd = tmp[1]
        need_retry = true
      end
    end
    unless wait_thr.value.success?
      abort unless need_retry
    end
  end

  if need_retry
    puts cmd
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      stdout.each do |line|
        puts line
        tmp = line.match(/  session id: (.+)/)
        session_id = tmp[1] if tmp
      end
      stderr.each do |line|
        STDERR.puts line
      end
      unless wait_thr.value.success?
        abort
      end
    end
  end

  cmd = "digdag session " + session_id
  loop do
    sleep 10
    status = nil
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      stdout.each do |line|
        tmp = line.match(/  status: (.+)/)
        status = tmp[1] unless tmp.nil?
      end
      unless wait_thr.value.success?
        stdout.each do |line|        puts line end
        stderr.each do |line| STDERR.puts line end
        abort
      end
    end
    if status == "error"
      puts " error"
      abort
    elsif status == "success"
      puts " success"
      break
    else
      print "."
    end
  end

  70.times do print "-" end
  puts
end

実行方法

$ ruby each_daily.rb 2016-02-01 2016-02-02 2016-03-01 2016-03-02
$ ruby each_daily.rb -f 2016-03-01 2016-03-02
3
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
3
2