//updateTask获取客户端发送的Task信息,并调用Job的UpdateTask方法将其 //持久化并更新至Job中。 //成功返回更新后的Task信息 func UpdateTask(params martini.Params, r render.Render, Ss *schedule.ScheduleManager, task schedule.Task) { // {{{ var err error sid, sidok := params["sid"] ssid, _ := strconv.Atoi(sid) if !sidok || task.Name == "" || task.JobId == 0 { e := fmt.Sprintf("[UpdateTask] task name is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(ssid)); s != nil { j, err := s.GetJobById(task.JobId) if err != nil { e := fmt.Sprintf("[UpdateTask] get job error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } err = j.UpdateTask(&task) } if err == nil { r.JSON(200, task) } else { e := fmt.Sprintf("[UpdateTask] update task error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } } // }}}
func DeleteRelTask(params martini.Params, ctx *web.Context, r render.Render, Ss *schedule.ScheduleManager) { // {{{ sid, _ := strconv.Atoi(params["sid"]) jid, _ := strconv.Atoi(params["jid"]) id, _ := strconv.Atoi(params["id"]) relid, _ := strconv.Atoi(params["relid"]) if sid == 0 || jid == 0 || id == 0 || relid == 0 { e := fmt.Sprintf("[DeleteRelTask] [sid jid id relid] is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(sid)); s != nil { t := s.GetTaskById(int64(id)) if t == nil { e := fmt.Sprintf("[DeleteRelTask] task is required") g.L.Warningln(e) r.JSON(500, e) return } err := t.DeleteRelTask(int64(relid)) if err != nil { e := fmt.Sprintf("[DeleteRelTask] delete task is error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } r.JSON(200, t) } } // }}}
//addTask获取客户端发送的Task信息,调用Task的AddTask方法持久化。 //成功后根据其中的JobId找到对应Job将其添加 //成功返回添加好的Job信息 //错误返回err信息 func AddTask(params martini.Params, r render.Render, Ss *schedule.ScheduleManager, task schedule.Task) { // {{{ sid, sidok := params["sid"] ssid, _ := strconv.Atoi(sid) if !sidok || task.Name == "" || task.JobId == 0 { e := fmt.Sprintf("[AddTask] sid or Job name is required") g.L.Warningln(e) r.JSON(500, e) return } task.TaskType = 1 task.CreateUserId = 1 task.ModifyUserId = 1 task.CreateTime = time.Now() task.ModifyTime = time.Now() if s := Ss.GetScheduleById(int64(ssid)); s != nil { err := s.AddTask(&task) if err != nil { e := fmt.Sprintf("[AddTask] add task error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } } r.JSON(200, task) } // }}}
//addJob获取客户端发送的Job信息,并调用Schedule的AddJob方法将其 //持久化并添加至Schedule中。 //成功返回添加好的Job信息 //错误返回err信息 func AddJob(r render.Render, Ss *schedule.ScheduleManager, job schedule.Job) { // {{{ if job.Name == "" { e := fmt.Sprintf("[AddJob] Job name is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(job.ScheduleId)); s != nil { job.ScheduleCyc = s.Cyc job.CreateUserId = 1 job.ModifyUserId = 1 job.CreateTime = time.Now() job.ModifyTime = time.Now() if err := s.AddJob(&job); err != nil { e := fmt.Sprintf("[AddJob] add job error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } else { r.JSON(200, job) } } else { e := fmt.Sprintf("[AddJob] schedule not found.") g.L.Warningln(e) r.JSON(500, e) return } } // }}}
//调用Schedule的DeleteJob方法删除作业 func DeleteJob(params martini.Params, r render.Render, Ss *schedule.ScheduleManager) { // {{{ sid, sidok := params["sid"] id, idok := params["id"] if !sidok || !idok { e := fmt.Sprintf("[DeleteJob] sid or id not null.") g.L.Warningln(e) r.JSON(500, e) return } ssid, _ := strconv.Atoi(sid) iid, _ := strconv.Atoi(id) if s := Ss.GetScheduleById(int64(ssid)); s != nil { if err := s.DeleteJob(int64(iid)); err != nil { e := fmt.Sprintf("[DeleteJob] delete job error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } else { e := fmt.Sprintf("[DeleteJob] delete job success.") r.JSON(204, e) } } } // }}}
//updateSchedule获取客户端发送的Schedule信息,并调用Schedule的Update方法将其 //持久化并更新至Schedule中。 //成功返回更新后的Schedule信息 func UpdateSchedule(params martini.Params, r render.Render, Ss *schedule.ScheduleManager, scd schedule.Schedule) { // {{{ if scd.Name == "" { e := fmt.Sprintf("[UpdateSchedule] Schedule name is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(scd.Id)); s != nil { s.Name, s.Desc, s.Cyc, s.StartMonth = scd.Name, scd.Desc, scd.Cyc, scd.StartMonth s.StartSecond, s.ModifyTime, s.ModifyUserId = scd.StartSecond, time.Now(), scd.ModifyUserId if err := s.UpdateSchedule(); err != nil { e := fmt.Sprintf("[UpdateSchedule] update schedule error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } else { r.JSON(200, s) } } else { e := fmt.Sprintf("[UpdateSchedule] schedule not found.") g.L.Warningln(e) r.JSON(500, e) return } } // }}}
func DeleteSchedule(params martini.Params, ctx *web.Context, r render.Render, Ss *schedule.ScheduleManager) { // {{{ id, _ := strconv.Atoi(params["id"]) if id == 0 { e := fmt.Sprintf("[DeleteSchedule] id is required") g.L.Warningln(e) r.JSON(500, e) return } if err := Ss.DeleteSchedule(int64(id)); err != nil { e := fmt.Sprintf("[DeleteSchedule] delete schedule error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } r.JSON(200, nil) } // }}}
//添加Schedule func AddSchedule(params martini.Params, r render.Render, Ss *schedule.ScheduleManager, scd schedule.Schedule) { // {{{ if scd.Name == "" { e := fmt.Sprintf("[AddSchedule] Schedule name is required") g.L.Warningln(e) r.JSON(500, e) return } err := Ss.AddSchedule(&scd) if err != nil { e := fmt.Sprintf("[AddSchedule] add schedule error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } r.JSON(200, scd) return } // }}}
func GetJobsForSchedule(params martini.Params, r render.Render, res http.ResponseWriter, Ss *schedule.ScheduleManager) { // {{{ sid, sidok := params["sid"] if !sidok { e := fmt.Sprintf("[GetJobsForSchedule] sid is required") g.L.Warningln(e) r.JSON(500, e) return } ssid, _ := strconv.Atoi(sid) if s := Ss.GetScheduleById(int64(ssid)); s != nil { r.JSON(200, s.Jobs) } else { e := fmt.Sprintf("[GetJobsForSchedule] schedule not found.") g.L.Warningln(e) r.JSON(500, e) return } return } // }}}
//deleteTask从调度结构中删除指定的Task,并持久化。 func DeleteTask(params martini.Params, r render.Render, Ss *schedule.ScheduleManager) { // {{{ sid, _ := strconv.Atoi(params["sid"]) jid, _ := strconv.Atoi(params["jid"]) id, _ := strconv.Atoi(params["id"]) if sid == 0 || jid == 0 || id == 0 { e := fmt.Sprintf("[Delete Task] sid jid id is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(sid)); s != nil { if err := s.DeleteTask(int64(id)); err != nil { e := fmt.Sprintf("[Delete Task] delete task error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } else { r.JSON(200, nil) } } } // }}}
//updateJob获取客户端发送的Job信息,并调用Schedule的UpdateJob方法将其 //持久化并更新至Schedule中。 //成功返回更新后的Job信息 func UpdateJob(r render.Render, Ss *schedule.ScheduleManager, job schedule.Job) { // {{{ if job.Name == "" { e := fmt.Sprintf("[UpdateJob] Job name is required") g.L.Warningln(e) r.JSON(500, e) return } if s := Ss.GetScheduleById(int64(job.ScheduleId)); s != nil { if err := s.UpdateJob(&job); err != nil { e := fmt.Sprintf("[UpdateJob] update job error %s.", err.Error()) g.L.Warningln(e) r.JSON(500, e) return } else { r.JSON(200, job) } } else { e := fmt.Sprintf("[UpdateJob] schedule not found.") g.L.Warningln(e) r.JSON(500, e) return } } // }}}