dao.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package email
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/job/main/aegis/conf"
  6. "go-common/app/job/main/aegis/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "gopkg.in/gomail.v2"
  10. "time"
  11. )
  12. type Dao struct {
  13. c *conf.Config
  14. redis *redis.Pool
  15. email *gomail.Dialer
  16. }
  17. const (
  18. // MoniEmailKey 监控邮件队列key
  19. MoniEmailKey = "monitor_stats_email"
  20. )
  21. // New is new redis dao.
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. c: c,
  25. email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
  26. redis: redis.NewPool(c.Redis),
  27. }
  28. return d
  29. }
  30. // MonitorEmailAsync 异步发送监控邮件
  31. func (d *Dao) MonitorEmailAsync(c context.Context, members []string, title, content string) (err error) {
  32. var (
  33. conn = d.redis.Get(c)
  34. bs []byte
  35. )
  36. defer conn.Close()
  37. temp := &model.MoniTemp{
  38. From: d.c.Mail.Username,
  39. Members: members,
  40. Subject: title,
  41. Body: content,
  42. }
  43. if bs, err = json.Marshal(temp); err != nil {
  44. log.Error("d.MonitorEmailAsync() json.Marshal(%+v) error(%v) key(%s)", temp, err, MoniEmailKey)
  45. return
  46. }
  47. if _, err = conn.Do("RPUSH", MoniEmailKey, bs); err != nil {
  48. log.Error("d.MonitorEmailAsync() conn.Do(RPUSH, %s, %s) error(%v)", MoniEmailKey, bs, err)
  49. }
  50. return
  51. }
  52. // MonitorEmailProc 发送监控邮件
  53. func (d *Dao) MonitorEmailProc() (err error) {
  54. var (
  55. bs []byte
  56. temp *model.MoniTemp
  57. headers map[string][]string
  58. )
  59. headers = make(map[string][]string)
  60. bs, err = d.PopRedis(context.TODO(), MoniEmailKey)
  61. if err != nil || bs == nil {
  62. log.Warn("d.MonitorEmailProc() warn:%v content:%s", err, bs)
  63. time.Sleep(5 * time.Second)
  64. return
  65. }
  66. if err = json.Unmarshal(bs, &temp); err != nil {
  67. log.Error("d.MonitorEmailProc() json.unmarshal error(%v) content(%s)", err, bs)
  68. return
  69. }
  70. msg := gomail.NewMessage()
  71. headers["From"] = []string{d.c.Mail.Username}
  72. headers["To"] = temp.Members
  73. headers["Subject"] = []string{temp.Subject}
  74. msg.SetHeaders(headers)
  75. msg.SetBody("text/html", temp.Body)
  76. if err = d.email.DialAndSend(msg); err != nil {
  77. log.Error("d.email.DialAndSend(%+v) error:%v", msg, err)
  78. return
  79. }
  80. return
  81. }
  82. // PopRedis lpop fail item from redis
  83. func (d *Dao) PopRedis(c context.Context, key string) (bs []byte, err error) {
  84. var conn = d.redis.Get(c)
  85. defer conn.Close()
  86. if bs, err = redis.Bytes(conn.Do("LPOP", key)); err != nil && err != redis.ErrNil {
  87. log.Error("d.PopRedis() redis.Bytes(conn.Do(LPOP, %s)) error(%v)", key, err)
  88. }
  89. return
  90. }