forked from codeskyblue/gosuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.go
398 lines (365 loc) · 10.1 KB
/
fsm.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// 研究了一天的状态机,天气也热,正当我写的即将昏迷之际,我突然醒悟了,原来状态机是这么一回事
// - 状态比喻成 数据结构
// - 事件比喻成 用户输入
// - 状态转移则是函数调用
// 如此依赖写成函数,也就是 (Orz 原来如此)
// type FSM struct {
// State FSMState
// TransformFuncs map[FSMState] func()
// }
// func (f *FSM) UserAction(action FSMAction) {
// ...
// }
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/codeskyblue/gosuv/pushover"
"github.com/codeskyblue/kexec"
"github.com/kennygrant/sanitize"
"github.com/qiniu/log"
)
type FSMState string
type FSMEvent string
type FSMHandler func()
type FSM struct {
mu sync.Mutex
state FSMState
handlers map[FSMState]map[FSMEvent]FSMHandler
StateChange func(oldState, newState FSMState)
}
func (f *FSM) AddHandler(state FSMState, event FSMEvent, hdlr FSMHandler) *FSM {
_, ok := f.handlers[state]
if !ok {
f.handlers[state] = make(map[FSMEvent]FSMHandler)
}
if _, ok = f.handlers[state][event]; ok {
log.Fatalf("set twice for state(%s) event(%s)", state, event)
}
f.handlers[state][event] = hdlr
return f
}
func (f *FSM) State() FSMState {
return f.state
}
func (f *FSM) SetState(newState FSMState) {
if f.StateChange != nil {
f.StateChange(f.state, newState)
}
f.state = newState
}
func (f *FSM) Operate(event FSMEvent) FSMState {
f.mu.Lock()
defer f.mu.Unlock()
eventMap := f.handlers[f.State()]
if eventMap == nil {
return f.State()
}
if fn, ok := eventMap[event]; ok {
fn()
}
return f.State()
}
func NewFSM(initState FSMState) *FSM {
return &FSM{
state: initState,
handlers: make(map[FSMState]map[FSMEvent]FSMHandler),
}
}
// Only 4 states now is enough, I think
// 2016-09-18 now five
var (
Running = FSMState("running")
Stopped = FSMState("stopped")
Fatal = FSMState("fatal")
RetryWait = FSMState("retry wait")
Stopping = FSMState("stopping")
StartEvent = FSMEvent("start")
StopEvent = FSMEvent("stop")
RestartEvent = FSMEvent("restart")
)
type Program struct {
Name string `yaml:"name" json:"name"`
Command string `yaml:"command" json:"command"`
Environ []string `yaml:"environ" json:"environ"`
Dir string `yaml:"directory" json:"directory"`
StartAuto bool `yaml:"start_auto" json:"startAuto"`
StartRetries int `yaml:"start_retries" json:"startRetries"`
StartSeconds int `yaml:"start_seconds,omitempty" json:"startSeconds"`
StopTimeout int `yaml:"stop_timeout,omitempty" json:"stopTimeout"`
User string `yaml:"user,omitempty" json:"user"`
Notifications struct {
Pushover struct {
ApiKey string `yaml:"api_key"`
Users []string `yaml:"users"`
} `yaml:"pushover,omitempty"`
} `yaml:"notifications,omitempty" json:"-"`
WebHook struct {
Github struct {
Secret string `yaml:"secret"`
} `yaml:"github"`
Command string `yaml:"command"`
Timeout int `yaml:"timeout"`
} `yaml:"webhook,omitempty" json:"-"`
}
func (p *Program) Check() error {
if p.Name == "" {
return errors.New("Program name empty")
}
if p.Command == "" {
return errors.New("Program command empty")
}
// Disable check, for Dir may contains env-vars
//if p.Dir != "" && !IsDir(p.Dir) {
// return fmt.Errorf("Program dir(%s) not exists", p.Dir)
//}
return nil
}
func (p *Program) RunNotification() {
po := p.Notifications.Pushover
if po.ApiKey != "" && len(po.Users) > 0 {
for _, user := range po.Users {
err := pushover.Notify(pushover.Params{
Token: po.ApiKey,
User: user,
Title: "gosuv",
Message: fmt.Sprintf("%s change to fatal", p.Name),
})
if err != nil {
log.Warnf("pushover error: %v", err)
}
}
}
}
func IsRoot() bool {
u, err := user.Current()
return err == nil && u.Username == "root"
}
type Process struct {
*FSM `json:"-"`
Program `json:"program"`
cmd *kexec.KCommand
Stdout *QuickLossBroadcastWriter `json:"-"`
Stderr *QuickLossBroadcastWriter `json:"-"`
Output *QuickLossBroadcastWriter `json:"-"`
OutputFile *os.File `json:"-"`
stopC chan syscall.Signal
retryLeft int
Status string `json:"status"`
mu sync.Mutex
}
// FIXME(ssx): maybe need to return error
func (p *Process) buildCommand() *kexec.KCommand {
cmd := kexec.CommandString(p.Command)
// cmd := kexec.Command(p.Command[0], p.Command[1:]...)
logDir := filepath.Join(defaultConfigDir, "log", sanitize.Name(p.Name))
if !IsDir(logDir) {
os.MkdirAll(logDir, 0755)
}
var fout io.Writer
var err error
p.OutputFile, err = os.OpenFile(filepath.Join(logDir, "output.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Warn("create stdout log failed:", err)
fout = ioutil.Discard
} else {
fout = p.OutputFile
}
cmd.Stdout = io.MultiWriter(p.Stdout, p.Output, fout)
cmd.Stderr = io.MultiWriter(p.Stderr, p.Output, fout)
// config environ
cmd.Env = os.Environ() // inherit current vars
environ := map[string]string{}
if p.User != "" {
if !IsRoot() {
log.Warnf("detect not root, can not switch user")
} else if err := cmd.SetUser(p.User); err != nil {
log.Warnf("[%s] chusr to %s failed, %v", p.Name, p.User, err)
} else {
var homeDir string
switch runtime.GOOS {
case "linux":
homeDir = "/home/" + p.User // FIXME(ssx): maybe there is a better way
case "darwin":
homeDir = "/Users/" + p.User
}
cmd.Env = append(cmd.Env, "HOME="+homeDir, "USER="+p.User)
environ["HOME"] = homeDir
environ["USER"] = p.User
}
}
cmd.Env = append(cmd.Env, p.Environ...)
mapping := func(key string) string {
val := os.Getenv(key)
if val != "" {
return val
}
return environ[key]
}
cmd.Dir = os.Expand(p.Dir, mapping)
if strings.HasPrefix(cmd.Dir, "~") {
cmd.Dir = mapping("HOME") + cmd.Dir[1:]
}
log.Infof("[%s] use dir: %s\n", p.Name, cmd.Dir)
return cmd
}
func (p *Process) waitNextRetry() {
p.SetState(RetryWait)
if p.retryLeft <= 0 {
p.retryLeft = p.StartRetries
p.SetState(Fatal)
return
}
p.retryLeft -= 1
select {
case <-time.After(2 * time.Second): // TODO: need put it into Program
p.startCommand()
case <-p.stopC:
p.stopCommand()
}
}
func (p *Process) stopCommand() {
p.mu.Lock()
defer p.mu.Unlock()
defer p.SetState(Stopped)
if p.cmd == nil {
return
}
p.SetState(Stopping)
if p.cmd.Process != nil {
p.cmd.Process.Signal(syscall.SIGTERM) // TODO(ssx): add it to config
}
select {
case <-GoFunc(p.cmd.Wait):
log.Printf("program(%s) quit normally", p.Name)
case <-time.After(time.Duration(p.StopTimeout) * time.Second): // TODO: add 3s to config
log.Printf("program(%s) terminate all", p.Name)
p.cmd.Terminate(syscall.SIGKILL) // cleanup
}
err := p.cmd.Wait() // This is OK, because Signal KILL will definitely work
prefixStr := "\n--- GOSUV LOG " + time.Now().Format("2006-01-02 15:04:05")
if err == nil {
io.WriteString(p.cmd.Stderr, fmt.Sprintf("%s exit success ---\n\n", prefixStr))
} else {
io.WriteString(p.cmd.Stderr, fmt.Sprintf("%s exit %v ---\n\n", prefixStr, err))
}
if p.OutputFile != nil {
p.OutputFile.Close()
p.OutputFile = nil
}
p.cmd = nil
}
func (p *Process) IsRunning() bool {
return p.State() == Running || p.State() == RetryWait
}
func (p *Process) startCommand() {
// p.Stdout.Reset()
// p.Stderr.Reset()
// p.Output.Reset() // Donot reset because log is still needed.
log.Printf("start cmd(%s): %s", p.Name, p.Command)
p.cmd = p.buildCommand()
p.SetState(Running)
if err := p.cmd.Start(); err != nil {
log.Warnf("program %s start failed: %v", p.Name, err)
p.SetState(Fatal)
return
}
go func() {
errC := GoFunc(p.cmd.Wait)
startTime := time.Now()
select {
case <-errC:
// if p.cmd.Wait() returns, it means program and its sub process all quited. no need to kill again
// func Wait() will only return when program session finishs. (Only Tested on mac)
log.Printf("program(%s) finished, time used %v", p.Name, time.Since(startTime))
if time.Since(startTime) < time.Duration(p.StartSeconds)*time.Second {
if p.retryLeft == p.StartRetries { // If first time quit so fast, just set to fatal
p.SetState(Fatal)
log.Printf("program(%s) exit too quick, status -> fatal", p.Name)
return
}
}
p.waitNextRetry()
case <-p.stopC:
log.Println("recv stop command")
p.stopCommand() // clean up all process
}
}()
}
func NewProcess(pg Program) *Process {
outputBufferSize := 24 * 1024 // 24K
pr := &Process{
FSM: NewFSM(Stopped),
Program: pg,
stopC: make(chan syscall.Signal),
retryLeft: pg.StartRetries,
Status: string(Stopped),
Output: NewQuickLossBroadcastWriter(outputBufferSize),
Stdout: NewQuickLossBroadcastWriter(outputBufferSize),
Stderr: NewQuickLossBroadcastWriter(outputBufferSize),
}
pr.StateChange = func(_, newStatus FSMState) {
pr.Status = string(newStatus)
// TODO: status need to filter with config, not hard coded.
if newStatus == Fatal {
go pr.Program.RunNotification()
}
}
if pr.StartSeconds <= 0 {
pr.StartSeconds = 3
}
if pr.StopTimeout <= 0 {
pr.StopTimeout = 3
}
pr.AddHandler(Stopped, StartEvent, func() {
pr.retryLeft = pr.StartRetries
pr.startCommand()
})
pr.AddHandler(Fatal, StartEvent, pr.startCommand)
pr.AddHandler(Running, StopEvent, func() {
select {
case pr.stopC <- syscall.SIGTERM:
case <-time.After(200 * time.Millisecond):
}
}).AddHandler(Running, RestartEvent, func() {
go func() {
pr.Operate(StopEvent)
// TODO: start laterly
time.Sleep(1 * time.Second)
pr.Operate(StartEvent)
}()
})
return pr
}
func init() {
// pg := Program{
// Name: "demo",
// Command: "echo hello world && sleep 1 && echo end",
// }
// proc := NewProcess(pg)
// log.Println(proc.State())
// proc.Operate(StartEvent)
// log.Println(proc.State())
// time.Sleep(2 * time.Second)
// log.Println(proc.State())
// proc.Operate(StopEvent)
// time.Sleep(1 * time.Second)
// log.Println(proc.State())
// log.Println(light.State)
// light.AddHandler(Opened, Close, func() {
// log.Println("Close light")
// light.State = Closed
// })
// light.Operate(Close)
// log.Println(light.State)
}