<commit_before>package org.clulab.processors.coserver

import com.typesafe.config.{ Config, ConfigValueFactory, ConfigFactory }
import com.typesafe.scalalogging.LazyLogging

import akka.actor._
import akka.event.Logging
import akka.routing.FromConfig

import org.clulab.processors._
import org.clulab.processors.bionlp._
import org.clulab.processors.corenlp._
import org.clulab.processors.fastnlp._
import org.clulab.processors.shallownlp._

/**
  * Application to wrap and serve various processor capabilities.
  *   Written by: <NAME>. 6/5/2017.
  *   Last Modified: Use explicit single instance.
  */
object ProcessorCoreServer extends App with LazyLogging {

  // save any command line arguments
  private val argsList = args.toList

  // THE instance of the the processor core server
  private var _server: ProcessorCoreServer = _

  /** Return an actor path to the current instance of the processor pool. */
  def getPath: ActorPath = instance

  /** Create a single instance of the processor core server, only if it has been created. */
  def instance: ActorPath = {
    val config = ConfigFactory.load().getConfig("ProcessorCoreService")
    logger.debug(s"(ProcessorCoreServer.instance): config=${config}")
    if (_server == null)
      _server = new ProcessorCoreServer(config)
    _server.getPath
  }

}


class ProcessorCoreServer (

  /** Application-specific portion of the configuration file. */
  val config: Config

) extends LazyLogging {

  if (config == null)
    throw new RuntimeException("Unable to read configuration from configuration file.")

  // create the Processor engine specified by the configuration and used by this server
  private val processor: Processor = {
    val proc = if (config.hasPath("server.processor")) config.getString("server.processor")
               else "core"
    proc.toLowerCase match {
      case "bio" => new BioNLPProcessor(removeFigTabReferences = true)
      case "core" => new CoreNLPProcessor()
      case "fast" => new FastNLPProcessor(useMalt = false)
      case "fastbio" => new FastBioNLPProcessor(removeFigTabReferences = true)
      case _ => new ShallowNLPProcessor()
    }
  }
  logger.debug(s"(ProcessorCoreServer.Class): processor=${processor}")

  // fire up the actor system
  private val system = ActorSystem("proc-core-server", config)

  logger.debug(s"(ProcessorCoreServer.Class): system=${system}")

  // create a pool of processor actors waiting for work
  private val procPool = system.actorOf(
    FromConfig.props(ProcessorActor.props(processor)), "proc-actor-pool")

  logger.debug(s"(ProcessorCoreServer.Class): procPoll=${procPool}")

  // TODO: set supervisory strategy property of the pool to handle errors LATER

  /** Return an actor path to the current instance of the processor pool. */
  def getPath: ActorPath = procPool.path

}<commit_msg>Set supervisory strategy on router. Redo instance creation.<commit_after>package org.clulab.processors.coserver

import com.typesafe.config.{ Config, ConfigValueFactory, ConfigFactory }
import com.typesafe.scalalogging.LazyLogging

import akka.actor._
import akka.actor.SupervisorStrategy._
import akka.routing._
import akka.event.Logging

import org.clulab.processors._
import org.clulab.processors.bionlp._
import org.clulab.processors.corenlp._
import org.clulab.processors.fastnlp._
import org.clulab.processors.shallownlp._

/**
  * Application to wrap and serve various processor capabilities.
  *   Written by: <NAME>. 6/5/2017.
  *   Last Modified: Set supervisory strategy on router. Redo instance creation.
  */
object ProcessorCoreServer extends App with LazyLogging {

  // save any command line arguments
  private val argsList = args.toList

  // THE instance of the the processor core server
  private var _server: ProcessorCoreServer = _

  /** Return an actor path to the current instance of the processor pool. */
  def getPath: ActorPath = instance

  /** Create a single instance of the processor core server, only if it has been created. */
  def instance: ActorPath = {
    logger.debug(s"(ProcessorCoreServer.instance): server = ${_server}")
    if (_server == null) {                  // create server, iff not already created
      val config = ConfigFactory.load().getConfig("ProcessorCoreServer")
      if (config == null)
        throw new RuntimeException("(ProcessorCoreServer.instance): Unable to read configuration from configuration file.")
      logger.debug(s"(ProcessorCoreServer.instance): config=${config}")
      _server = new ProcessorCoreServer(config)
    }
    logger.debug(s"(ProcessorCoreServer.instance): server => ${_server}")
    _server.getPath                         // return actor path to processor pool
  }

}


class ProcessorCoreServer (

  /** Application-specific portion of the configuration file. */
  val config: Config

) extends LazyLogging {

  if (config == null)
    throw new RuntimeException("(ProcessorCoreServer.ctor): Empty configuration argument not allowed.")

  // create the Processor engine specified by the configuration and used by this server
  private val processor: Processor = {
    val proc = if (config.hasPath("server.processor")) config.getString("server.processor")
               else "core"
    proc.toLowerCase match {
      case "bio" => new BioNLPProcessor(removeFigTabReferences = true)
      case "core" => new CoreNLPProcessor()
      case "fast" => new FastNLPProcessor(useMalt = false)
      case "fastbio" => new FastBioNLPProcessor(removeFigTabReferences = true)
      case _ => new ShallowNLPProcessor()
    }
  }
  logger.debug(s"(ProcessorCoreServer.ctor): processor=${processor}")

  // fire up the actor system
  private val system = ActorSystem("proc-core-server", config)

  logger.debug(s"(ProcessorCoreServer.ctor): system=${system}")

  // set supervisory strategy property of the pool to handle errors
  private final val restartEachStrategy: SupervisorStrategy =
    OneForOneStrategy() { case _ => Restart }

  // create a pool of processor actors waiting for work
  private val procPool = system.actorOf(
    ProcessorActor.props(processor).withRouter(
      FromConfig.withSupervisorStrategy(restartEachStrategy)),
    "proc-actor-pool")

  logger.debug(s"(ProcessorCoreServer.ctor): procPool=${procPool}")


  /** Returns an actor path to the current instance of the processor pool. */
  val getPath: ActorPath = procPool.path
}
{'fraction_non_alphanumeric': 0.09639498432601881, 'fraction_numerical': 0.0018808777429467085, 'mean_word_length': 3.9084615384615384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '1092189', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1725, 'n_words': 546}