From 1a641ec70963d1c98c41b824656169d324503585 Mon Sep 17 00:00:00 2001 From: Piotr Wielgolaski Date: Wed, 18 Nov 2020 14:27:20 +0100 Subject: [PATCH] feat: add configurable shutdown delay --- README.md | 2 ++ .../src/main/java/com/tomtom/james/agent/ShutdownHook.java | 5 +++-- .../com/tomtom/james/configuration/AgentConfiguration.java | 1 + .../james/configuration/AgentConfigurationFacade.java | 7 +++++++ .../configuration/EventPublisherConfigurationFacade.java | 2 +- .../groovy/com/tomtom/james/agent/ShutdownHookSpec.groovy | 1 + 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5440112..8d7d2b1 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,8 @@ example of core settings configuration: quiet: true # Don't print out ASCII banner at the start (default: false) logLevel: trace # Level of James logs - trace|debug|info|warn|error|fatal # (default: warn) +shutdownDelay: 500 # Shutdown hook delay in ms make possible to capture closing events + # default: 1000 plugins: includeDirectories: diff --git a/james-agent/src/main/java/com/tomtom/james/agent/ShutdownHook.java b/james-agent/src/main/java/com/tomtom/james/agent/ShutdownHook.java index 6a78de8..510e336 100644 --- a/james-agent/src/main/java/com/tomtom/james/agent/ShutdownHook.java +++ b/james-agent/src/main/java/com/tomtom/james/agent/ShutdownHook.java @@ -17,15 +17,15 @@ package com.tomtom.james.agent; import com.google.common.collect.ImmutableList; +import com.google.common.util.concurrent.Uninterruptibles; import com.tomtom.james.common.api.Closeable; import com.tomtom.james.common.api.publisher.EventPublisher; import com.tomtom.james.common.api.script.ScriptEngine; import com.tomtom.james.common.log.Logger; import com.tomtom.james.configuration.AgentConfiguration; -import com.tomtom.james.newagent.MethodExecutionContextHelper; - import java.util.Collection; import java.util.Objects; +import java.util.concurrent.TimeUnit; public class ShutdownHook extends Thread { @@ -50,6 +50,7 @@ public ShutdownHook(ControllersManager controllersManager, @Override public void run() { + Uninterruptibles.sleepUninterruptibly(agentConfiguration.getShutdownDelay(), TimeUnit.MILLISECONDS); closeables.forEach(Closeable::close); if (!agentConfiguration.isQuiet()) { LOG.info("Agent shutdown complete."); diff --git a/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfiguration.java b/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfiguration.java index 8729094..a47df71 100644 --- a/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfiguration.java +++ b/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfiguration.java @@ -39,5 +39,6 @@ public interface AgentConfiguration { boolean isQuiet(); Logger.Level getLogLevel(); + Integer getShutdownDelay(); } diff --git a/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfigurationFacade.java b/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfigurationFacade.java index 89c0b05..7c56d13 100644 --- a/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfigurationFacade.java +++ b/james-agent/src/main/java/com/tomtom/james/configuration/AgentConfigurationFacade.java @@ -120,6 +120,13 @@ public boolean isQuiet() { .orElse(false); } + @Override + public Integer getShutdownDelay() { + return configuration.get("shutdownDelay") + .map(StructuredConfiguration::asInteger) + .orElse(1000); + } + @Override public Logger.Level getLogLevel() { return configuration.get("logLevel") diff --git a/james-agent/src/main/java/com/tomtom/james/configuration/EventPublisherConfigurationFacade.java b/james-agent/src/main/java/com/tomtom/james/configuration/EventPublisherConfigurationFacade.java index 078bce9..23fde6e 100644 --- a/james-agent/src/main/java/com/tomtom/james/configuration/EventPublisherConfigurationFacade.java +++ b/james-agent/src/main/java/com/tomtom/james/configuration/EventPublisherConfigurationFacade.java @@ -48,7 +48,7 @@ public int getAsyncWorkers() { public int getMaxAsyncJobQueueCapacity() { return structuredConfiguration.get("maxAsyncJobQueueCapacity") .map(StructuredConfiguration::asInteger) - .orElse(10_000); + .orElse(16_384); } @Override diff --git a/james-agent/src/test/groovy/com/tomtom/james/agent/ShutdownHookSpec.groovy b/james-agent/src/test/groovy/com/tomtom/james/agent/ShutdownHookSpec.groovy index 5b057b5..7458907 100644 --- a/james-agent/src/test/groovy/com/tomtom/james/agent/ShutdownHookSpec.groovy +++ b/james-agent/src/test/groovy/com/tomtom/james/agent/ShutdownHookSpec.groovy @@ -34,6 +34,7 @@ class ShutdownHookSpec extends Specification { def "Should close resources on execution"() { when: + agentConfiguration.getShutdownDelay() >> 1 shutdownHook.run() then: