close notifier in shutdown sequence

This commit is contained in:
Umputun
2018-11-04 11:51:08 -06:00
parent 3747eb27fd
commit 0f3243e19b
5 changed files with 21 additions and 13 deletions
+12 -8
View File
@@ -141,13 +141,14 @@ type SSLGroup struct {
// serverApp holds all active objects
type serverApp struct {
*ServerCommand
restSrv *api.Rest
migratorSrv *api.Migrator
exporter migrator.Exporter
devAuth *auth.DevAuthServer
dataService *service.DataStore
avatarStore avatar.Store
terminated chan struct{}
restSrv *api.Rest
migratorSrv *api.Migrator
exporter migrator.Exporter
devAuth *auth.DevAuthServer
dataService *service.DataStore
avatarStore avatar.Store
notifyService *notify.Service
terminated chan struct{}
}
// Execute is the entry point for "server" command, called by flag parser
@@ -288,6 +289,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
devAuth: devAuth,
dataService: dataService,
avatarStore: avatarStore,
notifyService: notifyService,
terminated: make(chan struct{}),
}, nil
}
@@ -301,6 +303,7 @@ func (a *serverApp) run(ctx context.Context) error {
go func() {
// shutdown on context cancellation
<-ctx.Done()
log.Print("[INFO] shutdown initiated")
a.restSrv.Shutdown()
if a.devAuth != nil {
a.devAuth.Shutdown()
@@ -311,7 +314,8 @@ func (a *serverApp) run(ctx context.Context) error {
if e := a.avatarStore.Close(); e != nil {
log.Printf("[WARN] failed to close avatar store, %s", e)
}
a.notifyService.Close()
log.Print("[INFO] shutdown completed")
}()
a.activateBackup(ctx) // runs in goroutine for each site
if a.Auth.Dev {
+2 -2
View File
@@ -192,7 +192,7 @@ func TestServerApp_Shutdown(t *testing.T) {
func TestServerApp_MainSignal(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
require.Nil(t, err)
}()
@@ -203,7 +203,7 @@ func TestServerApp_MainSignal(t *testing.T) {
p := flags.NewParser(&s, flags.Default)
args := []string{"test", "--store.bolt.path=/tmp/xyz", "--backup=/tmp", "--avatar.type=bolt",
"--avatar.bolt.file=/tmp/ava-test.db", "--port=18100"}
"--avatar.bolt.file=/tmp/ava-test.db", "--port=18100", "--notify.type=none"}
defer os.Remove("/tmp/ava-test.db")
_, err := p.ParseArgs(args)
require.Nil(t, err)
+2 -2
View File
@@ -17,7 +17,7 @@ import (
func TestMain(t *testing.T) {
os.Args = []string{"test", "server", "--secret=123456", "--store.bolt.path=/tmp/xyz", "--backup=/tmp",
"--avatar.fs.path=/tmp", "--port=18202", "--url=https://demo.remark42.com", "--dbg"}
"--avatar.fs.path=/tmp", "--port=18202", "--url=https://demo.remark42.com", "--dbg", "--notify.type=none"}
go func() {
time.Sleep(500 * time.Millisecond)
@@ -50,7 +50,7 @@ func TestMain(t *testing.T) {
func TestMain_SSLStaticMode(t *testing.T) {
os.Args = []string{"test", "server", "--secret=123456", "--store.bolt.path=/tmp/xyz", "--backup=/tmp",
"--avatar.fs.path=/tmp", "--port=18080", "--url=https://localhost:18443", "--dbg",
"--avatar.fs.path=/tmp", "--port=18080", "--url=https://localhost:18443", "--dbg", "--notify.type=none",
"--ssl.type=static", "--ssl.cert=testdata/cert.pem", "--ssl.key=testdata/key.pem", "--ssl.port=18443"}
go func() {
+2
View File
@@ -77,6 +77,7 @@ func (s *Service) Submit(comment store.Comment) {
// Close queue channel and wait for completion
func (s *Service) Close() {
if s.queue != nil {
log.Print("[DEBUG] close notifier")
close(s.queue)
s.cancel()
<-s.ctx.Done()
@@ -98,6 +99,7 @@ func (s *Service) do() {
}
wg.Wait()
}
log.Print("[WARN] terminated notifier")
}
// NopService is do-nothing notifier, without destinations
+3 -1
View File
@@ -27,7 +27,7 @@ const telegramAPIPrefix = "https://api.telegram.org/bot"
// NewTelegram makes telegram bot for notifications
func NewTelegram(token string, channelName string, timeout time.Duration, api string) (*Telegram, error) {
log.Printf("[DEBUG] create new telegram notifier for cham %s, timeout=%s, api=%s", channelName, timeout, api)
res := Telegram{channelName: channelName, token: token, apiPrefix: api, timeout: timeout}
res.channelName = strings.TrimPrefix(res.channelName, "@")
if res.apiPrefix == "" {
@@ -36,6 +36,8 @@ func NewTelegram(token string, channelName string, timeout time.Duration, api st
if res.timeout == 0 {
res.timeout = telegramTimeOut
}
log.Printf("[DEBUG] create new telegram notifier for cham %s, timeout=%s, api=%s", channelName, res.timeout, res.timeout)
client := http.Client{Timeout: telegramTimeOut}
resp, err := client.Get(fmt.Sprintf("%s%s/getMe", res.apiPrefix, token))
if err != nil {