Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkadijs/d9e313d7626608d0881a to your computer and use it in GitHub Desktop.
Save arkadijs/d9e313d7626608d0881a to your computer and use it in GitHub Desktop.
Server-sent events in Play2 / Iteratee for Hystrix console
import play.api.libs.concurrent._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.iteratee._
import scala.concurrent.duration._
def circuitBreakers = Action {
Ok.stream(Enumerator.generateM {
val promise = Promise[String]()
Akka.system.scheduler.scheduleOnce(1 second) {
promise.completeWith(circuitBreakers)
}
promise.future.map { msg => Some(msg) }
}).as("text/event-stream")
}
// `data: ` is important
def circuitBreakers = {
import play.api.libs.json.Json
import models.InsightsModels.HystrixCommandWrites
val now = System.currentTimeMillis
val window = 10
try {
Future.sequence(stats2breaker.map {
case (service, breakerName) =>
serviceStatus(window, service).map { stats =>
val requests = stats.wsCallSuccess + stats.wsCallFail
val percents = if (requests > 0) (100 * (stats.wsCallFail / requests.toDouble)).toInt else 0
"data: " + Json.stringify(Json.toJson(HystrixCommand(breakerName, now,
CircuitBreakerStateHolder.currentlyOpen().map(_._1).contains(breakerName),
percents, stats.wsCallFail, requests, stats.cacheHit, stats.wsCallSuccess, stats.wsCallTime, window)))
}
}).map {
_.mkString("", "\n\n", "\n\n")
}
} catch {
case x: Throwable => Future failed x
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment