Add cron class with online username updater job
parent
4cd5e131fc
commit
e6dc7ce4d4
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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 = {
|
||||
|
|
|
@ -4,6 +4,7 @@ mongo {
|
|||
dbName = lichess
|
||||
collection {
|
||||
game = game2
|
||||
user = user
|
||||
}
|
||||
}
|
||||
redis {
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue