Skip to content

Instantly share code, notes, and snippets.

@jchapuis
Last active May 20, 2020 11:02
Show Gist options
  • Save jchapuis/01a28cf559b2e5c809abedb9bb52293e to your computer and use it in GitHub Desktop.
Save jchapuis/01a28cf559b2e5c809abedb9bb52293e to your computer and use it in GitHub Desktop.
Markdium-Orchestrating startup and shutdown in Scala
implicit class FluentCoordinatedShutdown(coordinatedShutdown: CoordinatedShutdown) {
/**
* The first pre-defined phase that applications can add tasks to.
*/
def beforeServiceUnbind(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, task, name)
/**
* Stop accepting new incoming connections.
* This is where you can register tasks that makes a server stop accepting new connections. Already
* established connections should be allowed to continue and complete if possible.
*/
def serviceUnbind(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseServiceUnbind, task, name)
/**
* Wait for requests that are in progress to be completed.
* This is where you register tasks that will wait for already established connections to complete, potentially
* also first telling them that it is time to close down.
*/
def serviceRequestsDone(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseServiceRequestsDone, task, name)
/**
* Final shutdown of service endpoints.
* This is where you would add tasks that forcefully kill connections that are still around.
*/
def serviceStop(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseServiceStop, task, name)
/**
* Phase for custom application tasks that are to be run
* after service shutdown and before cluster shutdown.
*/
def beforeClusterShutdown(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseBeforeClusterShutdown, task, name)
/**
* Phase for custom application tasks that are to be run
* after cluster shutdown and before ActorSystem termination.
*/
def beforeActorSystemTerminate(task: () => Future[Done], name: String): CoordinatedShutdown =
withTask(CoordinatedShutdown.PhaseBeforeActorSystemTerminate, task, name)
private def withTask(phase: String, task: () => Future[Done], name: String) = {
coordinatedShutdown.addTask(phase, name)(task)
coordinatedShutdown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment