-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
216 lines (178 loc) · 5.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package model
import (
"fmt"
"io"
"os"
"path"
"strconv"
"time"
"github.com/hamster-shared/aline-engine/output"
"github.com/hamster-shared/aline-engine/utils"
)
type Status int
const (
STATUS_NOTRUN Status = 0
STATUS_RUNNING Status = 1
STATUS_FAIL Status = 2
STATUS_SUCCESS Status = 3
STATUS_STOP Status = 4
)
func (s Status) ToString() string {
list := [...]string{
"not run",
"running",
"fail",
"success",
"stop",
}
return list[s]
}
type Job struct {
Version string `yaml:"version,omitempty" json:"version"`
Name string `yaml:"name,omitempty" json:"name"`
Stages map[string]Stage `yaml:"stages,omitempty" json:"stages"`
Parameter map[string]string `yaml:"parameter,omitempty" json:"parameter"`
UserId string `yaml:"user_id"`
}
type JobVo struct {
Version string `yaml:"version" json:"version"`
Name string `yaml:"name" json:"name"`
Stages map[string]Stage `yaml:"stages" json:"stages"`
Status Status `json:"status"`
StartTime time.Time `yaml:"startTime" json:"startTime"`
Duration int64 `json:"duration"`
TriggerMode string `yaml:"triggerMode" json:"triggerMode"`
PipelineDetailId int `json:"pipelineDetailId"`
Error string `json:"error"`
CreateTime time.Time `json:"createTime"`
}
type JobDetail struct {
Id int `json:"id"`
Job
Status Status `json:"status"`
TriggerMode string `yaml:"triggerMode" json:"triggerMode"`
Stages []StageDetail `json:"stages"`
StartTime time.Time `yaml:"startTime" json:"startTime"`
Duration int64 `json:"duration"`
ActionResult `yaml:"actionResult" json:"actionResult"`
Output *output.Output `json:"output"`
Error string `yaml:"error,omitempty" json:"error"`
}
func (jd *JobDetail) ToString() string {
str := ""
for _, s := range jd.Stages {
str += s.ToString() + "\n"
}
return fmt.Sprintf("job: %s, Status: %d, StartTime: %s , Duration: %d, stages: [\n%s]", jd.Name, jd.Status, jd.StartTime, jd.Duration, str)
}
// StageSort job 排序
func (job *Job) StageSort() ([]StageDetail, error) {
stages := make(map[string]Stage)
for key, stage := range job.Stages {
stages[key] = stage
}
sortedMap := make(map[string]any)
stageList := make([]StageDetail, 0)
for len(stages) > 0 {
last := len(stages)
for key, stage := range stages {
allContains := true
for _, needs := range stage.Needs {
_, ok := sortedMap[needs]
if !ok {
allContains = false
}
}
if allContains {
sortedMap[key] = ""
delete(stages, key)
stageList = append(stageList, NewStageDetail(key, stage))
}
}
if len(stages) == last {
return nil, fmt.Errorf("cannot resolve dependency, %v", stages)
}
}
return stageList, nil
}
func (jd *JobDetail) AddArtifactory(file *os.File) error {
arti := Artifactory{
Name: file.Name(),
Url: fmt.Sprintf("/artifactory/%s/%d/%s", jd.Name, jd.Id, file.Name()),
}
dir := path.Join(utils.DefaultConfigDir(), "artifactory", jd.Name, strconv.Itoa(jd.Id))
_ = os.MkdirAll(dir, os.ModePerm)
fullPath := path.Join(dir, file.Name())
destination, err := os.Create(fullPath)
if err != nil {
return err
}
_, err = io.Copy(destination, file)
if err != nil {
return err
}
if len(jd.Artifactorys) > 0 {
jd.Artifactorys = append(jd.Artifactorys, arti)
} else {
jd.Artifactorys = make([]Artifactory, 0)
jd.Artifactorys = append(jd.Artifactorys, arti)
}
return nil
}
type JobLog struct {
// 开始时间
StartTime time.Time `json:"startTime"`
// 持续时间
Duration time.Duration `json:"duration"`
// 日志内容
Content string `json:"content"`
// 最后一行 行号
LastLine int `json:"lastLine"`
}
type JobStageLog struct {
// 开始时间
StartTime time.Time `json:"startTime"`
// 持续时间
Duration time.Duration `json:"duration"`
// 日志内容
Content string `json:"content"`
// 最后一行 行号
LastLine int `json:"lastLine"`
// 是否结束
End bool `yaml:"end" json:"end"`
}
type JobPage struct {
Data []JobVo `json:"data"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
type JobDetailPage struct {
Data []JobDetail `json:"data"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
type JobDetailDecrement []JobDetail
func (s JobDetailDecrement) Len() int { return len(s) }
func (s JobDetailDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s JobDetailDecrement) Less(i, j int) bool { return s[i].Id > s[j].Id }
type JobVoTimeDecrement []JobVo
func (s JobVoTimeDecrement) Len() int { return len(s) }
func (s JobVoTimeDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s JobVoTimeDecrement) Less(i, j int) bool { return s[i].CreateTime.After(s[j].CreateTime) }
func IntToStatus(s int) (Status, error) {
switch s {
case 0:
return STATUS_NOTRUN, nil
case 1:
return STATUS_RUNNING, nil
case 2:
return STATUS_FAIL, nil
case 3:
return STATUS_SUCCESS, nil
case 4:
return STATUS_STOP, nil
}
return STATUS_NOTRUN, fmt.Errorf("unknown status: %d", s)
}