Andrew Jones
Andrew Jones

Reputation: 1382

Scheduling Recurring Jobs with Blocking Operations

I have a set of several Akka jobs that run at different schedules. Recently, I added a few jobs that are to run once a day that require reading a file from the disk and processing the data. Because reading from the disk is a blocking operation, my code waits and doesn't run any scheduled jobs when the schedule operation is performed. Does anyone know of a way for the scheduling thread to not be blocked by the pending disk IO operations? I've included the code of one of my actors below.

The scheduler code:

lazy val system = akka.actor.ActorSystem("system")
lazy val emailActor = system.actorOf(Props[EmailActor], name = "EmailActor")

system.scheduler.schedule(3 hours, 24 hours)(emailActor ! System.currentTimeMillis)

The actor implementation:

class EmailActor extends Actor {
  override def receive = {
    case _ => EmailSyncer.process()
  }
}

The processor:

def process() = {
  DataWarehouse.dataWarehouse withSession {
    val file = "/some/file/name"
    val emails = Try { Source.fromFile(file).getLines.map(l => Email(l)) }

    ...
  }
}

Upvotes: 3

Views: 322

Answers (1)

Dante Romero
Dante Romero

Reputation: 1230

This is a common problem in Akka. And so a pattern to solve it is readily available. Look into "Bulkheading". I've included a blog post below that shows you exactly what you need to do.

The general idea is that you can put different actors on different execution contexts or in slang "failure zones". This allows you to keep resource exhaustion and other problems from bleeding over into other parts of your app. Only the one execution context hits starvation, nothing else.

You can also tune the thread pools differently based on what each failure zone is doing. Generally, a large # of threads for blocking operations and for computation-intensive things it would be one thread per core to start and tune from there.

Good Luck.

http://letitcrash.com/post/40755146949/tuning-dispatchers-in-akka-applications

Upvotes: 3

Related Questions