Module: TreasureData::API::Schedule

Included in:
TreasureData::API
Defined in:
lib/td/client/api/schedule.rb

Instance Method Summary collapse

Instance Method Details

#create_schedule(name, opts) ⇒ String

Parameters:

  • name (String)
  • opts (Hash)

Returns:

  • (String)


11
12
13
14
15
16
17
18
19
# File 'lib/td/client/api/schedule.rb', line 11

def create_schedule(name, opts)
  params = opts.update({:type=> opts[:type] || opts['type'] || 'hive'})
  code, body, res = post("/v3/schedule/create/#{e name}", params)
  if code != "200"
    raise_error("Create schedule failed", res)
  end
  js = checked_json(body)
  return js['start']
end

#delete_schedule(name) ⇒ Array

Parameters:

  • name (String)

Returns:

  • (Array)


23
24
25
26
27
28
29
30
# File 'lib/td/client/api/schedule.rb', line 23

def delete_schedule(name)
  code, body, res = post("/v3/schedule/delete/#{e name}")
  if code != "200"
    raise_error("Delete schedule failed", res)
  end
  js = checked_json(body, %w[])
  return js['cron'], js["query"]
end

#history(name, from = 0, to = nil) ⇒ Array

Parameters:

  • name (String)
  • from (Fixnum) (defaults to: 0)
  • to (Fixnum) (defaults to: nil)

    (to is exclusive)

Returns:

  • (Array)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/td/client/api/schedule.rb', line 71

def history(name, from=0, to=nil)
  params = {}
  params['from'] = from.to_s if from
  params['to'] = to.to_s if to
  code, body, res = get("/v3/schedule/history/#{e name}", params)
  if code != "200"
    raise_error("List history failed", res)
  end
  js = checked_json(body, %w[history])
  result = []
  js['history'].each {|m|
    job_id = m['job_id']
    type = (m['type'] || '?').to_sym
    database = m['database']
    status = m['status']
    query = m['query']
    start_at = m['start_at']
    end_at = m['end_at']
    scheduled_at = m['scheduled_at']
    result_url = m['result']
    priority = m['priority']
    result << [scheduled_at, job_id, type, status, query, start_at, end_at, result_url, priority, database]
  }
  return result
end

#list_schedulesArray

Returns:

  • (Array)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/td/client/api/schedule.rb', line 33

def list_schedules
  code, body, res = get("/v3/schedule/list")
  if code != "200"
    raise_error("List schedules failed", res)
  end
  js = checked_json(body, %w[schedules])
  result = []
  js['schedules'].each {|m|
    name = m['name']
    cron = m['cron']
    query = m['query']
    database = m['database']
    result_url = m['result']
    timezone = m['timezone']
    delay = m['delay']
    next_time = m['next_time']
    priority = m['priority']
    retry_limit = m['retry_limit']
    result << [name, cron, query, database, result_url, timezone, delay, next_time, priority, retry_limit, nil] # same as database
  }
  return result
end

#run_schedule(name, time, num) ⇒ Array

Parameters:

  • name (String)
  • time (String)
  • num (Fixnum)

Returns:

  • (Array)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/td/client/api/schedule.rb', line 101

def run_schedule(name, time, num)
  params = {}
  params = {'num' => num} if num
  code, body, res = post("/v3/schedule/run/#{e name}/#{e time}", params)
  if code != "200"
    raise_error("Run schedule failed", res)
  end
  js = checked_json(body, %w[jobs])
  result = []
  js['jobs'].each {|m|
    job_id = m['job_id']
    scheduled_at = m['scheduled_at']
    type = (m['type'] || '?').to_sym
    result << [job_id, type, scheduled_at]
  }
  return result
end

#update_schedule(name, params) ⇒ nil

Parameters:

  • name (String)
  • params (Hash)

Returns:

  • (nil)


59
60
61
62
63
64
65
# File 'lib/td/client/api/schedule.rb', line 59

def update_schedule(name, params)
  code, body, res = post("/v3/schedule/update/#{e name}", params)
  if code != "200"
    raise_error("Update schedule failed", res)
  end
  return nil
end