// 查看日志详情 func (this *TaskController) ViewLog() { id, _ := this.GetInt("id") taskLog, err := models.TaskLogGetById(id) if err != nil { this.showMsg(err.Error()) } task, err := models.TaskGetById(taskLog.TaskId) if err != nil { this.showMsg(err.Error()) } data := make(map[string]interface{}) data["id"] = taskLog.Id data["output"] = taskLog.Output data["error"] = taskLog.Error data["start_time"] = beego.Date(time.Unix(taskLog.CreateTime, 0), "Y-m-d H:i:s") data["process_time"] = float64(taskLog.ProcessTime) / 1000 data["ouput_size"] = libs.SizeFormat(float64(len(taskLog.Output))) data["status"] = taskLog.Status this.Data["task"] = task this.Data["data"] = data this.Data["pageTitle"] = "查看日志" this.display() }
// 任务执行日志列表 func (this *TaskController) Logs() { taskId, _ := this.GetInt("id") page, _ := this.GetInt("page") if page < 1 { page = 1 } task, err := models.TaskGetById(taskId) if err != nil { this.showMsg(err.Error()) } result, count := models.TaskLogGetList(page, this.pageSize, "task_id", task.Id) list := make([]map[string]interface{}, len(result)) for k, v := range result { row := make(map[string]interface{}) row["id"] = v.Id row["start_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s") row["process_time"] = float64(v.ProcessTime) / 1000 row["ouput_size"] = libs.SizeFormat(float64(len(v.Output))) row["status"] = v.Status list[k] = row } this.Data["pageTitle"] = "任务执行日志" this.Data["list"] = list this.Data["task"] = task this.Data["pageBar"] = libs.NewPager(page, int(count), this.pageSize, beego.URLFor("TaskController.Logs", "id", taskId), true).ToString() this.display() }
// 编辑任务 func (this *TaskController) Edit() { id, _ := this.GetInt("id") task, err := models.TaskGetById(id) if err != nil { this.showMsg(err.Error()) } if this.isPost() { task.TaskName = strings.TrimSpace(this.GetString("task_name")) task.Description = strings.TrimSpace(this.GetString("description")) task.GroupId, _ = this.GetInt("group_id") task.Concurrent, _ = this.GetInt("concurrent") task.CronSpec = strings.TrimSpace(this.GetString("cron_spec")) task.Command = strings.TrimSpace(this.GetString("command")) task.Notify, _ = this.GetInt("notify") task.Timeout, _ = this.GetInt("timeout") notifyEmail := strings.TrimSpace(this.GetString("notify_email")) if notifyEmail != "" { tmp := strings.Split(notifyEmail, "\n") emailList := make([]string, 0, len(tmp)) for _, v := range tmp { v = strings.TrimSpace(v) if !libs.IsEmail([]byte(v)) { this.ajaxMsg("无效的Email地址:"+v, MSG_ERR) } else { emailList = append(emailList, v) } } task.NotifyEmail = strings.Join(emailList, "\n") } if task.TaskName == "" || task.CronSpec == "" || task.Command == "" { this.ajaxMsg("请填写完整信息", MSG_ERR) } if _, err := libcron.Parse(task.CronSpec); err != nil { this.ajaxMsg("cron表达式无效", MSG_ERR) } if err := task.Update(); err != nil { this.ajaxMsg(err.Error(), MSG_ERR) } this.ajaxMsg("", MSG_OK) } // 分组列表 groups, _ := models.TaskGroupGetList(1, 100) this.Data["groups"] = groups this.Data["task"] = task this.Data["pageTitle"] = "编辑任务" this.display() }
// 批量操作 func (this *TaskController) Batch() { action := this.GetString("action") ids := this.GetStrings("ids") if len(ids) < 1 { this.ajaxMsg("请选择要操作的项目", MSG_ERR) } for _, v := range ids { id, _ := strconv.Atoi(v) if id < 1 { continue } switch action { case "active": if task, err := models.TaskGetById(id); err == nil { job, err := jobs.NewJobFromTask(task) if err == nil { jobs.AddJob(task.CronSpec, job) task.Status = 1 task.Update() } } case "pause": jobs.RemoveJob(id) if task, err := models.TaskGetById(id); err == nil { task.Status = 0 task.Update() } case "delete": models.TaskDel(id) models.TaskLogDelByTaskId(id) jobs.RemoveJob(id) } } this.ajaxMsg("", MSG_OK) }
// 立即执行 func (this *TaskController) Run() { id, _ := this.GetInt("id") task, err := models.TaskGetById(id) if err != nil { this.showMsg(err.Error()) } job, err := jobs.NewJobFromTask(task) if err != nil { this.showMsg(err.Error()) } job.Run() this.redirect(beego.URLFor("TaskController.ViewLog", "id", job.GetLogId())) }
// 暂停任务 func (this *TaskController) Pause() { id, _ := this.GetInt("id") task, err := models.TaskGetById(id) if err != nil { this.showMsg(err.Error()) } jobs.RemoveJob(id) task.Status = 0 task.Update() refer := this.Ctx.Request.Referer() if refer == "" { refer = beego.URLFor("TaskController.List") } this.redirect(refer) }
// 首页 func (this *MainController) Index() { this.Data["pageTitle"] = "系统概况" // 即将执行的任务 entries := jobs.GetEntries(30) jobList := make([]map[string]interface{}, len(entries)) for k, v := range entries { row := make(map[string]interface{}) job := v.Job.(*jobs.Job) row["task_id"] = job.GetId() row["task_name"] = job.GetName() row["next_time"] = beego.Date(v.Next, "Y-m-d H:i:s") jobList[k] = row } // 最近执行的日志 logs, _ := models.TaskLogGetList(1, 20) recentLogs := make([]map[string]interface{}, len(logs)) for k, v := range logs { task, err := models.TaskGetById(v.TaskId) taskName := "" if err == nil { taskName = task.TaskName } row := make(map[string]interface{}) row["task_name"] = taskName row["id"] = v.Id row["start_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s") row["process_time"] = float64(v.ProcessTime) / 1000 row["ouput_size"] = libs.SizeFormat(float64(len(v.Output))) row["output"] = beego.Substr(v.Output, 0, 100) row["status"] = v.Status recentLogs[k] = row } this.Data["recentLogs"] = recentLogs this.Data["jobs"] = jobList this.Data["cpuNum"] = runtime.NumCPU() this.display() }
// 启动任务 func (this *TaskController) Start() { id, _ := this.GetInt("id") task, err := models.TaskGetById(id) if err != nil { this.showMsg(err.Error()) } job, err := jobs.NewJobFromTask(task) if err != nil { this.showMsg(err.Error()) } if jobs.AddJob(task.CronSpec, job) { task.Status = 1 task.Update() } refer := this.Ctx.Request.Referer() if refer == "" { refer = beego.URLFor("TaskController.List") } this.redirect(refer) }