module TreasureData module Command def sched_list(op) op.cmd_parse client = get_client scheds = client.schedules rows = [] scheds.each {|sched| rows << {:Name => sched.name, :Cron => sched.cron, :Result => sched.rset_name, :Query => sched.query} } rows = rows.sort_by {|map| map[:Name] } puts cmd_render_table(rows, :fields => [:Name, :Cron, :Result, :Query]) end def sched_create(op) db_name = nil result = nil op.on('-d', '--database DB_NAME', 'use the database (required)') {|s| db_name = s } op.on('-r', '--result RESULT_TABLE', 'write result to the result table (use result:create command)') {|s| result = s } name, cron, sql = op.cmd_parse unless db_name $stderr.puts "-d, --database DB_NAME option is required." exit 1 end client = get_client # local existance check get_database(client, db_name) begin first_time = client.create_schedule(name, :cron=>cron, :query=>sql, :database=>db_name, :result=>result) rescue AlreadyExistsError cmd_debug_error $! $stderr.puts "Schedule '#{name}' already exists." exit 1 end $stderr.puts "Schedule '#{name}' is created. It starts at #{first_time}." end def sched_delete(op) name = op.cmd_parse client = get_client begin client.delete_schedule(name) rescue NotFoundError cmd_debug_error $! $stderr.puts "Schedule '#{name}' does not exist." $stderr.puts "Use '#{$prog} sched:list' to show list of the schedules." exit 1 end $stderr.puts "Schedule '#{name}' is deleted." end def sched_history(op) page = 0 skip = 0 op.on('-p', '--page PAGE', 'skip N pages', Integer) {|i| page = i } op.on('-s', '--skip N', 'skip N schedules', Integer) {|i| skip = i } name, max = op.cmd_parse max = (max || 20).to_i if page skip += max * page end client = get_client begin history = client.history(name, skip, skip+max-1) rescue NotFoundError cmd_debug_error $! $stderr.puts "Schedule '#{name}' does not exist." $stderr.puts "Use '#{$prog} sched:list' to show list of the schedules." exit 1 end rows = [] history.each {|j| rows << {:Time => j.scheduled_at.localtime, :JobID => j.job_id, :Status => j.status, :Result=>j.rset_name} } puts cmd_render_table(rows, :fields => [:JobID, :Time, :Status, :Result]) end end end