forked from moiot/moha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (84 loc) · 2.36 KB
/
main.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
// Copyright 2018 MOBIKE, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
"time"
"github.com/fuguohong1024/moha/agent"
"github.com/fuguohong1024/moha/pkg/log"
)
func main() {
cfg := agent.NewConfig()
if err := cfg.Parse(os.Args[1:]); err != nil {
log.Fatalf("verifying flags error, %v. See '%s --help'", err, agent.DefaultName)
}
log.Info("tear-down init logger")
err := agent.InitLogger(cfg)
if err != nil {
log.Error("error when init logger. error: ", err)
}
mysqlStopCh := make(chan interface{})
go func() {
waitProcessToStop(os.Getenv("MYSQL_PID_FILE"))
close(mysqlStopCh)
}()
log.Info("tear-down begin to wait4 mysql")
var mysqlStopSuccess bool
select {
case <-mysqlStopCh:
log.Info("detect mysql has been stopped by wait4 or error happens")
mysqlStopSuccess = true
break
case <-time.After(3 * time.Second):
log.Info("timeout when wait4 mysql")
break
}
if mysqlStopSuccess {
log.Info("mysql stop success, start clean")
err = agent.StartClean(cfg)
if err != nil {
log.Error("fail to run agent-cleaner err: ", err)
}
} else {
log.Warn("mysql stop timeout, force shutdown and lease remains until ttl expires")
}
}
func waitProcessToStop(pidFile string) error {
log.Info("load pid file from ", pidFile)
bs, err := ioutil.ReadFile(pidFile)
if err != nil {
log.Error("error loading mysql pid file, err ", err)
log.Error("skip mysql waiting")
return err
}
str := strings.TrimRight(string(bs), "\n")
pid, err := strconv.Atoi(str)
if err != nil {
log.Error("error converting pid, ", str, " to int , err ", err)
log.Error("skip mysql waiting")
return err
}
var wstatus syscall.WaitStatus
wpid, err := syscall.Wait4(pid, &wstatus, 0, nil)
if err != nil {
log.Error("error waiting pid, ", pid, " , err ", err)
log.Error("skip mysql waiting")
return err
}
log.Info("success waiting pid ", wpid, " exits.")
return nil
}