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?
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With