diff options
author | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-07-04 13:16:21 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-02 12:01:26 +0200 |
commit | e31d59f63ebe5536d2d2d868703eb8896924b63d (patch) | |
tree | 0ed280beb287c59fbd7a3494684884f5738e377d /hv-collector-main/src/main | |
parent | e5ce5ac06cf1ce95a65f18ad05be9e8432be6d49 (diff) |
Use IO monad when starting servers
Change-Id: I3e97161535fc721dda6109c4cb5f23a1db0afde3
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-main/src/main')
-rw-r--r-- | hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt | 44 |
1 files changed, 26 insertions, 18 deletions
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<String>) { - 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<String>) = + 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 +} |