forked from hybrik/hybrik-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
107 lines (90 loc) · 2.46 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
96
97
98
99
100
101
102
103
104
105
106
107
package hybrik
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
)
var (
// ErrGopSizeNan is an error returned when the GopSize field of db.Preset is not a valid number
ErrGopSizeNan = fmt.Errorf("bitrate non a number")
)
// ErrNotDeleted is the error returned when a job is not successfully deleted from Hybrik
type ErrNotDeleted struct {
JobID string
}
func (e ErrNotDeleted) Error() string {
return fmt.Sprintf("Job of ID %s not deleted", e.JobID)
}
type transcodeErrResult struct {
Success bool `json:"success"`
Msg string `json:"message"`
Error string `json:"error"`
}
// JobInfo is the GetJobInfo response containing basic details about a job
type JobInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Progress int `json:"progress"`
}
// QueueJob takes a hybrik job json and submits a new job
func (c *Client) QueueJob(jobJSON string) (string, error) {
resp, err := c.client.CallAPI("POST", "/jobs", nil, strings.NewReader(jobJSON))
if err != nil {
return "", err
}
var ter transcodeErrResult
err = json.Unmarshal([]byte(resp), &ter)
if err == nil {
if !ter.Success && ter.Error != "" {
return "", fmt.Errorf("%s", ter.Error)
}
}
var job struct {
ID string `json:"id"`
}
err = json.Unmarshal([]byte(resp), &job)
if err != nil {
return "", err
}
return job.ID, err
}
// StopJob takes a jobID and deletes is from Hybrik
func (c *Client) StopJob(jobID string) error {
resp, err := c.client.CallAPI("PUT", fmt.Sprintf("/jobs/%s/stop", jobID), nil, nil)
if err != nil {
return err
}
var job struct {
ID string `json:"id"`
}
err = json.Unmarshal([]byte(resp), &job)
if job.ID != jobID || err != nil {
return ErrNotDeleted{JobID: jobID}
}
return nil
}
// GetJobInfo takes a jobID and obtains basic status details about the corresponding job
func (c *Client) GetJobInfo(jobID string) (JobInfo, error) {
values := url.Values{}
values.Add("fields[]", "id")
values.Add("fields[]", "name")
values.Add("fields[]", "progress")
values.Add("fields[]", "status")
values.Add("fields[]", "start_time")
values.Add("fields[]", "end_time")
resp, err := c.client.CallAPI("GET", fmt.Sprintf("/jobs/%s/info", jobID), values, nil)
if err != nil {
return JobInfo{}, err
}
var ji JobInfo
err = json.Unmarshal([]byte(resp), &ji)
if err != nil {
return JobInfo{}, err
}
return ji, nil
}