123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package service
- import (
- "encoding/json"
- "fmt"
- "github.com/pkg/errors"
- "github.com/robfig/cron"
- "kpt.notice/apiserver/model"
- "kpt.notice/middleware/notice"
- "kpt.notice/pkg/log"
- )
- func JobInit() {
- c := cron.New()
- // ReadMsgs()
- // notice.SendMessage(nil)
- c.AddFunc("30 * * * * *", ReadMsgs)
- 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() {
- log.Errorf("%v\n", "-------------------发送消息--------------------")
- var msgs []model.Message
- var err error
- if msgs, err = svc.QueryMsg(); err != nil {
- log.Errorf("%v", err)
- return
- }
- fmt.Println("msg len=", len(msgs))
- if len(msgs) == 0 {
- return
- }
- fmt.Printf("%v\n", msgs)
- for _, val := range msgs {
- e := SendMsg(&val)
- log.Error("----e----", e)
- }
- }
- func SendMsg(val *model.Message) (err error) {
- svc.UpdateMessageStatus(val.ID)
- // if val.SysName != "tmrWatch_test" {
- // return
- // } else {
- // val.Target = `["oLd1b56PwpexCa0QK4NCkza9TKyY"]`
- // }
- data := make(map[string]interface{})
- targets := make([]string, 10)
- if e := json.Unmarshal([]byte(val.MsgContent), &data); e != nil {
- log.Errorf("%v", err)
- return
- }
- if e := json.Unmarshal([]byte(val.Target), &targets); e != nil {
- log.Errorf("%v", err)
- return
- }
- for _, openid := range targets {
- data["touser"] = openid
- if jsonstr, e := json.Marshal(data); e != nil {
- err = errors.Wrap(e, "SendMessage{json marshal error of data}=%v")
- log.Errorf("%v", err)
- continue
- } else {
- if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
- err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
- 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 {
- log.Infof("send msg success%v", string(resp))
- if e := svc.UpdateMessage(val); e != nil {
- err = errors.Wrap(e, "SendMessage{update message error}=%v")
- log.Errorf("%v", err)
- }
- }
- }
- }
- return
- }
|