Created
February 10, 2017 11:16
-
-
Save meddulla/4e2254d0674bc0a3f9ff6cbd0982994b to your computer and use it in GitHub Desktop.
Shutdown play framework gracefully
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from https://github.com/playframework/playframework/issues/1789 | |
| import scala.concurrent.duration.Duration; | |
| import scala.concurrent.duration.FiniteDuration; | |
| import play.api.Application; | |
| import play.api.Play; | |
| import akka.actor.ActorSystem; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import sun.misc.Signal; | |
| import java.util.concurrent.TimeUnit; | |
| import javax.inject.Inject; | |
| import javax.inject.Provider; | |
| import javax.inject.Singleton; | |
| @Singleton | |
| public class SignalHandler { | |
| private static final Logger LOG = LoggerFactory.getLogger(SignalHandler.class); | |
| private static final FiniteDuration STOP_DELAY = Duration.create(40, TimeUnit.SECONDS); | |
| private volatile boolean isShuttingDown = false; | |
| @Inject | |
| SignalHandler(ActorSystem actorSystem, Provider<Application> applicationProvider) { | |
| Signal.handle(new Signal("TERM"), new sun.misc.SignalHandler() { | |
| @Override | |
| public void handle(Signal signal) { | |
| isShuttingDown = true; | |
| LOG.info("Termination required, swallowing SIGTERM to allow current requests to finish"); | |
| actorSystem.scheduler().scheduleOnce(STOP_DELAY, () -> { | |
| Play.stop(applicationProvider.get()); | |
| }, actorSystem.dispatcher()); | |
| } | |
| }); | |
| } | |
| public boolean isShuttingDown() { | |
| return isShuttingDown; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment