Skip to content
Snippets Groups Projects
Commit 8dacb768 authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

:loud_sound: Add error handling to analysis http server"

parent fc01c115
No related branches found
No related tags found
No related merge requests found
package httpserver package httpserver
import ( import (
"errors"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/iotaledger/goshimmer/packages/shutdown" "github.com/iotaledger/goshimmer/packages/shutdown"
"github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/logger"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
var ( var (
log *logger.Logger
httpServer *http.Server httpServer *http.Server
router *http.ServeMux router *http.ServeMux
) )
func Configure(plugin *node.Plugin) { const name = "Analysis HTTP Server"
func Configure() {
log = logger.NewLogger(name)
router = http.NewServeMux() router = http.NewServeMux()
httpServer = &http.Server{Addr: ":80", Handler: router} httpServer = &http.Server{Addr: ":80", Handler: router}
...@@ -24,12 +31,30 @@ func Configure(plugin *node.Plugin) { ...@@ -24,12 +31,30 @@ func Configure(plugin *node.Plugin) {
router.HandleFunc("/", index) router.HandleFunc("/", index)
} }
func Run(plugin *node.Plugin) { func Run() {
daemon.BackgroundWorker("Analysis HTTP Server", func(shutdownSignal <-chan struct{}) { if err := daemon.BackgroundWorker(name, start, shutdown.ShutdownPriorityAnalysis); err != nil {
go httpServer.ListenAndServe() log.Errorf("Error starting as daemon: %s", err)
<-shutdownSignal }
ctx, cancel := context.WithTimeout(context.Background(), 0*time.Second) }
defer cancel()
httpServer.Shutdown(ctx) func start(shutdownSignal <-chan struct{}) {
}, shutdown.ShutdownPriorityAnalysis) var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
log.Infof(name+" started: address=%s", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Warnf("Error listening: %s", err)
}
}()
<-shutdownSignal
log.Info("Stopping " + name + " ...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Errorf("Error closing: %s", err)
}
wg.Wait()
log.Info("Stopping " + name + " ... done")
} }
...@@ -7,10 +7,10 @@ import ( ...@@ -7,10 +7,10 @@ import (
) )
func Configure(plugin *node.Plugin) { func Configure(plugin *node.Plugin) {
httpserver.Configure(plugin) httpserver.Configure()
recordedevents.Configure(plugin) recordedevents.Configure(plugin)
} }
func Run(plugin *node.Plugin) { func Run(plugin *node.Plugin) {
httpserver.Run(plugin) httpserver.Run()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment