forked from goodrain/rainbond
/
job_record.go
132 lines (116 loc) · 4.25 KB
/
job_record.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package job
import (
"time"
"github.com/pquerna/ffjson/ffjson"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/core/store"
"github.com/twinj/uuid"
)
//ExecutionRecord 任务执行记录
type ExecutionRecord struct {
ID string `json:"id"`
JobID string `json:"job_id"` // 任务 Id,索引
TaskID string `json:"task_id"`
User string `json:"user"` // 执行此次任务的用户
Name string `json:"name"` // 任务名称
Node string `json:"node"` // 运行此次任务的节点 ip,索引
Command string `json:"command,omitempty"` // 执行的命令,包括参数
Output string `json:"output"` // 任务输出的所有内容
Success bool `json:"success"` // 是否执行成功
BeginTime time.Time `json:"beginTime"` // 任务开始执行时间,精确到毫秒,索引
EndTime time.Time `json:"endTime"` // 任务执行完毕时间,精确到毫秒
IsHandle bool `json:"is_handle"` //是否已经处理
HandleTime time.Time `json:"handle_time"` //处理时间
}
func (e ExecutionRecord) String() string {
body, _ := ffjson.Marshal(e)
return string(body)
}
//GetExecutionRecordByID 获取执行记录
func GetExecutionRecordByID(id string) (l *ExecutionRecord, err error) {
return nil, nil
}
//GetJobExecutionRecords 获取某个任务执行记录
func GetJobExecutionRecords(JobID string) ([]*ExecutionRecord, error) {
return nil, nil
}
//IsHandleRight 是否具有处理结果权限
func (e ExecutionRecord) IsHandleRight() bool {
if e.IsHandle {
return false
}
resp, err := store.DefalutClient.Grant(5)
if err != nil {
logrus.Infof("execution record[%s] didn't get a lock, err: %s", e.ID, err.Error())
return false
}
ok, err := store.DefalutClient.GetLock(e.ID, resp.ID)
if err != nil {
logrus.Infof("execution record[%s] didn't get a lock, err: %s", e.ID, err.Error())
return false
}
return ok
}
//CompleteHandle 完成处理记录
//master节点处理完成后调用
func (e ExecutionRecord) CompleteHandle() {
e.HandleTime = time.Now()
e.IsHandle = true
_, err := store.DefalutClient.Put(option.Config.ExecutionRecordPath+"/"+e.JobID+"/"+e.ID, e.String())
if err != nil {
logrus.Error("put exec record to etcd in complete handle error.", err.Error())
}
}
//ParseExecutionRecord 解析
func ParseExecutionRecord(body []byte) (e ExecutionRecord) {
ffjson.Unmarshal(body, &e)
return
}
//CreateExecutionRecord 创建存储记录
func CreateExecutionRecord(j *Job, t time.Time, rs string, success bool) {
//存储执行记录,master端获取结果,并处理结果
//例如 检测任务,安装任务等
record := ExecutionRecord{
ID: uuid.NewV4().String(),
JobID: j.ID,
TaskID: j.TaskID,
User: j.User,
Name: j.Name,
Node: j.NodeID,
Command: j.Command,
Output: rs,
Success: success,
BeginTime: t,
EndTime: time.Now(),
}
_, err := store.DefalutClient.Put(option.Config.ExecutionRecordPath+"/"+record.JobID+"/"+record.ID, record.String())
if err != nil {
logrus.Error("put exec record to etcd error.", err.Error())
}
status := "Success"
if !success {
status = "Failure"
}
j.RunStatus = &RunStatus{
Status: status,
StartTime: t,
EndTime: time.Now(),
RecordID: record.ID,
}
PutJob(j)
}