From f1b35a9a25d15f4008b30a84a3f7818f7d113a0e Mon Sep 17 00:00:00 2001 From: linxiaozhi Date: Sat, 23 Mar 2019 03:29:17 +0800 Subject: [PATCH] =?UTF-8?q?1.=E9=80=9A=E7=9F=A5=E6=A8=A1=E6=9D=BF=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=94=99=E8=AF=AF=E8=BE=93=E5=87=BA=E5=8F=98=E9=87=8F?= =?UTF-8?q?=202.=E9=92=89=E9=92=89=E9=80=9A=E7=9F=A5=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=203.=E6=9B=B4=E6=96=B0=E7=B3=BB=E7=BB=9F=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=80=9A=E7=9F=A5=E6=A8=A1=E6=9D=BFsql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jobs/job.go | 25 ++++++++++++++++++++----- notify/dingtalk.go | 12 +++--------- ppgo_job2.sql | 4 ++-- views/notifytpl/add.html | 1 + views/notifytpl/edit.html | 1 + 5 files changed, 27 insertions(+), 16 deletions(-) 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}}"}}