1 Star 0 Fork 0

蒙蒙的男孩 / eosc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pidfile.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
蒙蒙的男孩 提交于 2024-01-10 13:49 . 重定义项目地址
/*
* Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package pidfile
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"gitee.com/meng_mengs_boys/eosc/env"
"gitee.com/meng_mengs_boys/eosc/log"
)
var (
ErrorPidForking = errors.New("pid file is forking")
ErrorPidNotForking = errors.New("pid file not forking")
)
type PidFile struct {
locker sync.Mutex
path string
}
func New() (*PidFile, error) {
path := getPath()
if err := checkPIDFileAlreadyExists(path); err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0666)); err != nil {
return nil, err
}
if err := os.WriteFile(path, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
return nil, err
}
return &PidFile{path: path}, nil
}
func (p *PidFile) Remove() error {
log.Info("remove pidfile:", p.path)
p.locker.Lock()
defer p.locker.Unlock()
if p.path == "" {
return nil
}
err := os.Remove(p.path)
if err != nil {
log.Warn("remove pidfile :", err)
return err
}
p.path = ""
return nil
}
func (p *PidFile) UnFork() error {
p.locker.Lock()
defer p.locker.Unlock()
old := getOldPath()
if !strings.EqualFold(old, p.path) {
return ErrorPidNotForking
}
if !exist(old) {
return os.ErrNotExist
}
path := getPath()
if exist(path) {
return os.ErrExist
}
e := os.Rename(old, path)
if e != nil {
return e
}
p.path = path
return nil
}
func (p *PidFile) TryFork() error {
p.locker.Lock()
defer p.locker.Unlock()
target := getOldPath()
if strings.EqualFold(p.path, target) {
return ErrorPidForking
}
if exist(target) {
// 强制清理旧文件
os.Remove(target)
}
err := os.Rename(p.path, target)
if err != nil {
return err
}
p.path = target
return nil
}
func processExistsByFile(path string) bool {
if exist(path) {
pidByte, err := os.ReadFile(path)
if err != nil {
return false
}
pidString := strings.TrimSpace(string(pidByte))
pid, err := strconv.Atoi(pidString)
if err == nil {
return ProcessExists(pid)
}
}
return false
}
func checkPIDFileAlreadyExists(path string) error {
if processExistsByFile(path) {
return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path)
}
return nil
}
func exist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func Exist() bool {
return exist(getPath())
}
func getPath() string {
name := env.AppName()
path, _ := filepath.Abs(fmt.Sprintf("%s/%s.pid", env.PidFileDir(), name))
return path
}
func getOldPath() string {
name := env.AppName()
path, _ := filepath.Abs(fmt.Sprintf("%s/%s.old.pid", env.PidFileDir(), name))
return path
}
1
https://gitee.com/meng_mengs_boys/eosc.git
git@gitee.com:meng_mengs_boys/eosc.git
meng_mengs_boys
eosc
eosc
v1.15.7

搜索帮助