node: 添加任务解释出错时记录执行失败日志

pull/1/head
miraclesu 2017-02-16 10:59:25 +08:00
parent 55bf3ed394
commit 63f7b2ba52
2 changed files with 8 additions and 6 deletions

View File

@ -173,11 +173,11 @@ func (j *Job) Run() {
cmd, t := strings.Split(j.Command, " "), time.Now() cmd, t := strings.Split(j.Command, " "), time.Now()
out, err := exec.Command(cmd[0], cmd[1:]...).Output() out, err := exec.Command(cmd[0], cmd[1:]...).Output()
if err != nil { if err != nil {
j.fail(t, err) j.Fail(t, err)
return return
} }
j.success(t, out) j.Success(t, out)
} }
func JobKey(group, id string) string { func JobKey(group, id string) string {
@ -217,10 +217,10 @@ func (j *Job) Check() error {
} }
// 执行结果写入 mongoDB // 执行结果写入 mongoDB
func (j *Job) success(t time.Time, out []byte) { func (j *Job) Success(t time.Time, out []byte) {
CreateJobLog(j, t, string(out), true) CreateJobLog(j, t, string(out), true)
} }
func (j *Job) fail(t time.Time, err error) { func (j *Job) Fail(t time.Time, err error) {
CreateJobLog(j, t, err.Error(), false) CreateJobLog(j, t, err.Error(), false)
} }

View File

@ -144,14 +144,16 @@ func (n *Node) addJob(job *models.Job) bool {
n.jobs[j.GetID()] = j n.jobs[j.GetID()] = j
} }
j.RunOn(n.Node.ID)
if err := n.Cron.AddJob(sch, j); err != nil { if err := n.Cron.AddJob(sch, j); err != nil {
log.Warnf("job[%s] timer[%s] parse err: %s", j.GetID(), sch) err = fmt.Errorf("job[%s] timer[%s] parse err: %s", j.GetID(), sch, err.Error())
log.Warn(err.Error())
j.Fail(time.Now(), err)
delete(n.jobs, j.GetID()) delete(n.jobs, j.GetID())
return false return false
} }
n.addLink(gid, j.GetID()) n.addLink(gid, j.GetID())
j.RunOn(n.Node.ID)
return true return true
} }