19 Star 123 Fork 0

kelvins-io/kelvins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tableflip_linux.go 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
// +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)
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kelvins-io/kelvins.git
git@gitee.com:kelvins-io/kelvins.git
kelvins-io
kelvins
kelvins
v1.5.12

搜索帮助