Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Last active March 14, 2016 23:14
Show Gist options
  • Save andyczerwonka/494be0801961aaae2a76 to your computer and use it in GitHub Desktop.
Save andyczerwonka/494be0801961aaae2a76 to your computer and use it in GitHub Desktop.
Example of class that isn't being called
import akka.actor._
import javax.inject._
import database.MongoEnvironment
import play.api.inject.ApplicationLifecycle
import play.api._
import scala.concurrent.ExecutionContext
@Singleton
class Tyrion @Inject()(config: Configuration,
lifecycle: ApplicationLifecycle,
akka: ActorSystem,
mongoEnvironment: MongoEnvironment,
implicit val ec: ExecutionContext) {
import scala.concurrent.duration.{FiniteDuration, Duration, DurationInt}
implicit val db = mongoEnvironment.db
startMockSiteDaemon()
startSiteCommandDaemon()
lifecycle.addStopHook { () =>
akka.terminate()
}
def startMockSiteDaemon() = {
config.getString("tyrion.mocksite.apikey") map { apikey =>
val image = config.getString("tyrion.mocksite.image").getOrElse(throw new RuntimeException("Missing image"))
val imageInterval = config.getMilliseconds("tyrion.mocksite.imageInterval").getOrElse(0L)
val data = config.getDoubleSeq("tyrion.mocksite.data").map(_.map(_.toDouble)).getOrElse(Seq.empty)
val dataInterval = config.getMilliseconds("tyrion.mocksite.dataInterval").getOrElse(0L)
val siteAgent = akka.actorOf(Props[MockSiteAgent], "Mock-Site")
akka.scheduler.schedule(0 second, Duration(imageInterval, "millisecond"), siteAgent, SiteImage(apikey, image))
akka.scheduler.schedule(2 second, Duration(dataInterval, "millisecond"), siteAgent, SiteReading(akka, apikey, data))
}
}
def startSiteCommandDaemon() = {
// TODO: refactor this to remove the actor
val commandAgent = akka.actorOf(Props[CommandAgent], "CommandAgent")
akka.scheduler.schedule(0 seconds, 20 seconds, commandAgent, AddCommandToEachSite)
val logsDuration = config.getMilliseconds("tyrion.site.logs").getOrElse(60L * 60L * 1000L)
akka.scheduler.schedule(0 seconds, Duration(logsDuration, "millisecond")) {
import models.{Command, Site, LogsRequest}
Site.findAll() foreach { site =>
Command.push(site, LogsRequest :: Nil)
}
}
val locationDuration = config.getMilliseconds("tyrion.site.location").getOrElse(60L * 60L * 1000L * 24)
akka.scheduler.schedule(0 seconds, Duration(locationDuration, "millisecond")) {
import models.{Command, Site, LocationRequest}
Site.findAll() foreach { site =>
Command.push(site, LocationRequest :: Nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment