mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 18:10:25 +08:00
启动时如果无法连接数据库,则尝试N分钟,防止因为数据库连接失败而异常退出
This commit is contained in:
@@ -3,6 +3,7 @@ package nodes
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
@@ -43,8 +44,15 @@ func NewAPINode() *APINode {
|
|||||||
func (this *APINode) Start() {
|
func (this *APINode) Start() {
|
||||||
logs.Println("[API_NODE]start api node, pid: " + strconv.Itoa(os.Getpid()))
|
logs.Println("[API_NODE]start api node, pid: " + strconv.Itoa(os.Getpid()))
|
||||||
|
|
||||||
|
// 检查数据库连接
|
||||||
|
err := this.checkDB()
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("[API_NODE]" + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 本地Sock
|
// 本地Sock
|
||||||
err := this.listenSock()
|
err = this.listenSock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[API_NODE]" + err.Error())
|
logs.Println("[API_NODE]" + err.Error())
|
||||||
return
|
return
|
||||||
@@ -108,7 +116,7 @@ func (this *APINode) Start() {
|
|||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实现守护进程
|
// Daemon 实现守护进程
|
||||||
func (this *APINode) Daemon() {
|
func (this *APINode) Daemon() {
|
||||||
path := os.TempDir() + "/edge-api.sock"
|
path := os.TempDir() + "/edge-api.sock"
|
||||||
isDebug := lists.ContainsString(os.Args, "debug")
|
isDebug := lists.ContainsString(os.Args, "debug")
|
||||||
@@ -153,7 +161,7 @@ func (this *APINode) Daemon() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安装系统服务
|
// InstallSystemService 安装系统服务
|
||||||
func (this *APINode) InstallSystemService() error {
|
func (this *APINode) InstallSystemService() error {
|
||||||
shortName := teaconst.SystemdServiceName
|
shortName := teaconst.SystemdServiceName
|
||||||
|
|
||||||
@@ -263,6 +271,35 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查数据库
|
||||||
|
func (this *APINode) checkDB() error {
|
||||||
|
logs.Println("checking database connection ...")
|
||||||
|
|
||||||
|
db, err := dbs.Default()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
maxTries := 600
|
||||||
|
for i := 0; i <= maxTries; i++ {
|
||||||
|
_, err := db.Exec("SELECT 1")
|
||||||
|
if err != nil {
|
||||||
|
if i == maxTries-1 {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if i%10 == 0 { // 这让提示不会太多
|
||||||
|
logs.Println("[API_NODE]reconnecting to database (" + fmt.Sprintf("%.1f", float32(i * 100)/float32(maxTries+1)) + "%) ...")
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 自动升级
|
// 自动升级
|
||||||
func (this *APINode) autoUpgrade() error {
|
func (this *APINode) autoUpgrade() error {
|
||||||
if Tea.IsTesting() {
|
if Tea.IsTesting() {
|
||||||
|
|||||||
Reference in New Issue
Block a user