creating a scheduled task in scala with play 2.5 - scala

I want to create a scheduled task in Play 2.5. I found some resources related to this topic but none of them were for Play 2.5. I found out this resource related to what I am looking for and it looks good. Also on the same link there is a migration guide from 2.4 to 2.5.
The examples from older versions used GlobalSettings as base but this was deprecated in 2.5. The migration guide is important because it says that we should use dependency injection instead of extending this trait. I am not sure how to do that.
Can you give me some guidance?

You need to run sheduled task inside Akka Actor:
SchedulerActor.scala
package scheduler
import javax.inject.{Inject, Singleton}
import akka.actor.Actor
import org.joda.time.DateTime
import play.api.Logger
import scala.concurrent.ExecutionContext
#Singleton
class SchedulerActor #Inject()()(implicit ec: ExecutionContext) extends Actor {
override def receive: Receive = {
case _ =>
// your job here
}
}
Scheduler.scala
package scheduler
import javax.inject.{Inject, Named}
import akka.actor.{ActorRef, ActorSystem}
import play.api.{Configuration, Logger}
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
class Scheduler #Inject() (val system: ActorSystem, #Named("scheduler-actor") val schedulerActor: ActorRef, configuration: Configuration)(implicit ec: ExecutionContext) {
val frequency = configuration.getInt("frequency").get
var actor = system.scheduler.schedule(
0.microseconds, frequency.seconds, schedulerActor, "update")
}
JobModule.scala
package modules
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
import scheduler.{Scheduler, SchedulerActor}
class JobModule extends AbstractModule with AkkaGuiceSupport {
def configure() = {
bindActor[SchedulerActor]("scheduler-actor")
bind(classOf[Scheduler]).asEagerSingleton()
}
}
application.conf
play.modules.enabled += "modules.JobModule"

If you don't want to use akka, you can use java:
ScheduledFuture
ScheduledExecutorService
DemoDaemon.scala:
import java.util.concurrent.{Executors, ScheduledFuture, TimeUnit}
import javax.inject._
import play.Configuration
import scala.util.Try
class DemoDaemon #Inject() (conf: Configuration) {
val isEnabled = conf.getBoolean("daemon.enabled")
val delay = conf.getLong("daemon.delay")
private var scheduledTaskOption : Option[ScheduledFuture[_]] = None
def task(): Unit = {
Try {
println("doSomething")
} recover {
case e: Throwable => println(e.getMessage)
}
}
def start(): Unit = {
if (isEnabled) {
val executor = Executors.newScheduledThreadPool(1)
scheduledTaskOption = Some(
executor.scheduleAtFixedRate(
new Runnable {
override def run() = task()
},
delay, delay, TimeUnit.SECONDS
)
)
} else {
println("not enabled")
}
}
def stop(): Unit = {
scheduledTaskOption match {
case Some(scheduledTask) =>
println("Canceling task")
val mayInterruptIfRunning = false
scheduledTask.cancel(mayInterruptIfRunning)
case None => println("Stopped but was never started")
}
}
}
DaemonService.scala
import javax.inject.Inject
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
class DaemonService #Inject() (appLifecycle: ApplicationLifecycle, daemon: DemoDaemon) {
daemon.start()
appLifecycle.addStopHook{ () =>
Future.successful(daemon.stop())
}
}
JobModule.scala
import com.google.inject.AbstractModule
class JobModule extends AbstractModule {
def configure(): Unit = {
bind(classOf[DaemonService]).asEagerSingleton()
}
}
application.conf
daemon.enabled = true
daemon.delay = 10
play.modules.enabled += "com.demo.daemon.JobModule"

Related

Eventually didn't tolerate unsuccessful attempts before giving up

I have a simple test case which is using TestActorRef and eventually to verify a timeout call of a method. Here is details of 3 file sources:
TestRestUtility.scala
import play.api.http.HttpVerbs
import play.api.libs.ws.WSClient
import javax.inject.Inject
import scala.concurrent.Future
class TestRestUtility #Inject()(ws: WSClient) extends HttpVerbs {
import scala.concurrent.ExecutionContext.Implicits.global
def getHealthStatus(): Future[Int] = {
ws.url("https://www.google.com").get().map { response =>
response.status
}
}
}
TestHealthCheckActor.scala
import akka.actor.{Actor, ActorLogging, Props}
import play.api.http.Status
import javax.inject.Inject
import scala.concurrent.Future
import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.util.{Failure, Success}
object TestHealthCheckActor {
def props(testRestUtility: TestRestUtility): Props = {
Props(new TestHealthCheckActor(testRestUtility: TestRestUtility))
}
}
class TestHealthCheckActor #Inject()(testRestUtility: TestRestUtility)
extends Actor with ActorLogging with Status {
import context.dispatcher
val checkPeriod: FiniteDuration = 1.seconds
var apiStatus: Int = _
override def preStart(): Unit = {
context.system.scheduler.scheduleWithFixedDelay(
0.milliseconds,
checkPeriod,
self,
RefreshHealthStatus
)
}
override def receive: Receive = {
case RefreshHealthStatus =>
val health: Future[Int] = testRestUtility.getHealthStatus()
health.onComplete({
case Success(result) =>
result match {
case OK => apiStatus = Status.OK
case _ => apiStatus = Status.REQUEST_TIMEOUT
}
case Failure(e) =>
println(e)
})
}
}
TestSpec.scala
import akka.actor.{ActorSystem, PoisonPill}
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import org.mockito.Mockito
import org.mockito.Mockito.{times, verify, when}
import org.scalatest
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatest.concurrent.Eventually
import org.scalatest.matchers.should.Matchers
import org.scalatest.time.Span
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatestplus.mockito.MockitoSugar
import play.api.http.Status
import scala.concurrent.Future
class TestSpec extends TestKit(ActorSystem("HealthCheckActorSpec")) with AnyWordSpecLike with Matchers
with BeforeAndAfterEach with MockitoSugar with ImplicitSender with Eventually with BeforeAndAfterAll with Status with TestHarnessConstants {
import scala.concurrent.ExecutionContext.Implicits.global
var mockTestRestUtility: TestRestUtility = _
"HealthCheckActor" should {
"time out and call subscription api" in {
mockTestRestUtility = mock[TestRestUtility]
when(mockTestRestUtility.getHealthStatus()).thenReturn(Future(OK)).thenReturn(Future(OK)).thenReturn(Future(OK))
val healthCheckActorShortPeriod: TestActorRef[TestHealthCheckActor] = TestActorRef(TestHealthCheckActor.props(mockTestRestUtility))
eventually(timeout(Span(9, scalatest.time.Seconds)), interval(Span(1, scalatest.time.Seconds))) {
verify(mockTestRestUtility, Mockito.atLeast(3)).getHealthStatus()
}
healthCheckActorShortPeriod ! PoisonPill
}
}
}
As description about eventually in "scalatest-core_2.12-3.2.3-sources.jar", it tolerates unsuccessful attempts before giving up, so the test case is expected to have 3 calls the method getHealthStatus() to be successful as the returned value from mock. But I got a failed test case with below error message. I don't know why the method was called only one time:
testRestUtility.getHealthStatus();
Wanted *at least* 3 times:
-> at com.deere.isg.ingest.supporttool.testharness.TestSpec.$anonfun$new$8(TestSpec.scala:44)
But was 1 time:
-> at com.deere.isg.ingest.supporttool.testharness.TestHealthCheckActor$$anonfun$receive$1.applyOrElse(TestHealthCheckActor.scala:36)
org.mockito.exceptions.verification.TooFewActualInvocations:
testRestUtility.getHealthStatus();
Wanted *at least* 3 times:
-> at com.deere.isg.ingest.supporttool.testharness.TestSpec.$anonfun$new$8(TestSpec.scala:44)
But was 1 time:
-> at com.deere.isg.ingest.supporttool.testharness.TestHealthCheckActor$$anonfun$receive$1.applyOrElse(TestHealthCheckActor.scala:36)
Future(OK) will be considered successful by eventually because it doesn't throw an exception (technically neither would a failed Future, unless you unwrapped it in the eventually). Since the first call succeeded, eventually won't make any attempts after the first.

Slick database access in Actor

I have a play-scala application using SqLite with slick. My tables are defined like this:
#Singleton
class DataSets #Inject()(protected val dbConfigProvider: DatabaseConfigProvider, keys: PublicKeys) extends DataSetsComponent
with HasDatabaseConfigProvider[JdbcProfile] {
import driver.api._
val DataSets = TableQuery[DataSetsTable]
def all = db.run(DataSets.sortBy { _.id }.result)
...
}
My controllers get access via DI:
#Singleton
class DataSetsController #Inject() (dataSets: DataSets, env: play.Environment) extends Controller {
...
How do I get a database handle in an Actor?
class TrainActor #Inject() (dataSets: DataSets) extends Actor {
...
of course does not work as Guice does not find the DataSets class.
Edit: to clarify: I do not want to use the actor for database access in the controller (via ask), but to start some resource intensive computations from the controller after a request and store them in the db afterwards (async).
I now found a way which integrates with DI, closely following the official documentation. Because for the need of an ActorContext, InjectedActorSupport can only be inherited by Actors. This means I had to create an actor which does nothing than instantiate and start new "worker" Actors. Maybe there is an easier way, but this works correctly.
TrainActor.scala:
package actors
import javax.inject.Inject
import akka.actor._
import com.google.inject.assistedinject.Assisted
import models.{DataSet, Model, PublicKey}
import play.api.Logger
import tables.DataSets
import scala.concurrent.ExecutionContext.Implicits.global
object TrainActor {
case object Start
case class LoadData(d: DataSet, k: PublicKey)
trait Factory {
def apply(model: Model): Actor
}
}
class TrainActor #Inject() (val dataSets: DataSets, #Assisted val model: Model) extends Actor {
import TrainActor._
def receive = {
case Start =>
dataSets.findWithKey(model.id.get)
...
TrainActorStarter.scala:
package actors
import javax.inject.Inject
import akka.actor.{Actor, ActorRef}
import models.Model
import play.api.libs.concurrent.InjectedActorSupport
object TrainActorStarter {
case class StartTraining(model: Model)
}
/**
* https://www.playframework.com/documentation/2.5.x/ScalaAkka#Dependency-injecting-actors
* #param childFactory
*/
class TrainActorStarter #Inject() (childFactory: TrainActor.Factory) extends Actor with InjectedActorSupport {
import TrainActorStarter._
def receive = {
case StartTraining(model: Model) =>
val trainer: ActorRef = injectedChild(childFactory(model), s"train-model-model-${model.id.get}")
trainer ! TrainActor.Start
}
}
ActorModule.scala:
package actors
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
class ActorModule extends AbstractModule with AkkaGuiceSupport {
def configure(): Unit = {
bindActor[TrainActorStarter]("train-actor-starter")
bindActorFactory[TrainActor, TrainActor.Factory]
}
}
And finally in the controller:
package controllers
import javax.inject._
import actors.{TrainActorStarter, TrainCallbackActor}
import akka.actor.{ActorRef, ActorSystem, _}
import akka.stream.Materializer
...
#Singleton
class ModelsController #Inject() (implicit system: ActorSystem, materializer: Materializer, ..., #Named("train-actor-starter") trainActorStarter: ActorRef) extends Controller with InjectedActorSupport {
def startTraining(model: Model): Unit = {
if(model.id.isEmpty) return
trainActorStarter ! TrainActorStarter.StartTraining(model)
}
You can inject dependencies into an actor:
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[TrainActor]("injected-train-actor")
}
}
After that just inject actor into controller:
class MyController #Inject()(#Named("injected-train-actor") trainActor: ActorRef) {
def endpointTest = Action.async {
for {
items <- (trainActor ? FetchAll).mapTo[Seq[DataSetsTableRow]]
} yield Ok(Json.toJson(items))
}
}
Instead of having
#Singleton
class DataSets
one can declare it as a simple scala object that can act as the DataSetsDAO
object DataSets
and then in the actor just use DataSets.dbOperation just bear in mind that the result type of that will be a Future, so just schedule a message to self in the actor on the onComplete to avoid any side effects.

Scala Play 2.4 run akka scheduler service on Application start

I want to use Akka scheduler to do some cron jobs in my Play application.
Since Play 2.4 GlobalSettings is not recommended. Anyone has some sample code on how to do that?
Even I tried the simple code from Play docs and it still not working.
class CustomApplicationLoader extends GuiceApplicationLoader() {
val logger: Logger = Logger(this.getClass)
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
logger.info("start")
val extra = Configuration("a" -> 1)
initialBuilder
.in(context.environment)
.loadConfig(extra ++ context.initialConfiguration)
.overrides(overrides(context): _*)
}
}
play.application.loader = "com.xxx.CustomApplicationLoader"
Why I can't get the logging message to print?
How I can use Akka on Application start?
This is how do it in my app:
Start of by defining a trait Scheduled.
package scheduled
import akka.actor.Cancellable
trait Scheduled {
var cancellable: Option[Cancellable] = None
val defaultInterval = 60
val secondsToWait = {
import scala.concurrent.duration._
10 seconds
}
def start()
def stop() = {
cancellable.map(_.cancel())
}
}
Then implement Scheduled with Akka magic
package scheduled
import akka.actor.ActorSystem
import play.api.{Configuration, Logger}
import com.google.inject.{Singleton, Inject}
import play.api.libs.concurrent.Execution.Implicits._
trait ScheduledWorker extends Scheduled
#Singleton
class ScheduledWorkerImpl #Inject()(
actorSystem: ActorSystem,
configuration: Configuration
) extends ScheduledWorker {
start()
lazy val intervalKey = "worker.interval"
lazy val jobEnabled = "worker.enabled"
override def start(): Unit = {
import scala.concurrent.duration._
lazy val i = configuration.getInt(intervalKey).getOrElse(defaultInterval)
lazy val isEnabled = Option(System.getProperty(jobEnabled)).getOrElse(
configuration.getString(jobEnabled).getOrElse("false")
).equals("true")
cancellable = isEnabled match {
case true =>
Some(
actorSystem.scheduler.schedule(0 seconds, i minutes) {
.. MAJOR COOL CODE!!! ;))) ...
}
)
case _ => None
}
}
}
create a module to eagerly start the scheduled stuff
package modules
import play.api.{Configuration, Environment}
import play.api.inject.Module
import scheduled.{ScheduledWorker}
class ScheduledModule extends Module {
def bindings(environment: Environment,
configuration: Configuration) = Seq(
bind[ScheduledWorker].to[ScheduledWorkerImpl].eagerly()
)
}
make sure that your config specifies ScheduledModule.
play.modules.enabled += "modules.ScheduledModule"
And voila you have a working scheduled task when your play 2.4 app starts =)

Implementing Akka in Play Framework 2.4 for Scala

I am trying to replicate the basic example proposed in the Integrating with Akka, Play 2.4 for Scala doc. But I have difficulties in placing the final pieces together...
I have defined the actor (see paragraph Writing actors) at app/actors/HelloActor.scala with the following code:
package actors
import akka.actor._
object HelloActor {
def props = Props[HelloActor]
case class SayHello(name: String)
}
class HelloActor extends Actor {
import HelloActor._
def receive = {
case SayHello(name: String) =>
sender() ! "Hello, " + name
}
}
Then (see Creating and using actors) I suppose I should create a controller at app/controllers/Hello.scala with something like:
package controllers
import play.api.mvc._
import akka.actor._
import javax.inject._
import actors.HelloActor
#Singleton
class Hello #Inject() (system: ActorSystem) extends Controller {
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
...
}
The question: where and how I utilize the code in the following paragraph Asking things of actors to have a working solution? I have tried to add it to the above Hello.scala controller but without success.
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
implicit val timeout = 5.seconds
def sayHello(name: String) = Action.async {
(helloActor ? SayHello(name)).mapTo[String].map { message =>
Ok(message)
}
}
Found the solution, I had some problems with defining the implicit timeout, this is the working controller:
package controllers
import play.api.mvc._
import akka.actor._
import javax.inject._
import actors.HelloActor
import actors.HelloActor.SayHello
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
import akka.util.Timeout
#Singleton
class Hello #Inject() (system: ActorSystem) extends Controller {
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
implicit val timeout: Timeout = 5.seconds
def sayHello(name: String) = Action.async {
(helloActor ? SayHello(name)).mapTo[String].map { message ⇒
Ok(message)
}
}
}
Plus I added the following route in app/conf/routes:
# Actor test
GET /hello/:name controllers.Hello.sayHello(name)

How do I wait for an actor to stop during Play Framework shutdown?

The below code throws a java.lang.IllegalMonitorStateException exception when I shutdown the play server; it is caused by the a.wait(1000) call in the onStop method. Could anyone tell me why this is the case and how to gracefully wait for an actor to complete within Play framework shutdown?
import play.api.GlobalSettings
import play.api.libs.concurrent.Akka
import akka.actor.{ Actor, Props }
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
import models.{ TestActor, StartMessage, StopMessage }
object Global extends GlobalSettings {
override def onStart(application : play.api.Application) {
val a = Akka.system.actorOf(Props[TestActor], name = "test-actor")
a ! StartMessage("Start instruction")
}
override def onStop(application : play.api.Application) {
val a = Akka.system.actorSelection("akka://application/user/test-actor")
a ! StopMessage("Stop instruction")
a.wait(1000)
Akka.system.shutdown()
}
}
Update:
Here is the complete solution, taking the below answer:
import play.api.GlobalSettings
import play.api.libs.concurrent.Akka
import akka.actor.{ Actor, Props }
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
import models.{ TestActor, StartMessage, StopMessage }
import akka.pattern.gracefulStop
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.Await
import com.typesafe.config.impl.ResolveContext
import com.typesafe.config.impl.ResolveContext
import akka.actor.ActorIdentity
object Global extends GlobalSettings {
override def onStart(application : play.api.Application) {
val a = Akka.system.actorOf(Props[TestActor], name = "test-actor")
a ! StartMessage("Start Instruction")
}
override def onStop(application : play.api.Application) {
val a = Akka.system.actorFor("akka://application/user/test-actor")
a ! StopMessage("Stop Instruction")
try {
val stopped : Future[Boolean] = gracefulStop(a, scala.concurrent.duration.Duration(5, "seconds"))
Await.result(stopped, scala.concurrent.duration.Duration(6, "seconds"))
// the actor has been stopped
}
catch {
case e : akka.pattern.AskTimeoutException => // the actor wasn't stopped within 5 seconds
}
Akka.system.shutdown()
Akka.system.awaitTermination()
}
}
"Graceful Stop
gracefulStop is useful if you need to wait for termination or compose ordered termination of several actors:
import akka.pattern.gracefulStop
import akka.dispatch.Await
import akka.actor.ActorTimeoutException
try {
val stopped: Future[Boolean] = gracefulStop(actorRef, 5 seconds)(system)
Await.result(stopped, 6 seconds)
// the actor has been stopped
} catch {
case e: ActorTimeoutException ⇒ // the actor wasn't stopped within 5 seconds
}
"
http://doc.akka.io/docs/akka/2.0/scala/actors.html#Graceful_Stop
PS. Googling for "graceful actor akka" gives the answer as the top result.