Digdag 0.9.20まで動作確認済
period_daily.rb
require 'date'
require 'open3'
STDOUT.sync = true
# 第一引数に-fを指定すると同一日付のセッションが既にあった場合にリトライする
force_retry = false
if ARGV[0] == "-f"
force_retry = true
puts "同一日付のセッションが既にある場合にはリトライします。"
ARGV.shift
end
# 指定日Fromから指定日Toまでの各日について日次処理を実行
# (指定なければ「本日」を指定したものとみなす)
fromdate = ARGV[0].nil? ? Date.today : Date.parse(ARGV[0])
todate = ARGV[1].nil? ? Date.today : Date.parse(ARGV[1])
puts fromdate.to_s + "~" + todate.to_s + "の日次処理を実行します。(" + (fromdate - 1).to_s + "~" + (todate - 1).to_s + "のデータを処理します)"
# print "よろしいですか?[y/N] "
# if STDIN.gets.chomp != "y"
# exit
# end
puts
(fromdate..todate).each do |date|
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 period_daily.rb 2016-02-02 2017-03-04
$ ruby period_daily.rb -f 2016-03-01 2017-03-04