Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6aa83fd

Browse files
committed
impl: improved poll job lifecycle report
Poll jobs now have a random friendly name that changes each time a new job is created, and the logging for when the jobs are created or cancelled is also improved. The idea behind this commit is to improve debugging sessions on client logs by: - having clear log lines saying when a job is created/destructed - easy to remember and cross-match job names (instead of cryptic java reference values)
1 parent 35e64f2 commit 6aa83fd

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.coder.toolbox.sdk.ex.APIResponseException
77
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
88
import com.coder.toolbox.util.CoderProtocolHandler
99
import com.coder.toolbox.util.DialogUi
10+
import com.coder.toolbox.util.friendlyName
11+
import com.coder.toolbox.util.name
1012
import com.coder.toolbox.util.toURL
1113
import com.coder.toolbox.util.waitForTrue
1214
import com.coder.toolbox.util.withPath
@@ -89,7 +91,7 @@ class CoderRemoteProvider(
8991
* first time).
9092
*/
9193
private fun poll(client: CoderRestClient, cli: CoderCLIManager): Job =
92-
context.cs.launch(CoroutineName("Workspace Poller")) {
94+
context.cs.launch(CoroutineName("Workspace Poller - ${friendlyName()}")) {
9395
var lastPollTime = TimeSource.Monotonic.markNow()
9496
while (isActive) {
9597
try {
@@ -242,7 +244,10 @@ class CoderRemoteProvider(
242244
* Also called as part of our own logout.
243245
*/
244246
override fun close() {
245-
pollJob?.cancel()
247+
pollJob?.let {
248+
it.cancel()
249+
context.logger.info("Cancelled workspace poll job ${pollJob.name()}")
250+
}
246251
client?.close()
247252
lastEnvironments.clear()
248253
environments.value = LoadableState.Value(emptyList())
@@ -327,6 +332,7 @@ class CoderRemoteProvider(
327332

328333
environments.showLoadingMessage()
329334
pollJob = poll(restClient, cli)
335+
context.logger.info("Workspace poll job with name ${pollJob.name()} was created while handling URI $uri")
330336
isInitialized.waitForTrue()
331337
}
332338
} catch (ex: Exception) {
@@ -406,13 +412,13 @@ class CoderRemoteProvider(
406412
this.client = client
407413
pollJob?.let {
408414
it.cancel()
409-
context.logger.info("Workspace poll job with reference ${pollJob} was canceled")
415+
context.logger.info("Cancelled workspace poll job ${pollJob.name()} in order to start a new one")
410416
}
411417
environments.showLoadingMessage()
412418
coderHeaderPage.setTitle(context.i18n.pnotr(client.url.toString()))
413419
context.logger.info("Displaying ${client.url} in the UI")
414420
pollJob = poll(client, cli)
415-
context.logger.info("Workspace poll job created with reference $pollJob")
421+
context.logger.info("Workspace poll job with name ${pollJob.name()} was created")
416422
}
417423

418424
private fun MutableStateFlow<LoadableState<List<CoderRemoteEnvironment>>>.showLoadingMessage() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coder.toolbox.util
2+
3+
import kotlinx.coroutines.CoroutineName
4+
import kotlinx.coroutines.Job
5+
6+
fun Job?.name(): String = this?.get(CoroutineName)?.toString() ?: this.toString()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coder.toolbox.util
2+
3+
import kotlin.random.Random
4+
5+
val adjectives = listOf(
6+
"brave", "calm", "clever", "curious", "eager",
7+
"fast", "gentle", "happy", "kind", "lively",
8+
"mighty", "noble", "quiet", "rapid", "shiny",
9+
"swift", "tough", "vast", "wise", "young"
10+
)
11+
12+
val nouns = listOf(
13+
"bear", "cloud", "dragon", "eagle", "fire",
14+
"forest", "hawk", "lion", "moon", "mountain",
15+
"owl", "panther", "river", "shadow", "sky",
16+
"star", "storm", "tree", "wolf", "wind"
17+
)
18+
19+
/**
20+
* Easy to remember names, helps with logs investigation
21+
*/
22+
fun friendlyName(): String {
23+
val number = Random.nextInt(10, 99) // 2 digits for extra uniqueness
24+
return "${adjectives.random()}-${nouns.random()}-$number"
25+
}

0 commit comments

Comments
 (0)