func (c Tasks) TaskOperate(id string, task models.Task) revel.Result { group := models.SelectTaskGroupListByUserId(c.SessionGetUser().UserId) if c.IsGet() { if id != "" { if task = models.SelectTaskOneById(id); task.TaskId == "" { return c.ErrDataBase(MsgSeleteError) } return c.Render(task, group) } return c.Render(group) } else { if task.TaskId != "" { task.ValidateUpdate(c.Validation) if c.Validation.HasErrors() { return c.Render(task, group) } if !models.UpdateTaskOne(&task) { return c.ErrDataBase(MsgUpdateError) } } else { task.UserId = c.SessionGetUser().UserId task.ValidateInsert(c.Validation) if c.Validation.HasErrors() { return c.Render(task, group) } if err := models.DBMap().Insert(&task); err != nil { return c.ErrDataBase(MsgInsertError) } } return c.Redirect(routes.Tasks.TaskList(task.GroupId)) } }
func (j Job) Run() { //revel.INFO.Panicln("执行---------") revel.INFO.Println("---------run----------") defer func() { if err := recover(); err != nil { revel.ERROR.Println(err, "\n", string(debug.Stack())) } }() if j.Concurrent { j.running.Lock() defer j.running.Unlock() } if workPool != nil { workPool <- true defer func() { <-workPool }() } j.status = 1 defer func() { j.status = 0 }() //_, _, err := j.runFunc() bout, berr, err := j.runFunc() if err != nil { revel.ERROR.Println("job.runFunc", err) } t := time.Now() ut := time.Now().Sub(t) / time.Millisecond // 插入日志 log := new(models.TaskLog) log.UserId = j.task.UserId log.TaskId = j.id log.TaskName = j.task.TaskName log.Output = string(bout) log.Error = string(berr) log.ProcessTime = int(ut) if err != nil { log.Status = 0 log.Error = err.Error() + ":" + string(berr) } else { log.Status = 1 } j.SendNotifyEmail(err) models.InsertTaskLogOne(log) // 更新上次执行时间 // j.task.PrevTime = t.Unix() j.task.ExecuteTimes++ models.UpdateTaskOne(j.task) }
// 关闭任务 func (c Tasks) TaskStop(id string) revel.Result { task := models.SelectTaskOneById(id) if task.TaskId == "" { return c.ErrDataBase(MsgSeleteError) } if jobs.RemoveJob(task.TaskId) { revel.INFO.Println("关闭job成功") task.Status = 0 models.UpdateTaskOne(&task) } return c.Redirect(routes.Tasks.TaskList("")) }
// 启动任务 func (c Tasks) TaskStart(id string) revel.Result { task := models.SelectTaskOneById(id) if task.TaskId == "" { return c.ErrDataBase(MsgSeleteError) } job, err := jobs.NewJobFromTask(&task) if err != nil { return c.ErrInputData("加入任务队列失败") } if jobs.AddJob(task.CronSpec, job) { revel.INFO.Println("加入job成功") task.Status = 1 models.UpdateTaskOne(&task) } return c.Redirect(routes.Tasks.TaskList("")) }