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