[optimize] close the DB connections when reloading
This commit is contained in:
parent
d3349edf09
commit
9a54e15ae9
3 changed files with 30 additions and 4 deletions
|
|
@ -44,6 +44,7 @@ func TestCommandStart(t *testing.T) {
|
||||||
service.Stop(func() {})
|
service.Stop(func() {})
|
||||||
log.Println("服务已关闭")
|
log.Println("服务已关闭")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
os.Args = append(os.Args, "start")
|
os.Args = append(os.Args, "start")
|
||||||
main()
|
main()
|
||||||
|
|
@ -68,9 +69,9 @@ func TestCommandStart(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待服务启动
|
// 等待服务启动
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
times := 0
|
times := 0
|
||||||
for times < 20 { // 2秒超时
|
for times < 30 { // 2秒超时
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
times++
|
times++
|
||||||
fmt.Printf("Trying(%d)...", times)
|
fmt.Printf("Trying(%d)...", times)
|
||||||
res, err := request()
|
res, err := request()
|
||||||
|
|
@ -115,9 +116,9 @@ func TestCommandStop(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待服务启动
|
// 等待服务启动
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
times := 0
|
times := 0
|
||||||
for times < 20 { // 2秒超时
|
for times < 30 { // 2秒超时
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
times++
|
times++
|
||||||
res, err := request()
|
res, err := request()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ func Stop(onComplete func()) {
|
||||||
select {
|
select {
|
||||||
case <-shutdownComplete:
|
case <-shutdownComplete:
|
||||||
share.SessionStop()
|
share.SessionStop()
|
||||||
|
share.DBClose()
|
||||||
onComplete()
|
onComplete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
24
share/db.go
24
share/db.go
|
|
@ -2,6 +2,7 @@ package share
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
|
|
@ -45,3 +46,26 @@ func DBConnect(dbconfig config.DBConfig) (err error) {
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DBClose close the database connections
|
||||||
|
func DBClose() error {
|
||||||
|
messages := []string{}
|
||||||
|
capsule.Global.Connections.Range(func(key, value any) bool {
|
||||||
|
log.Trace("[DBClose] %s", key)
|
||||||
|
if conn, ok := value.(*capsule.Connection); ok {
|
||||||
|
err := conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
messages = append(messages, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(messages) > 0 {
|
||||||
|
msg := fmt.Sprintf("[DBClose] %s ", strings.Join(messages, ";"))
|
||||||
|
log.Error(msg)
|
||||||
|
return fmt.Errorf(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue