Skip to content

Commit

Permalink
Adds logging to VirtualMachineSSHClient
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbs committed Mar 11, 2024
1 parent 8354478 commit d12ab50
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct VirtualMachineSSHClient<SSHClientType: SSHClient> {
let connection = try await connectToVirtualMachine(
named: virtualMachine.name,
on: ipAddress,
remainingAttempts: 3
maximumAttempts: 3
)
try await connectionHandler.didConnect(to: virtualMachine, through: connection)
return connection
Expand All @@ -72,21 +72,34 @@ private extension VirtualMachineSSHClient {
private func connectToVirtualMachine(
named virtualMachineName: String,
on host: String,
remainingAttempts: Int
attempt: Int = 1,
maximumAttempts: Int
) async throws -> SSHClientType.SSHConnectionType {
do {
if attempt < 3 {
throw NSError(domain: "dk.simonbs.Tartelet", code: -42)
}
try Task.checkCancellation()
return try await connectToVirtualMachine(named: virtualMachineName, on: host)
} catch {
guard remainingAttempts > 1 else {
logger.error(
"Attempt \(attempt) out of \(maximumAttempts) to establish an SSH connection"
+ " to the virtual machine named \(virtualMachineName) failed."
)
guard attempt < maximumAttempts else {
logger.error(
"Last attempt to establish an SSH connection to"
+ " virtual machine named \(virtualMachineName) failed."
)
throw VirtualMachineSSHClientError.failedConnectingToVirtualMachineAfterRetries
}
try Task.checkCancellation()
try await Task.sleep(for: .seconds(2))
return try await connectToVirtualMachine(
named: virtualMachineName,
on: host,
remainingAttempts: remainingAttempts - 1
attempt: attempt + 1,
maximumAttempts: maximumAttempts
)
}
}
Expand Down

0 comments on commit d12ab50

Please sign in to comment.