Created
February 14, 2014 04:16
-
-
Save sirmes/8995692 to your computer and use it in GitHub Desktop.
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
| package controllers | |
| import play.api.mvc._ | |
| import ProductDetailService.app.models.{ProductLike, Product} | |
| import play.api.libs.json.{Json, JsValue} | |
| import scala.concurrent.{Await, Future} | |
| import com.s5a.service.utils.Slf4JLogger | |
| import com.s5a.service.metrics.Timer | |
| import com.s5a.metrics.{MetricNamespace, Recorder} | |
| import scala.concurrent.duration._ | |
| import java.util.concurrent.{TimeoutException, TimeUnit} | |
| import scala.util.{Failure, Success} | |
| // For Future[T].map | |
| import play.api.libs.concurrent.Execution.Implicits._ | |
| trait SaksTimeout { | |
| import play.api.libs.concurrent.Promise | |
| def actionTimeout:FiniteDuration = FiniteDuration(1500, TimeUnit.MILLISECONDS) | |
| def timeout[JsValue](body : Future[JsValue]) : Future[JsValue] = { | |
| val prom = Promise.timeout(body, actionTimeout) | |
| prom.map { | |
| f => | |
| Await.result(f, actionTimeout) | |
| } | |
| } | |
| } | |
| trait ProductControllerLike extends Slf4JLogger with Timer with SaksTimeout { | |
| this: Controller => | |
| //TODO | |
| //should not be defined here, should be defined in a trait or class where actual value is assigned | |
| val actorSystemName = "pda" | |
| def recorder(tag: String, durationMillis: Long): Unit = { | |
| Recorder.time(MetricNamespace.WEBSITE, tag, durationMillis) | |
| } | |
| val productModel: ProductLike | |
| def show(productCode: String) = Action { | |
| debug(s"productCode(show): ${productCode}") | |
| time("ProductControllerLike_show") { | |
| val resultFuture: Future[JsValue] = productModel.get(productCode) | |
| Async { | |
| timeout(resultFuture).map { | |
| result => | |
| debug("result: " + result.toString()) | |
| //TODO if the result has errors, it shouldn't be "OK" | |
| time("ProductControllerLike_show_Ok") { | |
| Ok(result).as(JSON) | |
| } | |
| }.recover { | |
| case _ : TimeoutException => RequestTimeout("{\"error\" : \"bad things happens when things run into timeouts\"}") | |
| } | |
| } | |
| } | |
| } | |
| // def show(productCode: String) = Action { | |
| // debug(s"productCode(show): ${productCode}") | |
| // time("ProductControllerLike_show") { | |
| // val resultFuture: Future[JsValue] = productModel.get(productCode) | |
| // Async { | |
| // resultFuture.map { | |
| // result => | |
| // debug("result: " + result.toString()) | |
| // //TODO if the result has errors, it shouldn't be "OK" | |
| // time("ProductControllerLike_show_Ok") { | |
| // Ok(result).as(JSON) | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } | |
| def details(productCode: String) = Action { | |
| debug(s"productCode(details): ${productCode}") | |
| time("ProductControllerLike_detail") { | |
| val resultFuture: Future[JsValue] = productModel.getDetailed(productCode) | |
| Async { | |
| resultFuture.map { | |
| result => | |
| debug("result: " + result.toString()) | |
| time("ProductControllerLike_details_ok") { | |
| Ok(result) | |
| .as(JSON) | |
| .withHeaders( | |
| CACHE_CONTROL -> "max-age=3600", | |
| ETAG -> "xx" | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| object ProductController extends Controller with ProductControllerLike { | |
| override val productModel = new Product | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment