Skip to content

Commit

Permalink
remove graceful pid
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Apr 22, 2024
1 parent 28b4dd8 commit 0b33e05
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
require (
github.com/coocood/freecache v1.2.4
github.com/fbiville/markdown-table-formatter v0.3.0
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
github.com/go-echarts/statsview v0.3.4
github.com/go-playground/validator v9.31.0+incompatible
github.com/go-spectest/markdown v0.0.7
Expand All @@ -29,6 +28,7 @@ require (
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 // indirect
github.com/go-echarts/go-echarts/v2 v2.3.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
return
}

pkg.GracefulServerWithPid(pkg.NewEcho(f.baseUrl, publicDir, favicon), f.host, f.port)
pkg.StartEcho(pkg.NewEcho(f.baseUrl, publicDir, favicon), f.host, f.port)
}

func SetupFlags() {
Expand Down
25 changes: 3 additions & 22 deletions app/pkg/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package pkg

import (
"embed"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/fvbock/endless"
"github.com/go-echarts/statsview"
"github.com/go-echarts/statsview/viewer"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -39,25 +37,8 @@ func NewEcho(baseURL string, publicDir embed.FS, favicon embed.FS) *echo.Echo {
return e
}

// GracefulServerWithPid reloads server with pid
// kill -HUP when binary is changed
// kill -9 when want to kill the process and make the application dead and want to restart
// kill -9 is NOT FOR FAINT HEARTED and must not be done on prod unless SOUT
func GracefulServerWithPid(e *echo.Echo, host string, port string) {
log := Logger()
server := endless.NewServer(host+":"+port, e)
server.BeforeBegin = func(add string) {
pidFile := filepath.Join(port + ".pid")
_ = os.Remove(pidFile)
err := os.WriteFile(pidFile, []byte(strconv.Itoa(os.Getpid())), 0600)
if err != nil {
log.Error("write pid file error: ", err)
}
log.Info("started server on localhost:", port)
}
if err := server.ListenAndServe(); err != nil {
log.Error("graceful error: ", err)
}
func StartEcho(e *echo.Echo, host string, port string) {
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port)))
}

// HTTPErrorResponse is the response for HTTP errors
Expand Down

0 comments on commit 0b33e05

Please sign in to comment.