Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reload daemon then http server can't listen on other port #64

Open
qianxiansheng90 opened this issue Nov 11, 2022 · 0 comments
Open

reload daemon then http server can't listen on other port #64

qianxiansheng90 opened this issue Nov 11, 2022 · 0 comments

Comments

@qianxiansheng90
Copy link

qianxiansheng90 commented Nov 11, 2022

env:
Linux l-qfy0.dba.dev.cn0 3.10.0-327.28.3.el7.x86_64 #1 SMP Thu Aug 18 19:05:49 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

go version go1.17 linux/amd64

test.go

import (
	"context"
	"flag"
	"fmt"
	"os/exec"
	"strconv"
	"syscall"

	"github.com/fvbock/endless"
	"github.com/gin-gonic/gin"
)

var ctx context.Context
var cancel context.CancelFunc
var reload bool

var port = flag.Int("port", 8080, "")

func main() {
	flag.Parse()
	ListenHTTP(setReload)
}

func setReload() {
	reload = true
}
func ListenHTTP(reloadFunc func()) error {
	var handler = gin.Default()
	handler.GET("/", startDaemon())
	handler.GET("/hello", hello())

	ctx, cancel = context.WithCancel(context.Background())
	defer cancel()
	s := endless.NewServer(":"+strconv.Itoa(*port), handler)
	go func() {
		select {
		case <-ctx.Done():
		}
		if s != nil {
			if err := s.Shutdown(ctx); err != nil {
				fmt.Println(err)
			}
		}
	}()
	s.SignalHooks[endless.PRE_SIGNAL][syscall.SIGHUP] = append(
		s.SignalHooks[endless.PRE_SIGNAL][syscall.SIGHUP],
		reloadFunc)
	if err := s.ListenAndServe(); err != nil {
		return err
	}
	return nil
}
func hello() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("hello ", *port)
	}
}
func startDaemon() gin.HandlerFunc {
	return func(c *gin.Context) {
		start(c)
	}
}

func start(c *gin.Context) (err error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	cmd := exec.CommandContext(ctx, "sh", []string{"start.sh"}...)
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
		return
	}
	return
}

start.sh

nohup /home/qfy/tmp/test --port=8081 > /dev/null 2>&1 &

basedir:/root/tmp

steps:

  1. run test

image

  1. start with new shell, then send http requst,now 8081 is listening

image

  1. kill 8081 ,and reload test

image

4.then send http requst,but 8081 is not listening
image

Is that a bug need to fix ?
And how can I resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant