/engine/standalone/app/src/main/scala/pl/touk/nussknacker/engine/standalone/http/StandaloneHttpApp.scala

https://github.com/TouK/nussknacker · Scala · 89 lines · 64 code · 25 blank · 0 comment · 0 complexity · ffa63a8917b0db39f536d3891049f78f MD5 · raw file

  1. package pl.touk.nussknacker.engine.standalone.http
  2. import akka.actor.ActorSystem
  3. import akka.http.scaladsl.Http
  4. import akka.http.scaladsl.server.Directives
  5. import akka.stream.ActorMaterializer
  6. import com.typesafe.config.{Config, ConfigFactory}
  7. import com.typesafe.scalalogging.LazyLogging
  8. import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
  9. import io.dropwizard.metrics5.MetricRegistry
  10. import pl.touk.nussknacker.engine.standalone.deployment.DeploymentService
  11. import pl.touk.nussknacker.engine.standalone.utils.StandaloneContextPreparer
  12. import pl.touk.nussknacker.engine.standalone.utils.logging.StandaloneRequestResponseLogger
  13. import pl.touk.nussknacker.engine.standalone.utils.metrics.dropwizard.{DropwizardMetricsProvider, StandaloneMetricsReporter}
  14. import pl.touk.nussknacker.engine.util.loader.ScalaServiceLoader
  15. import scala.util.Try
  16. import scala.util.control.NonFatal
  17. object StandaloneHttpApp extends Directives with FailFastCirceSupport with LazyLogging with App {
  18. private val config = ConfigFactory.load()
  19. implicit val system: ActorSystem = ActorSystem("nussknacker-standalone-http", config)
  20. import system.dispatcher
  21. implicit private val materializer: ActorMaterializer = ActorMaterializer()
  22. val metricRegistry = StandaloneMetrics.prepareRegistry(config)
  23. val standaloneApp = new StandaloneHttpApp(config, metricRegistry)
  24. val managementPort = Try(args(0).toInt).getOrElse(8070)
  25. val processesPort = Try(args(1).toInt).getOrElse(8080)
  26. Http().bindAndHandle(
  27. standaloneApp.managementRoute.route,
  28. interface = "0.0.0.0",
  29. port = managementPort
  30. )
  31. Http().bindAndHandle(
  32. standaloneApp.processRoute.route(StandaloneRequestResponseLogger.get(Thread.currentThread.getContextClassLoader)),
  33. interface = "0.0.0.0",
  34. port = processesPort
  35. )
  36. }
  37. object StandaloneMetrics extends LazyLogging {
  38. def prepareRegistry(config: Config): MetricRegistry = {
  39. val metricRegistry = new MetricRegistry
  40. val metricReporters = loadMetricsReporters()
  41. metricReporters.foreach { reporter =>
  42. reporter.createAndRunReporter(metricRegistry, config)
  43. }
  44. metricRegistry
  45. }
  46. private def loadMetricsReporters(): List[StandaloneMetricsReporter] = {
  47. try {
  48. val reporters = ScalaServiceLoader.load[StandaloneMetricsReporter](Thread.currentThread().getContextClassLoader)
  49. logger.info(s"Loaded metrics reporters: ${reporters.map(_.getClass.getCanonicalName).mkString(", ")}")
  50. reporters
  51. } catch {
  52. case NonFatal(ex) =>
  53. logger.warn("Metrics reporter load failed. There will be no metrics reporter in standalone", ex)
  54. List.empty
  55. }
  56. }
  57. }
  58. class StandaloneHttpApp(config: Config, metricRegistry: MetricRegistry)(implicit as: ActorSystem)
  59. extends Directives with LazyLogging {
  60. private val contextPreparer = new StandaloneContextPreparer(new DropwizardMetricsProvider(metricRegistry))
  61. private val deploymentService = DeploymentService(contextPreparer, config)
  62. val managementRoute = new ManagementRoute(deploymentService)
  63. val processRoute = new ProcessRoute(deploymentService)
  64. }