From e31d59f63ebe5536d2d2d868703eb8896924b63d Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Wed, 4 Jul 2018 13:16:21 +0200 Subject: Use IO monad when starting servers Change-Id: I3e97161535fc721dda6109c4cb5f23a1db0afde3 Signed-off-by: Piotr Jaszczyk Issue-ID: DCAEGEN2-601 --- .../org/onap/dcae/collectors/veshv/main/main.kt | 44 +++++++++++++--------- .../veshv/main/ArgBasedServerConfigurationTest.kt | 13 +++---- 2 files changed, 32 insertions(+), 25 deletions(-) (limited to 'hv-collector-main') diff --git a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt index d1c3b4a7..f5efab2b 100644 --- a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt +++ b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt @@ -19,34 +19,38 @@ */ package org.onap.dcae.collectors.veshv.main -import arrow.core.flatMap import org.onap.dcae.collectors.veshv.boundary.Server +import org.onap.dcae.collectors.veshv.boundary.ServerHandle import org.onap.dcae.collectors.veshv.factory.CollectorFactory import org.onap.dcae.collectors.veshv.factory.ServerFactory import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory import org.onap.dcae.collectors.veshv.model.ServerConfiguration -import org.onap.dcae.collectors.veshv.utils.commandline.handleErrorsInMain +import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure +import org.onap.dcae.collectors.veshv.utils.arrow.unsafeRunEitherSync +import org.onap.dcae.collectors.veshv.utils.arrow.void +import org.onap.dcae.collectors.veshv.utils.commandline.handleWrongArgumentErrorCurried import org.onap.dcae.collectors.veshv.utils.logging.Logger private val logger = Logger("org.onap.dcae.collectors.veshv.main") private const val PROGRAM_NAME = "java org.onap.dcae.collectors.veshv.main.MainKt" -fun main(args: Array) { - ArgBasedServerConfiguration().parse(args) - .toEither() - .map(::createServer) - .map(Server::start) - .flatMap { it.attempt().unsafeRunSync() } - .fold( - { ex -> - handleErrorsInMain(ex, PROGRAM_NAME, logger) - }, - { handle -> - logger.info("Server started. Listening on ${handle.host}:${handle.port}") - handle.await().unsafeRunSync() - } - ) -} +fun main(args: Array) = + ArgBasedServerConfiguration().parse(args) + .mapLeft(handleWrongArgumentErrorCurried(PROGRAM_NAME)) + .map(::createServer) + .map { + it.start() + .map(::logServerStarted) + .flatMap(ServerHandle::await) + } + .unsafeRunEitherSync( + { ex -> + logger.error("Failed to start a server", ex) + ExitFailure(1) + }, + { logger.info("Gentle shutdown") } + ) + private fun createServer(config: ServerConfiguration): Server { val sink = if (config.dummyMode) AdapterFactory.loggingSink() else AdapterFactory.kafkaSink() @@ -60,3 +64,7 @@ private fun createServer(config: ServerConfiguration): Server { return ServerFactory.createNettyTcpServer(config, collectorProvider) } +private fun logServerStarted(handle: ServerHandle): ServerHandle { + logger.info("HighVolume VES Collector is up and listening on ${handle.host}:${handle.port}") + return handle +} diff --git a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt index a14801da..2c49cf98 100644 --- a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt +++ b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt @@ -21,6 +21,7 @@ package org.onap.dcae.collectors.veshv.main import arrow.core.Failure import arrow.core.Success +import arrow.core.identity import org.assertj.core.api.Assertions.assertThat import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe @@ -49,13 +50,11 @@ object ArgBasedServerConfigurationTest : Spek({ cut = ArgBasedServerConfiguration() } - fun parse(vararg cmdLine: String): ServerConfiguration { - val result = cut.parse(cmdLine) - return when (result) { - is Success -> result.value - is Failure -> throw AssertionError("Parsing result should be present") - } - } + fun parse(vararg cmdLine: String): ServerConfiguration = + cut.parse(cmdLine).fold( + {throw AssertionError("Parsing result should be present")}, + ::identity + ) describe("parsing arguments") { given("all parameters are present in the long form") { -- cgit 1.2.3-korg