From 0d3d921285f397239e739790bf62d1cb8768ca7b Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Tue, 8 Jan 2019 11:00:41 +0100 Subject: Handle sigterm signal Change-Id: If6f431bfdc42f8d53497078b18813147cad1bad0 Issue-ID: DCAEGEN2-1065 Signed-off-by: Piotr Jaszczyk --- .../veshv/utils/logging/reactive_logging.kt | 2 +- .../dcae/collectors/veshv/utils/server_handle.kt | 11 +++++- .../dcae/collectors/veshv/utils/shutdown_hook.kt | 41 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt (limited to 'sources/hv-collector-utils/src/main') 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 Flux.filterFailedWithLog(logger: Logger, logger.trace(context, it) Mono.just(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 @@ -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 { 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 + * @since January 2019 + */ + +fun registerShutdownHook(job: () -> Unit) { + Runtime.getRuntime().addShutdownHook(object : Thread() { + override fun run() { + job() + } + }) +} + +fun registerShutdownHook(job: IO) = IO { + registerShutdownHook { + job.unsafeRunSync() + } +} -- cgit 1.2.3-korg