stop the service properly and update doc
This commit is contained in:
+29
-5
@@ -5,8 +5,10 @@ import (
|
||||
"mailsrv/mail"
|
||||
"mailsrv/runtime"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
@@ -50,7 +52,7 @@ func (s Sender) SendMail(email mail.Email) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// every `TickerInterval` reads the `outbox` directory and put JSON format e-mail in the queue
|
||||
// watchOutbox reads the `outbox` directory every `TickInterval` and put JSON format e-mail in the queue
|
||||
func (s Sender) watchOutbox() {
|
||||
ticker := time.NewTicker(TickerInterval)
|
||||
go func() {
|
||||
@@ -75,8 +77,8 @@ func (s Sender) watchOutbox() {
|
||||
}()
|
||||
}
|
||||
|
||||
// loops over the queue and send email
|
||||
func (s Sender) processNextMail() bool {
|
||||
// processNextEmail loops over the queue and send email
|
||||
func (s Sender) processNextEmail() bool {
|
||||
item, quit := s.queue.Get()
|
||||
if quit {
|
||||
return false
|
||||
@@ -104,10 +106,32 @@ func (s Sender) processNextMail() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// run starts processing the queue
|
||||
func (s Sender) run() <-chan struct{} {
|
||||
queueCh := make(chan struct{})
|
||||
go func() {
|
||||
for s.processNextEmail() {
|
||||
}
|
||||
queueCh <- struct{}{}
|
||||
}()
|
||||
return queueCh
|
||||
}
|
||||
|
||||
// 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")
|
||||
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
s.watchOutbox()
|
||||
for s.processNextMail() {
|
||||
}
|
||||
queueCh := s.run()
|
||||
|
||||
<-sigCh
|
||||
s.logger.Log("msg", "stop signal received, stopping e-mail queue...")
|
||||
s.queue.Shutdown()
|
||||
|
||||
<-queueCh
|
||||
s.logger.Log("msg", "sender service stopped successfully")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user