stop the service properly and update doc
This commit is contained in:
parent
42b36a9234
commit
8b0af491c0
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// mandatory parameters for the STMP server connection
|
||||
// SMTPConfig handles mandatory parameters for the STMP server connection
|
||||
type SMTPConfig struct {
|
||||
User string
|
||||
Password string
|
||||
|
||||
@ -40,10 +40,10 @@ func FromJSON(path string) (Email, error) {
|
||||
|
||||
func (e Email) Generate() []byte {
|
||||
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, ","),
|
||||
e.Sender,
|
||||
e.Sender,
|
||||
e.Subject,
|
||||
e.Content,
|
||||
)
|
||||
return []byte(mail)
|
||||
|
||||
6
main.go
6
main.go
@ -17,7 +17,7 @@ const (
|
||||
DefaultOutboxPath string = "outbox"
|
||||
)
|
||||
|
||||
// simply collects binary arguments
|
||||
// GetConfigPath simply collects binary arguments
|
||||
func GetConfigPath() (string, error) {
|
||||
switch len(os.Args) {
|
||||
case 1:
|
||||
@ -43,7 +43,7 @@ func LoadIni() (*ini.File, error) {
|
||||
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) {
|
||||
var config cfg.SMTPConfig
|
||||
|
||||
@ -61,7 +61,7 @@ func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) {
|
||||
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
|
||||
func GetOutboxPath(iniFile *ini.File) (string, error) {
|
||||
outboxPath := iniFile.Section("service").Key("outbox_path").String()
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user