grace.Process for more options and control
This commit is contained in:
parent
e79c66960f
commit
4636466d77
34
grace.go
34
grace.go
@ -136,8 +136,11 @@ func (l *listener) Accept() (net.Conn, error) {
|
|||||||
return conn{Conn: c, wg: &l.wg}, nil
|
return conn{Conn: c, wg: &l.wg}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Process struct {
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for signals to gracefully terminate or restart the process.
|
// Wait for signals to gracefully terminate or restart the process.
|
||||||
func Wait(listeners []Listener) (err error) {
|
func (p *Process) Wait(listeners []Listener) (err error) {
|
||||||
ch := make(chan os.Signal, 2)
|
ch := make(chan os.Signal, 2)
|
||||||
signal.Notify(ch, syscall.SIGTERM, syscall.SIGUSR2)
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGUSR2)
|
||||||
for {
|
for {
|
||||||
@ -168,7 +171,7 @@ func Wait(listeners []Listener) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to inherit listeners from the parent process.
|
// Try to inherit listeners from the parent process.
|
||||||
func Inherit() (listeners []Listener, err error) {
|
func (p *Process) Inherit() (listeners []Listener, err error) {
|
||||||
countStr := os.Getenv(envCountKey)
|
countStr := os.Getenv(envCountKey)
|
||||||
if countStr == "" {
|
if countStr == "" {
|
||||||
return nil, ErrNotInheriting
|
return nil, ErrNotInheriting
|
||||||
@ -193,7 +196,7 @@ func Inherit() (listeners []Listener, err error) {
|
|||||||
|
|
||||||
// Start the Close process in the parent. This does not wait for the
|
// Start the Close process in the parent. This does not wait for the
|
||||||
// parent to close and simply sends it the TERM signal.
|
// parent to close and simply sends it the TERM signal.
|
||||||
func CloseParent() error {
|
func (p *Process) CloseParent() error {
|
||||||
ppid := os.Getppid()
|
ppid := os.Getppid()
|
||||||
if ppid == 1 { // init provided sockets, for example systemd
|
if ppid == 1 { // init provided sockets, for example systemd
|
||||||
return nil
|
return nil
|
||||||
@ -202,7 +205,7 @@ func CloseParent() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restart the process passing the given listeners to the new process.
|
// Restart the process passing the given listeners to the new process.
|
||||||
func Restart(listeners []Listener) (err error) {
|
func (p *Process) Restart(listeners []Listener) (err error) {
|
||||||
if len(listeners) == 0 {
|
if len(listeners) == 0 {
|
||||||
return errors.New("restart must be given listeners.")
|
return errors.New("restart must be given listeners.")
|
||||||
}
|
}
|
||||||
@ -248,3 +251,26 @@ func Restart(listeners []Listener) (err error) {
|
|||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultProcess = &Process{}
|
||||||
|
|
||||||
|
// Wait for signals to gracefully terminate or restart the process.
|
||||||
|
func Wait(listeners []Listener) (err error) {
|
||||||
|
return defaultProcess.Wait(listeners)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to inherit listeners from the parent process.
|
||||||
|
func Inherit() (listeners []Listener, err error) {
|
||||||
|
return defaultProcess.Inherit()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the Close process in the parent. This does not wait for the
|
||||||
|
// parent to close and simply sends it the TERM signal.
|
||||||
|
func CloseParent() error {
|
||||||
|
return defaultProcess.CloseParent()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart the process passing the given listeners to the new process.
|
||||||
|
func Restart(listeners []Listener) (err error) {
|
||||||
|
return defaultProcess.Restart(listeners)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user