Skip to content

Commit

Permalink
refactor: refactor mongo connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gulivero1773 committed Jun 12, 2024
1 parent 76d5fe3 commit bb83820
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Sources/HealthChecks/Extensions/Application+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,14 @@ extension Application {
}

/// Variable for `MongoDatabase`
public var mongoCluster: MongoCluster? {
public var healthCheckMongoCluster: MongoCluster? {
get { storage[MongoClusterKey.self] }
set { storage[MongoClusterKey.self] = newValue }
}

/// Initialize MongoDB
/// - Parameter connectionString: URI as `String`. Example: "mongodb://localhost/myapp
public func initializeHealthCheckMongoCluster(connectionString: String) async throws {
self.healthCheckMongoCluster = try await MongoCluster(connectingTo: ConnectionSettings(connectionString))
}
}
10 changes: 5 additions & 5 deletions Sources/HealthChecks/MongoHealthChecks/MongoRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public struct MongoRequest: MongoRequestSendable {
/// - Parameter url: `String`
/// - Returns: `String`
public func getConnection(by url: String) async throws -> String {
await app.mongoCluster?.disconnect()
app.mongoCluster = try? MongoCluster(lazyConnectingTo: ConnectionSettings(url))
let connection = "\(app.mongoCluster?.connectionState ?? .disconnected)"
await app.mongoCluster?.disconnect()
return connection
let connectionState = "\(app.healthCheckMongoCluster?.connectionState ?? .disconnected)"
if connectionState.contains("disconnected") {
app.logger.error("ERROR: MongoCluster not connection")
}
return connectionState
}
}

0 comments on commit bb83820

Please sign in to comment.