Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds logging to VirtualMachineSSHClient #68

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading