fix: add send precheck msg
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
"message-nest/pkg/app"
|
||||
"message-nest/pkg/e"
|
||||
"message-nest/service/send_message_service"
|
||||
@@ -37,18 +39,26 @@ func DoSendMassage(c *gin.Context) {
|
||||
HTML: req.HTML,
|
||||
MarkDown: req.MarkDown,
|
||||
CallerIp: c.ClientIP(),
|
||||
DefaultLogger: logrus.WithFields(logrus.Fields{
|
||||
"prefix": "[Message Instance]",
|
||||
}),
|
||||
}
|
||||
task, err := msgService.SendPreCheck()
|
||||
if err != nil {
|
||||
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("发送检查不通过:%s", err), nil)
|
||||
return
|
||||
}
|
||||
if req.Mode == "sync" {
|
||||
// 同步发送
|
||||
err := msgService.Send()
|
||||
if err != "" {
|
||||
_, err := msgService.Send(task)
|
||||
if err != nil {
|
||||
appG.CResponse(http.StatusBadRequest, "发送失败!", nil)
|
||||
return
|
||||
}
|
||||
appG.CResponse(http.StatusOK, "发送成功!", nil)
|
||||
} else {
|
||||
// 异步发送
|
||||
msgService.AsyncSend()
|
||||
msgService.AsyncSend(task)
|
||||
appG.CResponse(http.StatusOK, "提交成功!", nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,12 @@ var ClearLogsTaskId cron.EntryID
|
||||
// ClearLogs 清除日志的定时任务
|
||||
func ClearLogs() {
|
||||
var errStr string
|
||||
sm := send_message_service.SendMessageService{TaskID: constant.CleanLogsTaskId}
|
||||
sm := send_message_service.SendMessageService{
|
||||
TaskID: constant.CleanLogsTaskId,
|
||||
DefaultLogger: logrus.WithFields(logrus.Fields{
|
||||
"prefix": "[Cron Clear Logs]",
|
||||
}),
|
||||
}
|
||||
sm.Status = send_message_service.SendSuccess
|
||||
|
||||
sm.LogsAndStatusMark("开始清除日志", sm.Status)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package send_message_service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"message-nest/models"
|
||||
@@ -15,10 +16,6 @@ const (
|
||||
SendFail = 0
|
||||
)
|
||||
|
||||
//var logger = logrus.WithFields(logrus.Fields{
|
||||
// "prefix": "[Message]",
|
||||
//})
|
||||
|
||||
func errStrIsSuccess(errStr string) int {
|
||||
if errStr == "" {
|
||||
return SendSuccess
|
||||
@@ -36,6 +33,8 @@ type SendMessageService struct {
|
||||
|
||||
Status int
|
||||
LogOutput []string
|
||||
|
||||
DefaultLogger *logrus.Entry
|
||||
}
|
||||
|
||||
// LogsAndStatusMark 记录执行的日志和状态标记
|
||||
@@ -44,11 +43,11 @@ func (sm *SendMessageService) LogsAndStatusMark(errStr string, status int) {
|
||||
if status == SendFail {
|
||||
sm.Status = SendFail
|
||||
}
|
||||
logrus.Infof("%s, 状态:%d", strings.Trim(errStr, "\n"), status)
|
||||
sm.DefaultLogger.Infof("%s, 状态:%d", strings.Trim(errStr, "\n"), status)
|
||||
}
|
||||
|
||||
// AsyncSend 异步发送一个消息任务的所有实例
|
||||
func (sm *SendMessageService) AsyncSend() {
|
||||
func (sm *SendMessageService) AsyncSend(task models.TaskIns) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logrus.Error("AsyncSend: Recovered from panic:", r)
|
||||
@@ -61,31 +60,58 @@ func (sm *SendMessageService) AsyncSend() {
|
||||
<-constant.MaxSendTaskSemaphoreChan
|
||||
}()
|
||||
|
||||
go sm.Send()
|
||||
go func() {
|
||||
entry := logrus.WithFields(logrus.Fields{
|
||||
"prefix": "[Send Goroutine]",
|
||||
})
|
||||
_, err := sm.Send(task)
|
||||
if err != nil {
|
||||
entry.Errorf("任务[%s][%s]发送错误: %s", sm.TaskID, sm.Title, err)
|
||||
} else {
|
||||
entry.Infof("完成任务[%s][%s]发送", sm.TaskID, sm.Title)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Send 发送一个消息任务的所有实例
|
||||
func (sm *SendMessageService) Send() string {
|
||||
sm.Status = SendSuccess
|
||||
// SendPreCheck 发送前数据准备和预检查
|
||||
func (sm *SendMessageService) SendPreCheck() (models.TaskIns, error) {
|
||||
errStr := ""
|
||||
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("发送标题《%s》 \n", sm.Title), sm.Status)
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("开始任务[%s]的发送", sm.TaskID), sm.Status)
|
||||
entry := logrus.WithFields(logrus.Fields{
|
||||
"prefix": "[Message PreChecK]",
|
||||
})
|
||||
sendTaskService := send_task_service.SendTaskService{
|
||||
ID: sm.TaskID,
|
||||
}
|
||||
task, err := sendTaskService.GetTaskWithIns()
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("任务名称:%s", task.Name), sm.Status)
|
||||
if err != nil {
|
||||
errStr = fmt.Sprintf("任务[%s]不存在!退出发送!", sm.TaskID)
|
||||
sm.LogsAndStatusMark(errStr, SendFail)
|
||||
return errStr
|
||||
errStr = fmt.Sprintf("任务[%s]查询失败!", sm.TaskID)
|
||||
entry.Errorf(errStr)
|
||||
return task, errors.New(errStr)
|
||||
}
|
||||
if task.ID == "" {
|
||||
errStr = fmt.Sprintf("任务[%s]不存在!", sm.TaskID)
|
||||
entry.Errorf(errStr)
|
||||
return task, errors.New(errStr)
|
||||
}
|
||||
if len(task.InsData) == 0 {
|
||||
errStr = fmt.Sprintf("任务[%s]没有关联任何实例!!", sm.TaskID)
|
||||
entry.Errorf(errStr)
|
||||
return task, errors.New(errStr)
|
||||
}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// Send 发送一个消息任务的所有实例
|
||||
func (sm *SendMessageService) Send(task models.TaskIns) (string, error) {
|
||||
sm.Status = SendSuccess
|
||||
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("发送标题《%s》 \n", sm.Title), sm.Status)
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("开始任务[%s]的发送", sm.TaskID), sm.Status)
|
||||
|
||||
for idx, ins := range task.InsData {
|
||||
way, err := models.GetWayByID(ins.WayID)
|
||||
if err != nil {
|
||||
errStr = fmt.Sprintf("渠道[%s]信息不存在!跳过这个实例的发送", ins.WayID)
|
||||
errStr := fmt.Sprintf("渠道[%s]信息不存在!跳过这个实例的发送", ins.WayID)
|
||||
sm.LogsAndStatusMark(errStr, SendFail)
|
||||
continue
|
||||
}
|
||||
@@ -162,13 +188,17 @@ func (sm *SendMessageService) Send() string {
|
||||
|
||||
}
|
||||
|
||||
// 追加记录发送内容
|
||||
sm.AppendSendContent()
|
||||
// 日志写到数据库
|
||||
sm.RecordSendLog()
|
||||
|
||||
totalOutputLog := strings.Join(sm.LogOutput, "\n")
|
||||
if sm.Status == SendSuccess {
|
||||
return ""
|
||||
return totalOutputLog, nil
|
||||
} else {
|
||||
return totalOutputLog, errors.New("发送过程中有失败,请检查详细日志")
|
||||
}
|
||||
return strings.Join(sm.LogOutput, "\n")
|
||||
}
|
||||
|
||||
// AppendSendContent 添加发送内容
|
||||
@@ -187,9 +217,6 @@ func (sm *SendMessageService) AppendSendContent() {
|
||||
|
||||
// RecordSendLog 记录发送日志
|
||||
func (sm *SendMessageService) RecordSendLog() {
|
||||
if len(sm.LogOutput) <= 0 {
|
||||
return
|
||||
}
|
||||
log := models.SendTasksLogs{
|
||||
Log: strings.Join(sm.LogOutput, "\n"),
|
||||
TaskID: sm.TaskID,
|
||||
@@ -198,7 +225,7 @@ func (sm *SendMessageService) RecordSendLog() {
|
||||
}
|
||||
err := log.Add()
|
||||
if err != nil {
|
||||
logrus.Error(fmt.Sprintf("添加日志失败!原因是:%s", err))
|
||||
sm.DefaultLogger.Errorf("添加日志失败!原因是:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user