Skip to content

Commit

Permalink
Only perform the "Try stopping a cancelled server" on manual cancel
Browse files Browse the repository at this point in the history
Before, we had this issue:

1: Terminal 1 - `kobweb run`, wait for server to start
2: Terminal 2 - `kobweb run`, say "yes" that you'd like to stop the running server
3: Terminal 1 - Detects its server got cancelled. Looks for an active server and tries to shut it down.
4: Terminal 2 - Detects its server got cancelled.

After this change, the third step will not follow up on the cancellation
event; not all cancel types are created equal. We only want to respond to the
user's direct action here.
  • Loading branch information
bitspittle committed Oct 22, 2024
1 parent ab11c16 commit ff1a8f5
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions kobweb/src/main/kotlin/com/varabyte/kobweb/cli/run/Run.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.varabyte.kotter.foundation.input.onKeyPressed
import com.varabyte.kotter.foundation.liveVarOf
import com.varabyte.kotter.foundation.runUntilSignal
import com.varabyte.kotter.foundation.shutdown.addShutdownHook
import com.varabyte.kotter.foundation.text.bold
import com.varabyte.kotter.foundation.text.cyan
import com.varabyte.kotter.foundation.text.green
import com.varabyte.kotter.foundation.text.red
Expand Down Expand Up @@ -124,11 +123,12 @@ private fun handleRun(
}
val serverStateFile = ServerStateFile(kobwebFolder)

val ellipsisAnim = textAnimOf(Anims.ELLIPSIS)
var runState by liveVarOf(RunState.STARTING)
val gradleAlertBundle = GradleAlertBundle(this)
var userRequestedCancelWhileBuilding = false

run {
val ellipsisAnim = textAnimOf(Anims.ELLIPSIS)
var runState by liveVarOf(RunState.STARTING)
var serverState: ServerState? = null // Set on and after RunState.RUNNING
var cancelReason by liveVarOf("")
var exception by liveVarOf<Exception?>(null) // Set if RunState.INTERRUPTED
Expand Down Expand Up @@ -225,6 +225,7 @@ private fun handleRun(
startServerProcess.onCompleted = {
cancelReason = "User quit before server could confirm it had started up."
runState = RunState.CANCELLED
userRequestedCancelWhileBuilding = true
signal()
}
}
Expand Down Expand Up @@ -278,9 +279,8 @@ private fun handleRun(
var runningServerDetected by liveVarOf(false)
// Only wait for a running server if at least one task has run. If the user cancelled their run before
// that early, the chance of a server starting is zero.
if (runState == RunState.CANCELLED && gradleAlertBundle.hasFirstTaskRun) {
val timeToWaitMs = 5000
var remainingTimeMs by liveVarOf(timeToWaitMs) // In tests, we usually detect a server within 2 seconds.
if (userRequestedCancelWhileBuilding && gradleAlertBundle.hasFirstTaskRun) {
var remainingTimeMs by liveVarOf(5000) // In practice, we usually detect a server within 2 seconds.

fun Int.msToSecTimeString() = "${this / 1000}.${(this % 1000).toString().padEnd(3, '0')}s"

Expand Down

0 comments on commit ff1a8f5

Please sign in to comment.