stop the service properly and update doc

This commit is contained in:
landrigun 2022-10-16 10:35:59 +00:00
parent 42b36a9234
commit 8b0af491c0
4 changed files with 35 additions and 11 deletions

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
) )
// mandatory parameters for the STMP server connection // SMTPConfig handles mandatory parameters for the STMP server connection
type SMTPConfig struct { type SMTPConfig struct {
User string User string
Password string Password string

View File

@ -40,10 +40,10 @@ func FromJSON(path string) (Email, error) {
func (e Email) Generate() []byte { func (e Email) Generate() []byte {
mail := fmt.Sprintf( mail := fmt.Sprintf(
"To: %s\nFrom: %s\nContent-Type: text/html\nSubject: %s\n\n%s", "To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s",
strings.Join(e.Receivers, ","), strings.Join(e.Receivers, ","),
e.Sender, e.Sender,
e.Sender, e.Subject,
e.Content, e.Content,
) )
return []byte(mail) return []byte(mail)

View File

@ -17,7 +17,7 @@ const (
DefaultOutboxPath string = "outbox" DefaultOutboxPath string = "outbox"
) )
// simply collects binary arguments // GetConfigPath simply collects binary arguments
func GetConfigPath() (string, error) { func GetConfigPath() (string, error) {
switch len(os.Args) { switch len(os.Args) {
case 1: case 1:
@ -43,7 +43,7 @@ func LoadIni() (*ini.File, error) {
return ini, nil return ini, nil
} }
// collects mandatory SMTP parameters to send an e-mail from the `.ini` file // LoadSMTPConfig collects mandatory SMTP parameters to send an e-mail from the `.ini` file
func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) { func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) {
var config cfg.SMTPConfig var config cfg.SMTPConfig
@ -61,7 +61,7 @@ func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) {
return config, nil return config, nil
} }
// tries to get the outbox path from the `.ini` file and creates the directory // GetOutBoxPath tries to get the outbox path from the `.ini` file and creates the directory
// if the path does not exist, create a default one: `outbox` next to the binary // if the path does not exist, create a default one: `outbox` next to the binary
func GetOutboxPath(iniFile *ini.File) (string, error) { func GetOutboxPath(iniFile *ini.File) (string, error) {
outboxPath := iniFile.Section("service").Key("outbox_path").String() outboxPath := iniFile.Section("service").Key("outbox_path").String()

View File

@ -5,8 +5,10 @@ import (
"mailsrv/mail" "mailsrv/mail"
"mailsrv/runtime" "mailsrv/runtime"
"os" "os"
"os/signal"
"path" "path"
"strings" "strings"
"syscall"
"time" "time"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
@ -50,7 +52,7 @@ func (s Sender) SendMail(email mail.Email) error {
return nil 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() { func (s Sender) watchOutbox() {
ticker := time.NewTicker(TickerInterval) ticker := time.NewTicker(TickerInterval)
go func() { go func() {
@ -75,8 +77,8 @@ func (s Sender) watchOutbox() {
}() }()
} }
// loops over the queue and send email // processNextEmail loops over the queue and send email
func (s Sender) processNextMail() bool { func (s Sender) processNextEmail() bool {
item, quit := s.queue.Get() item, quit := s.queue.Get()
if quit { if quit {
return false return false
@ -104,10 +106,32 @@ func (s Sender) processNextMail() bool {
return true 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() { func (s Sender) Run() {
s.logger.Log("msg", "sender service is running") s.logger.Log("msg", "sender service is running")
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
s.watchOutbox() 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")
} }