liuhaijun 3f5f28d785 add sheduling agent
Change-Id: I89f35fb3984044c57f10727432755012542f9fd8
2023-11-16 10:55:57 +00:00

74 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package data
import (
"fmt"
c "git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/config"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/pkg/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
var PostgreSqlDB = make(map[string]*gorm.DB)
type Writer interface {
Printf(string, ...interface{})
}
type WriterLog struct {
}
func (w WriterLog) Printf(format string, args ...interface{}) {
log.Logger.Sugar().Infof(format, args...)
}
func initPostgres() {
var err error
postgreSqlConfigMap := c.Config.PostgreSql
if c.Config.PostgreSql != nil && len(postgreSqlConfigMap) > 0 {
for name, postgreSqlConfig := range postgreSqlConfigMap {
if !postgreSqlConfig.Enable {
continue
}
var writerLog WriterLog
if postgreSqlConfig.PrintSql {
writerLog = WriterLog{}
}
logConfig := logger.New(
writerLog,
logger.Config{
SlowThreshold: 0, // 慢 SQL 阈值
LogLevel: logger.LogLevel(postgreSqlConfig.LogLevel), // 日志级别
IgnoreRecordNotFoundError: false, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 是否启用彩色打印
},
)
configs := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: postgreSqlConfig.TablePrefix, // 表名前缀
// SingularTable: true, // 使用单数表名
},
Logger: logConfig,
}
dsn := fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s",
postgreSqlConfig.Host,
postgreSqlConfig.Port,
postgreSqlConfig.Username,
postgreSqlConfig.Database,
postgreSqlConfig.Password,
)
PostgreSqlDB[name], err = gorm.Open(postgres.Open(dsn), configs)
if err != nil {
panic("postgres connection failed" + err.Error())
}
}
}
}