|
@@ -18,9 +18,49 @@ func JobInit() {
|
|
// notice.SendMessage(nil)
|
|
// notice.SendMessage(nil)
|
|
c.AddFunc("30 * * * * *", ReadMsgs)
|
|
c.AddFunc("30 * * * * *", ReadMsgs)
|
|
c.Start()
|
|
c.Start()
|
|
|
|
+
|
|
|
|
+ // 定时清理数据(记录+删除)
|
|
|
|
+ c2 := cron.New()
|
|
|
|
+ c2.AddFunc("0 0 0/1 * * *", clearMessage)
|
|
|
|
+ c2.Start()
|
|
|
|
+
|
|
|
|
+ // 清理七天前的记录数据
|
|
|
|
+ c3 := cron.New()
|
|
|
|
+ c3.AddFunc("0 0 0 0/1 * *", clearMessageOfBeforeWeek)
|
|
|
|
+ c3.Start()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func clearMessageOfBeforeWeek() {
|
|
|
|
+ log.Errorf("%v\n", "-------------------定时清理七天前的备份数据--------------------")
|
|
|
|
+ errCopy := svc.DB.Exec(" delete from message_history where created_at< DATE_SUB(NOW(),INTERVAL 7 day) ")
|
|
|
|
+ if errCopy.Error == nil {
|
|
|
|
+ log.Errorf("%v\n", "-------------------备份数据删除完成!--------------------")
|
|
|
|
+ } else {
|
|
|
|
+ log.Errorf("%v\n", "-------------------备份数据删除失败!--------------------")
|
|
|
|
+ log.Errorf("%v", errCopy.Error)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func clearMessage() {
|
|
|
|
+ log.Errorf("%v\n", "-------------------定时清理数据,开始copy数据--------------------")
|
|
|
|
+ errCopy := svc.DB.Exec(" insert into message_history select * from message where `status`!=0 or target='null' ")
|
|
|
|
+ if errCopy.Error == nil {
|
|
|
|
+ log.Errorf("%v\n", "-------------------copy数据完成,开始删除数据--------------------")
|
|
|
|
+ errDel := svc.DB.Exec(" delete from message where `status`!=0 or target='null' ")
|
|
|
|
+ if errDel.Error == nil {
|
|
|
|
+ log.Errorf("%v\n", "-------------------删除数据完成,定时清理数据完成!--------------------")
|
|
|
|
+ } else {
|
|
|
|
+ log.Errorf("%v\n", "-------------------删除数据失败!--------------------")
|
|
|
|
+ log.Errorf("%v", errDel.Error)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.Errorf("%v\n", "-------------------copy数据失败!--------------------")
|
|
|
|
+ log.Errorf("%v", errCopy.Error)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
func ReadMsgs() {
|
|
func ReadMsgs() {
|
|
|
|
+ log.Errorf("%v\n", "-------------------发送消息--------------------")
|
|
var msgs []model.Message
|
|
var msgs []model.Message
|
|
var err error
|
|
var err error
|
|
if msgs, err = svc.QueryMsg(); err != nil {
|
|
if msgs, err = svc.QueryMsg(); err != nil {
|
|
@@ -31,10 +71,11 @@ func ReadMsgs() {
|
|
if len(msgs) == 0 {
|
|
if len(msgs) == 0 {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- // fmt.Printf("%v\n", msgs)
|
|
|
|
|
|
+ fmt.Printf("%v\n", msgs)
|
|
for _, val := range msgs {
|
|
for _, val := range msgs {
|
|
|
|
+
|
|
e := SendMsg(&val)
|
|
e := SendMsg(&val)
|
|
- log.Error(e)
|
|
|
|
|
|
+ log.Error("----e----", e)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -66,6 +107,13 @@ func SendMsg(val *model.Message) (err error) {
|
|
if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
|
|
if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
|
|
err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
|
|
err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
|
|
log.Errorf("%v", err)
|
|
log.Errorf("%v", err)
|
|
|
|
+ val.ErrInfo = tmperr.Error()
|
|
|
|
+ // 失败记录错误信息、状态
|
|
|
|
+ // if e := svc.UpdateMessageFail(val); e != nil {
|
|
|
|
+ // err = errors.Wrap(e, "SendMessage{update message error}=%v")
|
|
|
|
+ // log.Errorf("%v", err)
|
|
|
|
+ // }
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
log.Infof("send msg success%v", string(resp))
|
|
log.Infof("send msg success%v", string(resp))
|
|
if e := svc.UpdateMessage(val); e != nil {
|
|
if e := svc.UpdateMessage(val); e != nil {
|