Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Akka actors in Play

I have a Play! application with some tasks I need to run periodically. I can schedule the tasks using Akka, but I am not sure how to start the scheduler itself. What I am doing right now is having a Scheduler object and starting it from Global.scala, like this

// app/jobs/Scheduler.scala
package jobs

import akka.util.duration._
import play.api.libs.concurrent.Akka
import play.api.Play.current

object Scheduler {
  def start() {
    Akka.system.scheduler.schedule(0 seconds, 1 minutes) {
      SomeTask.start()
    }
  }
}

and then

// app/Global.scala
import play.api._
import jobs.Scheduler

object Global extends GlobalSettings {
  override def onStart(app: Application) {
    Scheduler.start()
  }
}

The problem is that in this, the task runs even in development mode and during tests, that becomes soon very annoying.

Is there a way to schedule jobs with Akka only in production mode?

like image 850
Andrea Avatar asked Dec 06 '12 08:12

Andrea


1 Answers

Methods isProd, isDev and isTest on Play object could be helpful. Even if you doesn't have implicit Application in scope, you can pass it explicitly

override def onStart(app: Application) {
  if (isProd(app)) Scheduler.start()
}
like image 182
4e6 Avatar answered Sep 30 '22 18:09

4e6