Skip to content

Commit

Permalink
1.通知模板添加错误输出变量
Browse files Browse the repository at this point in the history
2.钉钉通知模板修改为自定义格式
3.更新系统默认通知模板sql
  • Loading branch information
linxiaozhi committed Mar 22, 2019
1 parent 141d6ce commit f1b35a9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
25 changes: 20 additions & 5 deletions jobs/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -402,14 +402,20 @@ 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)
title = strings.Replace(title, "{{ExecuteCommand}}", j.task.Command, -1)
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 != "" {
Expand All @@ -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 != "" {
Expand All @@ -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), &param)
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), &param)
if err != nil {
Expand Down
12 changes: 3 additions & 9 deletions notify/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Text struct {

type Dingtalk struct {
Dingtalks map[string]string
Content string
Content map[string]interface{}
}

var DingtalkChan chan *Dingtalk
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ppgo_job2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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<p style=\"font-size:16px;\">任务执行详情:</p>\r\n<p style=\"display:block; padding:10px; background:#efefef;border:1px solid #e4e4e4\">\r\n任务 ID:{{TaskId}}<br/>\r\n任务名称:{{TaskName}}<br/>\r\n执行命令:{{ExecuteCommand}}<br/>\r\n执行时间:{{ExecuteTime}}<br/>\r\n执行耗时:{{ProcessTime}}秒<br/>\r\n执行状态:{{ExecuteStatus}}\r\n</p>\r\n<p style=\"font-size:16px;\">任务执行输出</p>\r\n<p style=\"display:block; padding:10px; background:#efefef;border:1px solid #e4e4e4\">\r\n{{TaskOutput}}\r\n</p>', 1, 1550255030, 1, 1550338305, 1);
INSERT INTO `pp_notify_tpl` VALUES(1, 'system', '默认邮箱通知模板', 0, '定时任务异常:{{TaskName}}', 'Hello,定时任务出问题了:\r\n<p style=\"font-size:16px;\">任务执行详情:</p>\r\n<p style=\"display:block; padding:10px; background:#efefef;border:1px solid #e4e4e4\">\r\n任务 ID:{{TaskId}}<br/>\r\n任务名称:{{TaskName}}<br/>\r\n执行命令:{{ExecuteCommand}}<br/>\r\n执行时间:{{ExecuteTime}}<br/>\r\n执行耗时:{{ProcessTime}}秒<br/>\r\n执行状态:{{ExecuteStatus}}\r\n</p>\r\n<p style=\"font-size:16px;\">任务执行输出</p>\r\n<p style=\"display:block; padding:10px; background:#efefef;border:1px solid #e4e4e4\">\r\n{{TaskOutput}}\r\n</p>\r\n<p style=\"font-size:16px;\">错误输出</p>\r\n<p style=\"display:block; padding:10px; background:#efefef;border:1px solid #e4e4e4\">\r\n{{ErrorOutput}}\r\n</p>', 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;

Expand Down
1 change: 1 addition & 0 deletions views/notifytpl/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
{{"执行耗时 {{ProcessTime}}"}}
{{"执行状态 {{ExecuteStatus}}"}}
{{"任务输出 {{TaskOutput}}"}}
{{"错误输出 {{ErrorOutput}}"}}

</pre>
</div>
Expand Down
1 change: 1 addition & 0 deletions views/notifytpl/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
{{"执行耗时 {{ProcessTime}}"}}
{{"执行状态 {{ExecuteStatus}}"}}
{{"任务输出 {{TaskOutput}}"}}
{{"错误输出 {{ErrorOutput}}"}}

</pre>
</div>
Expand Down

0 comments on commit f1b35a9

Please sign in to comment.