Skip to content

Instantly share code, notes, and snippets.

@itochu0523
Last active September 24, 2015 10:02
Show Gist options
  • Save itochu0523/e47123eea1ffb0c3ac0f to your computer and use it in GitHub Desktop.
Save itochu0523/e47123eea1ffb0c3ac0f to your computer and use it in GitHub Desktop.
td-job.rb
require 'td'
require 'td-client'
require 'time'
require 'date'
# auth
auth_key = File.read("client.cfg", :encoding => Encoding::UTF_8)
cln = TreasureData::Client.new(auth_key)
# argv
date = ARGV[0]
if date.nil?
dt = Date.today
date = dt.to_s
ts = Time.parse(date).to_i - 86400*1
old = Time.parse(date).to_i - 86400*8
else
if date =~ /^([1-2][0-9][0-9][0-9])-?([0][1-9]|[1][0-2])-?([0-3][0-9])$/
dt = date
ts = Time.parse(date).to_i
old = Time.parse(date).to_i - 86400*7
#puts "fixed date:#{date}" # debug
else
puts "usage: invalid time format (yyyy-mm-dd or yyyymmdd)"
exit
end
end
# create database
t = Time.at(ts).strftime("%Y%m%d")
dest_db = "backup_" + t
# delete database
old_date = Time.at(old).strftime("%Y%m%d")
del_db = "backup_" + old_date
begin
cln.create_database(dest_db)
rescue
cln.delete_database(dest_db)
cln.create_database(dest_db)
end
list = "./table.list"
@table = File.open(list).readlines
# logging
def logging(id,stats,dest_table)
t = Time.now
str = t.strftime("[%Y-%m-%d %H:%M:%S]")
puts "#{str} job_id:#{id} status:#{stats} import:#{dest_table}"
end
# job
def exec_job(cln,db,dest_table,date)
# build query
q = "select * from " + dest_table
dest_url = "td://@/" + db + "/" + dest_table
job = cln.query("sanple_db", q, result_url=dest_url )
id = job.job_id
stats = job.status
logging(id,stats,dest_table)
until job.finished?
sleep 2
job.update_progress!
end
job.update_status!
stats = job.status
logging(id,stats,dest_table)
end
for t in @table do
table = t.chomp!
cln.create_log_table(dest_db,table)
exec_job(cln,dest_db,table,date)
end
# delete database
begin
cln.delete_database(del_db)
rescue
puts "not found database : #{del_db}"
end
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment