-
Notifications
You must be signed in to change notification settings - Fork 13
/
pidfile.go
156 lines (137 loc) · 2.98 KB
/
pidfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 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"
"github.com/eolinker/eosc/env"
"github.com/eolinker/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
}