Hi all, I would like to run a small server in kto...
# ktor
c
Hi all, I would like to run a small server in ktor, and I need a simple GUI to perform some customization on it. (I thought would be a good chance to get into jetbrains compose 😁*)* I can’t figure out you to stop and re-start server either with “embedded” nor “Engine version on ktor At the moment relevant code looks like
Copy code
@Composable
fun MainScreen() {

    val ktorServer = embeddedServer(Jetty, port = 8080,) {
       
    ...module declaration....
 
    }

    Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
        Button(modifier = Modifier.align(Alignment.CenterHorizontally),
            onClick = {
                ktorServer.start(wait = false)
            }
        ) {
            Text("START")
        }
        Button(modifier = Modifier.align(Alignment.CenterHorizontally),
            onClick = {
                ktorServer.stop(5000, 6000, TimeUnit.MILLISECONDS)
            }
        ) {
            Text("STOP")
        }
    }
}
At the moment • “start” button click works as expected and the server run properly • “stop” doesn’t take any action at all!!!! The only way, I figure out to stop the server is by shutdown-url, but in this way the all app is closed (I would like to keep the compose windows open with the change of re-start server again) Using EngineMain like this:
Copy code
scope.launch(Dispatchers.Default) {
   val args = arrayOf<String>()
   EngineMain.main(args)
}
Again I can run the server but don’t know how to stop it.
a
I recommend calling
stop
on the server instance and then creating a new instance by calling
embeddedServer
to replace the reference for your
ktorServer
property. Unfortunately, there is no way to restart a server (KTOR-2762) because stop is actually means destroy.
c
what do you mean “calling stop on server instance” 🤔
because at the moment if i run
Copy code
ktorServer.stop(x,y,Timeunit.MILLISECONDS)
nothing happen!
a
What engine do you use? What do you expect to happen when you call
stop
?
c
What engine do you use?
Engine of embeddedServer is set to Jetty
What do you expect to happen when you call stop ?
I would like server get stopped (or killed due of that you said above) The strange behavior I can’t understand is: after declare a var like
ktorServer = embeddedServer(Jetty, port = 8080) {
authentication()
logging()
routing()
security()
serialization()
webSocket()
}
with
ktorServer.start(false)
server started properly with
ktorServer.stop(1000, 2000, Timeunit.MILLISECONS)
server doesn’t stop
a
I cannot reproduce it with the following code:
Copy code
suspend fun main(args: Array<String>): Unit = coroutineScope {
    launch {
        val server = embeddedServer(Jetty, port = 7070) {
            routing {
                get("/") {
                    call.respondText { "Hello" }
                }
            }
        }

        server.start(false)
        delay(2000)
        server.stop(1000, 2000, TimeUnit.MILLISECONDS)
        println("stopped")
    }

    launch {
        while (true) {}
    }
}
Please file an issue and attach a code snippet there to reproduce your problem.
c
OK, My bad….really sorry. I didn’t reference the right object when I tried to stop the server. Everything works smoothly now Many thanks