Skip to content

Instantly share code, notes, and snippets.

@leogrim
Last active June 29, 2016 22:49
Show Gist options
  • Save leogrim/00be2141739051b6d33794e7c7cc848b to your computer and use it in GitHub Desktop.
Save leogrim/00be2141739051b6d33794e7c7cc848b to your computer and use it in GitHub Desktop.
trait BatchProcessingActor[T] extends ConcurrentTaskProcessingActor[Seq[T]] { _: Actor =>
final protected val minConcurrentTasks: Int = 1
final protected val maxConcurrentTasks: Int = 1
final protected def pullTasks(limit: Int): Future[Seq[Seq[T]]] = {
if (limit == 1) nextBatch.map(Seq(_).filter(_.nonEmpty))(immediately) else Future.successful(Seq.empty)
}
final protected def processTasks(tasks: Seq[Seq[T]]): Map[Seq[T], Future[Unit]] = {
tasks.map { batch => batch -> processBatch(batch) }.toMap
}
protected def nextBatch: Future[Seq[T]]
protected def processBatch(batch: Seq[T]): Future[Unit]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment