From dbed1427e0c6083fd8cbc1a0c3b2704d778e0096 Mon Sep 17 00:00:00 2001 From: vlad Date: Fri, 9 Sep 2016 18:22:09 -0700 Subject: Async app startup --- src/main/scala/com/drivergrp/core/app.scala | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/main/scala/com/drivergrp') diff --git a/src/main/scala/com/drivergrp/core/app.scala b/src/main/scala/com/drivergrp/core/app.scala index 6438ab5..fc51307 100644 --- a/src/main/scala/com/drivergrp/core/app.scala +++ b/src/main/scala/com/drivergrp/core/app.scala @@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory import spray.json.DefaultJsonProtocol import scala.compat.Platform.ConcurrentModificationException -import scala.concurrent.Await +import scala.concurrent.{Await, Future} import scala.concurrent.duration._ object app { @@ -37,14 +37,14 @@ object app { implicit private lazy val materializer = ActorMaterializer()(actorSystem) private lazy val http = Http()(actorSystem) - def run() = { + def run(): Unit = { activateServices(modules) scheduleServicesDeactivation(modules) bindHttp(modules) - Console.print(s"${this.getClass.getName} App is started") + Console.print(s"${this.getClass.getName} App is started\n") } - def stop() = { + def stop(): Unit = { http.shutdownAllConnectionPools().onComplete { _ => val _ = actorSystem.terminate() val terminated = Await.result(actorSystem.whenTerminated, 30.seconds) @@ -95,12 +95,14 @@ object app { } } - val _ = http.bindAndHandle(route2HandlerFlow(handleExceptions(generalExceptionHandler) { - logRequestResult("log")(modules.map(_.route).foldLeft(versionRt ~ swaggerRoutes)(_ ~ _)) - }), interface, port)(materializer) + val _ = Future { + http.bindAndHandle(route2HandlerFlow(handleExceptions(generalExceptionHandler) { + logRequestResult("log")(modules.map(_.route).foldLeft(versionRt ~ swaggerRoutes)(_ ~ _)) + }), interface, port)(materializer) + } } - protected def versionRoute(version: String, buildNumber: Int) = { + protected def versionRoute(version: String, buildNumber: Int): Route = { import SprayJsonSupport._ import DefaultJsonProtocol._ @@ -117,7 +119,7 @@ object app { /** * Initializes services */ - protected def activateServices(services: Seq[Module]) = { + protected def activateServices(services: Seq[Module]): Unit = { services.foreach { service => Console.print(s"Service ${service.name} starts ...") try { -- cgit v1.2.3