Q:

How to return value from scheduler

I would like to update a value (used later) periodically inside akka http server, the only way i found is to use a scheduler, but i'm not able to return a value from that. 
i tried with akka scheduler, i should maybe use Future but don't see how. 
The point is to not restart the http server when the value is updated 
def initializeWebServer(interface : String, port : Int) = {

system.scheduler.schedule(0 seconds, 10 seconds){
 val result = /***/
}

val route: Route =
  concat(
    get{
      path("getResult"){
        complete(result)
      }
    }
  )

val bindingFuture = Http().bindAndHandle(route, interface, port.toInt)
println(s"Server online at http://$interface:$port/")

CoordinatedShutdown(system).addJvmShutdownHook({
  bindingFuture
    .flatMap(_.unbind())
})

}

What i would like is something like : 
val result = schedule( 10 sec){do something and return result}

A:

I think you want something like this:
var result = 0
val cancellable = scheduler.schedule(0.seconds, 10.seconds) {
  result = result + 1
}

You can use the cancellable value to stop the schedule when it is no longer needed.

Here is the full code:
def initializeWebServer(interface: String, port: Int) = {

  var result = 0
  val canceallable = context.system.scheduler.schedule(0.seconds, 10.seconds) {
    result = result + 1
  }

  val route: Route =
    concat(
      get {
        path("getResult") {
          complete(result)
        }
      }
    )

  val bindingFuture = Http().bindAndHandle(route, interface, port.toInt)
  println(s"Server online at http://$interface:$port/")

  CoordinatedShutdown(system).addJvmShutdownHook({
    bindingFuture
      .flatMap(_.unbind())
  })
}

