Ver código fonte

Merge branch 'fix/change_email'

Roc 2 anos atrás
pai
commit
46728fc33f
2 arquivos alterados com 70 adições e 0 exclusões
  1. 40 0
      main.go
  2. 30 0
      services/alarm_msg/alarm_msg.go

+ 40 - 0
main.go

@@ -1,9 +1,16 @@
 package main
 
 import (
+	"fmt"
 	_ "hongze/hongze_chart_lib/routers"
+	"hongze/hongze_chart_lib/services/alarm_msg"
+	"hongze/hongze_chart_lib/utils"
+	"runtime"
+	"time"
 
+	"github.com/beego/beego/v2/adapter/logs"
 	beego "github.com/beego/beego/v2/server/web"
+	"github.com/beego/beego/v2/server/web/context"
 )
 
 func main() {
@@ -11,6 +18,39 @@ func main() {
 		beego.BConfig.WebConfig.DirectoryIndex = true
 		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
 	}
+	beego.BConfig.RecoverFunc = Recover
 
 	beego.Run()
 }
+
+func Recover(ctx *context.Context, conf *beego.Config) {
+	if err := recover(); err != nil {
+		if err == beego.ErrAbort {
+			return
+		}
+		if !beego.BConfig.RecoverPanic {
+			panic(err)
+		}
+		stack := ""
+		msg := fmt.Sprintf("The request url is  %v", ctx.Input.URL())
+		stack += msg + "</br>"
+		logs.Critical(msg)
+		msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
+		stack += msg + "</br>"
+		logs.Critical(msg)
+		msg = fmt.Sprintf("Handler crashed with error %v", err)
+		stack += msg + "</br>"
+		logs.Critical(msg)
+		for i := 1; ; i++ {
+			_, file, line, ok := runtime.Caller(i)
+			if !ok {
+				break
+			}
+			logs.Critical(fmt.Sprintf("%s:%d", file, line))
+			stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
+		}
+		//go utils.SendEmail(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05"), stack, utils.EmailSendToUsers)
+		go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
+	}
+	return
+}

+ 30 - 0
services/alarm_msg/alarm_msg.go

@@ -0,0 +1,30 @@
+package alarm_msg
+
+import (
+	"encoding/json"
+	"github.com/rdlucklib/rdluck_tools/http"
+	"hongze/hongze_chart_lib/utils"
+)
+
+var (
+	AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
+)
+
+// SendAlarmMsg
+//projectName-项目名称
+//runMode-运行模式
+//msgBody-消息内容
+//level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
+func SendAlarmMsg(msgBody string, level int) {
+	params := make(map[string]interface{})
+	params["ProjectName"] = utils.APPNAME
+	params["RunMode"] = utils.RunMode
+	params["MsgBody"] = msgBody
+	params["Level"] = level
+	param, err := json.Marshal(params)
+	if err != nil {
+		utils.FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
+		return
+	}
+	http.Post(AlarmMsgUrl, string(param))
+}