Handle sigterm signal 45/75445/3
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Tue, 8 Jan 2019 10:00:41 +0000 (11:00 +0100)
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Tue, 8 Jan 2019 12:15:47 +0000 (13:15 +0100)
Change-Id: If6f431bfdc42f8d53497078b18813147cad1bad0
Issue-ID: DCAEGEN2-1065
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
development/docker-compose.yml
sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt
sources/hv-collector-main/src/main/scripts/entry.sh
sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt
sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/server_handle.kt
sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt [new file with mode: 0644]

index adf8947..5d39a73 100644 (file)
@@ -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:
index c29c5d1..16da372 100644 (file)
@@ -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()
-
index 2e8cb0c..a612e39 100755 (executable)
@@ -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}
index e7aca55..99ecfd7 100644 (file)
@@ -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
+        }
index bdb63b6..b8784c6 100644 (file)
@@ -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 (file)
index 0000000..2678a8d
--- /dev/null
@@ -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()
+    }
+}