-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
117 lines (103 loc) · 2.7 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
108
109
110
111
112
113
114
115
116
117
package dksync
import (
"fmt"
"github.com/gemalto/requester"
log "github.com/sirupsen/logrus"
"io/ioutil"
)
type (
job struct {
Name string `json:"name"`
DisplayName string `json:"displayname"`
Timezone string `json:"timezone"`
Schedule string `json:"schedule"`
Owner string `json:"owner"`
OwnerEmail string `json:"owner_email"`
Disabled bool `json:"disabled"`
Tags map[string]string `json:"tags"`
Metadata interface{} `json:"metadata"`
Processors map[string]map[string]string `json:"processors"`
Retries int `json:"retries"`
Concurrency string `json:"concurrency"`
Executor string `json:"executor"`
ExecutorConfig struct {
Command string `json:"command"`
Cwd string `json:"cwd,omitempty"`
Env string `json:"env,omitempty"`
Shell string `json:"shell"`
} `json:"executor_config"`
}
)
// create
func (job *job) create() {
resp, err := requester.Send(
requester.JSON(false),
requester.Body(job),
requester.Post(dKronHost),
requester.ExpectCode(201),
requester.AddHeader("Accept", "application/json"),
requester.AddHeader("Content-Type", "application/json"),
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Debugf("New Job: %v", resp)
log.Infoln(resp.Status)
return
}
// update
func (job *job) update() {
resp, err := requester.Send(
requester.Post(dKronHost),
requester.Body(&job),
requester.ExpectCode(201),
requester.AddHeader("Accept", "application/json"),
requester.AddHeader("Content-Type", "application/json"),
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Debugf("Updated Job: %s, Name: %s", resp.Status, job.Name)
return
}
// checkJob check if job already exists
func (job *job) checkJob() int {
response, err := requester.Send(
requester.Get(fmt.Sprintf("%s%s", dKronHost, job.Name)))
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
log.Debugln(response)
return response.StatusCode
}
// getJobs
func getJobs() []byte {
resp, err := requester.Send(
requester.Get(dKronHost),
requester.ExpectCode(200),
requester.AddHeader("Accept", "application/json"),
)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Debugf("Get Jobs: %v", resp)
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return result
}
// checkHost
func checkHost() {
if dKronHost == "" {
dKronHost = defaultHost
}
}
// setDebug
func setDebug() {
log.SetLevel(log.DebugLevel)
}