将发送消息通知调整为异步

This commit is contained in:
marvzhang
2020-01-15 13:51:12 +08:00
parent 00c9312f83
commit 1fde42b932
4 changed files with 59 additions and 26 deletions

View File

@@ -26,6 +26,7 @@ type User struct {
type UserSetting struct {
NotificationTrigger string `json:"notification_trigger" bson:"notification_trigger"`
DingTalkRobotWebhook string `json:"ding_talk_robot_webhook" bson:"ding_talk_robot_webhook"`
WechatRobotWebhook string `json:"wechat_robot_webhook" bson:"wechat_robot_webhook"`
}
func (user *User) Save() error {

View File

@@ -212,19 +212,13 @@ func GetMe(c *gin.Context) {
}
func PostMe(c *gin.Context) {
type ReqBody struct {
Email string `json:"email"`
Password string `json:"password"`
NotificationTrigger string `json:"notification_trigger"`
DingTalkRobotWebhook string `json:"ding_talk_robot_webhook"`
}
ctx := context.WithGinContext(c)
user := ctx.User()
if user == nil {
ctx.FailedWithError(constants.ErrorUserNotFound, http.StatusUnauthorized)
return
}
var reqBody ReqBody
var reqBody model.User
if err := c.ShouldBindJSON(&reqBody); err != nil {
HandleErrorF(http.StatusBadRequest, c, "invalid request")
return
@@ -235,11 +229,14 @@ func PostMe(c *gin.Context) {
if reqBody.Password != "" {
user.Password = utils.EncryptPassword(reqBody.Password)
}
if reqBody.NotificationTrigger != "" {
user.Setting.NotificationTrigger = reqBody.NotificationTrigger
if reqBody.Setting.NotificationTrigger != "" {
user.Setting.NotificationTrigger = reqBody.Setting.NotificationTrigger
}
if reqBody.DingTalkRobotWebhook != "" {
user.Setting.DingTalkRobotWebhook = reqBody.DingTalkRobotWebhook
if reqBody.Setting.DingTalkRobotWebhook != "" {
user.Setting.DingTalkRobotWebhook = reqBody.Setting.DingTalkRobotWebhook
}
if reqBody.Setting.WechatRobotWebhook != "" {
user.Setting.WechatRobotWebhook = reqBody.Setting.WechatRobotWebhook
}
if err := user.Save(); err != nil {
HandleError(http.StatusInternalServerError, c, err)

View File

@@ -7,7 +7,7 @@ import (
"runtime/debug"
)
func SendDingTalkNotification(webhook string, title string, content string) error {
func SendMobileNotification(webhook string, title string, content string) error {
type ResBody struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
@@ -22,8 +22,9 @@ func SendDingTalkNotification(webhook string, title string, content string) erro
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
"title": title,
"text": content,
"title": title,
"text": content,
"content": content,
},
"at": req.Param{
"atMobiles": []string{},

View File

@@ -491,11 +491,21 @@ func ExecuteTask(id int) {
t, _ = model.GetTask(t.Id)
if user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskEnd || user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskError {
if user.Email != "" {
SendTaskEmail(user, t, spider)
go func() {
SendTaskEmail(user, t, spider)
}()
}
if user.Setting.DingTalkRobotWebhook != "" {
SendTaskDingTalk(user, t, spider)
go func() {
SendTaskDingTalk(user, t, spider)
}()
}
if user.Setting.WechatRobotWebhook != "" {
go func() {
SendTaskWechat(user, t, spider)
}()
}
}
return
@@ -523,11 +533,25 @@ func ExecuteTask(id int) {
// 如果是任务结束时发送通知,则发送通知
if user.Setting.NotificationTrigger == constants.NotificationTriggerOnTaskEnd {
if user.Email != "" {
SendTaskEmail(user, t, spider)
go func() {
SendTaskEmail(user, t, spider)
}()
}
if user.Setting.DingTalkRobotWebhook != "" {
SendTaskDingTalk(user, t, spider)
if user.Email != "" {
go func() {
if user.Setting.DingTalkRobotWebhook != "" {
SendTaskDingTalk(user, t, spider)
}
}()
}
if user.Email != "" {
go func() {
if user.Setting.WechatRobotWebhook != "" {
SendTaskWechat(user, t, spider)
}
}()
}
}
@@ -749,13 +773,15 @@ Please login to Crawlab to view the details.
)
}
func GetTaskDingTalkMarkdownContent(t model.Task, s model.Spider) string {
func GetTaskMarkdownContent(t model.Task, s model.Spider) string {
n, _ := model.GetNode(t.NodeId)
errMsg := ""
statusMsg := fmt.Sprintf(`<font color=green>%s</font>`, t.Status)
errLog := "-"
statusMsg := fmt.Sprintf(`<font color="#00FF00">%s</font>`, t.Status)
if t.Status == constants.StatusError {
errMsg = `<font color="red">(有错误)</font>`
statusMsg = fmt.Sprintf(`<font color=red>%s</font>`, t.Status)
errMsg = `<font color="#FF0000">有错误</font>`
errLog = fmt.Sprintf(`<font color="#FF0000">%s</font>`, t.Error)
statusMsg = fmt.Sprintf(`<font color="#FF0000">%s</font>`, t.Status)
}
return fmt.Sprintf(`
您的任务已完成%s请查看任务信息如下。
@@ -791,7 +817,7 @@ func GetTaskDingTalkMarkdownContent(t model.Task, s model.Spider) string {
t.RuntimeDuration,
t.TotalDuration,
t.ResultCount,
t.Error,
errLog,
)
}
@@ -818,8 +844,16 @@ func SendTaskDingTalk(u model.User, t model.Task, s model.Spider) {
statusMsg = "发生错误"
}
title := fmt.Sprintf("[Crawlab] \"%s\" 任务%s", s.Name, statusMsg)
content := GetTaskDingTalkMarkdownContent(t, s)
if err := notification.SendDingTalkNotification(u.Setting.DingTalkRobotWebhook, title, content); err != nil {
content := GetTaskMarkdownContent(t, s)
if err := notification.SendMobileNotification(u.Setting.DingTalkRobotWebhook, title, content); err != nil {
log.Errorf(err.Error())
debug.PrintStack()
}
}
func SendTaskWechat(u model.User, t model.Task, s model.Spider) {
content := GetTaskMarkdownContent(t, s)
if err := notification.SendMobileNotification(u.Setting.WechatRobotWebhook, "", content); err != nil {
log.Errorf(err.Error())
debug.PrintStack()
}