1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 06:30:55 +00:00
Beego/reload.go

165 lines
3.4 KiB
Go
Raw Normal View History

2013-06-07 09:48:04 +00:00
package beego
import (
"errors"
"fmt"
"log"
"net"
"os"
"os/exec"
"os/signal"
"reflect"
"strconv"
"sync"
2013-12-07 08:52:39 +00:00
//"syscall"
2013-06-07 09:48:04 +00:00
)
const (
// An environment variable when restarting application http listener.
2013-06-07 09:48:04 +00:00
FDKey = "BEEGO_HOT_FD"
)
// Export an error equivalent to net.errClosing for use with Accept during
// a graceful exit.
var ErrClosing = errors.New("use of closed network connection")
var ErrInitStart = errors.New("init from")
// Allows for us to notice when the connection is closed.
type conn struct {
net.Conn
2013-06-26 14:13:25 +00:00
wg *sync.WaitGroup
isclose bool
2013-06-26 14:16:03 +00:00
lock sync.Mutex
2013-06-07 09:48:04 +00:00
}
// Close current processing connection.
2013-06-07 09:48:04 +00:00
func (c conn) Close() error {
2013-06-26 14:16:03 +00:00
c.lock.Lock()
defer c.lock.Unlock()
2013-06-07 09:48:04 +00:00
err := c.Conn.Close()
2013-06-26 15:34:32 +00:00
if !c.isclose && err == nil {
2013-06-26 14:13:25 +00:00
c.wg.Done()
c.isclose = true
}
2013-06-07 09:48:04 +00:00
return err
}
type stoppableListener struct {
net.Listener
count int64
stopped bool
wg sync.WaitGroup
}
var theStoppable *stoppableListener
func newStoppable(l net.Listener) (sl *stoppableListener) {
sl = &stoppableListener{Listener: l}
// this goroutine monitors the channel. Can't do this in
// Accept (below) because once it enters sl.Listener.Accept()
// it blocks. We unblock it by closing the fd it is trying to
// accept(2) on.
go func() {
WaitSignal(l)
sl.stopped = true
sl.Listener.Close()
}()
return
}
// Set stopped Listener to accept requests again.
// it returns the accepted and closable connection or error.
2013-06-07 09:48:04 +00:00
func (sl *stoppableListener) Accept() (c net.Conn, err error) {
c, err = sl.Listener.Accept()
if err != nil {
return
}
sl.wg.Add(1)
// Wrap the returned connection, so that we can observe when
// it is closed.
c = conn{Conn: c, wg: &sl.wg}
return
}
// Listener waits signal to kill or interrupt then restart.
2013-06-07 09:48:04 +00:00
func WaitSignal(l net.Listener) error {
ch := make(chan os.Signal, 1)
2013-12-07 08:52:39 +00:00
signal.Notify(ch, os.Interrupt, os.Kill)
2013-06-07 09:48:04 +00:00
for {
sig := <-ch
log.Println(sig.String())
switch sig {
2013-12-07 08:52:39 +00:00
case os.Kill:
2013-06-07 09:48:04 +00:00
return nil
2013-12-07 08:52:39 +00:00
case os.Interrupt:
2013-06-07 09:48:04 +00:00
err := Restart(l)
if nil != err {
return err
}
return nil
}
}
return nil // It'll never get here.
}
// Kill current running os process.
2013-06-07 09:48:04 +00:00
func CloseSelf() error {
ppid := os.Getpid()
if ppid == 1 { // init provided sockets, for example systemd
return nil
}
p, err := os.FindProcess(ppid)
if err != nil {
return err
}
return p.Kill()
}
// Re-exec this image without dropping the listener passed to this function.
func Restart(l net.Listener) error {
argv0, err := exec.LookPath(os.Args[0])
if nil != err {
return err
}
wd, err := os.Getwd()
if nil != err {
return err
}
v := reflect.ValueOf(l).Elem().FieldByName("fd").Elem()
fd := uintptr(v.FieldByName("sysfd").Int())
allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr},
os.NewFile(fd, string(v.FieldByName("sysfile").String())))
p, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
Dir: wd,
Env: append(os.Environ(), fmt.Sprintf("%s=%d", FDKey, fd)),
Files: allFiles,
})
if nil != err {
return err
}
log.Printf("spawned child %d\n", p.Pid)
return nil
}
// Get current net.Listen in running process.
func GetInitListener(tcpaddr *net.TCPAddr) (l net.Listener, err error) {
2013-06-07 09:48:04 +00:00
countStr := os.Getenv(FDKey)
if countStr == "" {
return net.ListenTCP("tcp", tcpaddr)
}
2013-06-25 13:40:42 +00:00
count, err := strconv.Atoi(countStr)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(count), "listen socket")
l, err = net.FileListener(f)
if err != nil {
return nil, err
}
return l, nil
2013-06-07 09:48:04 +00:00
}