summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2019-01-08 11:00:41 +0100
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2019-01-08 13:15:47 +0100
commit0d3d921285f397239e739790bf62d1cb8768ca7b (patch)
tree4f4005dd58970e0f5c4a30d5c408f773182ce6aa
parent8b4e282df3863042c69dae60460ec2397e12562e (diff)
Handle sigterm signal
Change-Id: If6f431bfdc42f8d53497078b18813147cad1bad0 Issue-ID: DCAEGEN2-1065 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
-rw-r--r--development/docker-compose.yml4
-rw-r--r--sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt10
-rwxr-xr-xsources/hv-collector-main/src/main/scripts/entry.sh17
-rw-r--r--sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt2
-rw-r--r--sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt11
-rw-r--r--sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt41
6 files changed, 77 insertions, 8 deletions
diff --git a/development/docker-compose.yml b/development/docker-compose.yml
index adf8947d..5d39a738 100644
--- a/development/docker-compose.yml
+++ b/development/docker-compose.yml
@@ -67,7 +67,9 @@ services:
"--config-url", "http://consul-server:8500/v1/kv/veshv-config?raw=true",
"--kafka-bootstrap-servers", "message-router-kafka:9092",
"--key-store-password", "onaponap",
- "--trust-store-password", "onaponap"]
+ "--trust-store-password", "onaponap",
+ "--first-request-delay", "2",
+ "--log-level", "DEBUG"]
environment:
JAVA_OPTS: "-Dio.netty.leakDetection.level=paranoid"
healthcheck:
diff --git a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
index c29c5d16..16da3721 100644
--- a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
+++ b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
@@ -30,6 +30,7 @@ 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.commandline.handleWrongArgumentErrorCurried
import org.onap.dcae.collectors.veshv.utils.logging.Logger
+import org.onap.dcae.collectors.veshv.utils.registerShutdownHook
private const val VESHV_PACKAGE = "org.onap.dcae.collectors.veshv"
private val logger = Logger("$VESHV_PACKAGE.main")
@@ -44,7 +45,7 @@ fun main(args: Array<String>) =
logger.withError { log("Failed to start a server", ex) }
ExitFailure(1)
},
- { logger.info { "Gentle shutdown" } }
+ { logger.info { "Finished" } }
)
private fun startAndAwaitServers(config: ServerConfiguration) =
@@ -52,7 +53,8 @@ private fun startAndAwaitServers(config: ServerConfiguration) =
Logger.setLogLevel(VESHV_PACKAGE, config.logLevel)
logger.info { "Using configuration: $config" }
HealthCheckServer.start(config).bind()
- VesServer.start(config).bind()
- .await().bind()
+ VesServer.start(config).bind().run {
+ registerShutdownHook(shutdown()).bind()
+ await().bind()
+ }
}.fix()
-
diff --git a/sources/hv-collector-main/src/main/scripts/entry.sh b/sources/hv-collector-main/src/main/scripts/entry.sh
index 2e8cb0c5..a612e393 100755
--- a/sources/hv-collector-main/src/main/scripts/entry.sh
+++ b/sources/hv-collector-main/src/main/scripts/entry.sh
@@ -2,4 +2,19 @@
set -euo pipefail
-java ${JAVA_OPTS:-''} -cp '*:' org.onap.dcae.collectors.veshv.main.MainKt $@
+pid=-1
+
+function handle_sigterm() {
+ if [[ ${pid} -ge 0 ]]; then
+ echo "Caught SIGTERM signal. Redirecting to process with pid=${pid}"
+ kill -TERM "${pid}"
+ wait ${pid}
+ fi
+ exit 143 # 128 + 15 -- SIGTERM
+}
+trap "handle_sigterm" SIGTERM
+
+java ${JAVA_OPTS:-} -cp '*:' org.onap.dcae.collectors.veshv.main.MainKt $@ &
+pid=$!
+echo "Service started with pid=${pid}"
+wait ${pid}
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt
index e7aca55d..99ecfd74 100644
--- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt
+++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt
@@ -71,4 +71,4 @@ fun <T> Flux<T>.filterFailedWithLog(logger: Logger,
logger.trace(context, it)
Mono.just<T>(t)
})
- } \ No newline at end of file
+ }
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt
index bdb63b68..b8784c64 100644
--- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt
+++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt
@@ -20,7 +20,9 @@
package org.onap.dcae.collectors.veshv.utils
import arrow.effects.IO
+import org.onap.dcae.collectors.veshv.utils.logging.Logger
import reactor.netty.DisposableServer
+import java.time.Duration
/**
* @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
@@ -37,10 +39,17 @@ abstract class ServerHandle(val host: String, val port: Int) {
*/
class NettyServerHandle(private val ctx: DisposableServer) : ServerHandle(ctx.host(), ctx.port()) {
override fun shutdown() = IO {
- ctx.disposeNow()
+ logger.info { "Graceful shutdown" }
+ ctx.disposeNow(SHUTDOWN_TIMEOUT)
+ logger.info { "Server disposed" }
}
override fun await() = IO<Unit> {
ctx.channel().closeFuture().sync()
}
+
+ companion object {
+ val logger = Logger(NettyServerHandle::class)
+ private val SHUTDOWN_TIMEOUT = Duration.ofSeconds(10)
+ }
}
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt
new file mode 100644
index 00000000..2678a8d5
--- /dev/null
+++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt
@@ -0,0 +1,41 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2019 NOKIA
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcae.collectors.veshv.utils
+
+import arrow.effects.IO
+
+/**
+ * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
+ * @since January 2019
+ */
+
+fun registerShutdownHook(job: () -> Unit) {
+ Runtime.getRuntime().addShutdownHook(object : Thread() {
+ override fun run() {
+ job()
+ }
+ })
+}
+
+fun registerShutdownHook(job: IO<Unit>) = IO {
+ registerShutdownHook {
+ job.unsafeRunSync()
+ }
+}