Last active
August 29, 2015 14:09
-
-
Save arkadijs/d9e313d7626608d0881a to your computer and use it in GitHub Desktop.
Server-sent events in Play2 / Iteratee for Hystrix console
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 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