Add gracefull shutdown to explorer api (#244)

This commit is contained in:
walker-16 2023-04-19 14:30:46 -03:00 committed by GitHub
parent 4f438ae188
commit ce83e1c3a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 1 deletions

View File

@ -6,7 +6,9 @@ import (
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/adaptor/v2"
@ -157,7 +159,27 @@ func main() {
})
}))
rootLogger.Fatal("http listen", zap.Error(app.Listen(":"+strconv.Itoa(cfg.PORT))))
go func() {
if err := app.Listen(":" + strconv.Itoa(cfg.PORT)); err != nil {
rootLogger.Error("http listen", zap.Error(err))
panic(err)
}
}()
// Waiting for signal
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
select {
case <-appCtx.Done():
rootLogger.Warn("terminating with root context cancelled.")
case signal := <-sigterm:
rootLogger.Info("terminating with signal.", zap.String("signal", signal.String()))
}
rootLogger.Info("cleanup tasks...")
rootLogger.Info("shutdown server...")
app.Shutdown()
rootLogger.Info("finished successfully wormhole api")
}
// NewCache return a CacheGetFunc to get a value by a Key from cache.