diff --git a/controllers/notify_tpl.go b/controllers/notify_tpl.go new file mode 100644 index 0000000..1159936 --- /dev/null +++ b/controllers/notify_tpl.go @@ -0,0 +1,166 @@ +/************************************************************ +** @Description: controllers +** @Author: Bee +** @Date: 2019-02-15 20:21 +** @Last Modified by: Bee +** @Last Modified time: 2019-02-15 20:21 +*************************************************************/ +package controllers + +import ( + "github.com/george518/PPGo_Job/models" + "strings" + "time" + "github.com/astaxie/beego" + "encoding/json" +) + +type NotifyTplController struct { + BaseController +} + +func (self *NotifyTplController) List() { + self.Data["pageTitle"] = "通知模板" + self.display() +} + +func (self *NotifyTplController) Add() { + self.Data["pageTitle"] = "新增通知模板" + self.Data["serverGroup"] = serverGroupLists(self.serverGroups, self.userId) + self.display() +} + +func (self *NotifyTplController) Edit() { + self.Data["pageTitle"] = "编辑通知模板" + + id, _ := self.GetInt("id", 0) + notifyTpl, _ := models.NotifyTplGetById(id) + row := make(map[string]interface{}) + row["id"] = notifyTpl.Id + row["tpl_name"] = notifyTpl.TplName + row["tpl_type"] = notifyTpl.TplType + row["title"] = notifyTpl.Title + row["content"] = notifyTpl.Content + row["status"] = notifyTpl.Status + self.Data["notifyTpl"] = row + self.display() +} + +func (self *NotifyTplController) AjaxSave() { + tpl_id, _ := self.GetInt("id") + if tpl_id == 0 { + notifyTpl := new(models.NotifyTpl) + notifyTpl.TplName = strings.TrimSpace(self.GetString("tpl_name")) + notifyTpl.TplType, _ = self.GetInt("tpl_type") + notifyTpl.Title = strings.TrimSpace(self.GetString("title")) + notifyTpl.Content = strings.TrimSpace(self.GetString("content")) + notifyTpl.CreateId = self.userId + notifyTpl.CreateTime = time.Now().Unix() + notifyTpl.Type = "default" + notifyTpl.Status, _ = self.GetInt("status") + + if notifyTpl.TplType == 1 { + m := make(map[string]string) + err := json.Unmarshal([]byte(notifyTpl.Content), &m) + if err != nil { + self.ajaxMsg("模板内容格式错误,"+err.Error(), MSG_ERR) + } + } + + if _, err := models.NotifyTplAdd(notifyTpl); err != nil { + self.ajaxMsg(err.Error(), MSG_ERR) + } + self.ajaxMsg("", MSG_OK) + } + + notifyTpl, _ := models.NotifyTplGetById(tpl_id) + //修改 + notifyTpl.Id = tpl_id + notifyTpl.UpdateId = self.userId + notifyTpl.UpdateTime = time.Now().Unix() + + notifyTpl.TplName = strings.TrimSpace(self.GetString("tpl_name")) + notifyTpl.TplType, _ = self.GetInt("tpl_type") + notifyTpl.Title = strings.TrimSpace(self.GetString("title")) + notifyTpl.Content = strings.TrimSpace(self.GetString("content")) + notifyTpl.Status, _ = self.GetInt("status") + + if notifyTpl.TplType == 1 { + m := make(map[string]string) + err := json.Unmarshal([]byte(notifyTpl.Content), &m) + if err != nil { + self.ajaxMsg("模板内容格式错误,"+err.Error(), MSG_ERR) + } + } + + if notifyTpl.Type == "system" { + self.ajaxMsg("系统模板禁止更新", MSG_ERR) + } + + if err := notifyTpl.Update(); err != nil { + self.ajaxMsg("更新失败,"+err.Error(), MSG_ERR) + } + self.ajaxMsg("", MSG_OK) +} + +func (self *NotifyTplController) AjaxDel() { + id, _ := self.GetInt("id") + notifyTpl, _ := models.NotifyTplGetById(id) + + if notifyTpl.Type == "system" { + self.ajaxMsg("系统模板禁止删除", MSG_ERR) + } + + if err := models.NotifyTplDelById(id); err != nil { + self.ajaxMsg("删除失败,"+err.Error(), MSG_ERR) + } + self.ajaxMsg("操作成功", MSG_OK) +} + +func (self *NotifyTplController) Table() { + //列表 + page, err := self.GetInt("page") + if err != nil { + page = 1 + } + limit, err := self.GetInt("limit") + if err != nil { + limit = 30 + } + tplName := strings.TrimSpace(self.GetString("tplName")) + StatusText := []string{ + "禁用", + "正常", + } + + TplTypeText := []string{ + "邮件", + "信息", + "钉钉", + } + + self.pageSize = limit + //查询条件 + filters := make([]interface{}, 0) + + if tplName != "" { + filters = append(filters, "tpl_name__icontains", tplName) + } + result, count := models.NotifyTplGetList(page, self.pageSize, filters...) + list := make([]map[string]interface{}, len(result)) + for k, v := range result { + row := make(map[string]interface{}) + row["id"] = v.Id + row["type"] = v.Type + row["tpl_name"] = v.TplName + row["tpl_type"] = v.TplType + row["tpl_type_text"] = TplTypeText[v.TplType] + row["status"] = v.Status + row["status_text"] = StatusText[v.Status] + row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s") + row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s") + list[k] = row + } + + self.ajaxList("成功", MSG_OK, count, list) +} diff --git a/controllers/task.go b/controllers/task.go index 777eca9..1469e60 100644 --- a/controllers/task.go +++ b/controllers/task.go @@ -81,6 +81,22 @@ func (self *TaskController) Edit() { } self.Data["notify_user_ids"] = notifyUserIds + + notifyTplList, _, err := models.NotifyTplGetByTplTypeList(task.NotifyType) + tplList := make([]map[string]interface{}, len(notifyTplList)) + + if err == nil { + for k, v := range notifyTplList { + row := make(map[string]interface{}) + row["id"] = v.Id + row["tpl_name"] = v.TplName + row["tpl_type"] = v.TplType + tplList[k] = row + } + } + + self.Data["notifyTpl"] = tplList + self.display() } @@ -197,8 +213,13 @@ func (self *TaskController) AjaxSave() { task.Timeout, _ = self.GetInt("timeout") task.IsNotify, _ = self.GetInt("is_notify") task.NotifyType, _ = self.GetInt("notify_type") + task.NotifyTplId, _ = self.GetInt("notify_tpl_id") task.NotifyUserIds = strings.TrimSpace(self.GetString("notify_user_ids")) + if task.IsNotify == 1 && task.NotifyTplId <= 0 { + self.ajaxMsg("请选择通知模板", MSG_ERR) + } + msg, isBan := checkCommand(task.Command) if !isBan { self.ajaxMsg("含有禁止命令:"+msg, MSG_ERR) @@ -213,6 +234,7 @@ func (self *TaskController) AjaxSave() { if task.TaskName == "" || task.CronSpec == "" || task.Command == "" { self.ajaxMsg("请填写完整信息", MSG_ERR) } + if _, err := cron.Parse(task.CronSpec); err != nil { self.ajaxMsg("cron表达式无效", MSG_ERR) } @@ -237,9 +259,15 @@ func (self *TaskController) AjaxSave() { task.Timeout, _ = self.GetInt("timeout") task.IsNotify, _ = self.GetInt("is_notify") task.NotifyType, _ = self.GetInt("notify_type") + task.NotifyTplId, _ = self.GetInt("notify_tpl_id") task.NotifyUserIds = strings.TrimSpace(self.GetString("notify_user_ids")) task.UpdateId = self.userId task.Status = 2 //审核中,超级管理员不需要 + + if task.IsNotify == 1 && task.NotifyTplId <= 0 { + self.ajaxMsg("请选择通知模板", MSG_ERR) + } + if self.userId == 1 { task.Status = 0 } @@ -487,6 +515,23 @@ func (self *TaskController) AjaxDel() { self.ajaxMsg("操作成功", MSG_OK) } +func (self *TaskController) AjaxNotifyType() { + notifyType, _ := self.GetInt("notify_type") + result, count, _ := models.NotifyTplGetByTplTypeList(notifyType) + + list := make([]map[string]interface{}, len(result)) + + for k, v := range result { + row := make(map[string]interface{}) + row["id"] = v.Id + row["tpl_name"] = v.TplName + row["tpl_type"] = v.TplType + list[k] = row + } + + self.ajaxList("成功", MSG_OK, count, list) +} + func (self *TaskController) Table() { //列表 page, err := self.GetInt("page") diff --git a/controllers/task_log.go b/controllers/task_log.go index d9bd18e..3fbe064 100644 --- a/controllers/task_log.go +++ b/controllers/task_log.go @@ -146,6 +146,15 @@ func (self *TaskLogController) Detail() { self.Data["CreateTime"] = beego.Date(time.Unix(task.CreateTime, 0), "Y-m-d H:i:s") self.Data["UpdateTime"] = beego.Date(time.Unix(task.UpdateTime, 0), "Y-m-d H:i:s") self.Data["task"] = task + + self.Data["NotifyTplName"] = "未知" + if task.IsNotify == 1 { + notifyTpl, err := models.NotifyTplGetById(task.NotifyTplId) + if err == nil { + self.Data["NotifyTplName"] = notifyTpl.TplName + } + } + // 分组列表 self.Data["taskGroup"] = taskGroupLists(self.taskGroups, self.userId) diff --git a/jobs/job.go b/jobs/job.go index c4201f8..adb8f54 100644 --- a/jobs/job.go +++ b/jobs/job.go @@ -24,6 +24,7 @@ import ( "github.com/george518/PPGo_Job/models" "github.com/george518/PPGo_Job/notify" "golang.org/x/crypto/ssh" + "encoding/json" ) type Job struct { @@ -294,85 +295,59 @@ func (j *Job) Run() { toEmail = strings.TrimRight(toEmail, ";") TextStatus := []string{ - "超时", - "错误", - "正常", + "超时", + "错误", + "正常", } status := log.Status + 2 + var title = "" + var content = "" + notifyTpl, err := models.NotifyTplGetById(j.task.NotifyTplId) + if err == nil { + title = notifyTpl.Title + content = notifyTpl.Content + } + + if title != "" { + title = strings.Replace(title, "{TaskId}", strconv.Itoa(j.task.Id), -1) + title = strings.Replace(title, "{TaskName}", j.task.TaskName, -1) + title = strings.Replace(title, "{CreateTime}", beego.Date(time.Unix(log.CreateTime, 0), "Y-m-d H:i:s"), -1) + title = strings.Replace(title, "{ProcessTime}", strconv.FormatFloat(float64(log.ProcessTime)/1000, 'f', 6, 64), -1) + title = strings.Replace(title, "{Status}", TextStatus[status], -1) + title = strings.Replace(title, "{TaskOut}", log.Error, -1) + } + + if content != "" { + content = strings.Replace(content, "{TaskId}", strconv.Itoa(j.task.Id), -1) + content = strings.Replace(content, "{TaskName}", j.task.TaskName, -1) + content = strings.Replace(content, "{CreateTime}", beego.Date(time.Unix(log.CreateTime, 0), "Y-m-d H:i:s"), -1) + content = strings.Replace(content, "{ProcessTime}", strconv.FormatFloat(float64(log.ProcessTime)/1000, 'f', 6, 64), -1) + content = strings.Replace(content, "{Status}", TextStatus[status], -1) + content = strings.Replace(content, "{TaskOut}", log.Error, -1) + } + if j.task.NotifyType == 0 && toEmail != "" { //邮件 - //SendToChan(to, subject, body, mailtype string) bool - subject := fmt.Sprintf("PPGo_Job定时任务异常:%s", j.task.TaskName) - body := fmt.Sprintf( - `Hello,定时任务出问题了: -
任务执行详情:
-
-任务 ID:%d
-任务名称:%s
-执行时间:%s
-执行耗时:%f秒
-执行状态:%s
-
任务执行输出
--%s -
------------------------------------------------------------------
-本邮件由PPGo_Job定时系统自动发出,请勿回复
-如果要取消邮件通知,请登录到系统进行设置
-
任务执行详情:
\r\n\r\n任务 ID:{TaskId}
\r\n任务名称:{TaskName}
\r\n执行时间:{CreateTime}
\r\n执行耗时:{ProcessTime}秒
\r\n执行状态:{Status}\r\n
任务执行输出
\r\n\r\n{TaskOut}\r\n
\r\n-----------------------------------------------------------------
\r\n本邮件由定时系统自动发出,请勿回复
\r\n如果要取消邮件通知,请登录到系统进行设置
\r\n