-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
225 lines (189 loc) · 6.25 KB
/
engine.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
217
218
219
220
221
222
223
224
225
package engine
import (
"fmt"
"os"
jober "github.com/hamster-shared/aline-engine/job"
"github.com/hamster-shared/aline-engine/logger"
"github.com/hamster-shared/aline-engine/model"
"github.com/hamster-shared/aline-engine/output"
"github.com/hamster-shared/aline-engine/utils"
"github.com/sirupsen/logrus"
)
type Engine interface {
CreateJob(name string, yaml string) error
SaveJobParams(name string, params map[string]string) error
SaveJobUserId(name string, userId string) error
DeleteJob(name string) error
UpdateJob(name, newName, jobYaml string) error
GetJob(name string) (*model.Job, error)
GetJobs(keyword string, page, size int) (*model.JobPage, error)
GetCodeInfo(name string, historyId int) (*model.CodeInfo, error)
ExecuteJob(name string, id int) (*model.JobDetail, error)
ReExecuteJob(name string, id int) error
GetJobHistory(name string, id int) (*model.JobDetail, error)
GetJobHistorys(name string, page, size int) (*model.JobDetailPage, error)
DeleteJobHistory(name string, id int) error
CreateJobDetail(name string, id int) (*model.JobDetail, error)
ExecuteJobDetail(name string, id int) error
RegisterStatusChangeHook(hook func(message model.StatusChangeMessage))
GetJobHistoryLog(name string, id int) (*model.JobLog, error)
GetJobHistoryStageLog(name string, id int, stageName string, start int) (*model.JobStageLog, error)
GetJobHistoryStepLog(name string, id int, stageName string, stepName string) (*output.Step, error)
TerminalJob(name string, id int) error
GetCurrentJobStatus(jobName string, jobID int) (model.Status, error)
IsValidWorker(w string) bool
GetWorkRootPath() string
}
type Role int
const (
RoleMaster Role = iota
RoleWorker
)
type engine struct {
role Role
master *masterEngine
worker *workerEngine
}
func NewMasterEngine(listenPort int) (Engine, error) {
logger.Init().ToStdoutAndFile().SetLevel(readLogLevelFromEnv())
e := &engine{}
e.role = RoleMaster
var err error
e.master, err = newMasterEngine(fmt.Sprintf("0.0.0.0:%d", listenPort))
if err != nil {
return nil, err
}
e.worker, err = newWorkerEngine(fmt.Sprintf("127.0.0.1:%d", listenPort))
if err != nil {
return nil, err
}
return e, nil
}
func NewWorkerEngine(masterAddress string) (Engine, error) {
e := &engine{}
e.role = RoleWorker
var err error
e.worker, err = newWorkerEngine(masterAddress)
if err != nil {
return nil, err
}
return e, nil
}
func (e *engine) CreateJob(name string, yaml string) error {
return jober.SaveJob(name, yaml)
}
func (e *engine) SaveJobParams(name string, params map[string]string) error {
return jober.SaveJobParams(name, params)
}
func (e *engine) SaveJobUserId(name string, userId string) error {
return jober.SaveJobUserId(name, userId)
}
func (e *engine) DeleteJob(name string) error {
return jober.DeleteJob(name)
}
func (e *engine) UpdateJob(name, newName, jobYaml string) error {
return jober.UpdateJob(name, newName, jobYaml)
}
func (e *engine) GetJob(name string) (*model.Job, error) {
return jober.GetJobObject(name)
}
func (e *engine) GetJobs(keyword string, page, size int) (*model.JobPage, error) {
return jober.JobList(keyword, page, size)
}
func (e *engine) GetCodeInfo(name string, historyId int) (*model.CodeInfo, error) {
jobDetail, err := jober.GetJobDetail(name, historyId)
if err != nil {
return nil, err
}
return &jobDetail.CodeInfo, nil
}
func (e *engine) ExecuteJob(name string, id int) (*model.JobDetail, error) {
if e.role != RoleMaster {
return nil, fmt.Errorf("only master can execute job")
}
jobDetail, err := e.CreateJobDetail(name, id)
if err != nil {
return nil, err
}
return jobDetail, e.master.dispatchJob(name, jobDetail.Id)
}
func (e *engine) ExecuteJobDetail(name string, id int) error {
if e.role != RoleMaster {
return fmt.Errorf("only master can execute job detail")
}
return e.master.dispatchJob(name, id)
}
func (e *engine) ReExecuteJob(name string, id int) error {
if e.role != RoleMaster {
return fmt.Errorf("only master can execute job")
}
return e.master.dispatchJob(name, id)
}
func (e *engine) CancelJob(name string, id int) error {
if e.role != RoleMaster {
return fmt.Errorf("only master can cancel job")
}
return e.master.cancelJob(name, id)
}
func (e *engine) GetJobHistory(name string, id int) (*model.JobDetail, error) {
return jober.GetJobDetail(name, id)
}
func (e *engine) DeleteJobHistory(name string, id int) error {
return jober.DeleteJobDetail(name, id)
}
func (e *engine) CreateJobDetail(name string, id int) (*model.JobDetail, error) {
return jober.CreateJobDetail(name, id)
}
func (e *engine) RegisterStatusChangeHook(hook func(message model.StatusChangeMessage)) {
if e.role != RoleMaster {
return
}
logger.Infof("register status change hook")
e.master.registerStatusChangeHook(hook)
}
func (e *engine) GetJobHistorys(name string, page, size int) (*model.JobDetailPage, error) {
return jober.JobDetailList(name, page, size)
}
func (e *engine) GetJobHistoryLog(name string, id int) (*model.JobLog, error) {
return jober.GetJobLog(name, id)
}
func (e *engine) GetJobHistoryStageLog(name string, id int, stageName string, start int) (*model.JobStageLog, error) {
return jober.GetJobStageLog(name, id, stageName, start)
}
func (e *engine) TerminalJob(name string, id int) error {
if e.role != RoleMaster {
return fmt.Errorf("only master can terminal job")
}
return e.master.cancelJob(name, id)
}
func readLogLevelFromEnv() logrus.Level {
levelStr := os.Getenv("ALINE_LOG_LEVEL")
if levelStr == "" {
return logrus.InfoLevel
}
level, err := logrus.ParseLevel(levelStr)
if err != nil {
return logrus.InfoLevel
}
return level
}
// GetCurrentJobStatus 获取当前任务的状态,不能获取历史任务的状态
func (e *engine) GetCurrentJobStatus(jobName string, jobID int) (model.Status, error) {
if e.role == RoleWorker {
return e.worker.GetJobStatus(jobName, jobID)
}
return e.master.getJobStatus(jobName, jobID)
}
// 校验是不是有效的 worker
func (e *engine) IsValidWorker(w string) bool {
if e.role == RoleWorker {
return false
}
return e.master.isValidWorker(w)
}
func (e *engine) GetJobHistoryStepLog(name string, id int, stageName string, stepName string) (*output.Step, error) {
return jober.GetJobStepLog(name, id, stageName, stepName)
}
func (e *engine) GetWorkRootPath() string {
return utils.DefaultConfigDir()
}