github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/health/checks/mongodbclient.go (about) 1 package checks 2 3 import ( 4 "context" 5 "fmt" 6 7 "go.mongodb.org/mongo-driver/mongo" 8 "go.mongodb.org/mongo-driver/mongo/readpref" 9 ) 10 11 type MongoClientCheck struct { 12 Ctx context.Context 13 MongoDBClient *mongo.Client 14 } 15 16 func NewMongoClientCheck(ctx context.Context, client *mongo.Client) MongoClientCheck { 17 return MongoClientCheck{ 18 Ctx: ctx, 19 MongoDBClient: client, 20 } 21 } 22 23 func CheckMongoClientStatus(ctx context.Context, client *mongo.Client) error { 24 check := NewMongoClientCheck(ctx, client) 25 return check.CheckStatus() 26 } 27 28 func (check MongoClientCheck) CheckStatus() error { 29 ctx := check.Ctx 30 err := check.MongoDBClient.Ping(ctx, readpref.Primary()) 31 if err != nil { 32 return fmt.Errorf("mongoDB health check failed on ping: %w", err) 33 } 34 35 return nil 36 }