How to define onStart method in scala play framework 2.4 - scala

how can i define startup job in play framework 2.4 with scala?
play framework GlobalSetting
I have already:
class StartupConfigurationModule extends AbstractModule{
override def configure(): Unit = {
Akka.system.scheduler.schedule(Duration(0,duration.HOURS),Duration(24,duration.HOURS))(Id3Service.start())
Akka.system.dispatcher
}
}

You need to register this in the modules.enabled of your application (in application.conf).
It should schedule a call to start on the Id3Service after 0 hours and then every 24 hours.
The issue is that the module doesn't declare a dependency on a running application, or more interestingly on a started actorSystem. Guice can decide to start it before the app is initialized.
The follwing is one way to force the dependency on the initialized actorSystem (and reduce the footprint of your dependency)
import javax.inject.{ Singleton, Inject }
import akka.actor.ActorSystem
import com.google.inject.AbstractModule
import scala.concurrent.duration._
class StartupConfigurationModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[Schedule]).asEagerSingleton()
}
}
#Singleton
class Schedule #Inject() (actorSystem: ActorSystem) {
implicit val ec = actorSystem.dispatcher
actorSystem.scheduler.schedule(Duration(0, HOURS), Duration(24, HOURS))(Id3Service.start())
}
object Id3Service {
def start(): Unit = println("started")
}

Related

How to execute action on Play framework startup?

I have this application using Play framework with Scala and I want to have a service that executes a method on startup of my application. I am doing everything that is said at How do I perform an action on server startup in the Scala Play Framework?. My version of Play is 2.6 and I am not using GlobalSettings for this.
package bootstrap
import com.google.inject.AbstractModule
class EagerLoaderModule extends AbstractModule {
override def configure() = {
println("EagerLoaderModule.configure")
bind(classOf[InitSparkContext]).to(classOf[InitSparkContextImpl]).asEagerSingleton()
}
}
On the application.conf I included the line play.modules.enabled += "bootstrap.EagerLoaderModule". Below is my Service that I want to start Spark context.
package bootstrap
import javax.inject.{Inject, Singleton}
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
trait InitSparkContext {
def init(): Unit
def stop(): Unit
}
#Singleton
class InitSparkContextImpl #Inject()(appLifecycle: ApplicationLifecycle) extends InitSparkContext {
override def init(): Unit = println("InitSparkContext.start")
override def stop(): Unit = println("InitSparkContext.stop")
appLifecycle.addStopHook { () =>
stop()
Future.successful(())
}
init()
}
Nothing is printed on the console, even println("EagerLoaderModule.configure") is not printed....
You can't use println on play application, you'll need to set up a Logger. So:
val log = play.api.Logger(getClass)
log.info("This happened")
Then you can use the logback file to configure your log files:
Here are some details on how to configure it:
https://www.playframework.com/documentation/2.6.x/SettingsLogger

How can a custom ApplicationLoader start Dependency Injecting Actors and get tested?

In "Dependency Injecting Actors" it's shown how to inject a parameter into the constructor of a child actor. The parent actor uses injectedChild to be allowed to pass to the child (at child creation time) only the non-injected parameter and then let Guice inject the rest. To do this, it extends InjectedActorSupport and gets the child's factory injected in the constructor:
class MyParent #Inject() (childFactory: MyChild.Factory,
#Assisted something: Something,
#Assisted somethingElse: SomethingElse) extends Actor with InjectedActorSupport
[..]
val child: ActorRef = injectedChild(childFactory(something, somethingElse), childName)
But what about the class that starts the parent and is not an actor but a custom ApplicationLoader?
How can I start the parent actor from there? No mention of this is in the documentation.
I tried doing the same for the loader as I did for parent:
class MyLoader #Inject() (parentFactory: MyParent.Factory) extends ApplicationLoader with Actor with InjectedActorSupport {
[..]
val parent = injectedChild(parentFactory(something, somethingElse), parentName)
would this be correct? How can I test it?
class MyModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[MyParent](parentName)
bindActor[MyLoader](loaderName)
bindActorFactory[MyChild, MyChild.Factory]
bindActorFactory[MyParent, MyParent.Factory]
}
}
So:
How do I start the parent from MyLoader while letting Guice dependency-inject what's required?
How can I test MyLoader?
This has been my test so far but now I need to pass the injected thingy to MyLoader and I don't know how (note the ***???**** in place of the argument which I do not know where to find):
class MyLoaderSpec(_system: ActorSystem, implicit val ec: ExecutionContext) extends TestKit(_system) with WordSpecLike with BeforeAndAfterAll with Matchers {
val loader = new SimstimLoader(???)
override def beforeAll(): Unit = {
loader.load(ApplicationLoader.createContext(new Environment(new File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test)))
}
Thanks a million in advance!
Here is how I solved this issue.
--> How to start a parent actor who needs dependency-injection.
First of all, manually starting such an actor is impossible if you, like me, need to dependency-inject an instance which you do not know how to pass and where from. The solution is to let Guice start the actor automagically. Here is how.
First, create your binder module for Guice:
class MyModule extends AbstractModule with AkkaGuiceSupport{
override def configure(): Unit = {
bindActor[Root](Root.NAME)
bind(classOf[StartupActors]).asEagerSingleton()
}
}
Then, tell Play where your binder module is located by adding the following in your conf/application.conf:
play.modules={
enabled += "my.path.to.MyModule"
}
The StartupActors is simply a class I use to log whenever the automagic start of dependency-injected actors actually takes place. I log the event so that I can be sure of when and whether it occurs:
class StartupActors #Inject() (#Named(Root.NAME) root: ActorRef) {
play.api.Logger.info(s"Initialised $root")
}
The Root actor in my case takes care of parsing a custom configuration. Since the resulting vars from the parsing is required by my parent actor and during the tests I need to mock such resulting vars, I delegate the parsing to an actor other than the parent actor, i.e., the Root actor:
object Root {
final val NAME = "THERoot"
case class ParseConfiguration()
}
class Root #Inject()(configuration: Configuration, projectDAO: ProjectDAO) extends Actor {
val resultingVar: Something = myConfigParsing()
override def preStart(): Unit = {
context.actorOf(Props(new MyParent(resultingVar: Something, somethingElse: SomethingElse, projectDAO: ProjectDAO)))
}
override def receive: Receive = {
case ParseConfiguration => sender ! myConfigParsing()
case _ => logger.error("Root actor received an unsupported message")
}
}
The ParseConfiguration message is used uniquely for testing purposes. Normally the configuration parsing occurs instead because of the initialisation of the resultingVar attribute.
This way, MyParent wont need to get anything injected. Only StartupActors and Root will get injected. MyParent will simply get projectDAO from Root and pass it on to all its children.
class MyParent(something: Something, somethingElse: SomethingElse, projectDAO: ProjectDAO) extends Actor { ... }
Finally, for completion, I'm reporting here how I wrote the tests since I had troubles finding enough information online around this as well.
import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{TestKit, TestProbe}
import com.typesafe.config.ConfigFactory
import org.mockito.Mockito.mock
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
import org.specs2.matcher.MustMatchers
import play.api.Configuration
import scala.concurrent.ExecutionContext
class RootSpec(_system: ActorSystem) extends TestKit(_system)
with WordSpecLike with BeforeAndAfterAll with MustMatchers {
implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
val conf: com.typesafe.config.Config = ConfigFactory.load()
val configuration: Configuration = Configuration(conf)
val projectDAOMock: ProjectDAO = mock(classOf[ProjectDAO])
private var mainActor: ActorRef = _
private var something: Something = Something.empty
def this() = this(ActorSystem("MySpec"))
override def afterAll: Unit = {
system.shutdown()
}
override def beforeAll(): Unit = {
mainActor = system.actorOf(Props(new Root(configuration, projectDAOMock)), Root.NAME)
}
"RootSpec: Root Actor" should {
val probe = TestProbe()
"successfully parse the configuration file" in {
probe.send(mainActor, ParseConfiguration)
something = probe.expectMsgPF() {
case msg => msg.asInstanceOf[Something]
}
}
}
}
and then I test MyParent by conveniently providing mock objects in place of vars resulting from the configuration parsing:
import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{TestKit, TestProbe}
import org.mockito.Mockito
import org.mockito.Mockito._
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
import org.specs2.matcher.MustMatchers
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}
case class AnyProjectAPI(val projectAPI: ProjectAPI) extends AnyVal
class MyParentSpec(_system: ActorSystem, implicit val ec: ExecutionContext) extends TestKit(_system)
with WordSpecLike with BeforeAndAfterAll with MustMatchers {
val something = mock(classOf[Something])
val somethingElse = mock(classOf[somethingElse])
val projectDAOMock: ProjectDAO = mock(classOf[ProjectDAO])
val projectTest: ProjectAPI = new ProjectAPI(allMyRandomConstructorArguments),
val projectsList: List[ProjectAPI] = List(projectTest)
val expectedCreationId = 1
private var parent: ActorRef = _
def this() = this(ActorSystem("MySpec"), scala.concurrent.ExecutionContext.global)
override def afterAll: Unit = {
system.shutdown()
}
override def beforeAll(): Unit = {
parent = system.actorOf(Props(new MyParent(something, somethingElse, projectDAOMock)), MyParent.NAME)
}
"MyParentTesting: parent's pull request" should {
when(myProjApi.getAllProjects).thenReturn(Future {projectsList})
val anyProject: AnyProjectAPI = AnyProjectAPI(org.mockito.Matchers.any[ProjectAPI])
Mockito.when(projectDAOMock.create(org.mockito.Matchers.any[ProjectAPI]))
.thenReturn(Future {expectedCreationId}: Future[Int])
val probe = TestProbe()
val probe1 = TestProbe()
"be successfully satisfied by all children when multiple senders are waiting for an answer" in {
probe.send(parent, UpdateProjects)
probe1.send(parent, UpdateProjects)
allChildren.foreach(child =>
probe.expectMsg(expectedCreationId))
allChildren.foreach(child =>
probe1.expectMsg(expectedCreationId))
}
}
}

Disable singleton when testing with ScalaTest in Play for Scala

I have the following #Singleton in my Play for Scala application that loads on startup:
#Singleton
class Scheduler #Inject()(#Named("mainEtl") mainEtl: ActorRef, system: ActorSystem) {
// some code
}
This is the module where Scheduler is declared. The module is enabled in application.conf:
class Module extends AbstractModule {
def configure() = {
bind(classOf[Scheduler]).asEagerSingleton
}
}
And the related module definition to configure the #Named injected object, also declared in application.conf:
class AkkaBindings extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[MainEtl]("mainEtl")
}
}
When I run any ScalaTest test, apparently the singleton starts running because I get an error saying that it doesn't find MainEtl (the object injected in the Scheduler class). The point is that I don't need to run the singleton for my tests, so I need to disable it.
This is how I invoke the Play application in my tests:
class ManageBanksTest extends PlaySpec with OneAppPerSuite with MockitoSugar {
implicit override lazy val app = new GuiceApplicationBuilder().build
// more test code
}
This is how I tried to disable it, but it doesn't work as I get the same error:
implicit override lazy val app = new GuiceApplicationBuilder()
.disable[Scheduler]
.build
Alternatively, I could mock Scheduler, but I would have to mock also the #Named injected object and I couldn't find information on how to achieve that.
Any ideas?
This is the solution: to disable the Module class not to declare Scheduler as singleton:
implicit override lazy val app = new GuiceApplicationBuilder()
.disable[Module]
.build
import com.google.inject.AbstractModule
import com.google.inject.name.Names
import org.specs2.mock.Mockito
import play.api.inject.guice.{GuiceApplicationBuilder, GuiceableModule}
val modules = Option(new AbstractModule {
override def configure() = {
val mockMainETL = mock[MainEtl]
bind(classOf[ActorRef])
.annotatedWith(Names.named("mainEtl"))
.toInstance(mockMainETL)
val mock1 = mock[ManageBanksDAO]
mock1.readMany answers { _ => Future{seqMany}}
val mockManageBanks = mock[ManageBanks]
bind(classOf[ManageBanks]).toInstance(new ManageBanks(mock1))
}
})
lazy val app = new GuiceApplicationBuilder()
.overrides(modules.map(GuiceableModule.guiceable).toSeq: _*)
.build
Try configuring your mock inside modules and add those modules while initializing your application. This will inject custom mocks.
Also instead of this :
val controller = new ManageBanks(mock1)
Try this:
val controller = app.injector.instanceOf(classOf[ManageBanks])

Play 2.4 create an actor for handle websocket with Guice

first of all thank you all for taking some time to review my problem. I'm a newbie in scala ecosystem so I think I'm confussing some concepts.
I'm introducing Guice in a Play 2.4 project and it's working in some REST controllers. I've edited build.sbt to append routesGenerator := InjectedRoutesGenerator as Play official doc recomends and edited my routes as you can see in the following example:
GET /socket #com.letgo.chat.controllers.ChatController.socket
My module for injecting actors looks like:
class ChatModule extends AbstractModule with ScalaModule with AkkaGuiceSupport with GuiceAkkaActorRefProvider {
override def configure() {
bindActor[TalkerProviderActor](TalkerProviderActor.name)
...
And all of this seems to be working.
But another endpoint handles a websocket with Play method WebSocket.acceptWithActor. I need to create an actor which also needs some dependencies.
The controller creates a ConnectionActor:
class ConnectionActor(
#Assisted() websocket: ActorRef,
monitoring: Monitoring,
#Named(BouncerActor.name) bouncer: ActorRef,
) extends Actor
...
class ChatController #Inject() extends Controller {
def socket(): WebSocket[String, JsValue] = WebSocket.acceptWithActor[String, JsValue] { request => out =>
// I had the following statement in order to build the actor before introducing Guice:
// ConnectionActor.props()
// Now, I need some magic here
}
}
So as you can see I need a websocket: ActorRef for the websocket output.
I've created some actors in other parts of the application using bindActorFactory method provided by AkkaGuiceSupport:
bindActorFactory[TalkerActor, TalkerActor.Factory]
But I don't know how I should create an actor for handling the websocket in this case. Can you guys help me?
Thank you
I believe this can do the trick:
package controllers
import javax.inject._
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.stream.Materializer
import play.api.libs.streams.ActorFlow
import play.api.libs.ws.WSClient
import play.api.mvc._
import scala.concurrent.ExecutionContext.Implicits.global
#Singleton
class SocketActorProvider #Inject() (ws: WSClient) {
def get(out: ActorRef) = Props(new SocketActor(out, ws))
}
class SocketActor(out: ActorRef, ws: WSClient) extends Actor {
override def receive: Receive = {
case "ping" => ws.url("http://localhost:9000/pong").get().foreach(pong => out ! pong.body)
}
}
#Singleton
class HomeController #Inject() (implicit system: ActorSystem, ws: WSClient, materializer: Materializer, provider: SocketActorProvider) extends Controller {
// route '/ws'
def socket = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef(out => provider.get(out))
}
// route '/pong'
def pong = Action {
Ok("PONG!")
}
// route '/'
def index = Action {
Ok("""
<script>
var ws = new WebSocket('ws://localhost:9000/ws');
ws.onopen = () => console.log('I am open!');
ws.onmessage = m => console.log(m.data);
setInterval(() => {console.log('ping'); ws.send('ping')}, 1000)
</script>
""").as("text/html")
}
}
As you can see there is a provider injected by Guice which had WSClient injected and when building an actor it passes in the dependency

Scheduling a repeating actor at system startup Play 2.4.3

I've recently started working in Scala and Play Framework and just upgraded a service I've been working on to Play 2.4.3. My end goal is to create a nightly process that starts up a service method in my play application for the purpose of scheduling events via a method call, which I'm currently calling with an Actor.
I had the basic idea of this working through a Global.scala file with an override onStart, but then I saw the play documentation about moving away from the use of GlobalSettings (https://www.playframework.com/documentation/2.4.x/GlobalSettings) and have been trying to move it to an injected dependency approach.
Here's what I've pieced together so far:
Module Code:
import javax.inject._
import com.myOrganization.myPackage.Actors.ScheduleActor
import play.api.libs.concurrent.AkkaGuiceSupport
import play.libs.Akka
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import akka.actor.{ActorRef, ActorSystem}
import scala.concurrent.duration._
import play.Application
import com.google.inject.AbstractModule
#Singleton
class NightlyEvalSchedulerStartup #Inject()(system: ActorSystem, #Named("ScheduleActor") scheduleActor: ActorRef) {
Akka.system.scheduler.schedule(10.seconds, 20.seconds, scheduleActor, "ScheduleActor")
}
class ScheduleModule extends AbstractModule with AkkaGuiceSupport {
def configure() = {
bindActor[ScheduleActor]("ScheduleActor")
bind(classOf[NightlyEvalSchedulerStartup]).asEagerSingleton
}
}
Actor Class:
import akka.actor.{Actor, Props}
import com.myOrganization.myPackage.services.MySchedulingService
object ScheduleActor {
def props = Props[ScheduleActor]
class updateSchedules
}
class ScheduleActor extends Actor {
val MySchedulingService: MySchedulingService = new MySchedulingService
def receive = {
case "runScheduler" => MySchedulingService.nightlyScheduledUpdate()
}
}
Application.conf
play.modules.enabled += "com.myOrganization.myPackage.modules.ScheduleModule"
The service is calling down to a method that is primarily based on scala logic code and database interactions via Anorm.
Every time I try to start the service up with activator start (or run, once an Http request is received) I get the following error:
Oops, cannot start the server.
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, java.lang.RuntimeException: There is no started application
I've tried running the same code by replacing the Aka.system.scheduler... piece with a simple println() and everything seemed to work fine, meaning the service started up and I saw my message on the console. So I'm guessing there's some dependency that I'm missing for the Akka scheduler that is causing it to blowup. Any suggestions you can offer would be great, I've been banging my head against this all day.
EDIT (Solved Code Per Request):
Module Code, with some added code for getting a rough estimation of 3am the next night. This might change down the line, but it works for now:
package com.myOrganization.performanceManagement.modules
import com.myOrganization.performanceManagement.Actors.ScheduleActor
import com.myOrganization.performanceManagement.Actors.ScheduleActor.nightlySchedule
import org.joda.time.{Seconds, LocalDate, LocalTime, LocalDateTime}
import play.api.libs.concurrent.AkkaGuiceSupport
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import akka.actor.{ActorRef, ActorSystem}
import scala.concurrent.duration.{FiniteDuration, SECONDS, HOURS }
import org.joda.time._
import com.google.inject.{Inject, Singleton, AbstractModule}
import com.google.inject.name.Named
class ScheduleModule extends AbstractModule with AkkaGuiceSupport {
override def configure() = {
bindActor[ScheduleActor]("ScheduleActor")
bind(classOf[NightlyEvalSchedulerStartup]).asEagerSingleton()
}
}
#Singleton
class NightlyEvalSchedulerStartup #Inject()(system: ActorSystem, #Named("ScheduleActor") scheduleActor: ActorRef) {
//Calculate initial delay to 3am the next day.
val currentTime: DateTime = DateTime.now
val targetDateTime = currentTime.plusDays(1).withTimeAtStartOfDay()
//Account for Daylight savings to an extent, not mandatory that it starts at 3am, just after midnight.
val initialDelaySeconds = targetDateTime.getHourOfDay match {
case 0 => new Duration(currentTime, targetDateTime.plusHours(3)).getStandardSeconds
case 1 => new Duration(currentTime, targetDateTime.plusHours(2)).getStandardSeconds
}
//Schedule first actor firing to occur at calculated delay and then every 24 hours.
system.scheduler.schedule(FiniteDuration(initialDelaySeconds, SECONDS), FiniteDuration(24, HOURS), scheduleActor, nightlySchedule)
}
Actor:
package com.myOrganization.performanceManagement.Actors
import akka.actor.{ActorSystem, Actor}
import com.google.inject.Inject
import com.myOrganization.performanceManagement.services.PMEvalSchedulingService
object ScheduleActor {
case object nightlySchedule
}
class ScheduleActor #Inject() (actorSystem: ActorSystem) extends Actor {
val pMEvalSchedulingService: PMEvalSchedulingService = new PMEvalSchedulingService
override def receive: Receive = {
case nightlySchedule =>
println("Called the scheduler")
pMEvalSchedulingService.nightlyScheduledEvaluationsUpdate()
}
}
Well in my case the biggest issue ended up being that when scheduling the actor call in NightlyEvalSchedulerStartup() I was calling Akka.system... which was causing the system to instantiate a new AkkaSystem before the app existed. By removing the akk system was came to represent the injected dependency which was ready to go. Hope this helps someone in the future!