diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 6ebf7ea0..dbc34ede 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -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 { diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index 053be5c5..5b8ce9ab 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -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) diff --git a/backend/app/main_test.go b/backend/app/main_test.go index 4eb6e344..cc425307 100644 --- a/backend/app/main_test.go +++ b/backend/app/main_test.go @@ -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() { diff --git a/backend/app/notify/notify.go b/backend/app/notify/notify.go index 77c44d87..5bfff2a8 100644 --- a/backend/app/notify/notify.go +++ b/backend/app/notify/notify.go @@ -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 diff --git a/backend/app/notify/telegram.go b/backend/app/notify/telegram.go index ed4fc89a..de1331b1 100644 --- a/backend/app/notify/telegram.go +++ b/backend/app/notify/telegram.go @@ -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 {