定时ping,保持数据库连接

pull/21/merge
ouqiang 2017-04-03 10:20:08 +08:00
parent cd7e20c481
commit beefe59858
3 changed files with 15 additions and 3 deletions

View File

@ -3,7 +3,7 @@ package main
/*--------------------------------------------------------
Linux crontab
HTTPSSH
HTTPSSH
--------------------------------------------------------*/
import (

View File

@ -8,6 +8,7 @@ import (
"github.com/ouqiang/cron-scheduler/modules/setting"
"gopkg.in/macaron.v1"
"strings"
"time"
)
type Status int8
@ -42,12 +43,14 @@ func CreateDb(configFile string) *xorm.Engine {
mapper := core.NewPrefixMapper(core.SnakeMapper{}, config["prefix"])
engine.SetTableMapper(mapper)
}
// 本地环境开日志
// 本地环境开日志
if macaron.Env == macaron.DEV {
engine.ShowSQL(true)
engine.Logger().SetLevel(core.LOG_DEBUG)
}
go keepDbAlived(engine)
return engine
}
@ -91,3 +94,12 @@ func getDbConfig(configFile string) map[string]string {
return db
}
// 定时ping, 防止因数据库超时设置被断开
func keepDbAlived(engine *xorm.Engine) {
t := time.Tick(180 * time.Second)
for {
<- t
engine.Ping()
}
}

View File

@ -14,7 +14,7 @@ import (
type InstallForm struct {
DbType string `binding:"IN(mysql)"`
DbHost string `binding:"Required"`
DbPort int `binding:"Required"`
DbPort int `binding:"Required"` // todo 限制端口范围1-65535, 为什么规则未生效?
DbUsername string `binding:"Required"`
DbPassword string `binding:"Required"`
DbName string `binding:"Required"`