Skip to content

Commit 0e95f0c

Browse files
author
wb-cjh663673
committed
add scheduled sql job instances api
1 parent 7b3f372 commit 0e95f0c

4 files changed

+306
-91
lines changed

client_interface.go

+3
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,7 @@ type ClientInterface interface {
263263
UpdateScheduledSQL(project string, scheduledsql *ScheduledSQL) error
264264
GetScheduledSQL(project string, name string) (*ScheduledSQL, error)
265265
ListScheduledSQL(project, name, displayName string, offset, size int) ([]*ScheduledSQL, int, int, error)
266+
GetScheduledSQLJobInstance(projectName, jobName, instanceId string, result bool) (instance *ScheduledSQLJobInstance, err error)
267+
ModifyScheduledSQLJobInstanceState(projectName, jobName, instanceId string, state ScheduledSQLState) error
268+
ListScheduledSQLJobInstances(projectName, jobName string, status *InstanceStatus) (instances []*ScheduledSQLJobInstance, total, count int64, err error)
266269
}

client_scheduled_sql.go

+103
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sls
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/url"
@@ -12,6 +13,7 @@ type ResourcePool string
1213
type DataFormat string
1314
type JobType string
1415
type Status string
16+
type ScheduledSQLState string
1517

1618
const (
1719
STANDARD SqlType = "standard"
@@ -42,6 +44,12 @@ const (
4244
DISABLED Status = "Disabled"
4345
)
4446

47+
const (
48+
ScheduledSQL_RUNNING ScheduledSQLState = "RUNNING"
49+
ScheduledSQL_FAILED ScheduledSQLState = "FAILED"
50+
ScheduledSQL_SUCCEEDED ScheduledSQLState = "SUCCEEDED"
51+
)
52+
4553
type ScheduledSQL struct {
4654
Name string `json:"name"`
4755
DisplayName string `json:"displayName"`
@@ -206,3 +214,98 @@ func (c *Client) ListScheduledSQL(project, name, displayName string, offset, siz
206214
}
207215
return scheduledSqlList.Results, scheduledSqlList.Total, scheduledSqlList.Count, err
208216
}
217+
218+
type ScheduledSQLJobInstance struct {
219+
InstanceId string `json:"instanceId"`
220+
JobName string `json:"jobName,omitempty"`
221+
DisplayName string `json:"displayName,omitempty"`
222+
Description string `json:"description,omitempty"`
223+
JobScheduleId string `json:"jobScheduleId,omitempty"`
224+
CreateTimeInMillis int64 `json:"createTimeInMillis"`
225+
ScheduleTimeInMillis int64 `json:"scheduleTimeInMillis"`
226+
UpdateTimeInMillis int64 `json:"updateTimeInMillis"`
227+
State ScheduledSQLState `json:"state"`
228+
ErrorCode string `json:"errorCode"`
229+
ErrorMessage string `json:"errorMessage"`
230+
Summary string `json:"summary,omitempty"`
231+
}
232+
233+
func (c *Client) GetScheduledSQLJobInstance(projectName, jobName, instanceId string, result bool) (*ScheduledSQLJobInstance, error) {
234+
h := map[string]string{
235+
"x-log-bodyrawsize": "0",
236+
"Content-Type": "application/json",
237+
}
238+
uri := fmt.Sprintf("/jobs/%s/jobinstances/%s?result=%t", jobName, instanceId, result)
239+
r, err := c.request(projectName, "GET", uri, h, nil)
240+
if err != nil {
241+
return nil, err
242+
}
243+
defer r.Body.Close()
244+
buf, _ := ioutil.ReadAll(r.Body)
245+
instance := &ScheduledSQLJobInstance{}
246+
if err = json.Unmarshal(buf, instance); err != nil {
247+
err = NewClientError(err)
248+
}
249+
return instance, err
250+
}
251+
252+
func (c *Client) ModifyScheduledSQLJobInstanceState(projectName, jobName, instanceId string, state ScheduledSQLState) error {
253+
if ScheduledSQL_RUNNING != state {
254+
return NewClientError(errors.New(fmt.Sprintf("Invalid state: %s, state must be RUNNING.", state)))
255+
}
256+
h := map[string]string{
257+
"x-log-bodyrawsize": "0",
258+
"Content-Type": "application/json",
259+
}
260+
261+
uri := fmt.Sprintf("/jobs/%s/jobinstances/%s?state=%s", jobName, instanceId, state)
262+
r, err := c.request(projectName, "PUT", uri, h, nil)
263+
if err != nil {
264+
return err
265+
}
266+
r.Body.Close()
267+
return nil
268+
}
269+
270+
type InstanceStatus struct {
271+
FromTime int64
272+
ToTime int64
273+
Offset int64
274+
Size int64
275+
State ScheduledSQLState
276+
}
277+
278+
func (c *Client) ListScheduledSQLJobInstances(projectName, jobName string, status *InstanceStatus) (instances []*ScheduledSQLJobInstance, total, count int64, err error) {
279+
h := map[string]string{
280+
"x-log-bodyrawsize": "0",
281+
"Content-Type": "application/json",
282+
}
283+
v := url.Values{}
284+
v.Add("jobType", "ScheduledSQL")
285+
v.Add("start", fmt.Sprintf("%d", status.FromTime))
286+
v.Add("end", fmt.Sprintf("%d", status.ToTime))
287+
v.Add("offset", fmt.Sprintf("%d", status.Offset))
288+
v.Add("size", fmt.Sprintf("%d", status.Size))
289+
if status.State != "" {
290+
v.Add("state", string(status.State))
291+
}
292+
293+
uri := fmt.Sprintf("/jobs/%s/jobinstances?%s", jobName, v.Encode())
294+
r, err := c.request(projectName, "GET", uri, h, nil)
295+
if err != nil {
296+
return nil, 0, 0, err
297+
}
298+
defer r.Body.Close()
299+
300+
type ScheduledSqlJobInstances struct {
301+
Total int64 `json:"total"`
302+
Count int64 `json:"count"`
303+
Results []*ScheduledSQLJobInstance `json:"results"`
304+
}
305+
buf, _ := ioutil.ReadAll(r.Body)
306+
jobInstances := &ScheduledSqlJobInstances{}
307+
if err = json.Unmarshal(buf, jobInstances); err != nil {
308+
err = NewClientError(err)
309+
}
310+
return jobInstances.Results, jobInstances.Total, jobInstances.Count, err
311+
}

0 commit comments

Comments
 (0)