<commit_before>package services.actors

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Cancellable
import akka.actor.Props
import akka.actor.actorRef2Scala
import models.command.Command
import play.api.libs.iteratee.Concurrent
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.libs.Akka
import services.commands.CmdToUser

case class Ping(idUser: String)
case class Ask()

/**
 * only in sse mode
 */
object PingActor {

  private val system: ActorSystem = ActorSystem("userInfos");

  def create(idUser: String, channelOut: Concurrent.Channel[JsValue]) = {
    val actor = system.actorOf(Props(new PingActor(idUser, channelOut)))
    system.eventStream.subscribe(actor, classOf[Ping])
    actor
  }

  def ping(userId: String) = {
    system.eventStream.publish(Ping(userId))
  }

}

class PingActor(idUser: String, channel:Concurrent.Channel[JsValue]) extends Actor {

  val scheduler = Akka.system.scheduler.schedule(0 second, 30 second) {
    self ! Ask
  }

  var schedulerKill: Cancellable = null

  def receive = {
    case Ask => {
      CmdToUser.sendTo(idUser, Command("ping"))
      if (schedulerKill == null || schedulerKill.isCancelled) {
        schedulerKill = Akka.system.scheduler.scheduleOnce(60 second) {
          self ! Dead
        }
      }
    }
    case Ping(id) => {
      if (id == idUser) {
        schedulerKill.cancel
      }
    }
    case Dead => {
      CmdToUser.sendTo(idUser, Command("disconnect"))
      UserInfosActor.killActorsForUser(idUser)<commit_msg>fix bug in prod : ping actor throw nullpointer and kill all server<commit_after>package services.actors

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Cancellable
import akka.actor.Props
import akka.actor.actorRef2Scala
import models.command.Command
import play.api.libs.iteratee.Concurrent
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.libs.Akka
import services.commands.CmdToUser

case class Ping(idUser: String)
case class Ask()

/**
 * only in sse mode
 */
object PingActor {

  private val system: ActorSystem = ActorSystem("userInfos");

  def create(idUser: String, channelOut: Concurrent.Channel[JsValue]) = {
    val actor = system.actorOf(Props(new PingActor(idUser, channelOut)))
    system.eventStream.subscribe(actor, classOf[Ping])
    actor
  }

  def ping(userId: String) = {
    system.eventStream.publish(Ping(userId))
  }

}

class PingActor(idUser: String, channel:Concurrent.Channel[JsValue]) extends Actor {

  val scheduler = Akka.system.scheduler.schedule(0 second, 30 second) {
    self ! Ask
  }

  var schedulerKill: Cancellable = null

  def receive = {
    case Ask => {
      CmdToUser.sendTo(idUser, Command("ping"))
      if (schedulerKill == null || schedulerKill.isCancelled) {
        schedulerKill = Akka.system.scheduler.scheduleOnce(60 second) {
          self ! Dead
        }
      }
    }
    case Ping(id) => {
      if (id == idUser && schedulerKill != null) {
        schedulerKill.cancel
      }
    }
    case Dead => {
      CmdToUser.sendTo(idUser, Command("disconnect"))
      UserInfosActor.killActorsForUser(idUser)
{'fraction_non_alphanumeric': 0.10699466508595139, 'fraction_numerical': 0.0035566093657379964, 'mean_word_length': 3.7202797202797204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5507031', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1062, 'n_words': 226}