Add cron class with online username updater job

pull/1/merge
Thibault Duplessis 2012-03-19 21:15:37 +01:00
parent 4cd5e131fc
commit e6dc7ce4d4
5 changed files with 45 additions and 3 deletions

28
app/Cron.scala 100644
View File

@ -0,0 +1,28 @@
package lila.http
import play.api._
import play.api.libs.concurrent.Akka
import akka.actor._
import akka.util.duration._
import akka.util.Duration
import scalaz.effects._
import lila.system.SystemEnv
final class Cron(env: SystemEnv)(implicit app: Application) {
spawn("online-username", 3 seconds) { env
env.userRepo updateOnlineUsernames env.usernameMemo.keys
}
object Tick
def spawn(name: String, freq: Duration)(f: SystemEnv IO[Unit]) = {
val actor = Akka.system.actorOf(Props(new Actor {
def receive = {
case Tick f(env).unsafePerformIO
}
}), name = name)
Akka.system.scheduler.schedule(freq, freq, actor, Tick)
}
}

View File

@ -14,6 +14,7 @@ object Global extends GlobalSettings {
override def onStart(app: Application) {
systemEnv = new SystemEnv(app.configuration.underlying)
new Cron(systemEnv)(app)
}
override def onHandlerNotFound(request: RequestHeader): Result = {

View File

@ -4,6 +4,7 @@ mongo {
dbName = lichess
collection {
game = game2
user = user
}
}
redis {

View File

@ -39,6 +39,9 @@ final class SystemEnv(config: Config) {
def gameRepo = new GameRepo(
mongodb(config getString "mongo.collection.game"))
def userRepo = new UserRepo(
mongodb(config getString "mongo.collection.user"))
def mongodb = MongoConnection(
config getString "mongo.host",
config getInt "mongo.port"

View File

@ -12,8 +12,17 @@ import scalaz.effects._
class UserRepo(collection: MongoCollection)
extends SalatDAO[User, ObjectId](collection) {
def updateOnlineUsernames(usernames: Iterable[String]): IO[Unit] = {
println("yep")
io(Unit)
def updateOnlineUsernames(usernames: Iterable[String]): IO[Unit] = io {
val names = usernames map (_.toLowerCase)
collection.update(
("usernameCanonical" $nin names.pp) ++ ("isOnline" -> true),
$set ("isOnline" -> false),
upsert = false,
multi = true)
collection.update(
("usernameCanonical" $in names) ++ ("isOnline" -> false),
$set ("isOnline" -> true),
upsert = false,
multi = true)
}
}