forked from topfreegames/apm
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathproc_container.go
More file actions
253 lines (225 loc) · 5.78 KB
/
Copy pathproc_container.go
File metadata and controls
253 lines (225 loc) · 5.78 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package process
import (
"errors"
"os"
"strconv"
"syscall"
"github.com/struCoder/pmgo/lib/utils"
)
// ProcContainer is a interface that about proc
type ProcContainer interface {
Start() error
ForceStop() error
GracefullyStop() error
Restart() error
Delete() error
IsAlive() bool
Identifier() string
ShouldKeepAlive() bool
AddRestart()
NotifyStopped()
SetStatus(status string)
SetUptime()
SetSysInfo()
GetPid() int
GetStatus() *ProcStatus
Watch() (*os.ProcessState, error)
release()
GetOutFile() string
GetPidFile() string
GetPath() string
GetErrFile() string
GetName() string
}
// Proc is a os.Process wrapper with Status and more info that will be used on Master to maintain
// the process health.
type Proc struct {
Name string
Cmd string
Cwd string
Args []string
Path string
Pidfile string
Outfile string
Errfile string
KeepAlive bool
Pid int
Status *ProcStatus
process *os.Process
}
// Start will execute the command Cmd that should run the process. It will also create an out, err and pidfile
// in case they do not exist yet.
// Returns an error in case there's any.
func (proc *Proc) Start() error {
outFile, err := utils.GetFile(proc.Outfile)
if err != nil {
return err
}
errFile, err := utils.GetFile(proc.Errfile)
if err != nil {
return err
}
if proc.Cwd == "" {
proc.Cwd, _ = os.Getwd()
}
procAtr := &os.ProcAttr{
Dir: proc.Cwd,
Env: os.Environ(),
Files: []*os.File{
os.Stdin,
outFile,
errFile,
},
}
args := append([]string{proc.Name}, proc.Args...)
process, err := os.StartProcess(proc.Cmd, args, procAtr)
if err != nil {
return err
}
proc.process = process
proc.Pid = proc.process.Pid
err = utils.WriteFile(proc.Pidfile, []byte(strconv.Itoa(proc.process.Pid)))
if err != nil {
return err
}
proc.Status.InitUptime()
proc.Status.SetStatus("started")
return nil
}
// ForceStop will forcefully send a SIGKILL signal to process killing it instantly.
// Returns an error in case there's any.
func (proc *Proc) ForceStop() error {
if proc.process != nil {
err := proc.process.Signal(syscall.SIGKILL)
proc.Status.SetStatus("stopped")
proc.release()
return err
}
return errors.New("Process does not exist.")
}
// GracefullyStop will send a SIGTERM signal asking the process to terminate.
// The process may choose to die gracefully or ignore this signal completely. In that case
// the process will keep running unless you call ForceStop()
// Returns an error in case there's any.
func (proc *Proc) GracefullyStop() error {
if proc.process != nil {
err := proc.process.Signal(syscall.SIGTERM)
proc.Status.SetStatus("asked to stop")
return err
}
return errors.New("Process does not exist.")
}
// Restart will try to gracefully stop the process and then Start it again.
// Returns an error in case there's any.
func (proc *Proc) Restart() error {
if proc.IsAlive() {
err := proc.GracefullyStop()
if err != nil {
return err
}
}
return proc.Start()
}
// Delete will delete everything created by this process, including the out, err and pid file.
// Returns an error in case there's any.
func (proc *Proc) Delete() error {
proc.release()
err := utils.DeleteFile(proc.Outfile)
if err != nil {
return err
}
err = utils.DeleteFile(proc.Errfile)
if err != nil {
return err
}
return os.RemoveAll(proc.Path)
}
// IsAlive will check if the process is alive or not.
// Returns true if the process is alive or false otherwise.
func (proc *Proc) IsAlive() bool {
p, err := os.FindProcess(proc.Pid)
if err != nil {
return false
}
return p.Signal(syscall.Signal(0)) == nil
}
// Watch will stop execution and wait until the process change its state. Usually changing state, means that the process died.
// Returns a tuple with the new process state and an error in case there's any.
func (proc *Proc) Watch() (*os.ProcessState, error) {
return proc.process.Wait()
}
// Will release the process and remove its PID file
func (proc *Proc) release() {
if proc.process != nil {
proc.process.Release()
}
utils.DeleteFile(proc.Pidfile)
}
// NotifyStopped that process was stopped so we can set its PID to -1
func (proc *Proc) NotifyStopped() {
proc.Pid = -1
}
// AddRestart is add one restart to proc status
func (proc *Proc) AddRestart() {
proc.Status.AddRestart()
}
// GetPid will return proc current PID
func (proc *Proc) GetPid() int {
return proc.Pid
}
// GetOutFile will return proc out file
func (proc *Proc) GetOutFile() string {
return proc.Outfile
}
// GetErrFile will return proc error file
func (proc *Proc) GetErrFile() string {
return proc.Errfile
}
// GetPidFile will return proc pid file
func (proc *Proc) GetPidFile() string {
return proc.Pidfile
}
// GetPath will return proc path
func (proc *Proc) GetPath() string {
return proc.Path
}
// GetStatus will return proc current status
func (proc *Proc) GetStatus() *ProcStatus {
if !proc.IsAlive() {
proc.ResetUpTime()
} else {
// update uptime
proc.SetUptime()
}
// update cpu and memory
proc.SetSysInfo()
return proc.Status
}
// SetStatus will set proc status
func (proc *Proc) SetStatus(status string) {
proc.Status.SetStatus(status)
}
// SetUptime will set Uptime
func (proc *Proc) SetUptime() {
proc.Status.SetUptime()
}
// ResetUpTime will set Uptime
func (proc *Proc) ResetUpTime() {
proc.Status.ResetUptime()
}
// SetSysInfo will get current proc cpu and memory usage
func (proc *Proc) SetSysInfo() {
proc.Status.SetSysInfo(proc.process.Pid)
}
// Identifier is that will be used by watcher to keep track of its processes
func (proc *Proc) Identifier() string {
return proc.Name
}
// ShouldKeepAlive will returns true if the process should be kept alive or not
func (proc *Proc) ShouldKeepAlive() bool {
return proc.KeepAlive
}
// GetName will return current proc name
func (proc *Proc) GetName() string {
return proc.Name
}