From 56eda6bbdfd2674235e202e950b63159cf8d49a9 Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Mon, 18 Jan 2021 12:27:13 -0800 Subject: [PATCH] Telemetry --- server/server/server.go | 19 ++++++++++++++++--- server/web/webserver.go | 27 +++++++++++++++------------ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/server/server/server.go b/server/server/server.go index 8b0027cdd..a72938c0a 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "runtime" + "sync" "time" "github.com/google/uuid" @@ -21,6 +22,7 @@ import ( "github.com/mattermost/mattermost-octo-tasks/server/services/webhook" "github.com/mattermost/mattermost-octo-tasks/server/web" "github.com/mattermost/mattermost-octo-tasks/server/ws" + "github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/services/filesstore" ) @@ -132,15 +134,24 @@ func New(cfg *config.Configuration, singleUser bool) (*Server, error) { } func (s *Server) Start() error { - if err := s.webServer.Start(); err != nil { - return err - } + httpServerExitDone := &sync.WaitGroup{} + httpServerExitDone.Add(1) + + s.webServer.Start(httpServerExitDone) + s.cleanUpSessionsTask = scheduler.CreateRecurringTask("cleanUpSessions", func() { if err := s.store.CleanUpSessions(s.config.SessionExpireTime); err != nil { s.logger.Error("Unable to clean up the sessions", zap.Error(err)) } }, 10*time.Minute) + if s.config.Telemetry { + firstRun := utils.MillisFromTime(time.Now()) + s.telemetry.RunTelemetryJob(firstRun) + } + + httpServerExitDone.Wait() + return nil } @@ -153,6 +164,8 @@ func (s *Server) Shutdown() error { s.cleanUpSessionsTask.Cancel() } + s.telemetry.Shutdown() + return s.store.Shutdown() } diff --git a/server/web/webserver.go b/server/web/webserver.go index d89f620d6..0fd3ddc37 100644 --- a/server/web/webserver.go +++ b/server/web/webserver.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "sync" "github.com/gorilla/mux" ) @@ -62,27 +63,29 @@ func (ws *Server) registerRoutes() { } // Start runs the web server and start listening for charsetnnections. -func (ws *Server) Start() error { +func (ws *Server) Start(wg *sync.WaitGroup) { ws.registerRoutes() isSSL := ws.ssl && fileExists("./cert/cert.pem") && fileExists("./cert/key.pem") if isSSL { log.Printf("https server started on :%d\n", ws.port) - err := ws.ListenAndServeTLS("./cert/cert.pem", "./cert/key.pem") - if err != nil { - return err - } + go func() { + defer wg.Done() + if err := ws.ListenAndServeTLS("./cert/cert.pem", "./cert/key.pem"); err != nil { + log.Fatalf("ListenAndServeTLS: %v", err) + } + }() - return nil + return } log.Printf("http server started on :%d\n", ws.port) - err := ws.ListenAndServe() - if err != nil { - return err - } - - return nil + go func() { + defer wg.Done() + if err := ws.ListenAndServe(); err != http.ErrServerClosed { + log.Fatalf("ListenAndServe: %v", err) + } + }() } func (ws *Server) Shutdown() error {