notice.go 998 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package logs
  2. import (
  3. "bp.jydev.jianyu360.cn/BP/jynsq/gonsq"
  4. "encoding/json"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "log"
  7. )
  8. type NoticeConfig struct {
  9. IsOpen bool //是否开启提示
  10. IsJsonEncode bool
  11. Address, TopPic string
  12. Id, Title, Text string
  13. }
  14. type Notice struct {
  15. p *gonsq.Producer
  16. c NoticeConfig
  17. }
  18. func newNotice(c NoticeConfig) (n *Notice, err error) {
  19. n = &Notice{
  20. c: c,
  21. }
  22. var producer *gonsq.Producer
  23. if c.IsOpen {
  24. producer, err = gonsq.NewProducer(c.Address, c.TopPic, c.IsJsonEncode)
  25. if err != nil {
  26. return
  27. }
  28. n.p = producer
  29. }
  30. return
  31. }
  32. // SendWarnMsg 发送异常通知
  33. func (n *Notice) SendWarnMsg(detail map[string]interface{}) error {
  34. if n.p == nil {
  35. return nil
  36. }
  37. m := &gonsq.Msg{n.c.Id, n.c.Title, n.c.Text, detail}
  38. bs, err := json.Marshal(m)
  39. if err != nil {
  40. return err
  41. }
  42. GInfo.Debugf(gctx.New(), "模拟发送信息----->", string(bs))
  43. if err := n.p.Publish(bs); err != nil {
  44. log.Println("nsq连接失败", err)
  45. }
  46. return nil
  47. }