summerNight
summerNight

Reputation: 1496

Wait for DB transaction results before sending successful JSON response

I have the following function inside a db object that uses reactivemongo to write documents to mongoDB.

def writeDocument(json: JsValue):Future[WriteResult] = {

  def collection: JSONCollection = {
     val driver = new MongoDriver
     val connection = driver.connection(List("localhost"))
     val db = connection("superman")
     db.collection[JSONCollection]("Powers")
  }

val document = json.as[JsObject]
val future = collection.insert(document)
future.onComplete {
  case Failure(e) => {
    logger.error("""Message="Database is not reachable." """ + """ transactionID=""" + (json \\ "transactionID") + " errorResponse=" + e.getLocalizedMessage)
    throw e}
  case Success(result) =>
    println("successfully inserted document with result = " + result)
}
future
 }

On the controller side, I have this part of code which calls the above writeDocument() function. Since reactivemongo is asynchronous, I always get a success JSON message even if the DB is not alive. I know that the DB writes are happening via Futures, how do I make sure that I only send a successful response back only after the data is written in the DB successfully?

def validateRequest(json: JsValue): Result = {

{
  val logger = LoggerFactory.getLogger("superman")
  val jsonSchema = Source.fromURL(getClass.getResource("/schema.json")).getLines.mkString
  val transactionID = (json \ "transactionID").get
  val result: VA[JsValue] = SchemaValidator.validate(Json.fromJson[SchemaType](
    Json.parse(jsonSchema.stripMargin)).get, json)

  result.fold(
    invalid = { errors =>

      var violatesList = List[String]()
      val invalidError = Json.obj("transactionID" -> transactionID, "status" -> "error", "description" -> "Invalid Request Received")
      for (msg <- (errors.toJson \\ "msgs")) {
        violatesList = (msg(0).get).as[String] :: violatesList
      }
      val errorResponse = Json.toJson(invalidError ++ Json.obj("violations" -> violatesList))
      logger.error( """Message="Invalid Request Received" for transactionID=""" + transactionID.toString() + "errorResponse:" + errorResponse)
      BadRequest(errorResponse)

    },

    valid = {
      post =>
        val insertResult = db.writeDocument(json)
        insertResult.map{ result =>
          val json = Json.obj("transactionID" -> transactionID.toString,
            "status" -> "OK", "message" -> ("Valid Request Received"))
          Ok(json)
        }.recover{ case error: Throwable =>
          val json = Json.obj("transactionID" -> transactionID.toString,
            "status" -> "FAILED")
          Ok(json)
        }




  )
}

}

Upvotes: 0

Views: 451

Answers (2)

David Frank
David Frank

Reputation: 6092

You need to make the following changes:

1.At the end of writeDocument instead of (or in addition to) handling the future directly, you should return the future.

2.In your controller you should map the future to a response, by writing something like:

def myAction() = Action.async { request =>
  insertResult.map{ result =>
    val json = Json.obj("transactionID" -> transactionID.toString,
                        "status" -> "OK", "message" -> ("Valid Request Received"))
    Ok(json)
  }.recover{ case error: Throwable =>
    val json = Json.obj("transactionID" -> transactionID.toString,
                        "status" -> "FAILED")
    Ok(json)
  }
}

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53839

Simply forward the future and map it to the wanted Result.

Play will serve this result as soon as the promise is redeemed: Play Async

def writeDocument(json: JsValue): Future[LastError] = {
  // ...
  future
}


valid = { post =>
  val future = db.writeDocument(json)
  future.map { _ =>
    val successResponse = Json.obj("transactionID" -> transactionID.toString, "status" -> "OK", "message" -> ("Valid Request Received"))
    Ok(successResponse)
  }
}

Upvotes: 1

Related Questions