Skip to content

Commit f417542

Browse files
committed
Break out humanizeDuration
1 parent 88c3297 commit f417542

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/main/kotlin/com/coder/gateway/CoderGatewayConnectionProvider.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.coder.gateway
44

5+
import com.coder.gateway.sdk.humanizeDuration
56
import com.coder.gateway.sdk.suspendingRetryWithExponentialBackOff
67
import com.coder.gateway.services.CoderRecentWorkspaceConnectionsService
78
import com.intellij.openapi.application.ApplicationManager
@@ -45,10 +46,10 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
4546
e is ConnectionException || e is TimeoutException
4647
|| e is SSHException || e is DeployException
4748
},
48-
update = { _, e, remaining, ->
49-
if (remaining != null) {
49+
update = { _, e, remainingMs ->
50+
if (remainingMs != null) {
5051
indicator.text2 = e.message ?: CoderGatewayBundle.message("gateway.connector.no-details")
51-
indicator.text = CoderGatewayBundle.message("gateway.connector.coder.connection.retry-error.text", remaining)
52+
indicator.text = CoderGatewayBundle.message("gateway.connector.coder.connection.retry-error.text", humanizeDuration(remainingMs))
5253
} else {
5354
ApplicationManager.getApplication().invokeAndWait {
5455
Messages.showMessageDialog(

src/main/kotlin/com/coder/gateway/sdk/Retry.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
3838
label: String,
3939
logger: Logger,
4040
predicate: (e: Throwable) -> Boolean,
41-
update: (attempt: Int, e: Throwable, remaining: String?) -> Unit,
41+
update: (attempt: Int, e: Throwable, remaining: Long?) -> Unit,
4242
action: suspend (attempt: Int) -> T?
4343
): T? {
4444
val random = Random()
@@ -67,8 +67,6 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
6767
logger.error("Failed to $label (attempt $attempt; will retry in $delayMs ms)", originalEx)
6868
var remainingMs = delayMs
6969
while (remainingMs > 0) {
70-
val remainingS = TimeUnit.MILLISECONDS.toSeconds(remainingMs)
71-
val remaining = if (remainingS < 1) "now" else "in $remainingS second${if (remainingS > 1) "s" else ""}"
7270
// When the worker upload times out Gateway just says it failed.
7371
// Even the root cause (IllegalStateException) is useless. The
7472
// error also includes a very long useless tmp path. With all
@@ -77,7 +75,7 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
7775
if (unwrappedEx is DeployException && unwrappedEx.message.contains("Worker binary deploy failed"))
7876
DeployException("Failed to upload worker binary...it may have timed out", unwrappedEx)
7977
else unwrappedEx
80-
update(attempt, mungedEx, remaining)
78+
update(attempt, mungedEx, remainingMs)
8179
val next = min(remainingMs, TimeUnit.SECONDS.toMillis(1))
8280
remainingMs -= next
8381
delay(next)
@@ -87,3 +85,15 @@ suspend fun <T> suspendingRetryWithExponentialBackOff(
8785
}
8886
error("Should never be reached")
8987
}
88+
89+
/**
90+
* Convert a millisecond duration into a human-readable string.
91+
*
92+
* < 1 second: "now"
93+
* 1 second: "in one second"
94+
* > 1 second: "in <duration> seconds"
95+
*/
96+
fun humanizeDuration(durationMs: Long): String {
97+
val seconds = TimeUnit.MILLISECONDS.toSeconds(durationMs)
98+
return if (seconds < 1) "now" else "in $seconds second${if (seconds > 1) "s" else ""}"
99+
}

src/main/kotlin/com/coder/gateway/views/steps/CoderLocateRemoteProjectStepView.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.coder.gateway.sdk.Arch
88
import com.coder.gateway.sdk.CoderCLIManager
99
import com.coder.gateway.sdk.CoderRestClientService
1010
import com.coder.gateway.sdk.OS
11+
import com.coder.gateway.sdk.humanizeDuration
1112
import com.coder.gateway.sdk.suspendingRetryWithExponentialBackOff
1213
import com.coder.gateway.sdk.toURL
1314
import com.coder.gateway.sdk.withPath
@@ -197,11 +198,11 @@ class CoderLocateRemoteProjectStepView(private val setNextButtonEnabled: (Boolea
197198
e is ConnectionException || e is TimeoutException
198199
|| e is SSHException || e is DeployException
199200
},
200-
update = { _, e, remaining ->
201+
update = { _, e, remainingMs ->
201202
cbIDEComment.foreground = UIUtil.getErrorForeground()
202203
cbIDEComment.text = e.message ?: CoderGatewayBundle.message("gateway.connector.no-details")
203204
cbIDE.renderer =
204-
if (remaining != null) IDECellRenderer(CoderGatewayBundle.message("gateway.connector.view.coder.remoteproject.retry-error.text", remaining))
205+
if (remainingMs != null) IDECellRenderer(CoderGatewayBundle.message("gateway.connector.view.coder.remoteproject.retry-error.text", humanizeDuration(remainingMs)))
205206
else IDECellRenderer(CoderGatewayBundle.message("gateway.connector.view.coder.remoteproject.error.text"), UIUtil.getBalloonErrorIcon())
206207
},
207208
)

0 commit comments

Comments
 (0)