Created
May 10, 2020 20:18
-
-
Save jchapuis/1bf9f2f8599666ebccc3a625c697aad8 to your computer and use it in GitHub Desktop.
Markdium-Orchestrating startup and shutdown in Scala
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
import scala.concurrent.{ ExecutionContext, Future, Promise } | |
/** | |
* Stoppable[T]: represents a process with a | |
* termination function of type `T` | |
*/ | |
trait Stoppable[T] { | |
protected implicit def executionContext: ExecutionContext | |
private val stoppedPromise = Promise[T]() | |
private val stopTriggeredPromise = Promise[Unit]() | |
/** | |
* Notify we have been stopped | |
* @param stopResult Result of stop operation | |
*/ | |
protected def notifyStopped(stopResult: T): Unit = stoppedPromise.trySuccess(stopResult) | |
/** | |
* Watch for stop operation completion | |
* @return Returned Future will complete with stop result upon stop | |
*/ | |
def stopped: Future[T] = stoppedPromise.future | |
/** | |
* Trigger stop: this only emits a notification in [[stopTriggered]] | |
*/ | |
def triggerStop(): Unit = stopTriggeredPromise.trySuccess(()) | |
/** | |
* Watch for stop triggers | |
* @return Returned Future will complete with unit upon stop triggered by a call to [[triggerStop]] | |
*/ | |
def stopTriggered: Future[Unit] = stopTriggeredPromise.future | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment