mirror of https://github.com/shunfei/cronsun
models: 记录 job 执行结果
parent
3dcde33480
commit
eefba51edc
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"sunteng/commons/log"
|
||||
"sunteng/cronsun/conf"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -33,6 +34,9 @@ type Job struct {
|
|||
schedule string
|
||||
gid string
|
||||
build bool
|
||||
|
||||
// 执行任务的结点,用于记录 job log
|
||||
runOn string
|
||||
}
|
||||
|
||||
type JobRule struct {
|
||||
|
@ -152,6 +156,10 @@ func (j *Job) GetID() string {
|
|||
return j.ID
|
||||
}
|
||||
|
||||
func (j *Job) RunOn(n string) {
|
||||
j.runOn = n
|
||||
}
|
||||
|
||||
func (j *Job) String() string {
|
||||
data, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
|
@ -162,14 +170,14 @@ func (j *Job) String() string {
|
|||
|
||||
// Run 执行任务
|
||||
func (j *Job) Run() {
|
||||
cmd := strings.Split(j.Command, " ")
|
||||
cmd, t := strings.Split(j.Command, " "), time.Now()
|
||||
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
|
||||
if err != nil {
|
||||
j.fail(err)
|
||||
j.fail(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
j.success(out)
|
||||
j.success(t, out)
|
||||
}
|
||||
|
||||
func JobKey(group, id string) string {
|
||||
|
@ -209,10 +217,10 @@ func (j *Job) Check() error {
|
|||
}
|
||||
|
||||
// 执行结果写入 mongoDB
|
||||
func (j *Job) success(out []byte) {
|
||||
|
||||
func (j *Job) success(t time.Time, out []byte) {
|
||||
CreateJobLog(j, t, string(out), true)
|
||||
}
|
||||
|
||||
func (j *Job) fail(err error) {
|
||||
|
||||
func (j *Job) fail(t time.Time, err error) {
|
||||
CreateJobLog(j, t, err.Error(), false)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,12 @@ import (
|
|||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
|
||||
"sunteng/commons/log"
|
||||
)
|
||||
|
||||
const (
|
||||
Coll_JobLog = "job_log"
|
||||
)
|
||||
|
||||
// 任务执行记录
|
||||
|
@ -15,7 +21,7 @@ type JobLog struct {
|
|||
Node string `bson:"node" json:"node"` // 运行此次任务的节点 ip,索引
|
||||
Command string `bson:"command" json:"command,omitempty"` // 执行的命令,包括参数
|
||||
Output string `bson:"output" json:"output,omitempty"` // 任务输出的所有内容
|
||||
ExitCode uint8 `bson:"exitCode" json:"exitCode"` // 脚本退出状态码
|
||||
Success bool `bson:"success" json:"success"` // 是否执行成功
|
||||
BeginTime time.Time `bson:"beginTime" json:"beginTime"` // 任务开始执行时间,精确到毫秒,索引
|
||||
EndTime time.Time `bson:"endTime" json:"endTime"` // 任务执行完毕时间,精确到毫秒
|
||||
}
|
||||
|
@ -27,3 +33,25 @@ func GetJobLogById(id bson.ObjectId) (*JobLog, error) {
|
|||
func GetJobLogList(query interface{}, skip, limit int) (list []*JobLog, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func CreateJobLog(j *Job, t time.Time, rs string, success bool) {
|
||||
jl := JobLog{
|
||||
Id: bson.NewObjectId(),
|
||||
JobId: j.GetID(),
|
||||
|
||||
JobGroup: j.Group,
|
||||
Name: j.Name,
|
||||
|
||||
Node: j.runOn,
|
||||
|
||||
Command: j.Command,
|
||||
Output: rs,
|
||||
Success: success,
|
||||
|
||||
BeginTime: t,
|
||||
EndTime: time.Now(),
|
||||
}
|
||||
if err := mgoDB.Insert(Coll_JobLog, jl); err != nil {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -151,6 +151,7 @@ func (n *Node) addJob(job *models.Job) bool {
|
|||
}
|
||||
|
||||
n.addLink(gid, j.GetID())
|
||||
j.RunOn(n.Node.ID)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue