mongo procedure
This commit is contained in:
parent
dbbda34117
commit
69d53f9d4d
@ -20,7 +20,7 @@ var (
|
|||||||
mngoDB *mongo.Database
|
mngoDB *mongo.Database
|
||||||
MngoCtx context.Context
|
MngoCtx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
|
isConnected bool
|
||||||
existingCollections []string
|
existingCollections []string
|
||||||
mngoCollections []string
|
mngoCollections []string
|
||||||
mngoConfig MongoConf
|
mngoConfig MongoConf
|
||||||
@ -40,29 +40,16 @@ type MongoDB struct {
|
|||||||
|
|
||||||
func (m *MongoDB) Init(collections []string, config MongoConf) {
|
func (m *MongoDB) Init(collections []string, config MongoConf) {
|
||||||
// var baseConfig string
|
// var baseConfig string
|
||||||
|
isConnected = false
|
||||||
m.Logger = logs.GetLogger()
|
m.Logger = logs.GetLogger()
|
||||||
ResourceMap = make(map[string]interface{})
|
ResourceMap = make(map[string]interface{})
|
||||||
|
|
||||||
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
|
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
|
||||||
mngoCollections = collections
|
mngoCollections = collections
|
||||||
mngoConfig = config
|
mngoConfig = config
|
||||||
if err := m.createClient(config.GetUrl()); err == nil {
|
err := m.createClient(config.GetUrl())
|
||||||
m.connect()
|
m.Logger.Error().Msg(err.Error())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoDB) connect() error {
|
|
||||||
if mngoClient == nil {
|
|
||||||
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
|
|
||||||
m.prepareDB(mngoCollections, mngoConfig)
|
|
||||||
|
|
||||||
m.Logger.Info().Msg("Database is READY")
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return m.createClient(mngoConfig.GetUrl())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MongoDB) createClient(MongoURL string) error {
|
func (m *MongoDB) createClient(MongoURL string) error {
|
||||||
var err error
|
var err error
|
||||||
// Allows us to use marshal and unmarshall with results of FindOne() and others
|
// Allows us to use marshal and unmarshall with results of FindOne() and others
|
||||||
@ -77,19 +64,27 @@ func (m *MongoDB) createClient(MongoURL string) error {
|
|||||||
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
|
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mngoClient = nil
|
mngoClient = nil
|
||||||
|
isConnected = false
|
||||||
return errors.New("Mongodb NewClient " + MongoURL + ":" + err.Error())
|
return errors.New("Mongodb NewClient " + MongoURL + ":" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ping the primary
|
// Ping the primary
|
||||||
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
|
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
|
||||||
mngoClient = nil
|
mngoClient = nil
|
||||||
|
isConnected = false
|
||||||
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
|
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
|
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
|
||||||
mngoClient = nil
|
mngoClient = nil
|
||||||
|
isConnected = false
|
||||||
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
|
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
|
||||||
}
|
}
|
||||||
|
if !isConnected {
|
||||||
|
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
|
||||||
|
m.prepareDB(mngoCollections, mngoConfig)
|
||||||
|
m.Logger.Info().Msg("Database is READY")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,6 +109,7 @@ func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
|
|||||||
CollectionMap[collection_name] = new_collection
|
CollectionMap[collection_name] = new_collection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
isConnected = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates the collection with index specified in mongo/mongo_collections
|
// Creates the collection with index specified in mongo/mongo_collections
|
||||||
|
Loading…
Reference in New Issue
Block a user