replace log by zerolog
This commit is contained in:
+21
-22
@@ -12,9 +12,9 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/log/level"
|
||||
"net/smtp"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,17 +25,14 @@ const (
|
||||
|
||||
type Sender struct {
|
||||
smtpConfig cfg.SMTPConfig
|
||||
logger log.Logger
|
||||
// fetch this directory to collect `.json` e-mail format
|
||||
outboxPath string
|
||||
queue *runtime.Queue
|
||||
}
|
||||
|
||||
func NewSender(logger log.Logger, config cfg.SMTPConfig, outboxPath string) Sender {
|
||||
logger = log.With(logger, "actor", "sender")
|
||||
func NewSender(config cfg.SMTPConfig, outboxPath string) Sender {
|
||||
return Sender{
|
||||
smtpConfig: config,
|
||||
logger: logger,
|
||||
outboxPath: outboxPath,
|
||||
queue: runtime.NewQueue(),
|
||||
}
|
||||
@@ -43,29 +40,30 @@ func NewSender(logger log.Logger, config cfg.SMTPConfig, outboxPath string) Send
|
||||
|
||||
func (s Sender) SendMail(email mail.Email) error {
|
||||
auth := smtp.PlainAuth("", s.smtpConfig.User, s.smtpConfig.Password, s.smtpConfig.Url)
|
||||
level.Debug(s.logger).Log("msg", "SMTP authentication succeed")
|
||||
log.Debug().Msg("SMTP authentication succeed")
|
||||
|
||||
if err := smtp.SendMail(s.smtpConfig.GetFullUrl(), auth, email.Sender, email.Receivers, email.Generate()); err != nil {
|
||||
level.Error(s.logger).Log("msg", "error while sending email", "err", err)
|
||||
log.Err(err).Msg("error while sending email")
|
||||
return err
|
||||
}
|
||||
|
||||
level.Debug(s.logger).Log("msg", "mail send successfully")
|
||||
log.Debug().Msg("mail send successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// watchOutbox reads the `outbox` directory every `TickInterval` and put JSON format e-mail in the queue
|
||||
func (s Sender) watchOutbox() {
|
||||
s.logger.Log("msg", "start watching outbox directory", "outbox", s.outboxPath)
|
||||
log.Info().Str("outbox", s.outboxPath).Msg("start watching outbox directory")
|
||||
|
||||
ticker := time.NewTicker(TickerInterval)
|
||||
|
||||
go func() {
|
||||
for _ = range ticker.C {
|
||||
level.Debug(s.logger).Log("action", "retrieving json e-mail format...", "path", s.outboxPath)
|
||||
log.Debug().Str("action", "retrieving json e-mail format...").Str("path", s.outboxPath)
|
||||
|
||||
files, err := os.ReadDir(s.outboxPath)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
level.Error(s.logger).Log("msg", "outbox directory does not exist", "err", err)
|
||||
log.Err(err).Msg("outbox directory does not exist")
|
||||
s.queue.Shutdown()
|
||||
}
|
||||
|
||||
@@ -75,7 +73,8 @@ func (s Sender) watchOutbox() {
|
||||
s.queue.Add(path.Join(s.outboxPath, filename))
|
||||
continue
|
||||
}
|
||||
level.Debug(s.logger).Log("msg", "incorrect suffix", "filename", filename)
|
||||
|
||||
log.Debug().Str("filename", filename).Msg("incorrect suffix")
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -91,17 +90,18 @@ func (s Sender) processNextEmail() bool {
|
||||
|
||||
path, ok := item.(string)
|
||||
if !ok {
|
||||
level.Error(s.logger).Log("msg", "unable to cast queue item into mail.Email", "item", item)
|
||||
log.Error().Any("item", item).Msg("unable to cast queue item into mail.Email")
|
||||
return true
|
||||
}
|
||||
|
||||
email, err := mail.FromJSON(path)
|
||||
if err != nil {
|
||||
level.Error(s.logger).Log("msg", "unable to parse JSON email", "path", path, "err", err)
|
||||
log.Err(err).Str("path", path).Msg("unable to parse JSON email")
|
||||
|
||||
// if JSON parsing failed the `path` is renamed with an error suffix to avoid enqueued it again
|
||||
newPath := fmt.Sprintf("%s%s", path, ErrorSuffix)
|
||||
if err := os.Rename(path, newPath); err != nil {
|
||||
level.Error(s.logger).Log("msg", "unable to rename bad JSON email path", "path", path, "newPath", newPath)
|
||||
log.Err(err).Str("path", path).Str("new path", newPath).Msg("unable to rename bad JSON email path")
|
||||
s.queue.Shutdown()
|
||||
}
|
||||
return true
|
||||
@@ -113,7 +113,7 @@ func (s Sender) processNextEmail() bool {
|
||||
if err := os.Remove(path); err != nil {
|
||||
// this is a fatal error, can't send same e-mail indefinitely
|
||||
if !os.IsExist(err) {
|
||||
level.Error(s.logger).Log("msg", "unable to remove the JSON email", "path", path, "err", err)
|
||||
log.Err(err).Str("path", path).Msg("unable to remove the JSON email")
|
||||
s.queue.Shutdown()
|
||||
}
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func (s Sender) run() <-chan struct{} {
|
||||
// Run launches the queue processing and the outbox watcher
|
||||
// catches `SIGINT` and `SIGTERM` to properly stopped the queue
|
||||
func (s Sender) Run() {
|
||||
s.logger.Log("msg", "sender service is running")
|
||||
log.Info().Msg("sender service is running")
|
||||
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||
@@ -145,12 +145,11 @@ func (s Sender) Run() {
|
||||
|
||||
select {
|
||||
case <-sigCh:
|
||||
s.logger.Log("msg", "stop signal received, stopping e-mail queue...")
|
||||
log.Warn().Msg("stop signal received, stopping e-mail queue...")
|
||||
s.queue.Shutdown()
|
||||
case <-queueCh:
|
||||
s.logger.Log("msg", "e-mail queue stopped successfully")
|
||||
|
||||
log.Info().Msg("e-mail queue stopped successfully")
|
||||
}
|
||||
|
||||
s.logger.Log("msg", "sender service stopped successfully")
|
||||
log.Info().Msg("sender service stopped successfully")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user