浏览代码

update 增加对不同数据库组件的断言,避免未注册组件panic

Alvin 8 月之前
父节点
当前提交
d3cf08c30c
共有 1 个文件被更改,包括 30 次插入10 次删除
  1. 30 10
      game/game_cluster/internal/mdb/component.go

+ 30 - 10
game/game_cluster/internal/mdb/component.go

@@ -2,11 +2,13 @@ package mdb
 
 
 import (
 import (
 	"github.com/go-redis/redis/v8"
 	"github.com/go-redis/redis/v8"
+	mhayaGORM "github.com/mhaya/components/gorm"
 	mhayaMongo "github.com/mhaya/components/mongo"
 	mhayaMongo "github.com/mhaya/components/mongo"
 	clog "github.com/mhaya/logger"
 	clog "github.com/mhaya/logger"
 	cactor "github.com/mhaya/net/actor"
 	cactor "github.com/mhaya/net/actor"
 	cprofile "github.com/mhaya/profile"
 	cprofile "github.com/mhaya/profile"
 	"go.mongodb.org/mongo-driver/mongo"
 	"go.mongodb.org/mongo-driver/mongo"
+	"gorm.io/gorm"
 )
 )
 
 
 type ActorDB struct {
 type ActorDB struct {
@@ -14,8 +16,9 @@ type ActorDB struct {
 }
 }
 
 
 var (
 var (
-	MDB *mongo.Database
-	RDB redis.UniversalClient
+	LogstashDB *gorm.DB
+	MDB        *mongo.Database
+	RDB        redis.UniversalClient
 )
 )
 
 
 func (p *ActorDB) AliasID() string {
 func (p *ActorDB) AliasID() string {
@@ -24,17 +27,34 @@ func (p *ActorDB) AliasID() string {
 
 
 // OnInit Actor初始化前触发该函数
 // OnInit Actor初始化前触发该函数
 func (p *ActorDB) OnInit() {
 func (p *ActorDB) OnInit() {
-	mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
-	if mongo == nil {
-		clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
+	if p.App().Find(mhayaMongo.Name) != nil {
+		mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
+		if mongo == nil {
+			clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
+		}
+
+		// 获取 db_id = "center_db_1" 的配置
+		dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
+		MDB = mongo.GetDb(dbID)
+		if MDB == nil {
+			clog.Panic("game_db_id not found")
+		}
 	}
 	}
 
 
-	// 获取 db_id = "center_db_1" 的配置
-	dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
-	MDB = mongo.GetDb(dbID)
-	if MDB == nil {
-		clog.Panic("game_db_id not found")
+	if p.App().Find(mhayaGORM.Name) != nil {
+		gormComponent := p.App().Find(mhayaGORM.Name).(*mhayaGORM.Component)
+		if gormComponent == nil {
+			clog.DPanicf("[component = %s] not found.", mhayaGORM.Name)
+		}
+
+		// 获取 db_id = "center_db_1" 的配置
+		dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
+		LogstashDB = gormComponent.GetDb(dbID)
+		if LogstashDB == nil {
+			clog.Panic("game_db_id not found")
+		}
 	}
 	}
+
 	redisConfig := cprofile.GetConfig("redis")
 	redisConfig := cprofile.GetConfig("redis")
 	/*	tlsConfig := &tls.Config{
 	/*	tlsConfig := &tls.Config{
 		MinVersion:               tls.VersionTLS12,
 		MinVersion:               tls.VersionTLS12,