Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a configurable delay before shutting down services #2238

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions misk/api/misk.api
Original file line number Diff line number Diff line change
Expand Up @@ -1507,8 +1507,8 @@ public final class misk/web/SocketAddress$Unix : misk/web/SocketAddress {
}

public final class misk/web/WebConfig : wisp/config/Config {
public fun <init> (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;Z)V
public synthetic fun <init> (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZI)V
public synthetic fun <init> (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZIILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()I
public final fun component10 ()Ljava/lang/Integer;
public final fun component11 ()I
Expand All @@ -1523,15 +1523,16 @@ public final class misk/web/WebConfig : wisp/config/Config {
public final fun component2 ()J
public final fun component20 ()Ljava/util/Map;
public final fun component21 ()Z
public final fun component22 ()I
public final fun component3 ()I
public final fun component4 ()Ljava/lang/String;
public final fun component5 ()Lmisk/web/WebSslConfig;
public final fun component6 ()Lmisk/web/WebUnixDomainSocketConfig;
public final fun component7 ()Z
public final fun component8 ()Ljava/lang/Integer;
public final fun component9 ()Ljava/lang/Integer;
public final fun copy (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;Z)Lmisk/web/WebConfig;
public static synthetic fun copy$default (Lmisk/web/WebConfig;IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZILjava/lang/Object;)Lmisk/web/WebConfig;
public final fun copy (IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZI)Lmisk/web/WebConfig;
public static synthetic fun copy$default (Lmisk/web/WebConfig;IJILjava/lang/String;Lmisk/web/WebSslConfig;Lmisk/web/WebUnixDomainSocketConfig;ZLjava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;IIIZLmisk/web/exceptions/ActionExceptionLogLevelConfig;Ljava/lang/Integer;DZILjava/util/Map;ZIILjava/lang/Object;)Lmisk/web/WebConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getAcceptors ()Ljava/lang/Integer;
public final fun getAction_exception_log_level ()Lmisk/web/exceptions/ActionExceptionLogLevelConfig;
Expand All @@ -1552,6 +1553,7 @@ public final class misk/web/WebConfig : wisp/config/Config {
public final fun getPort ()I
public final fun getQueue_size ()Ljava/lang/Integer;
public final fun getSelectors ()Ljava/lang/Integer;
public final fun getShutdown_sleep_ms ()I
public final fun getSsl ()Lmisk/web/WebSslConfig;
public final fun getUnix_domain_socket ()Lmisk/web/WebUnixDomainSocketConfig;
public fun hashCode ()I
Expand Down
6 changes: 6 additions & 0 deletions misk/src/main/kotlin/misk/MiskApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.beust.jcommander.ParameterException
import com.google.common.annotations.VisibleForTesting
import com.google.common.util.concurrent.ServiceManager
import com.google.inject.Guice
import com.google.inject.Key
import com.google.inject.Module
import misk.inject.KAbstractModule
import misk.inject.getInstance
import misk.web.WebConfig
import wisp.logging.getLogger

/** The entry point for misk applications */
Expand Down Expand Up @@ -88,6 +90,10 @@ class MiskApplication(private val modules: List<Module>, commands: List<MiskComm
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
log.info { "received a shutdown hook! performing an orderly shutdown" }
if (injector.getExistingBinding(Key.get(WebConfig::class.java)) != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would make a lot more sense at the start of JettyService#shutDown(). Saves having to check with the injector too, and makes the functionality testable (at least, in theory—testing a sleep call probably won't be nice).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that does not actually help with the problem - the issue is with other services being shut down before the jetty service is finished servicing requests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could move the sleep to the hibernate service instead, I suppose, at the risk of uncovering other services that may also be affected in the same way. Sleeping here avoids the risk of playing whack-a-mole for something that is intended to be a short-term workaround.

val config = injector.getInstance<WebConfig>()
sleep(config.shutdown_sleep_ms.toLong())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my attempt to make the delay configurable. It's not clear to me what other avenues exist for making it possible for consumers to customize behaviour at this layer of the framework.

}
serviceManager.stopAsync()
serviceManager.awaitStopped()
log.info { "orderly shutdown complete" }
Expand Down
3 changes: 3 additions & 0 deletions misk/src/main/kotlin/misk/web/WebConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ data class WebConfig(

/** If true, disables automatic load shedding when degraded. */
val concurrency_limiter_disabled: Boolean = false,

/** The number of milliseconds to sleep before commencing service shutdown. */
val shutdown_sleep_ms: Int = 0,
) : Config

data class WebSslConfig(
Expand Down