notice.go 1013 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package notice
  2. import (
  3. log "app.yhyue.com/moapp/jylog"
  4. "bp.jydev.jianyu360.cn/BP/jynsq/gonsq"
  5. "encoding/json"
  6. )
  7. type NoticeConfig struct {
  8. IsOpen bool //是否开启提示
  9. IsJsonEncode bool
  10. Address, TopPic string
  11. Id, Title, Text string
  12. }
  13. type Notice struct {
  14. p *gonsq.Producer
  15. c NoticeConfig
  16. }
  17. func NewNotice(c NoticeConfig) (n *Notice, err error) {
  18. if c.IsOpen {
  19. n = &Notice{
  20. c: c,
  21. }
  22. var producer *gonsq.Producer
  23. producer, err = gonsq.NewProducer(c.Address, c.TopPic, c.IsJsonEncode)
  24. if err != nil {
  25. return
  26. }
  27. n.p = producer
  28. return n, err
  29. }
  30. return nil, nil
  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. log.Debugf("模拟发送信息----->%s", string(bs))
  43. if err := n.p.Publish(bs); err != nil {
  44. log.Errorf("nsq连接失败 %v", err)
  45. }
  46. return nil
  47. }