I have a jobs_queue
collection in MongoDB. It's a capped collection which I'm polling using a tailable cursor:
val cur =
jobsQueue
.find(Json.obj("done" -> Json.obj("$ne" -> true)))
.options(QueryOpts().tailable.awaitData)
.cursor[JsObject]
cur.enumerate() |>>> Iteratee.foreach { queuedDoc =>
// do some processing and store the results back in the DB
}
This is being called from a regular Scala App
, so there's no Akka or Play wrapping at all.
What would be the most appropriate way to make sure the App
doesn't exit until I explicitly break out of the Iteratee.foreach
? Also, I don't have to use play-iteratees at all if there's a simpler (even if slightly less elegant) way.
P.S. I do ensure the collection is capped:
val jobsQueueMaybe = db.collection[JSONCollection]("jobs_queue")
val jobsQueue: JSONCollection =
jobsQueueMaybe.stats()
.flatMap {
case stats if !stats.capped =>
jobsQueueMaybe.convertToCapped(size = 1024 * 1024, maxDocuments = None)
case _ =>
Future(jobsQueueMaybe)
}
.recover { case _ => jobsQueueMaybe.createCapped(size = 1024 * 1024, maxDocuments = None) }
.map { _ => jobsQueueMaybe }
P.P.S.
I will also appreciate any criticism as to how I've designed this bit of logic, and how I could solve this by rethinking my approach and slightly overhauling the implementation.