diff --git a/jobs/job.go b/jobs/job.go index df9f623..7ab9c99 100644 --- a/jobs/job.go +++ b/jobs/job.go @@ -388,7 +388,7 @@ func (j *Job) Run() { status := log.Status + 2 - title, content := "", "" + title, content, taskOutput, errOutput := "", "", "", "" notifyTpl, err := models.NotifyTplGetById(j.task.NotifyTplId) if err != nil { @@ -402,6 +402,11 @@ func (j *Job) Run() { content = notifyTpl.Content } + taskOutput = strings.Replace(log.Output, "\n", " ", -1) + taskOutput = strings.Replace(taskOutput, "\"", "\\\"", -1) + errOutput = strings.Replace(log.Error, "\n", " ", -1) + errOutput = strings.Replace(errOutput, "\"", "\\\"", -1) + if title != "" { title = strings.Replace(title, "{{TaskId}}", strconv.Itoa(j.task.Id), -1) title = strings.Replace(title, "{{TaskName}}", j.task.TaskName, -1) @@ -409,7 +414,8 @@ func (j *Job) Run() { title = strings.Replace(title, "{{ExecuteTime}}", 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, "{{ExecuteStatus}}", TextStatus[status], -1) - title = strings.Replace(title, "{{TaskOutput}}", log.Error, -1) + title = strings.Replace(title, "{{TaskOutput}}", taskOutput, -1) + title = strings.Replace(title, "{{ErrorOutput}}", errOutput, -1) } if content != "" { @@ -419,7 +425,8 @@ func (j *Job) Run() { content = strings.Replace(content, "{{ExecuteTime}}", 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, "{{ExecuteStatus}}", TextStatus[status], -1) - content = strings.Replace(content, "{{TaskOutput}}", log.Error, -1) + content = strings.Replace(content, "{{TaskOutput}}", taskOutput, -1) + content = strings.Replace(content, "{{ErrorOutput}}", errOutput, -1) } if j.task.NotifyType == 0 && toEmail != "" { @@ -444,12 +451,20 @@ func (j *Job) Run() { } } else if j.task.NotifyType == 2 && len(dingtalk) > 0 { //钉钉 - ok := notify.SendDingtalkToChan(dingtalk, content) + param := make(map[string]interface{}) + + err := json.Unmarshal([]byte(content), ¶m) + if err != nil { + fmt.Println("发送钉钉错误", err) + return + } + + ok := notify.SendDingtalkToChan(dingtalk, param) if !ok { fmt.Println("发送钉钉错误", dingtalk) } } else if j.task.NotifyType == 3 && len(wechat) > 0 { - //信息 + //微信 param := make(map[string]string) err := json.Unmarshal([]byte(content), ¶m) if err != nil { diff --git a/notify/dingtalk.go b/notify/dingtalk.go index 4675860..e68bc8a 100644 --- a/notify/dingtalk.go +++ b/notify/dingtalk.go @@ -28,7 +28,7 @@ type Text struct { type Dingtalk struct { Dingtalks map[string]string - Content string + Content map[string]interface{} } var DingtalkChan chan *Dingtalk @@ -57,7 +57,7 @@ func init() { } -func SendDingtalkToChan(dingtalks map[string]string, content string) bool { +func SendDingtalkToChan(dingtalks map[string]string, content map[string]interface{}) bool { dingTalk := &Dingtalk{ Dingtalks: dingtalks, Content: content, @@ -74,13 +74,7 @@ func SendDingtalkToChan(dingtalks map[string]string, content string) bool { func (s *Dingtalk) SendDingtalk() error { for _, v := range s.Dingtalks { - - msg := Msg{MsgType: "text"} - text := new(Text) - text.Content = s.Content - msg.Text = text - - body, err := json.Marshal(msg) + body, err := json.Marshal(s.Content) if err != nil { log.Println(err) return err diff --git a/ppgo_job2.sql b/ppgo_job2.sql index c0a462a..0104b78 100644 --- a/ppgo_job2.sql +++ b/ppgo_job2.sql @@ -312,9 +312,9 @@ CREATE TABLE `pp_notify_tpl` ( -- 转存表中的数据 `pp_notify_tpl` -- BEGIN; -INSERT INTO `pp_notify_tpl` VALUES(1, 'system', '默认邮箱通知模板', 0, '定时任务异常:{{TaskName}}', 'Hello,定时任务出问题了:\r\n

任务执行详情:

\r\n

\r\n任务 ID:{{TaskId}}
\r\n任务名称:{{TaskName}}
\r\n执行命令:{{ExecuteCommand}}
\r\n执行时间:{{ExecuteTime}}
\r\n执行耗时:{{ProcessTime}}秒
\r\n执行状态:{{ExecuteStatus}}\r\n

\r\n

任务执行输出

\r\n

\r\n{{TaskOutput}}\r\n

', 1, 1550255030, 1, 1550338305, 1); +INSERT INTO `pp_notify_tpl` VALUES(1, 'system', '默认邮箱通知模板', 0, '定时任务异常:{{TaskName}}', 'Hello,定时任务出问题了:\r\n

任务执行详情:

\r\n

\r\n任务 ID:{{TaskId}}
\r\n任务名称:{{TaskName}}
\r\n执行命令:{{ExecuteCommand}}
\r\n执行时间:{{ExecuteTime}}
\r\n执行耗时:{{ProcessTime}}秒
\r\n执行状态:{{ExecuteStatus}}\r\n

\r\n

任务执行输出

\r\n

\r\n{{TaskOutput}}\r\n

\r\n

错误输出

\r\n

\r\n{{ErrorOutput}}\r\n

', 1, 1550255030, 1, 1553282382, 1); INSERT INTO `pp_notify_tpl` VALUES(2, 'system', '默认短信通知模板', 1, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_command\": \"{{ExecuteCommand}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550255030, 1, 1550338215, 1); -INSERT INTO `pp_notify_tpl` VALUES(3, 'system', '默认钉钉通知模板', 2, '', '任务执行异常详情:\r\n任务 ID:{{TaskId}}\r\n任务名称:{{TaskName}}\r\n执行命令:{{ExecuteCommand}}\r\n执行时间:{{ExecuteTime}}\r\n执行耗时:{{ProcessTime}}秒\r\n执行状态:{{ExecuteStatus}}\r\n任务执行输出:\r\n{{TaskOutput}}', 1, 1550255030, 1, 1550338880, 1); +INSERT INTO `pp_notify_tpl` VALUES(3, 'system', '默认钉钉通知模板', 2, '', '{\r\n \"msgtype\": \"text\",\r\n \"text\": {\r\n \"content\": \"任务执行异常详情:\\n任务 ID:{{TaskId}}\\n任务名称:{{TaskName}}\\n执行命令:{{ExecuteCommand}}\\n执行时间:{{ExecuteTime}}\\n执行耗时:{{ProcessTime}}秒\\n执行状态:{{ExecuteStatus}}\\n任务执行输出:\\n{{TaskOutput}}\\n错误输出:\\n{{ErrorOutput}}\"\r\n }\r\n}', 1, 1550255030, 1, 1553282245, 1); INSERT INTO `pp_notify_tpl` VALUES(4, 'system', '默认微信通知模板', 3, '', '{\r\n \"task_id\": \"{{TaskId}}\",\r\n \"task_name\": \"{{TaskName}}\",\r\n \"execute_command\": \"{{ExecuteCommand}}\",\r\n \"execute_status\": \"{{ExecuteStatus}}\"\r\n}', 1, 1550347183, 1, 1550347201, 1); COMMIT; diff --git a/views/notifytpl/add.html b/views/notifytpl/add.html index ffc9c9b..a259f6c 100644 --- a/views/notifytpl/add.html +++ b/views/notifytpl/add.html @@ -46,6 +46,7 @@ {{"执行耗时 {{ProcessTime}}"}} {{"执行状态 {{ExecuteStatus}}"}} {{"任务输出 {{TaskOutput}}"}} + {{"错误输出 {{ErrorOutput}}"}} diff --git a/views/notifytpl/edit.html b/views/notifytpl/edit.html index 14808ec..5997ea8 100644 --- a/views/notifytpl/edit.html +++ b/views/notifytpl/edit.html @@ -47,6 +47,7 @@ {{"执行耗时 {{ProcessTime}}"}} {{"执行状态 {{ExecuteStatus}}"}} {{"任务输出 {{TaskOutput}}"}} + {{"错误输出 {{ErrorOutput}}"}}