iGrind

Untitled

May 20th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rpcService.subscribe("scout.configurator.update-keywords")(receiveKeywords)
  2.     }
  3.  
  4.     var keywordsConf: Option[KeywordsConfig] = None
  5.  
  6.     log.info("Configurator started")
  7.  
  8.     def receive = {
  9.       case msg @ UpdateKeywordsConf(kwConfig)
  10.         log.warning("Update keywords config: {}", kwConfig)
  11.         keywordsConf = Some(kwConfig)
  12.  
  13.         unstashAll()
  14.         context.system.eventStream.publish(msg)
  15.  
  16.       case GetKeywordsConfig ⇒
  17.         keywordsConf match {
  18.           case Some(conf) ⇒ sender ! conf
  19.           case _ ⇒ stash()
  20.         }
  21.     }
  22.  
  23.     private def receiveKeywords(msg: Delivery) {
  24.       val cfg = KeywordsConfigProto.parseFrom(msg.body)
  25.  
  26.       self ! UpdateKeywordsConf(KeywordsConfig(
  27.         keywords  = cfg.keywords map { kw ⇒ Keyword(
  28.           kw.id,
  29.           kw.topicId,
  30.           kw.plain,
  31.           kw.lucene.filter(_.trim.nonEmpty),
  32.           kw.luceneSimple.filter(_.trim.nonEmpty)
  33.         ) },
  34.         customers = cfg.customers map { c ⇒ Customer(c.id, c.name, c.authKey) },
  35.         topics    = cfg.topics map { t ⇒ Topic(t.id, t.name, t.customerId) }
  36.       ))
  37.     }
  38.   }
  39.  
  40.  
  41.   /**
  42.    * Helper trait for get and subscribe to keywords update
  43.    */
  44.   trait KeywordsConfigConsumer extends Actor with Stash with ActorLogging {
  45.  
  46.     private var keywordsConfigOpt: Option[KeywordsConfig] = None
  47.  
  48.     protected def keywordsConfig = keywordsConfigOpt.get
  49.  
  50.     override def preStart() {
  51.       context.become({
  52.         case kw: KeywordsConfig ⇒
  53.           updateKeywords(kw)
  54.  
  55.           context.unbecome()
  56.           unstashAll()
  57.  
  58.           context.system.eventStream.subscribe(self, classOf[UpdateKeywordsConf])
  59.  
  60.         case _ ⇒ stash()
  61.       })
Advertisement
Add Comment
Please, Sign In to add comment