Skip to content
This repository has been archived by the owner on May 25, 2018. It is now read-only.

Commit

Permalink
update to play2.2 (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgambet committed Jan 27, 2014
1 parent 3f9ede0 commit 16fbab7
Show file tree
Hide file tree
Showing 48 changed files with 309 additions and 354 deletions.
11 changes: 6 additions & 5 deletions app/controllers/Job.scala
Expand Up @@ -83,17 +83,18 @@ object Job extends VSController {


import play.api.mvc._ import play.api.mvc._


def dispatcher(implicit id: JobId): ActionA = Action { implicit req => def dispatcher(implicit id: JobId): ActionA = Action.async { implicit req =>
(for { val action: String = (for {
body <- req.body.asFormUrlEncoded body <- req.body.asFormUrlEncoded
param <- body.get("action") param <- body.get("action")
action <- param.headOption action <- param.headOption
} yield action.toLowerCase match { } yield action.toLowerCase).getOrElse("notSpecified")
action match {
case "delete" => delete(id)(req) case "delete" => delete(id)(req)
case "run" => run(id)(req) case "run" => run(id)(req)
case "stop" => stop(id)(req) case "stop" => stop(id)(req)
case a => BadRequest(views.html.error._400(List(("error", Messages("debug.unexpected", "unknown action " + a))))) case a => Future.successful(BadRequest(views.html.error._400(List(("error", Messages("debug.unexpected", "unknown action " + a))))))
}).getOrElse(BadRequest(views.html.error._400(List(("error", Messages("debug.unexpected", "no action parameter was specified")))))) }
} }


def socket(jobId: JobId, typ: SocketType): Handler = { def socket(jobId: JobId, typ: SocketType): Handler = {
Expand Down
28 changes: 13 additions & 15 deletions app/controllers/VSController.scala
Expand Up @@ -28,7 +28,7 @@ trait VSController extends Controller {


def CloseWebsocket = (Iteratee.ignore[JsValue], Enumerator.eof) def CloseWebsocket = (Iteratee.ignore[JsValue], Enumerator.eof)


implicit def toAsynchronousResult(result: Result): Future[Result] = Future.successful(result) implicit def toAsynchronousResult(result: SimpleResult): Future[SimpleResult] = Future.successful(result)


def getUser()(implicit reqHeader: RequestHeader): Future[User] = { def getUser()(implicit reqHeader: RequestHeader): Future[User] = {
for { for {
Expand All @@ -46,7 +46,7 @@ trait VSController extends Controller {
getUser.map(Some(_)).recover{case _ => None} getUser.map(Some(_)).recover{case _ => None}
} }


def Timer(name: String)(f: Future[Result]): Future[Result] = { def Timer(name: String)(f: Future[SimpleResult]): Future[SimpleResult] = {
if (name == "") { if (name == "") {
f f
} else { } else {
Expand All @@ -57,21 +57,19 @@ trait VSController extends Controller {
} }
} }


def AsyncAction(name: String)(f: Request[AnyContent] => Future[Result]): Action[AnyContent] = Action { implicit req => def AsyncAction(name: String)(f: Request[AnyContent] => Future[SimpleResult]): Action[AnyContent] = Action.async { implicit req =>
Async {
Timer(name) { Timer(name) {
f(req) recover { f(req).recoverWith[SimpleResult] {
case AccessNotAllowed(msg) => Forbidden(views.html.error._403(msg)) case AccessNotAllowed(msg) => Future.successful(Forbidden(views.html.error._403(msg)))
case UnknownJob(_) => Global.onHandlerNotFound(req) case UnknownJob(_) => Global.onHandlerNotFound(req)
} }
} }
}
} }
def AsyncAction(f: Request[AnyContent] => Future[Result]): Action[AnyContent] = AsyncAction("")(f) def AsyncAction(f: Request[AnyContent] => Future[SimpleResult]): Action[AnyContent] = AsyncAction("")(f)


val AcceptsStream = Accepting(MimeTypes.EVENT_STREAM) val AcceptsStream = Accepting(MimeTypes.EVENT_STREAM)


def Authenticated(f: User => Future[Result])(implicit req: RequestHeader): Future[Result] = { def Authenticated(f: User => Future[SimpleResult])(implicit req: RequestHeader): Future[SimpleResult] = {
(for { (for {
user <- getUser() user <- getUser()
result <- f(user) result <- f(user)
Expand All @@ -90,7 +88,7 @@ trait VSController extends Controller {
} }
} }


def RootAction(f: Request[AnyContent] => User => Future[Result]): ActionA = AsyncAction { implicit req => def RootAction(f: Request[AnyContent] => User => Future[SimpleResult]): ActionA = AsyncAction { implicit req =>
Authenticated { user => Authenticated { user =>
user match { user match {
case user if user.isRoot => f(req)(user) case user if user.isRoot => f(req)(user)
Expand All @@ -99,21 +97,21 @@ trait VSController extends Controller {
} }
} }


def AuthenticatedAction(name: String)(f: Request[AnyContent] => User => Future[Result]): ActionA = def AuthenticatedAction(name: String)(f: Request[AnyContent] => User => Future[SimpleResult]): ActionA =
AsyncAction(name) { implicit req => Authenticated { user => f(req)(user) } } AsyncAction(name) { implicit req => Authenticated { user => f(req)(user) } }


def AuthenticatedAction(f: Request[AnyContent] => User => Future[Result]): ActionA = AuthenticatedAction("")(f) def AuthenticatedAction(f: Request[AnyContent] => User => Future[SimpleResult]): ActionA = AuthenticatedAction("")(f)


def UserAware(f: Option[User] => Future[Result])(implicit req: RequestHeader): Future[Result] = { def UserAware(f: Option[User] => Future[SimpleResult])(implicit req: RequestHeader): Future[SimpleResult] = {
for { for {
userO <- getUserOption() userO <- getUserOption()
result <- f(userO) result <- f(userO)
} yield result } yield result
} }


def UserAwareAction(name: String)(f: Request[AnyContent] => Option[User] => Future[Result]): ActionA = def UserAwareAction(name: String)(f: Request[AnyContent] => Option[User] => Future[SimpleResult]): ActionA =
AsyncAction(name) { implicit req => UserAware { user => f(req)(user) } } AsyncAction(name) { implicit req => UserAware { user => f(req)(user) } }


def UserAwareAction(f: Request[AnyContent] => Option[User] => Future[Result]): ActionA = UserAwareAction("")(f) def UserAwareAction(f: Request[AnyContent] => Option[User] => Future[SimpleResult]): ActionA = UserAwareAction("")(f)


} }
6 changes: 3 additions & 3 deletions app/model/Coupon.scala
Expand Up @@ -148,7 +148,7 @@ object Coupon {


def getAll()(implicit conf: Database): Future[List[Coupon]] = { def getAll()(implicit conf: Database): Future[List[Coupon]] = {
val cursor = collection.find(Json.obj()).cursor[JsValue] val cursor = collection.find(Json.obj()).cursor[JsValue]
cursor.toList() map { cursor.collect[List]() map {
list => list flatMap { coupon => list => list flatMap { coupon =>
try { try {
Some(coupon.as[Coupon]) Some(coupon.as[Coupon])
Expand All @@ -162,7 +162,7 @@ object Coupon {
def get(id: CouponId)(implicit conf: Database): Future[Coupon] = { def get(id: CouponId)(implicit conf: Database): Future[Coupon] = {
val query = Json.obj("_id" -> toJson(id)) val query = Json.obj("_id" -> toJson(id))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case None => throw new NoSuchCouponException(id.toString) case None => throw new NoSuchCouponException(id.toString)
case Some(json) => json.as[Coupon] case Some(json) => json.as[Coupon]
} }
Expand All @@ -171,7 +171,7 @@ object Coupon {
def get(code: String)(implicit conf: Database): Future[Coupon] = { def get(code: String)(implicit conf: Database): Future[Coupon] = {
val query = Json.obj("code" -> toJson(code)) val query = Json.obj("code" -> toJson(code))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case None => throw new NoSuchCouponException(code) case None => throw new NoSuchCouponException(code)
case Some(json) => json.as[Coupon] case Some(json) => json.as[Coupon]
} }
Expand Down
11 changes: 6 additions & 5 deletions app/model/Job.scala
Expand Up @@ -459,7 +459,7 @@ object Job {
def get(jobId: JobId)(implicit conf: Database): Future[Job] = { def get(jobId: JobId)(implicit conf: Database): Future[Job] = {
val query = Json.obj("_id" -> toJson(jobId)) val query = Json.obj("_id" -> toJson(jobId))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case None => throw UnknownJob(jobId) //new NoSuchElementException("Invalid jobId: " + jobId) case None => throw UnknownJob(jobId) //new NoSuchElementException("Invalid jobId: " + jobId)
case Some(json) => json.as[Job] case Some(json) => json.as[Job]
} }
Expand All @@ -468,7 +468,7 @@ object Job {
/**the list of all the Jobs */ /**the list of all the Jobs */
def getAll()(implicit conf: Database): Future[List[Job]] = { def getAll()(implicit conf: Database): Future[List[Job]] = {
val cursor = collection.find(Json.obj()).cursor[JsValue] val cursor = collection.find(Json.obj()).cursor[JsValue]
cursor.toList() map { cursor.collect[List]() map {
list => list flatMap { job => list => list flatMap { job =>
try { try {
Some(job.as[Job]) Some(job.as[Job])
Expand All @@ -487,7 +487,7 @@ object Job {
def getRunningJobs()(implicit conf: Database): Future[List[Job]] = { def getRunningJobs()(implicit conf: Database): Future[List[Job]] = {
val query = Json.obj("status.actorName" -> Json.obj("$exists" -> JsBoolean(true))) val query = Json.obj("status.actorName" -> Json.obj("$exists" -> JsBoolean(true)))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.toList() map { cursor.collect[List]() map {
list => list map { list => list map {
_.as[Job] _.as[Job]
} }
Expand Down Expand Up @@ -539,7 +539,7 @@ object Job {
/*def getFor(userId: UserId, jobId: JobId)(implicit conf: Database): Future[Job] = { /*def getFor(userId: UserId, jobId: JobId)(implicit conf: Database): Future[Job] = {
val query = Json.obj("_id" -> toJson(jobId)) val query = Json.obj("_id" -> toJson(jobId))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case Some(json) if (json \ "creator").asOpt[UserId] === Some(userId) => json.as[Job] case Some(json) if (json \ "creator").asOpt[UserId] === Some(userId) => json.as[Job]
case _ => throw UnknownJob(jobId) case _ => throw UnknownJob(jobId)
} }
Expand All @@ -563,7 +563,8 @@ object Job {


def save(job: Job)(implicit conf: Database): Future[Job] = { def save(job: Job)(implicit conf: Database): Future[Job] = {
val jobJson = toJson(job) val jobJson = toJson(job)
collection.insert(jobJson) map { println(Json.prettyPrint(jobJson))
collection.insert(jobJson, writeConcern = journalCommit) map {
lastError => job lastError => job
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion app/model/ResourceResponse.scala
Expand Up @@ -23,7 +23,7 @@ object ResourceResponse {
"runId" -> toJson(runId), "runId" -> toJson(runId),
"event" -> toJson("resource-response") ) "event" -> toJson("resource-response") )
val cursor = Run.collection.find(query).cursor[JsValue] val cursor = Run.collection.find(query).cursor[JsValue]
cursor.toList() map { list => cursor.collect[List]() map { list =>
list.map(json => json.as[ResourceResponseEvent](ResourceResponseEventFormat).rr).toSet list.map(json => json.as[ResourceResponseEvent](ResourceResponseEventFormat).rr).toSet
} }
} }
Expand Down
12 changes: 6 additions & 6 deletions app/model/Run.scala
Expand Up @@ -79,7 +79,7 @@ object Run {
"rd" -> BSONInteger(1), "rd" -> BSONInteger(1),
"_id" -> BSONInteger(0)) "_id" -> BSONInteger(0))
val cursor = collection.find(query, projection).cursor[JsValue] val cursor = collection.find(query, projection).cursor[JsValue]
cursor.headOption() flatMap { cursor.headOption flatMap {
case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state")) case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state"))
case Some(json) => case Some(json) =>
(json \ "rd").as[JsArray].value.collectFirst { case json if (json \ "url") == toJson(url) => (json \ "rd").as[JsArray].value.collectFirst { case json if (json \ "url") == toJson(url) =>
Expand All @@ -101,7 +101,7 @@ object Run {
"rd" -> BSONInteger(1), "rd" -> BSONInteger(1),
"_id" -> BSONInteger(0)) "_id" -> BSONInteger(0))
val cursor = collection.find(query, projection).cursor[JsValue] val cursor = collection.find(query, projection).cursor[JsValue]
cursor.headOption() flatMap { cursor.headOption flatMap {
case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state")) case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state"))
case Some(json) => Future.successful { case Some(json) => Future.successful {
val rds: Seq[ResourceData] = val rds: Seq[ResourceData] =
Expand All @@ -121,7 +121,7 @@ object Run {
"gad" -> BSONInteger(1), "gad" -> BSONInteger(1),
"_id" -> BSONInteger(0)) "_id" -> BSONInteger(0))
val cursor = collection.find(query, projection).cursor[JsValue] val cursor = collection.find(query, projection).cursor[JsValue]
cursor.headOption() flatMap { cursor.headOption flatMap {
case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state")) case None => Future.failed(new NoSuchElementException(s"${runId} does not exist or is not in Done state"))
case Some(json) => Future.successful { case Some(json) => Future.successful {
(json \ "gad").as[Seq[GroupedAssertionData]] (json \ "gad").as[Seq[GroupedAssertionData]]
Expand All @@ -140,7 +140,7 @@ object Run {
"runData" -> BSONInteger(1), "runData" -> BSONInteger(1),
"_id" -> BSONInteger(0)) "_id" -> BSONInteger(0))
val cursor = collection.find(query, projection).cursor[JsValue] val cursor = collection.find(query, projection).cursor[JsValue]
cursor.headOption() flatMap { cursor.headOption flatMap {
case None => Future.failed(new NoSuchElementException(runId.toString)) case None => Future.failed(new NoSuchElementException(runId.toString))
case Some(json) => Future.successful((json \ "runData").as[RunData]) case Some(json) => Future.successful((json \ "runData").as[RunData])
} }
Expand All @@ -158,7 +158,7 @@ object Run {
"runData" -> BSONInteger(1), "runData" -> BSONInteger(1),
"_id" -> BSONInteger(0)) "_id" -> BSONInteger(0))
val cursor = collection.find(query, projection).cursor[JsValue] val cursor = collection.find(query, projection).cursor[JsValue]
cursor.headOption() flatMap { cursor.headOption flatMap {
case None => Future.failed(new NoSuchElementException(runId.toString)) case None => Future.failed(new NoSuchElementException(runId.toString))
case Some(json) => Future.successful { case Some(json) => Future.successful {
val timestamp = (json \ "timestamp").as[DateTime] val timestamp = (json \ "timestamp").as[DateTime]
Expand All @@ -178,7 +178,7 @@ object Run {
def get(runId: RunId)(implicit conf: Database): Future[(Run, Iterable[RunAction])] = { def get(runId: RunId)(implicit conf: Database): Future[(Run, Iterable[RunAction])] = {
val query = Json.obj("runId" -> toJson(runId)) val query = Json.obj("runId" -> toJson(runId))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.toList() map { list => cursor.collect[List]() map { list =>
// the sort is done client-side // the sort is done client-side
val orderedEvents = list.map(_.as[RunEvent]).sortBy(_.timestamp) val orderedEvents = list.map(_.as[RunEvent]).sortBy(_.timestamp)
val (createRun, events) = orderedEvents match { val (createRun, events) = orderedEvents match {
Expand Down
6 changes: 3 additions & 3 deletions app/model/User.scala
Expand Up @@ -177,15 +177,15 @@ object User {
import conf._ import conf._
val query = Json.obj("_id" -> toJson(userId)) val query = Json.obj("_id" -> toJson(userId))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case Some(json) => json.as[User] case Some(json) => json.as[User]
case None => sys.error("user not found") case None => sys.error("user not found")
} }
} }


def getAll()(implicit conf: Database): Future[List[User]] = { def getAll()(implicit conf: Database): Future[List[User]] = {
val cursor = collection.find(Json.obj()).cursor[JsValue] val cursor = collection.find(Json.obj()).cursor[JsValue]
cursor.toList() map { cursor.collect[List]() map {
list => list flatMap { user => list => list flatMap { user =>
try { try {
Some(user.as[User]) Some(user.as[User])
Expand Down Expand Up @@ -280,7 +280,7 @@ object User {
import conf._ import conf._
val query = Json.obj("email" -> JsString(email.toLowerCase)) val query = Json.obj("email" -> JsString(email.toLowerCase))
val cursor = collection.find(query).cursor[JsValue] val cursor = collection.find(query).cursor[JsValue]
cursor.headOption() map { cursor.headOption map {
case Some(json) => json.as[User] case Some(json) => json.as[User]
case None => throw UnknownUser(email) case None => throw UnknownUser(email)
} }
Expand Down
60 changes: 0 additions & 60 deletions app/store/Migration.scala

This file was deleted.

2 changes: 1 addition & 1 deletion app/store/MongoStore.scala
Expand Up @@ -26,7 +26,7 @@ import scala.concurrent.ExecutionContext.Implicits.global
*/ */
object MongoStore { object MongoStore {


val journalCommit = GetLastError(awaitJournalCommit = true) val journalCommit = GetLastError(j = true)


def reInitializeDb()(implicit conf: Database): Future[Unit] = { def reInitializeDb()(implicit conf: Database): Future[Unit] = {
for { for {
Expand Down
17 changes: 9 additions & 8 deletions app/vs/Global.scala
Expand Up @@ -4,6 +4,7 @@ import play.api._
import play.api.mvc._ import play.api.mvc._
import play.api.mvc.Results._ import play.api.mvc.Results._
import play.api.Mode._ import play.api.Mode._
import concurrent.Future


object Global extends GlobalSettings with Rendering with AcceptExtractors { object Global extends GlobalSettings with Rendering with AcceptExtractors {


Expand Down Expand Up @@ -41,31 +42,31 @@ object Global extends GlobalSettings with Rendering with AcceptExtractors {
vs = null vs = null
} }


override def onHandlerNotFound(request : RequestHeader) : Result = { override def onHandlerNotFound(request : RequestHeader): Future[SimpleResult] = {
implicit val implReq = request implicit val implReq = request
Metrics.errors.e404() Metrics.errors.e404()
render { Future.successful(render {
case Accepts.Html() => NotFound(views.html.error._404()) case Accepts.Html() => NotFound(views.html.error._404())
case Accepts.Json() => NotFound case Accepts.Json() => NotFound
} })
} }


override def onError(request: RequestHeader, ex: Throwable): Result = { override def onError(request: RequestHeader, ex: Throwable): Future[SimpleResult] = {
implicit val implReq = request implicit val implReq = request
Metrics.errors.e500() Metrics.errors.e500()
render { Future.successful(render {
case Accepts.Html() => InternalServerError(views.html.error._500(List(("error", ex.getMessage)))) case Accepts.Html() => InternalServerError(views.html.error._500(List(("error", ex.getMessage))))
case Accepts.Json() => InternalServerError case Accepts.Json() => InternalServerError
} })
} }


override def onBadRequest(request: RequestHeader, error: String) = { override def onBadRequest(request: RequestHeader, error: String): Future[SimpleResult] = {
implicit val implReq = request implicit val implReq = request
error match { error match {
case "InvalidJobId" => onHandlerNotFound(request) case "InvalidJobId" => onHandlerNotFound(request)
case _ => { case _ => {
Metrics.errors.e400() Metrics.errors.e400()
BadRequest(views.html.error._400(List(("error", error)))) Future.successful(BadRequest(views.html.error._400(List(("error", error)))))
} }
} }
} }
Expand Down
1 change: 1 addition & 0 deletions build.sbt
@@ -0,0 +1 @@
logLevel := Level.Warn
2 changes: 1 addition & 1 deletion conf/application.conf
Expand Up @@ -82,7 +82,7 @@ application {
vs { vs {
akka{ akka{
loglevel = INFO loglevel = INFO
event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] loggers = ["akka.event.slf4j.Slf4jLogger"]
} }
jobactor-dispatcher { jobactor-dispatcher {
mailbox-type = org.w3.vs.actor.JobActorMailbox mailbox-type = org.w3.vs.actor.JobActorMailbox
Expand Down

0 comments on commit 16fbab7

Please sign in to comment.