aboutsummaryrefslogtreecommitdiffstats
path: root/hv-collector-main
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-07-04 13:16:21 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 12:01:26 +0200
commite31d59f63ebe5536d2d2d868703eb8896924b63d (patch)
tree0ed280beb287c59fbd7a3494684884f5738e377d /hv-collector-main
parente5ce5ac06cf1ce95a65f18ad05be9e8432be6d49 (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')
-rw-r--r--hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt44
-rw-r--r--hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt13
2 files changed, 32 insertions, 25 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
+}
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") {