forked from sql-machine-learning/gomaxcompute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
95 lines (80 loc) · 2.38 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
package gomaxcompute
import (
"encoding/xml"
"strconv"
)
// musts: Priority, Tasks
type odpsJob struct {
xml.Marshaler
Name string `xml:"Name"`
Comment string `xml:"Comment"`
Priority int `xml:"Priority"`
Tasks map[string]odpsTask `xml:"Tasks"`
}
func newJob(tasks ...odpsTask) *odpsJob {
taskMap := make(map[string]odpsTask, len(tasks))
for _, t := range tasks {
taskMap[t.GetName()] = t
}
return &odpsJob{
Priority: 1,
Tasks: taskMap,
}
}
func newSQLJob(sql string) *odpsJob {
return newJob(newAnonymousSQLTask(sql, nil))
}
func (j *odpsJob) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Instance"}})
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Job"}})
// optional job name
if j.Name != "" {
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Comment"}})
e.EncodeToken(xml.CharData(j.Name))
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Comment"}})
}
// optional job comment
if j.Comment != "" {
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Comment"}})
e.EncodeToken(xml.CharData(j.Comment))
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Comment"}})
}
// Job.Priority
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Priority"}})
e.EncodeToken(xml.CharData(strconv.Itoa(j.Priority)))
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Priority"}})
// Job.Tasks
e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "Tasks"}})
for _, task := range j.Tasks {
if err = e.EncodeElement(task, xml.StartElement{Name: xml.Name{Local: "Task"}}); err != nil {
return
}
}
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Tasks"}})
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Job"}})
e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "Instance"}})
return e.Flush()
}
func (j *odpsJob) SetName(name string) {
j.Name = name
}
func (j *odpsJob) SetComment(comment string) {
j.Comment = comment
}
func (j *odpsJob) SetPriority(priority int) {
j.Priority = priority
}
func (j *odpsJob) SetTasks(ts ...odpsTask) {
taskMap := make(map[string]odpsTask, len(ts))
for _, t := range ts {
taskMap[t.GetName()] = t
}
j.Tasks = taskMap
}
func (j *odpsJob) AddTask(t odpsTask) {
j.Tasks[t.GetName()] = t
}
func (j *odpsJob) XML() []byte {
body, _ := xml.MarshalIndent(j, " ", " ")
return body
}