Skip to content

Instantly share code, notes, and snippets.

@jchapuis
Created May 10, 2020 20:06
Show Gist options
  • Save jchapuis/4b8dc3729a04dce1d1946fea3bb92fb5 to your computer and use it in GitHub Desktop.
Save jchapuis/4b8dc3729a04dce1d1946fea3bb92fb5 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)
/**
* 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 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