debug scheduler
This commit is contained in:
parent
addd45b04d
commit
f9f5567a5e
|
@ -18,7 +18,9 @@ object PlayApp {
|
|||
|
||||
private def enableScheduler = !(loadConfig getBoolean "app.scheduler.disabled")
|
||||
|
||||
def scheduler = new Scheduler(system, enabled = enableScheduler && isServer)
|
||||
def scheduler = new Scheduler(system,
|
||||
enabled = enableScheduler && isServer,
|
||||
debug = loadConfig getBoolean "app.scheduler.debug")
|
||||
|
||||
def isDev = isMode(_.Dev)
|
||||
def isTest = isMode(_.Test)
|
||||
|
|
|
@ -4,9 +4,9 @@ import scala.concurrent.duration._
|
|||
|
||||
import akka.actor._
|
||||
import akka.pattern.{ ask, pipe }
|
||||
import ornicar.scalalib.Random.approximatly
|
||||
import ornicar.scalalib.Random.{approximatly, nextString}
|
||||
|
||||
final class Scheduler(system: ActorSystem, enabled: Boolean) {
|
||||
final class Scheduler(system: ActorSystem, enabled: Boolean, debug: Boolean) {
|
||||
|
||||
def message(freq: FiniteDuration)(to: ⇒ (ActorRef, Any)) {
|
||||
enabled ! system.scheduler.schedule(freq, randomize(freq), to._1, to._2)
|
||||
|
@ -25,11 +25,16 @@ final class Scheduler(system: ActorSystem, enabled: Boolean) {
|
|||
def future(freq: FiniteDuration, name: String)(op: ⇒ Funit) {
|
||||
enabled ! {
|
||||
val f = randomize(freq)
|
||||
name.nonEmpty ! loginfo("[cron] schedule %s every %s".format(name, freq))
|
||||
val doDebug = debug && freq > 5.seconds
|
||||
info("schedule %s every %s".format(name, freq))
|
||||
system.scheduler.schedule(f, f) {
|
||||
op onFailure {
|
||||
case e: Exception ⇒ logwarn("[CRON ERROR] (" + name + ") " + e.getMessage)
|
||||
}
|
||||
val tagged = "(%s) %s".format(nextString(3), name)
|
||||
doDebug ! info(tagged)
|
||||
val start = nowMillis
|
||||
op effectFold (
|
||||
e ⇒ err("(%s) %s".format(tagged, e.getMessage)),
|
||||
_ ⇒ doDebug ! info(tagged + " - %d ms".format(nowMillis - start))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +43,14 @@ final class Scheduler(system: ActorSystem, enabled: Boolean) {
|
|||
enabled ! system.scheduler.scheduleOnce(delay)(op)
|
||||
}
|
||||
|
||||
private def randomize(d: FiniteDuration, ratio: Float = 0.05f): FiniteDuration =
|
||||
private def info(msg: String) {
|
||||
println("[cron] " + msg)
|
||||
}
|
||||
|
||||
private def err(msg: String) {
|
||||
println("[cron error] " + msg)
|
||||
}
|
||||
|
||||
private def randomize(d: FiniteDuration, ratio: Float = 0.05f): FiniteDuration =
|
||||
approximatly(ratio)(d.toMillis) millis
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue