-
Notifications
You must be signed in to change notification settings - Fork 191
/
pidfile.go
70 lines (57 loc) · 1.05 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
package process
import (
"os"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
)
// PidFile struct
type PidFile struct {
pid int
file string
// body string
}
// NewPidFile instance
func NewPidFile(file string) *PidFile {
return &PidFile{
file: file,
}
}
// Exists of th pid file
func (pf *PidFile) Exists() bool {
return fsutil.FileExist(pf.file)
}
// File path
func (pf *PidFile) File() string {
return pf.file
}
// PID value
func (pf *PidFile) PID() int {
if pf.pid > 0 {
return pf.pid
}
if fsutil.FileExist(pf.file) {
bts, err := os.ReadFile(pf.file)
if err == nil {
pf.pid = strutil.QuietInt(string(bts))
}
}
return pf.pid
}
// String PID value string
func (pf *PidFile) String() string {
return mathutil.String(pf.pid)
}
// SetPID value
func (pf *PidFile) SetPID(val int) int {
pf.pid = val
return pf.pid
}
// Save PID value to file
func (pf *PidFile) Save() error {
if pf.pid < 1 {
return nil
}
_, err := fsutil.PutContents(pf.file, pf.String())
return err
}