Skip to content

Instantly share code, notes, and snippets.

@jchapuis
Created May 10, 2020 20:18
Show Gist options
  • Save jchapuis/91b10103fee203ce8615a782a0b47700 to your computer and use it in GitHub Desktop.
Save jchapuis/91b10103fee203ce8615a782a0b47700 to your computer and use it in GitHub Desktop.
Markdium-Orchestrating startup and shutdown in Scala
Observable
.fromTask(startTriggered)
.ambWith(Observable.fromTask(stopTriggered))
.flatMap(_ =>
Observable
.fromIterable(services)
.takeUntil(Observable.fromTask(stopTriggered))
.mapEval(startService)
.map(_.asRight)
.onErrorHandleWith {
case failure: ServiceStartFailed => Observable(failure.error.asLeft)
}
.takeWhileInclusive(_.isRight)
.scan(StartResults.empty)(
(results, lastStartResult) =>
lastStartResult match {
case Left(failure) => results.copy(failure = Some(failure))
case Right(service)=>
registerForServiceStopOrError(service, triggerStop)
results.copy(startedServices = results.startedServices :+ service)
}
): Observable[StartResults]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment