提交 6a274c66 编写于 作者: martianzhang's avatar martianzhang

add shutdown func and code lint

上级 31363ee4
......@@ -80,8 +80,7 @@ func main() {
// 环境初始化,连接检查线上环境+构建测试环境
vEnv, rEnv := env.BuildEnv()
// 使用CleanupTestDatabase命令手动清理残余的`optimizer_xxx`数据库
// 为了保护当前正在评审的SQL,只清除前1小时前的残余
// 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库
if common.Config.CleanupTestDatabase {
vEnv.CleanupTestDatabase()
}
......@@ -91,11 +90,9 @@ func main() {
defer vEnv.CleanUp()
}
//当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
// 当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
common.HandleSignal(func() {
if common.Config.DropTestTemporary {
vEnv.CleanUp()
}
shutdown(vEnv, rEnv)
})
// 对指定的库表进行索引重复检查
......@@ -493,3 +490,9 @@ func main() {
return
}
}
func shutdown(vEnv *env.VirtualEnv, rEnv *database.Connector) {
if common.Config.DropTestTemporary {
vEnv.CleanUp()
}
}
......@@ -20,10 +20,9 @@ import (
"os"
"os/signal"
"syscall"
"time"
)
//当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
// HandleSignal 当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
func HandleSignal(f func()) {
sc := make(chan os.Signal, 1)
signal.Notify(sc,
......@@ -33,13 +32,10 @@ func HandleSignal(f func()) {
syscall.SIGQUIT)
go func() {
select {
case n := <-sc:
Log.Info("receive signal %v, closing", n)
f()
time.Sleep(250 * time.Millisecond)
os.Exit(0)
}
}()
}
......@@ -157,7 +157,7 @@ func (ve *VirtualEnv) CleanupTestDatabase() {
if err == nil {
for _, row := range dbs.Rows {
testDatabase := row.Str(0)
// test temporary database format `opimtizer_YYMMDDHHmmss_randomString(16)`
// test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)`
if len(testDatabase) != 39 {
common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase)
continue
......@@ -165,26 +165,24 @@ func (ve *VirtualEnv) CleanupTestDatabase() {
s := strings.Split(testDatabase, "_")
pastTime, err := time.Parse("060102150405", s[1])
if err != nil {
common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err)
}
nowTime, err := time.Parse("060102150405", time.Now().Format("060102150405"))
if err != nil {
common.Log.Error("CleanupTestDatabase compute nowTime Error: %s", err)
common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error())
continue
}
// TODO: 1 hour should be configable
subHour := nowTime.Sub(pastTime).Hours()
// TODO: 1 hour should be config-able
subHour := time.Now().Sub(pastTime).Hours()
if subHour > 1 {
_, err := ve.Query("drop database %s", testDatabase)
if err != nil {
common.Log.Error("CleanupTestDatabase failed Error: %s", err)
common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error())
continue
}
common.Log.Debug("CleanupTestDatabase drop database %s", testDatabase)
common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase)
} else {
common.Log.Debug("CleanupTestDatabase by pass database %s, less than %d hours", testDatabase, subHour)
}
}
} else {
common.Log.Error("CleanupTestDatabase failed Error:%s", err)
common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error())
}
}
......
......@@ -115,26 +115,26 @@ func TestNewVirtualEnv(t *testing.T) {
func TestCleanupTestDatabase(t *testing.T) {
vEnv, _ := BuildEnv()
vEnv.Query("drop database if exists optimizer_100000000000_xxxxxxxxxxxxxxxx")
_, err := vEnv.Query("create database optimizer_100000000000_xxxxxxxxxxxxxxxx")
vEnv.Query("drop database if exists optimizer_060102150405_xxxxxxxxxxxxxxxx")
_, err := vEnv.Query("create database optimizer_060102150405_xxxxxxxxxxxxxxxx")
if err != nil {
t.Error(err)
}
vEnv.CleanupTestDatabase()
_, err = vEnv.Query("drop database optimizer_100000000000_xxxxxxxxxxxxxxxx")
_, err = vEnv.Query("show create database optimizer_060102150405_xxxxxxxxxxxxxxxx")
if err == nil {
t.Error("optimizer_100000000000_xxxxxxxxxxxxxxxx exist, should be droped")
t.Error("optimizer_060102150405_xxxxxxxxxxxxxxxx exist, should be droped")
}
vEnv.Query("drop database if exists optimizer_100000000000")
_, err = vEnv.Query("create database optimizer_100000000000")
vEnv.Query("drop database if exists optimizer_060102150405")
_, err = vEnv.Query("create database optimizer_060102150405")
if err != nil {
t.Error(err)
}
vEnv.CleanupTestDatabase()
_, err = vEnv.Query("drop database optimizer_100000000000")
_, err = vEnv.Query("drop database optimizer_060102150405")
if err != nil {
t.Error("optimizer_100000000000 not exist, should not be droped")
t.Error("optimizer_060102150405 not exist, should not be droped")
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册