代码拉取完成,页面将自动刷新
// +build linux
package kprocess
import (
"fmt"
"gitee.com/kelvins-io/kelvins/internal/logging"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/cloudflare/tableflip"
)
type KProcess struct {
pidFile string
pid int
processUp *tableflip.Upgrader
}
// This shows how to use the upgrader
// with the graceful shutdown facilities of net/http.
func (k *KProcess) Listen(network, addr, pidFile string) (ln net.Listener, err error) {
k.pid = os.Getpid()
logging.Infof(fmt.Sprintf("exec process pid %d \n", k.pid))
k.processUp, err = tableflip.New(tableflip.Options{
UpgradeTimeout: 5 * time.Second,
PIDFile: pidFile,
})
if err != nil {
return nil, err
}
k.pidFile = pidFile
go k.signal(k.upgrade, k.stop)
// Listen must be called before Ready
if network != "" && addr != "" {
ln, err = k.processUp.Listen(network, addr)
if err != nil {
return nil, err
}
}
if err := k.processUp.Ready(); err != nil {
return nil, err
}
return ln, nil
}
func (k *KProcess) stop() error {
if k.processUp != nil {
k.processUp.Stop()
return os.Remove(k.pidFile)
}
return nil
}
func (k *KProcess) upgrade() error {
if k.processUp != nil {
return k.processUp.Upgrade()
}
return nil
}
func (k *KProcess) Exit() <-chan struct{} {
if k.processUp != nil {
return k.processUp.Exit()
}
ch := make(chan struct{})
close(ch)
return ch
}
func (k *KProcess) signal(upgradeFunc, stopFunc func() error) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, os.Interrupt, os.Kill)
for s := range sig {
switch s {
case syscall.SIGTERM, os.Interrupt, os.Kill:
if stopFunc != nil {
err := stopFunc()
if err != nil {
logging.Infof("KProcess exec stopFunc failed:%v\n", err)
}
logging.Infof("process %d stop...\n", k.pid)
}
return
case syscall.SIGUSR1, syscall.SIGUSR2:
if upgradeFunc != nil {
err := upgradeFunc()
if err != nil {
logging.Infof("KProcess exec Upgrade failed:%v\n", err)
}
logging.Infof("process %d restart...\n", k.pid)
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。