From 6a274c661153835d50903b2af65323473577dcf9 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Fri, 2 Nov 2018 10:35:08 +0800 Subject: [PATCH] add shutdown func and code lint --- cmd/soar/soar.go | 15 +++++++++------ common/signal.go | 6 +----- env/env.go | 20 +++++++++----------- env/env_test.go | 16 ++++++++-------- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 7ba0bbd..ebe6796 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -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() + } +} diff --git a/common/signal.go b/common/signal.go index 4329c96..4f25a02 100644 --- a/common/signal.go +++ b/common/signal.go @@ -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) } }() } diff --git a/env/env.go b/env/env.go index a148c56..5b59789 100644 --- a/env/env.go +++ b/env/env.go @@ -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()) } } diff --git a/env/env_test.go b/env/env_test.go index 147e605..53f3960 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -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") } } -- GitLab