akka.pattern.pipe Scala Examples

The following examples show how to use akka.pattern.pipe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: OnlineAction.scala    From marvin-engine-executor   with Apache License 2.0 5 votes vote down vote up
package org.marvin.executor.actions

import akka.Done
import akka.actor.SupervisorStrategy._
import akka.actor.{Actor, ActorLogging, ActorRef, OneForOneStrategy, Props, Status}
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import io.grpc.StatusRuntimeException
import org.marvin.artifact.manager.ArtifactSaver
import org.marvin.executor.actions.OnlineAction.{OnlineExecute, OnlineHealthCheck, OnlineReload}
import org.marvin.executor.proxies.EngineProxy.{ExecuteOnline, HealthCheck, Reload}
import org.marvin.executor.proxies.OnlineActionProxy
import org.marvin.artifact.manager.ArtifactSaver.SaveToLocal
import org.marvin.model.{EngineActionMetadata, EngineMetadata}
import org.marvin.util.ProtocolUtil

import scala.collection.mutable.ListBuffer
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.{Failure, Success}

object OnlineAction {
  case class OnlineExecute(message: String, params: String)
  case class OnlineReload(protocol: String)
  case class OnlineHealthCheck()
}

class OnlineAction(actionName: String, metadata: EngineMetadata) extends Actor with ActorLogging {
  var onlineActionProxy: ActorRef = _
  var artifactSaver: ActorRef = _
  var engineActionMetadata: EngineActionMetadata = _
  var artifactsToLoad: String = _
  implicit val ec = context.dispatcher

  override def preStart() = {
    engineActionMetadata = metadata.actionsMap(actionName)
    artifactsToLoad = engineActionMetadata.artifactsToLoad.mkString(",")
    onlineActionProxy = context.actorOf(Props(new OnlineActionProxy(engineActionMetadata)), name = "onlineActionProxy")
    artifactSaver = context.actorOf(ArtifactSaver.build(metadata), name = "artifactSaver")
  }

  override val supervisorStrategy =
    OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = metadata.onlineActionTimeout milliseconds) {
      case _: StatusRuntimeException => Restart
      case _: Exception => Escalate
  }

  override def receive  = {
    case OnlineExecute(message, params) =>
      implicit val futureTimeout = Timeout(metadata.onlineActionTimeout milliseconds)

      log.info(s"Starting to process execute to $actionName. Message: [$message] and params: [$params].")

      val originalSender = sender
      ask(onlineActionProxy, ExecuteOnline(message, params)) pipeTo originalSender


    case OnlineReload(protocol) =>
      implicit val futureTimeout = Timeout(metadata.reloadTimeout milliseconds)

      log.info(s"Starting to process reload to $actionName. Protocol: [$protocol].")

      if(protocol == null || protocol.isEmpty){
        onlineActionProxy forward Reload()

      }else{
        val splitedProtocols = ProtocolUtil.splitProtocol(protocol, metadata)

        val futures:ListBuffer[Future[Any]] = ListBuffer[Future[Any]]()
        for(artifactName <- engineActionMetadata.artifactsToLoad) {
          futures += (artifactSaver ? SaveToLocal(artifactName, splitedProtocols(artifactName)))
        }

        val origSender = sender()
        Future.sequence(futures).onComplete{
          case Success(_) => onlineActionProxy.ask(Reload(protocol)) pipeTo origSender
          case Failure(e) => {
            log.error(s"Failure to reload artifacts using protocol $protocol.")
            origSender ! Status.Failure(e)
          }
        }
      }

    case OnlineHealthCheck =>
      implicit val futureTimeout = Timeout(metadata.healthCheckTimeout milliseconds)
      log.info(s"Starting to process health to $actionName.")

      val originalSender = sender
      ask(onlineActionProxy, HealthCheck) pipeTo originalSender

    case Done =>
      log.info("Work Done!")

    case _ =>
      log.warning(s"Not valid message !!")

  }
} 
Example 2
Source File: MonitorActor.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package k.grid.monitoring

import akka.actor.{Actor, Cancellable}
import ch.qos.logback.classic.{Level, Logger}
import com.typesafe.scalalogging.LazyLogging
import org.slf4j.LoggerFactory
import akka.pattern.pipe
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

object MonitorActor {
  val name = "MonitorActor"
}

object SetNodeLogLevel {
  val lvlMappings = Map(
    "OFF" -> Level.OFF,
    "ERROR" -> Level.ERROR,
    "WARN" -> Level.WARN,
    "INFO" -> Level.INFO,
    "DEBUG" -> Level.DEBUG,
    "TRACE" -> Level.TRACE,
    "ALL" -> Level.ALL
  )

  def levelTranslator(lvl: String): Option[Level] = {
    lvlMappings.get(lvl.toUpperCase)
  }
}

case object GetNodeLogLevel
case class NodeLogLevel(lvl: String)
case class SetNodeLogLevel(level: Level, levelDuration: Option[Int] = Some(10))

class MonitorActor extends Actor with LazyLogging {
  private[this] var originalLogLevel: Level = _
  private val editableLogger = "ROOT"
  private[this] var scheduledLogLevelReset: Cancellable = _

  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {
    //FIXME: what if logger is not logback? use jmx or write more defensive code
    originalLogLevel = {
      val l = LoggerFactory.getLogger(editableLogger)
      val f = l.getClass.getProtectionDomain.getCodeSource.getLocation.getFile
      l.info("logger is loaded from: " + f)
      l.asInstanceOf[ch.qos.logback.classic.Logger].getLevel
    }
  }

  override def receive: Receive = {
    case PingChildren =>
      MonitorUtil.pingChildren.pipeTo(sender)

    case SetNodeLogLevel(lvl, duration) =>
      if (scheduledLogLevelReset != null) {
        scheduledLogLevelReset.cancel()
        scheduledLogLevelReset = null
      }

      logger.info(s"Setting $editableLogger to log level $lvl")
      duration.foreach { d =>
        logger.info(s"Scheduling $editableLogger to be in level $originalLogLevel in $d minutes")
        scheduledLogLevelReset =
          context.system.scheduler.scheduleOnce(d.minutes, self, SetNodeLogLevel(originalLogLevel, None))
      }

      LoggerFactory.getLogger(editableLogger).asInstanceOf[ch.qos.logback.classic.Logger].setLevel(lvl)
      //change also the log level of the akka logger
      val akkaLoggerName = "akka"
      LoggerFactory.getLogger(akkaLoggerName) match {
        case akkaLogger: Logger =>
          if (akkaLogger != null)
            akkaLogger.setLevel(lvl)
        case _ =>
      }
    case GetNodeLogLevel =>
      val lvl = LoggerFactory.getLogger(editableLogger).asInstanceOf[ch.qos.logback.classic.Logger].getLevel
      sender ! NodeLogLevel(lvl.toString)
  }
} 
Example 3
Source File: CommandActor.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.ctrl.server

import akka.actor.{Actor, ActorLogging, ActorRef, ActorSelection}
import akka.pattern.pipe
import cmwell.ctrl.checkers._
import cmwell.ctrl.commands.{ControlCommand, StartElasticsearchMaster}
import cmwell.ctrl.config.{Config, Jvms}
import cmwell.ctrl.utils.ProcUtil
import k.grid.{Grid, GridJvm, GridJvm$}
import scala.concurrent.Future
import scala.sys.process._
import Config._

import scala.util.{Failure, Success, Try}
import scala.concurrent.ExecutionContext.Implicits.global


case class BashCommand(com: String)

object CommandActor {
  def select(host: String): ActorSelection = {
    Grid.selectActor(commandActorName, GridJvm(host, Some(Jvms.node)))
  }

  def all: Set[ActorSelection] = {
    Grid.availableMachines.map(host => Grid.selectActor(commandActorName, GridJvm(host, Some(Jvms.node))))
  }
}

class CommandActor extends Actor with ActorLogging {

  override def receive: Receive = {
    case BashCommand(com)   => sender ! ProcUtil.executeCommand(com)
    case CheckWeb           => WebChecker.check.pipeTo(sender())
    case CheckElasticsearch => ElasticsearchChecker.check.pipeTo(sender())
    case CheckCassandra     => CassandraChecker.check.pipeTo(sender())
    case cc: ControlCommand => cc.execute
  }
} 
Example 4
Source File: RestartSupervisor.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.utils.akka

import akka.pattern.pipe
import akka.actor.{Actor, ActorLogging, ActorRef, ActorRefFactory, Props, ReceiveTimeout, SupervisorStrategy, Terminated, Timers}
import io.hydrosphere.mist.utils.Logger

import scala.concurrent.{Future, Promise}
import scala.concurrent.duration._

class RestartSupervisor(
  name: String,
  start: () => Future[ActorRef],
  timeout: FiniteDuration,
  maxRetry: Int
) extends Actor with ActorLogging with Timers {

  override def receive: Receive = init

  import context._
  import RestartSupervisor._

  private def init: Receive = {
    case Event.Start(req) =>
      start().map(Event.Started) pipeTo self
      context become await(Some(req), 0)
  }

  private def await(req: Option[Promise[ActorRef]], attempts: Int): Receive = {
    case Event.Started(ref) =>
      req.foreach(_.success(self))
      context watch ref
      context become proxy(ref)

    case akka.actor.Status.Failure(e)  if maxRetry == attempts + 1 =>
      req.foreach(_.failure(e))
      log.error(e, "Starting child for {} failed, maxRetry reached", name)
      context stop self

    case akka.actor.Status.Failure(e) =>
      log.error(e, "Starting child for {} failed", name)
      timers.startSingleTimer("timeout", Event.Timeout, timeout)
      context become restartTimeout(req, attempts)
  }

  private def proxy(ref: ActorRef): Receive = {
    case Terminated(_) =>
      log.error(s"Reference for {} was terminated. Restarting", name)
      timers.startSingleTimer("timeout", Event.Timeout, timeout)
      context become restartTimeout(None, 0)

    case x => ref.forward(x)
  }

  private def restartTimeout(req: Option[Promise[ActorRef]], attempts: Int): Receive = {
    case Event.Timeout =>
      start().map(Event.Started) pipeTo self
      context become await(req, attempts + 1)
  }
}

object RestartSupervisor {

  sealed trait Event
  object Event {
    final case class Start(req: Promise[ActorRef]) extends Event
    case object Restart extends Event
    final case class Started(ref: ActorRef) extends Event
    case object Timeout extends Event
  }


  def props(
    name: String,
    start: () => Future[ActorRef],
    timeout: FiniteDuration,
    maxRetry: Int
  ): Props = {
    Props(classOf[RestartSupervisor], name, start, timeout, maxRetry)
  }

  def wrap(
    name: String,
    start: () => Future[ActorRef],
    timeout: FiniteDuration,
    maxRetry: Int
  )(implicit af: ActorRefFactory): Future[ActorRef] = {

    val ref = af.actorOf(props(name, start, timeout, maxRetry))
    val promise = Promise[ActorRef]
    ref ! Event.Start(promise)
    promise.future
  }

  def wrap(
    name: String,
    maxRetry: Int,
    start: () => Future[ActorRef]
  )(implicit af: ActorRefFactory): Future[ActorRef] = wrap(name, start, 5 seconds, maxRetry)(af)

} 
Example 5
Source File: CommitLogCoordinator.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.cluster.coordinator

import java.util.concurrent.TimeUnit

import akka.actor.ActorRef
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import io.radicalbit.nsdb.actors.MetricPerformerActor
import io.radicalbit.nsdb.actors.MetricPerformerActor.PersistedBits
import io.radicalbit.nsdb.commit_log.CommitLogWriterActor._
import io.radicalbit.nsdb.commit_log.RollingCommitLogFileWriter
import io.radicalbit.nsdb.util.ActorPathLogging

import scala.concurrent.Future


class CommitLogCoordinator extends ActorPathLogging {

  private def getWriter(db: String, namespace: String, metric: String): ActorRef =
    context
      .child(s"commit-log-writer-$db-$namespace-$metric")
      .getOrElse(
        context.actorOf(RollingCommitLogFileWriter.props(db, namespace, metric),
                        s"commit-log-writer-$db-$namespace-$metric")
      )

  implicit val timeout: Timeout = Timeout(
    context.system.settings.config.getDuration("nsdb.write-coordinator.timeout", TimeUnit.SECONDS),
    TimeUnit.SECONDS)

  def receive: Receive = {
    case msg @ WriteToCommitLog(db, namespace, metric, _, _, _) =>
      getWriter(db, namespace, metric).forward(msg)

    case persistedBits: PersistedBits =>
      import context.dispatcher
      // Handle successful events of Bit Persistence
      val successfullyPersistedBits = persistedBits.persistedBits

      val successfulCommitLogResponses: Future[Seq[WriteToCommitLogSucceeded]] =
        Future.sequence {
          successfullyPersistedBits.map { persistedBit =>
            (getWriter(persistedBit.db, persistedBit.namespace, persistedBit.metric) ?
              WriteToCommitLog(persistedBit.db,
                               persistedBit.namespace,
                               persistedBit.metric,
                               persistedBit.timestamp,
                               PersistedEntryAction(persistedBit.bit),
                               persistedBit.location)).collect {
              case s: WriteToCommitLogSucceeded => s
            }
          }
        }

      val response = successfulCommitLogResponses.map { responses =>
        if (responses.size == successfullyPersistedBits.size)
          MetricPerformerActor.PersistedBitsAck
        else
          context.system.terminate()
      }
      response.pipeTo(sender())

    case _ =>
      log.error("UnexpectedMessage")
  }
} 
Example 6
Source File: GenericServiceInvokerActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.dubbo.actor

import akka.actor.{Props, Status}
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import asura.common.actor.BaseActor
import asura.common.util.LogUtils
import asura.dubbo.actor.GenericServiceInvokerActor.{GetInterfaceMethodParams, GetInterfacesMessage, GetProvidersMessage}
import asura.dubbo.{DubboConfig, GenericRequest}

import scala.concurrent.{ExecutionContext, Future}

class GenericServiceInvokerActor extends BaseActor {

  implicit val ec: ExecutionContext = context.dispatcher
  implicit val timeout: Timeout = DubboConfig.DEFAULT_ACTOR_ASK_TIMEOUT

  val curatorClientCacheActor = context.actorOf(CuratorClientCacheActor.props())
  val referenceActor = context.actorOf(DubboReferenceCacheActor.props())

  override def receive: Receive = {
    case msg: GetInterfacesMessage =>
      curatorClientCacheActor ? msg pipeTo sender()
    case msg: GetProvidersMessage =>
      curatorClientCacheActor ? msg pipeTo sender()
    case msg: GenericRequest =>
      referenceActor ? msg pipeTo sender()
    case msg: GetInterfaceMethodParams =>
      context.actorOf(InterfaceMethodParamsActor.props(sender(), msg))
    case Status.Failure(t) =>
      log.warning(LogUtils.stackTraceToString(t))
      Future.failed(t) pipeTo sender()
    case _ =>
      Future.failed(new RuntimeException("Unknown message type")) pipeTo sender()
  }

  override def postStop(): Unit = {
    DubboConfig.DUBBO_EC.shutdown()
  }
}

object GenericServiceInvokerActor {

  def props() = Props(new GenericServiceInvokerActor())

  case class GetInterfacesMessage(
                                   zkConnectString: String,
                                   path: String,
                                   zkUsername: String = null,
                                   zkPassword: String = null,
                                 )

  case class GetProvidersMessage(
                                  zkConnectString: String,
                                  path: String,
                                  ref: String,
                                  zkUsername: String = null,
                                  zkPassword: String = null,
                                )

  case class GetInterfaceMethodParams(address: String, port: Int, ref: String)

} 
Example 7
Source File: DubboReferenceCacheActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.dubbo.actor

import akka.actor.Props
import akka.pattern.pipe
import asura.common.actor.BaseActor
import asura.common.cache.LRUCache
import asura.dubbo.{DubboConfig, GenericRequest}
import com.alibaba.dubbo.config.ReferenceConfig
import com.alibaba.dubbo.rpc.service.GenericService

import scala.concurrent.{ExecutionContext, Future}

class DubboReferenceCacheActor extends BaseActor {

  private val lruCache = LRUCache[String, ReferenceConfig[GenericService]](DubboConfig.DEFAULT_DUBBO_REF_CACHE_SIZE, (_, ref) => {
    ref.destroy()
  })
  implicit val actorEC: ExecutionContext = context.dispatcher

  override def receive: Receive = {
    case request: GenericRequest =>
      test(request) pipeTo sender()
    case _ =>
      Future.failed(new RuntimeException("Unknown message type")) pipeTo sender()
  }

  def test(request: GenericRequest): Future[Object] = {
    Future {
      val refConfig = request.toReferenceConfig()
      val cacheKey = request.generateCacheKey()
      val value = lruCache.get(cacheKey)
      val service = if (null == value) {
        val newService = refConfig.get()
        if (null != newService) lruCache.put(cacheKey, refConfig)
        newService
      } else {
        value.get()
      }
      if (null != service) {
        // https://github.com/apache/incubator-dubbo/issues/3163
        val args = request.getArgs()
        service.$invoke(request.method, request.getParameterTypes(), args)
      } else {
        new RuntimeException("Null dubbo generic service from reference config")
      }
    }(DubboConfig.DUBBO_EC)
  }

  override def postStop(): Unit = {
    log.debug(s"Destroy ReferenceConfig size: ${lruCache.size()}")
    lruCache.forEach((_, ref) => {
      ref.destroy()
    })
  }
}

object DubboReferenceCacheActor {
  def props() = Props(new DubboReferenceCacheActor())
} 
Example 8
Source File: InterfaceMethodParamsActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.dubbo.actor

import akka.actor.{ActorRef, Props, Status}
import akka.pattern.pipe
import akka.util.ByteString
import asura.common.actor.BaseActor
import asura.common.util.LogUtils
import asura.dubbo.DubboConfig
import asura.dubbo.actor.GenericServiceInvokerActor.GetInterfaceMethodParams
import asura.dubbo.model.InterfaceMethodParams
import asura.dubbo.model.InterfaceMethodParams.MethodSignature

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{ExecutionContext, Future}

class InterfaceMethodParamsActor(invoker: ActorRef, msg: GetInterfaceMethodParams) extends BaseActor {

  implicit val ec: ExecutionContext = context.dispatcher
  private val telnet: ActorRef = context.actorOf(TelnetClientActor.props(msg.address, if (msg.port > 0) msg.port else DubboConfig.DEFAULT_PORT, self))

  override def receive: Receive = {
    case telnetData: ByteString =>
      val utf8String = telnetData.utf8String
      if (utf8String.contains(TelnetClientActor.MSG_CONNECT_TO)) {
        log.debug(utf8String)
        if (utf8String.contains(TelnetClientActor.MSG_SUCCESS)) {
          telnet ! ByteString(s"ls -l ${msg.ref}\r\n")
        } else if (utf8String.contains(TelnetClientActor.MSG_FAIL)) {
          Future.failed(new RuntimeException(s"Remote connection to ${msg.address}:${msg.port} failed")) pipeTo invoker
          telnet ! TelnetClientActor.CMD_CLOSE
          context stop self
        } else {
          Future.failed(new RuntimeException(s"Unknown response ${utf8String}")) pipeTo invoker
          telnet ! TelnetClientActor.CMD_CLOSE
          context stop self
        }
      } else if (utf8String.contains("(") && utf8String.contains(")")) {
        getInterfaceMethodParams(msg.ref, utf8String) pipeTo invoker
        telnet ! TelnetClientActor.CMD_CLOSE
      } else {
        Future.failed(new RuntimeException(s"Unknown response: ${utf8String}")) pipeTo invoker
        telnet ! TelnetClientActor.CMD_CLOSE
        context stop self
      }
    case Status.Failure(t) =>
      val stackTrace = LogUtils.stackTraceToString(t)
      log.warning(stackTrace)
      context stop self
  }

  def getInterfaceMethodParams(ref: String, content: String): Future[InterfaceMethodParams] = {
    Future.successful {
      val methods = ArrayBuffer[MethodSignature]()
      content.split("\r\n")
        .filter(!_.startsWith(DubboConfig.DEFAULT_PROMPT))
        .map(signature => {
          val splits = signature.split(" ")
          if (splits.length == 2) {
            val ret = splits(0)
            val secondPart = splits(1)
            val idx = secondPart.indexOf("(")
            val method = secondPart.substring(0, idx)
            val params = secondPart.substring(idx + 1, secondPart.length - 1).split(",")
            methods += (MethodSignature(ret, method, params))
          }
        })
      InterfaceMethodParams(ref, methods)
    }
  }

  override def postStop(): Unit = log.debug(s"${self.path} stopped")
}

object InterfaceMethodParamsActor {
  def props(invoker: ActorRef, msg: GetInterfaceMethodParams) = {
    Props(new InterfaceMethodParamsActor(invoker, msg))
  }
} 
Example 9
Source File: JobManualActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.core.job.actor

import akka.actor.{ActorRef, PoisonPill, Props, Status}
import akka.pattern.pipe
import asura.common.actor.{BaseActor, SenderMessage}
import asura.common.util.{LogUtils, StringUtils}
import asura.core.CoreConfig
import asura.core.es.model.{Job, JobReport}
import asura.core.es.service.{JobReportService, JobService}
import asura.core.job.{JobCenter, JobExecDesc}

class JobManualActor(jobId: String, user: String, out: ActorRef) extends BaseActor {

  implicit val executionContext = context.dispatcher
  if (null != out) self ! SenderMessage(out)

  override def receive: Receive = {
    case SenderMessage(sender) =>
      context.become(handleRequest(sender))
      self ! jobId
  }

  def handleRequest(wsActor: ActorRef): Receive = {
    case job: Job =>
      val jobImplOpt = JobCenter.classAliasJobMap.get(job.classAlias)
      if (jobImplOpt.isEmpty) {
        wsActor ! s"Can't find job implementation of ${job.classAlias}"
        wsActor ! JobExecDesc.STATUS_FAIL
        wsActor ! Status.Success
      } else {
        val jobImpl = jobImplOpt.get
        val (isOk, errMsg) = jobImpl.checkJobData(job.jobData)
        if (isOk) {
          JobExecDesc.from(jobId, job, JobReport.TYPE_MANUAL, null, user).map(jobExecDesc => {
            jobImpl.doTestAsync(jobExecDesc, logMsg => {
              wsActor ! logMsg
            }).pipeTo(self)
          }).recover {
            case t: Throwable =>
              self ! Status.Failure(t)
          }
        } else {
          wsActor ! errMsg
          wsActor ! Status.Success
        }
      }
    case jobId: String =>
      if (StringUtils.isNotEmpty(jobId)) {
        JobService.getJobById(jobId).pipeTo(self)
      } else {
        wsActor ! s"jobId is empty."
        wsActor ! Status.Success
      }
    case execDesc: JobExecDesc =>
      execDesc.prepareEnd()
      val report = execDesc.report
      JobReportService.indexReport(execDesc.reportId, report).map { _ =>
        val reportUrl = s"view report: ${CoreConfig.reportBaseUrl}/${execDesc.reportId}"
        wsActor ! reportUrl
        wsActor ! execDesc.report.result
        wsActor ! Status.Success
      }.recover {
        case t: Throwable =>
          self ! Status.Failure(t)
      }
    case Status.Failure(t) =>
      val stackTrace = LogUtils.stackTraceToString(t)
      log.warning(stackTrace)
      wsActor ! t.getMessage
      wsActor ! JobExecDesc.STATUS_FAIL
      wsActor ! Status.Success
  }

  override def postStop(): Unit = {
    log.debug(s"${self.path} is stopped")
  }
}

object JobManualActor {
  def props(jobId: String, user: String, out: ActorRef = null) = Props(new JobManualActor(jobId, user, out))
} 
Example 10
Source File: HttpRunnerActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.core.http.actor

import akka.actor.Status.Success
import akka.actor.{ActorRef, Props}
import akka.pattern.pipe
import asura.common.actor.{BaseActor, ItemActorEvent, OverActorEvent, SenderMessage}
import asura.common.util.LogUtils
import asura.core.es.model.HttpCaseRequest
import asura.core.http.actor.HttpRunnerActor.{StepResult, TestCaseMessage}
import asura.core.http.{HttpResult, HttpRunner}
import asura.core.runtime.{ContextOptions, RuntimeContext}

import scala.concurrent.Future

class HttpRunnerActor extends BaseActor {

  implicit val ec = context.dispatcher
  var wsActor: ActorRef = null
  var docId: String = null
  var request: HttpCaseRequest = null
  var variables: Seq[java.util.Map[Any, Any]] = Nil

  override def receive: Receive = {
    case SenderMessage(wsSender) =>
      wsActor = wsSender
    case TestCaseMessage(docId, request, _) =>
      this.docId = docId
      this.request = request
      if (null != request.generator && null != request.generator.variables) {
        this.variables = request.generator.variables
      }
      if (this.variables.nonEmpty) {
        self ! 0
      } else {
        if (null != wsActor) wsActor ! OverActorEvent()
      }
    case idx: Int =>
      if (idx >= 0 && idx < this.variables.length) {
        execute(idx) pipeTo self
      } else {
        stopSelf()
      }
    case _ => stopSelf()
  }

  def execute(idx: Int): Future[Int] = {
    val varargs = this.variables(idx)
    val options = ContextOptions(initCtx = varargs)
    HttpRunner
      .test(this.docId, this.request, RuntimeContext(options = options))
      .map(result => {
        if (null != wsActor) wsActor ! ItemActorEvent(StepResult(idx, result))
        idx + 1
      })
      .recover {
        case t: Throwable =>
          val errMsg = LogUtils.stackTraceToString(t)
          if (null != wsActor) wsActor ! ItemActorEvent(StepResult(idx, null, errMsg))
          idx + 1
      }
  }

  def stopSelf(): Unit = {
    if (null != wsActor) wsActor ! Success
    context.stop(self)
  }
}

object HttpRunnerActor {

  def props() = Props(new HttpRunnerActor())

  case class TestCaseMessage(id: String, cs: HttpCaseRequest, options: ContextOptions)

  case class StepResult(idx: Int, result: HttpResult, errMsg: String = null)

} 
Example 11
Source File: SqlRequestInvokerActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.core.sql.actor

import java.sql.Connection

import akka.actor.Props
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import asura.common.actor.BaseActor
import asura.common.util.FutureUtils
import asura.core.CoreConfig
import asura.core.es.model.SqlRequest.SqlRequestBody
import asura.core.sql.actor.MySqlConnectionCacheActor.GetConnectionMessage
import asura.core.sql.{MySqlConnector, SqlConfig, SqlParserUtils}

import scala.concurrent.{ExecutionContext, Future}

class SqlRequestInvokerActor extends BaseActor {

  implicit val ec: ExecutionContext = context.dispatcher
  implicit val timeout: Timeout = CoreConfig.DEFAULT_ACTOR_ASK_TIMEOUT

  val connectionCacheActor = context.actorOf(MySqlConnectionCacheActor.props())

  override def receive: Receive = {
    case requestBody: SqlRequestBody =>
      getResponse(requestBody) pipeTo sender()
    case _ =>
      Future.failed(new RuntimeException("Unknown message type")) pipeTo sender()
  }

  def getResponse(requestBody: SqlRequestBody): Future[Object] = {
    implicit val sqlEc = SqlConfig.SQL_EC
    val futConn = (connectionCacheActor ? GetConnectionMessage(requestBody)).asInstanceOf[Future[Connection]]
    val (isOk, errMsg) = SqlParserUtils.isSelectStatement(requestBody.sql)
    if (null == errMsg) {
      futConn.flatMap(conn => {
        if (isOk) {
          Future {
            MySqlConnector.executeQuery(conn, requestBody.sql)
          }
        } else {
          Future {
            MySqlConnector.executeUpdate(conn, requestBody.sql)
          }
        }
      })
    } else {
      FutureUtils.requestFail(errMsg)
    }
  }

}

object SqlRequestInvokerActor {

  def props() = Props(new SqlRequestInvokerActor())
} 
Example 12
Source File: MySqlConnectionCacheActor.scala    From asura   with MIT License 5 votes vote down vote up
package asura.core.sql.actor

import java.sql.Connection

import akka.actor.Props
import akka.pattern.pipe
import akka.util.Timeout
import asura.common.actor.BaseActor
import asura.common.cache.LRUCache
import asura.core.CoreConfig
import asura.core.es.model.SqlRequest.SqlRequestBody
import asura.core.sql.actor.MySqlConnectionCacheActor.GetConnectionMessage
import asura.core.sql.{MySqlConnector, SqlConfig}

import scala.concurrent.{ExecutionContext, Future}

class MySqlConnectionCacheActor(size: Int) extends BaseActor {

  implicit val ec: ExecutionContext = context.dispatcher
  implicit val timeout: Timeout = CoreConfig.DEFAULT_ACTOR_ASK_TIMEOUT

  private val lruCache = LRUCache[String, Connection](size, (_, conn) => {
    conn.close()
  })

  override def receive: Receive = {
    case GetConnectionMessage(sqlRequest) =>
      getConnection(sqlRequest) pipeTo sender()
    case _ =>
      Future.failed(new RuntimeException("Unknown message type")) pipeTo sender()
  }

  private def getConnection(request: SqlRequestBody): Future[Connection] = {
    Future {
      val key = generateCacheKey(request)
      val conn = lruCache.get(key)
      if (null == conn || !conn.isValid(SqlConfig.SQL_CONN_CHECK_TIMEOUT)) {
        val newConn = MySqlConnector.connect(request)
        lruCache.put(key, newConn)
        newConn
      } else {
        conn
      }
    }(SqlConfig.SQL_EC)
  }

  private def generateCacheKey(request: SqlRequestBody): String = {
    val sb = StringBuilder.newBuilder
    sb.append(request.username).append(":")
      .append(request.encryptedPass).append("@")
      .append(request.host).append(":")
      .append(request.port).append("/")
      .append(request.database)
    sb.toString()
  }
}

object MySqlConnectionCacheActor {

  def props(size: Int = SqlConfig.DEFAULT_MYSQL_CONNECTOR_CACHE_SIZE) = Props(new MySqlConnectionCacheActor(size))

  case class GetConnectionMessage(request: SqlRequestBody)

} 
Example 13
Source File: OnlineActionProxy.scala    From marvin-engine-executor   with Apache License 2.0 5 votes vote down vote up
package org.marvin.executor.proxies

import actions.OnlineActionHandlerGrpc.{OnlineActionHandler, OnlineActionHandlerBlockingClient, OnlineActionHandlerBlockingStub, OnlineActionHandlerStub}
import actions._
import akka.pattern.pipe
import io.grpc.ManagedChannelBuilder
import org.marvin.executor.proxies.EngineProxy.{ExecuteOnline, HealthCheck, Reload}
import org.marvin.model.EngineActionMetadata

//Reload messages
final case class Reloaded(protocol: String)
final case class FailedToReload(protocol: String = "")

class OnlineActionProxy(metadata: EngineActionMetadata) extends EngineProxy (metadata)  {
  var engineAsyncClient:OnlineActionHandler = _
  var engineClient:OnlineActionHandlerBlockingClient = _

  implicit val ec = context.dispatcher

  override def preStart() = {
    log.info(s"${this.getClass().getCanonicalName} actor initialized...")
    val channel = ManagedChannelBuilder.forAddress(metadata.host, metadata.port).usePlaintext(true).build
    artifacts = metadata.artifactsToLoad.mkString(",")
    engineAsyncClient = OnlineActionHandlerGrpc.stub(channel)
    engineClient = OnlineActionHandlerGrpc.blockingStub(channel)
  }

  override def receive = {
    case ExecuteOnline(requestMessage, params) =>
      log.info(s"Start the execute remote procedure to ${metadata.name}.")
      val responseFuture = engineAsyncClient.RemoteExecute(OnlineActionRequest(message=requestMessage, params=params))
      responseFuture.collect{case response => response.message} pipeTo sender

    case HealthCheck =>
      log.info(s"Start the health check remote procedure to ${metadata.name}.")
      val statusFuture = engineAsyncClient.HealthCheck(HealthCheckRequest(artifacts=artifacts))
      statusFuture.collect{case response => response.status} pipeTo sender

    case Reload(protocol) =>
      log.info(s"Start the reload remote procedure to ${metadata.name}. Protocol [$protocol]")
      try{
        val message = engineClient.RemoteReload(ReloadRequest(artifacts=artifacts, protocol=protocol)).message
        log.info(s"Reload remote procedure to ${metadata.name} Done with [${message}]. Protocol [$protocol]")
        sender ! Reloaded(protocol)
      } catch {
        case _ : Exception => sender ! FailedToReload(protocol)
      }

    case _ =>
      log.warning(s"Not valid message !!")
  }
} 
Example 14
Source File: CircuitBreakerExample.scala    From Scala-Reactive-Programming   with MIT License 5 votes vote down vote up
package com.packt.publishing.reactive.patterns.circuitbreaker

import scala.concurrent.duration._
import akka.pattern.CircuitBreaker
import akka.pattern.pipe
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import scala.concurrent.Future
import akka.util.Timeout
import akka.pattern.ask

case class MessageOne(msg:String = "is my middle name")
case class MessageTwo(msg:String = "block for me")

class DangerousActor extends Actor with ActorLogging {

  import context.dispatcher

  val breaker =
    new CircuitBreaker(
      context.system.scheduler,
      maxFailures = 5,
      callTimeout = 10.seconds,
      resetTimeout = 1.minute).onOpen(notifyMeOnOpen())

  def notifyMeOnOpen(): Unit =
    log.warning("My CircuitBreaker is now open, and will not close for one minute")

  def dangerousCall: String = "This really isn't that dangerous of a call after all"

  def receive = {
    case MessageOne ⇒
      breaker.withCircuitBreaker(Future(dangerousCall)) pipeTo sender()
    case MessageTwo ⇒
      sender() ! breaker.withSyncCircuitBreaker(dangerousCall)
  }

}

object CircuitBreakerExample extends App {
  implicit val timeout: Timeout = 2.seconds
  val system = ActorSystem("WFSystem")
  val wfTeller = system.actorOf(Props[DangerousActor], "DangerousActor")

  val future1 = wfTeller ? MessageTwo
  val future2 = wfTeller ? MessageTwo
  val future3 = wfTeller ? MessageTwo
  val future4 = wfTeller ? MessageTwo
  val future5 = wfTeller ? MessageTwo
  val future6 = wfTeller ? MessageTwo

  import scala.concurrent.ExecutionContext.Implicits.global
  future1.onComplete { value =>
    println("************ value1 = "  + value)
  }
  future2.onComplete { value =>
    println("************ value2 = "  + value)
  }
  future3.onComplete { value =>
    println("************ value3 = "  + value)
  }
  future4.onComplete { value =>
    println("************ value4 = "  + value)
  }
  future5.onComplete { value =>
    println("************ value5 = "  + value)
  }
  future6.onComplete { value =>
    println("************ value6 = "  + value)
  }
  Thread.sleep(10000)

  system.terminate()
} 
Example 15
Source File: ScheduleAccessProxy.scala    From lemon-schedule   with GNU General Public License v2.0 5 votes vote down vote up
package com.gabry.job.db.proxy.actor
import akka.pattern.pipe
import com.gabry.job.core.actor.SimpleActor
import com.gabry.job.core.domain.UID
import com.gabry.job.core.po.SchedulePo
import com.gabry.job.db.DataTables
import com.gabry.job.db.access.ScheduleAccess
import com.gabry.job.db.proxy.DataAccessProxyException
import com.gabry.job.db.proxy.command.DatabaseCommand
import com.gabry.job.db.proxy.event.DatabaseEvent
import com.gabry.job.utils.ExternalClassHelper._

  override def userDefineEventReceive: Receive = {
    case cmd @ DatabaseCommand.Insert(schedulePo:SchedulePo,replyTo,originEvent) =>
      scheduleAccess.insert(schedulePo).mapAll(
        insertedSchedulePo => DatabaseEvent.Inserted(Some(insertedSchedulePo),originEvent),
        exception => DataAccessProxyException(cmd,exception))
        .pipeTo(replyTo)(sender())

    case cmd @ DatabaseCommand.UpdateField(DataTables.SCHEDULE,scheduleUid:UID,fields @ Array("DISPATCH"),replyTo,originEvent) =>
      scheduleAccess.setDispatched(scheduleUid,dispatched = true).mapAll(
        updateNum =>DatabaseEvent.FieldUpdated(DataTables.SCHEDULE,fields,updateNum,Some(scheduleUid),originEvent),
        exception =>DataAccessProxyException(cmd,exception))
        .pipeTo(replyTo)(sender())

    case cmd @ DatabaseCommand.UpdateField(DataTables.SCHEDULE,scheduleUid,fields @ Array("SUCCEED"),replyTo,originEvent) =>
      scheduleAccess.setSucceed(scheduleUid,succeed = true).mapAll(
        updateNum => DatabaseEvent.FieldUpdated(DataTables.SCHEDULE,fields,updateNum,Some(scheduleUid),originEvent),
        exception => DataAccessProxyException(cmd,exception))
        .pipeTo(replyTo)(sender())
    case cmd @ DatabaseCommand.UpdateField(DataTables.SCHEDULE,scheduleUid,fields @ Array("SUCCEED","FALSE"),replyTo,originEvent) =>
      scheduleAccess.setSucceed(scheduleUid,succeed = false).mapAll(
        updateNum => DatabaseEvent.FieldUpdated(DataTables.SCHEDULE,fields,updateNum,Some(scheduleUid),originEvent),
        exception =>DataAccessProxyException(cmd,exception))
        .pipeTo(replyTo)(sender())
    case DatabaseCommand.Select((DataTables.SCHEDULE,jobUid:UID,nodeAnchor:String,triggerTime:Long,jobParallel:Int),replyTo,originCommand) =>
      scheduleAccess.selectUnDispatchSchedule(jobUid,nodeAnchor,triggerTime,jobParallel){ schedulePo =>
        replyTo ! DatabaseEvent.Selected(Some(schedulePo),originCommand)
      }
  }
} 
Example 16
Source File: DependencyAccessProxy.scala    From lemon-schedule   with GNU General Public License v2.0 5 votes vote down vote up
package com.gabry.job.db.proxy.actor

import akka.pattern.pipe
import com.gabry.job.core.actor.SimpleActor
import com.gabry.job.core.command.JobTrackerCommand
import com.gabry.job.core.domain.UID
import com.gabry.job.core.po.DependencyPo
import com.gabry.job.db.DataTables
import com.gabry.job.db.access.DependencyAccess
import com.gabry.job.db.proxy._
import com.gabry.job.db.proxy.command.DatabaseCommand
import com.gabry.job.db.proxy.event.DatabaseEvent
import com.gabry.job.utils.ExternalClassHelper._

  override def userDefineEventReceive: Receive = {
    case cmd @ DatabaseCommand.Insert(row:Array[DependencyPo],replyTo,originCommand:JobTrackerCommand.SubmitJob) =>

      val insertDependency = dependencyAccess.deleteAllAndInsertMany(originCommand.job.uid,row)
      insertDependency.mapAll( insertedNum =>
        DatabaseEvent.BatchInserted(insertedNum.getOrElse(0),row.headOption,originCommand)
        ,exception => DataAccessProxyException(cmd,exception))
        .pipeTo(replyTo)(sender())
    case cmd @ DatabaseCommand.Select((DataTables.DEPENDENCY,jobId:UID,dataTime:Long),replyTo,originCommand) =>
      dependencyAccess.selectSucceedState(jobId,dataTime).mapAll(
        succeed => DatabaseEvent.Selected(Some((DataTables.DEPENDENCY,succeed)),originCommand),
        exception => DataAccessProxyException(cmd,exception) )
        .pipeTo(replyTo)(sender())
  }
} 
Example 17
Source File: ReplyActor.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.akka

import akka.actor.Actor
import akka.pattern.pipe
import io.vamp.common.notification._

import scala.concurrent.Future
import scala.language.implicitConversions
import scala.reflect._
import scala.util.{ Failure, Success, Try }

trait RequestError extends ErrorNotification {
  def request: Any

  def reason = request
}

trait ReplyActor extends ReplyCheck {
  this: Actor with ExecutionContextProvider with NotificationProvider ⇒

  def reply[T](magnet: ReplyMagnet[T], `class`: Class[_ <: Notification] = errorNotificationClass): Try[Future[T]] = {
    magnet.get.transform({
      future ⇒
        pipe {
          future andThen {
            case Success(s)                             ⇒ s
            case Failure(n: NotificationErrorException) ⇒ n
            case Failure(f)                             ⇒ failure(f)
          }
        } to sender()
        Success(future)
    }, {
      case n: NotificationErrorException ⇒ Failure(n)
      case f                             ⇒ Failure(failure(f, `class`))
    })
  }

  def unsupported(requestError: RequestError) = reply(Future.failed(reportException(requestError)))
}

trait ReplyCheck {
  this: ExecutionContextProvider with NotificationProvider ⇒

  def checked[T <: Any: ClassTag](future: Future[_], `class`: Class[_ <: Notification] = errorNotificationClass): Future[T] = future map {
    case result if classTag[T].runtimeClass.isAssignableFrom(result.getClass) ⇒ result.asInstanceOf[T]
    case any ⇒ throw failure(any, `class`)
  }

  def failure(failure: Any, `class`: Class[_ <: Notification] = errorNotificationClass): Exception =
    reportException(`class`.getConstructors()(0).newInstance(failure.asInstanceOf[AnyRef]).asInstanceOf[Notification])

  def errorNotificationClass: Class[_ <: ErrorNotification] = classOf[GenericErrorNotification]
}

sealed abstract class ReplyMagnet[+T] {
  def get: Try[Future[T]]
}

object ReplyMagnet {
  implicit def apply[T](any: ⇒ Future[T]): ReplyMagnet[T] = new ReplyMagnet[T] {
    override def get = Try(any)
  }
} 
Example 18
Source File: SignatureManagerActor.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.communication.security

import akka.actor.{Props, ActorRef, Actor}
import akka.util.Timeout
import org.apache.toree.communication.utils.OrderedSupport
import org.apache.toree.kernel.protocol.v5.KernelMessage
import org.apache.toree.utils.LogLike

import scala.concurrent.duration._
import akka.pattern.ask
import akka.pattern.pipe

class SignatureManagerActor(
  key: String, scheme: String
) extends Actor with LogLike with OrderedSupport {
  private val hmac = Hmac(key, HmacAlgorithm(scheme))

  def this(key: String) = this(key, HmacAlgorithm.SHA256.toString)

  // NOTE: Required to provide the execution context for futures with akka
  import context._

  // NOTE: Required for ask (?) to function... maybe can define elsewhere?
  implicit val timeout = Timeout(5.seconds)

  //
  // List of child actors that the signature manager contains
  //
  private var signatureChecker: ActorRef = _
  private var signatureProducer: ActorRef = _

  
  override def orderedTypes(): Seq[Class[_]] = Seq(
    classOf[(String, Seq[_])],
    classOf[KernelMessage]
  )
} 
Example 19
Source File: FactorialBackend.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package sample.cluster.factorial

import akka.actor.{ Actor, ActorLogging, ActorSystem, Props }
import akka.pattern.pipe
import com.typesafe.config.ConfigFactory

import scala.annotation.tailrec
import scala.concurrent.Future

class FactorialBackend extends Actor with ActorLogging {
  import context.dispatcher

  def receive = {
    case (n: Int) =>
      Future(factorial(n)) map { result =>
        (n, result)
      } pipeTo sender()
  }

  def factorial(n: Int): BigInt = {
    @tailrec def factorialAcc(acc: BigInt, n: Int): BigInt =
      if (n <= 1) acc
      else factorialAcc(acc * n, n - 1)
    factorialAcc(BigInt(1), n)
  }
}

object FactorialBackend {
  def main(args: Array[String]): Unit = {
    // Override the configuration of the port when specified as program argument
    val port = if (args.isEmpty) "0" else args(0)
    val config = ConfigFactory
      .parseString(s"""
        akka.remote.netty.tcp.port=$port
        akka.remote.artery.canonical.port=$port
        """)
      .withFallback(ConfigFactory.parseString("akka.cluster.roles = [backend]"))
      .withFallback(ConfigFactory.load("factorial"))

    val system = ActorSystem("ClusterSystem", config)
    system.actorOf(Props[FactorialBackend], name = "factorialBackend")

    system.actorOf(Props[MetricsListener], name = "metricsListener")
  }
} 
Example 20
Source File: Checker.scala    From cave   with MIT License 5 votes vote down vote up
package worker

import java.util.concurrent.Executor

import akka.actor.{Actor, ActorLogging, Status}
import akka.pattern.pipe
import com.cave.metrics.data._
import com.cave.metrics.data.evaluator.{CheckEvaluator, DataFetcher}
import init.Init

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

object Checker {
  type Result = Try[Boolean]

  case class Done(alarm: Result)
  case class Aborted(reason: String)
}

class Checker(check: Check) extends Actor with ActorLogging {

  implicit val exec = context.dispatcher.asInstanceOf[Executor with ExecutionContext]
  val evaluator = new CheckEvaluator(check)
  def fetcher = new DataFetcher(Init.influxClientFactory)

  this run check pipeTo self

  def receive = {
    case alarm: Checker.Result =>
      context.parent ! Checker.Done(alarm)
      stop()

    case x: Status.Failure =>
      context.parent ! Checker.Aborted(x.cause.getMessage)
      stop()
  }

  def stop(): Unit = {
    context stop self
  }

  private[worker] def run(check: Check)(implicit ec: ExecutionContext): Future[Try[Boolean]] = {
    val result = evaluator.evaluate(fetcher)
    result map { v =>
      log.warning("Result of evaluation: " + v)
    }
    result
  }
} 
Example 21
Source File: GroupByActor.scala    From coral   with Apache License 2.0 5 votes vote down vote up
package io.coral.actors.transform

import akka.actor.{ActorLogging, Props}
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import io.coral.actors.{NoEmitTrigger, CoralActor, CoralActorFactory}
import scaldi.Injector
import akka.pattern.pipe

object GroupByActor {
	implicit val formats = org.json4s.DefaultFormats

	def getParams(json: JValue) = {
		for {
			by <- (json \ "group" \ "by").extractOpt[String]
		} yield {
			by
		}
	}

	def apply(json: JValue)(implicit injector: Injector): Option[Props] = {
		getParams(json).map(_ => Props(classOf[GroupByActor], json, injector))
	}
}

class GroupByActor(json: JObject)(implicit injector: Injector)
	extends CoralActor(json)
	with NoEmitTrigger
	with ActorLogging {
	val Diff(_, _, jsonChildrenDef) = json diff JObject(("group", json \ "group"))
	val Diff(_, _, jsonDefinition) = json diff JObject(("timeout", json \ "timeout"))
	val by = GroupByActor.getParams(json).get
	override def jsonDef = jsonDefinition.asInstanceOf[JObject]
	override def state = Map(("actors", render(children)))

	override def noEmitTrigger(json: JObject) = {
		for {
			value <- (json \ by).extractOpt[String]
		} yield {
			val found = children.get(value) flatMap (id => actorRefFactory.child(id.toString))

			found match {
				case Some(actorRef) =>
					// We found the child, send it the original message
					actorRef forward json
				case None =>
					// Not found, create a new child
					val props = CoralActorFactory.getProps(jsonChildrenDef)
					props map { p =>
						val actor = actorRefFactory.actorOf(p, s"blabla")
						children += (value -> 1L)
						actor forward json
					}
			}
		}
	}
} 
Example 22
class TweetScannerActor(tweetWrite: ActorRef, queryUrl: String => String)
  extends Actor with TweetMarshaller {

  import context.dispatcher
  import akka.pattern.pipe

  private val pipeline = sendReceive ~> unmarshal[List[Tweet]]

  def receive: Receive = {
    case query: String => pipeline(Get(queryUrl(query))) pipeTo tweetWrite
  }
}

trait TweetMarshaller {
  type Tweets = List[Tweet]

  implicit object TweetUnmarshaller extends Unmarshaller[Tweets] {

    val dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy")

    def mkTweet(status: JsValue): Deserialized[Tweet] = {
      val json = status.asJsObject
    }

    def apply(entity: HttpEntity): Deserialized[Tweets] = {
      val json = JsonParser(entity.asString).asJsObject
    }
  } 
Example 23
import scala.collection.JavaConversions._
import cassandra.resultset._
import context.dispatcher
import akka.pattern.pipe

class TweetReaderActor(cluster: Cluster) extends Actor {
  val session = cluster.connect(Keyspaces.akkaCassandra)
  val countAll = 
    new BoundStatement(session.prepare("select count(*) from tweets;"))


  def buildTweet(r: Row): Tweet = { //folded code...}

  def receive: Receive = {

    case FindAll(maximum)  =>
      val query = QueryBuilder.select().all().from(
		Keyspaces.akkaCassandra, "tweets").limit( maximum )
            session.executeAsync(query) 
        map(_.all().map(buildTweet).toList) pipeTo sender

    case CountAll =>
      session.executeAsync(countAll) 
        map(_.one.getLong(0)) pipeTo sender
  }
} 
Example 24
import scala.collection.JavaConversions._
import cassandra.resultset._
import context.dispatcher
import akka.pattern.pipe

class TweetWriterActor(cluster: Cluster) extends Actor {
  val session = cluster.connect(Keyspaces.akkaCassandra)
  val preparedStatement = session.prepare(
    "INSERT INTO tweets(key, user, text, creation_date) VALUES (?, ?, ?, ?);")

  def saveTweet(tweet: Tweet): Unit =
    session.executeAsync(preparedStatement.bind(
		tweet.id, tweet.user, tweet.text, tweet.creation_date))

  def receive: Receive = {
    case tweets: List[Tweet] => tweets foreach saveTweet
    case tweet:  Tweet       => saveTweet(tweet)
  }
} 
Example 25
Source File: HttpIncomingReceiver.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.http_frontend

import java.time.Instant
import java.util.concurrent.TimeUnit

import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.pattern.pipe
import scala.concurrent.ExecutionContext.Implicits.global
import akka.http.scaladsl.model.ws.TextMessage
import akka.stream.ActorMaterializer
import com.sumologic.sumobot.core.HttpReceptionist
import com.sumologic.sumobot.core.model.{IncomingMessage, UserSender}

import scala.concurrent.duration.Duration

object HttpIncomingReceiver {
  case class StreamEnded()
  private val StrictTimeout = Duration.create(5, TimeUnit.SECONDS)
}

class HttpIncomingReceiver(outcomingRef: ActorRef) extends Actor with ActorLogging {
  private implicit val materializer = ActorMaterializer()

  override def receive: Receive = {
    case streamedMsg: TextMessage.Streamed =>
      streamedMsg.toStrict(HttpIncomingReceiver.StrictTimeout).pipeTo(self)(sender())

    case strictMsg: TextMessage.Strict =>
      val contents = strictMsg.getStrictText
      val incomingMessage = IncomingMessage(contents, true, HttpReceptionist.DefaultSumoBotChannel,
        formatDateNow(), None, Seq.empty, UserSender(HttpReceptionist.DefaultClientUser))
      context.system.eventStream.publish(incomingMessage)

    case HttpIncomingReceiver.StreamEnded =>
      context.stop(outcomingRef)
      context.stop(self)
  }

  private def formatDateNow(): String = {
    s"${Instant.now().getEpochSecond}.000000"
  }
} 
Example 26
Source File: DistShellAppMaster.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.examples.distributedshell

import org.apache.gearpump.cluster.MasterToAppMaster.WorkerList

import scala.concurrent.Future

import akka.actor.{Deploy, Props}
import akka.pattern.{ask, pipe}
import akka.remote.RemoteScope
import com.typesafe.config.Config
import org.slf4j.Logger

import org.apache.gearpump.cluster.ClientToMaster.ShutdownApplication
import org.apache.gearpump.cluster.appmaster.ExecutorSystemScheduler.{ExecutorSystemJvmConfig, ExecutorSystemStarted, StartExecutorSystemTimeout}
import org.apache.gearpump.cluster._
import org.apache.gearpump.examples.distributedshell.DistShellAppMaster._
import org.apache.gearpump.util.{ActorUtil, Constants, LogUtil, Util}

class DistShellAppMaster(appContext: AppMasterContext, app: AppDescription)
  extends ApplicationMaster {

  import appContext._
  import context.dispatcher
  implicit val timeout = Constants.FUTURE_TIMEOUT
  private val LOG: Logger = LogUtil.getLogger(getClass, app = appId)
  protected var currentExecutorId = 0
  private var workerNum: Option[Int] = None

  override def preStart(): Unit = {
    LOG.info(s"Distributed Shell AppMaster started")
    ActorUtil.launchExecutorOnEachWorker(masterProxy, getExecutorJvmConfig, self)
  }

  override def receive: Receive = {
    case ExecutorSystemStarted(executorSystem, _) =>
      import executorSystem.{address, resource => executorResource, worker}
      val executorContext = ExecutorContext(currentExecutorId, worker, appId, app.name,
        self, executorResource)
      // Start executor
      val executor = context.actorOf(Props(classOf[ShellExecutor], executorContext, app.userConfig)
        .withDeploy(Deploy(scope = RemoteScope(address))), currentExecutorId.toString)
      executorSystem.bindLifeCycleWith(executor)
      currentExecutorId += 1
      ActorUtil.tellMasterIfApplicationReady(workerNum, currentExecutorId, appContext)
    case WorkerList(workers) =>
      workerNum = Some(workers.length)
      ActorUtil.tellMasterIfApplicationReady(workerNum, currentExecutorId, appContext)
    case StartExecutorSystemTimeout =>
      LOG.error(s"Failed to allocate resource in time")
      masterProxy ! ShutdownApplication(appId)
      context.stop(self)
    case msg: ShellCommand =>
      Future.fold(context.children.map(_ ? msg))(new ShellCommandResultAggregator) {
        (aggregator, response) => {
          aggregator.aggregate(response.asInstanceOf[ShellCommandResult])
        }
      }.map(_.toString()) pipeTo sender
  }

  private def getExecutorJvmConfig: ExecutorSystemJvmConfig = {
    val config: Config = app.clusterConfig
    val jvmSetting = Util.resolveJvmSetting(config.withFallback(context.system.settings.config))
      .executor
    ExecutorSystemJvmConfig(jvmSetting.classPath, jvmSetting.vmargs,
      appJar, username, config)
  }
}

object DistShellAppMaster {
  case class ShellCommand(command: String)

  case class ShellCommandResult(executorId: Int, msg: Any)

  class ShellCommandResultAggregator {
    val result: StringBuilder = new StringBuilder

    def aggregate(response: ShellCommandResult): ShellCommandResultAggregator = {
      result.append(s"Execute results from executor ${response.executorId} : \n")
      result.append(response.msg + "\n")
      this
    }

    override def toString: String = result.toString()
  }
} 
Example 27
Source File: FastSyncStateStorageActor.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.blockchain.sync

import akka.actor.{Actor, ActorLogging}
import akka.pattern.pipe
import io.iohk.ethereum.blockchain.sync.FastSync.SyncState
import io.iohk.ethereum.blockchain.sync.FastSyncStateStorageActor.GetStorage
import io.iohk.ethereum.db.storage.FastSyncStateStorage

import scala.concurrent.Future
import scala.util.{Failure, Success, Try}


class FastSyncStateStorageActor extends Actor with ActorLogging {

  def receive: Receive = {
    // after initialization send a valid Storage reference
    case storage: FastSyncStateStorage => context become idle(storage)
  }

  def idle(storage: FastSyncStateStorage): Receive = {
    // begin saving of the state to the storage and become busy
    case state: SyncState => persistState(storage, state)

    case GetStorage => sender() ! storage.getSyncState()
  }

  def busy(storage: FastSyncStateStorage, stateToPersist: Option[SyncState]): Receive = {
    // update state waiting to be persisted later. we only keep newest state
    case state: SyncState => context become busy(storage, Some(state))
    // exception was thrown during persisting of a state. push
    case Failure(e) => throw e
    // state was saved in the storage. become idle
    case Success(s: FastSyncStateStorage) if stateToPersist.isEmpty => context become idle(s)
    // state was saved in the storage but new state is already waiting to be saved.
    case Success(s: FastSyncStateStorage) if stateToPersist.isDefined => stateToPersist.foreach(persistState(s, _))

    case GetStorage => sender() ! storage.getSyncState()
  }

  private def persistState(storage: FastSyncStateStorage, syncState: SyncState): Unit = {
    import context.dispatcher
    val persistingQueues: Future[Try[FastSyncStateStorage]] = Future {
      lazy val result = Try { storage.putSyncState(syncState) }
      if (log.isDebugEnabled) {
        val now = System.currentTimeMillis()
        result
        val end = System.currentTimeMillis()
        log.debug(s"Saving snapshot of a fast sync took ${end - now} ms")
        result
      } else {
        result
      }
    }
    persistingQueues pipeTo self
    context become busy(storage, None)
  }

}

object FastSyncStateStorageActor {
  case object GetStorage
} 
Example 28
Source File: Actors.scala    From scala-concurrency-playground   with MIT License 5 votes vote down vote up
package org.zalando.benchmarks

import akka.actor._
import akka.pattern.{ask, pipe}
import akka.routing.BalancingPool
import akka.util.Timeout
import org.zalando.benchmarks.ComputationFollowedByAsyncPublishing._

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._

class Actors(system: ActorSystem) {
  def benchmark(coreFactor: Int): Unit = {
    import system.dispatcher
    implicit val timeout = Timeout(1 hour)

    // Route computations through a balanced pool of (cpu bound) computation workers.
    val router = system actorOf BalancingPool(numWorkers(coreFactor)).props(Props[ComputeActor])

    try {
      // Collect the results, sum them up and print the sum.
      printResult(Await.result(Future.traverse(1 to numTasks map Job) { job =>
        (router ? job).mapTo[PublishResult]
      }, 1 hour))

    } finally {
      // Shut down the actors.
      router ! PoisonPill
    }
  }
}

// Actor responsible for the computation, and for delegating to the publishing actor(s).
class ComputeActor extends Actor {
  val publisher = context actorOf Props[PublishActor]

  def receive = {
    case job: Job =>
      // tell the publisher about who sent us the job, and the job results
      val s = sender()
      publisher ! (s, Computer compute job)
  }
}

// Actor responsible for publishing, and for sending the response back.
class PublishActor extends Actor {
  import context.dispatcher

  def receive = {
    case (s: ActorRef, r: JobResult) =>
      // just pipe the result back to the original sender
      Publisher.publish(r, context.system) pipeTo s
  }
} 
Example 29
Source File: Replica.scala    From Principles-of-Reactive-Programming   with GNU General Public License v3.0 5 votes vote down vote up
package kvstore

import akka.actor.{ OneForOneStrategy, Props, ActorRef, Actor }
import kvstore.Arbiter._
import scala.collection.immutable.Queue
import akka.actor.SupervisorStrategy.Restart
import scala.annotation.tailrec
import akka.pattern.{ ask, pipe }
import akka.actor.Terminated
import scala.concurrent.duration._
import akka.actor.PoisonPill
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy
import akka.util.Timeout

object Replica {
  sealed trait Operation {
    def key: String
    def id: Long
  }
  case class Insert(key: String, value: String, id: Long) extends Operation
  case class Remove(key: String, id: Long) extends Operation
  case class Get(key: String, id: Long) extends Operation

  sealed trait OperationReply
  case class OperationAck(id: Long) extends OperationReply
  case class OperationFailed(id: Long) extends OperationReply
  case class GetResult(key: String, valueOption: Option[String], id: Long) extends OperationReply

  def props(arbiter: ActorRef, persistenceProps: Props): Props = Props(new Replica(arbiter, persistenceProps))
}

class Replica(val arbiter: ActorRef, persistenceProps: Props) extends Actor {
  import Replica._
  import Replicator._
  import Persistence._
  import context.dispatcher

  
  val replica: Receive = {
    case _ =>
  }

} 
Example 30
Source File: PolicyManager.scala    From wookiee   with Apache License 2.0 5 votes vote down vote up
package com.webtrends.harness.policy

import akka.pattern.{ask, pipe}
import akka.actor.{ActorRef, Props}
import akka.routing.{RoundRobinPool, FromConfig}
import com.webtrends.harness.HarnessConstants
import com.webtrends.harness.app.{PrepareForShutdown, HActor}
import com.webtrends.harness.app.HarnessActor.SystemReady
import org.slf4j.LoggerFactory

import scala.collection.mutable
import scala.concurrent.{Future, Promise}
import scala.util.{Success, Failure}

case class GetPolicies()



class PolicyManager extends PrepareForShutdown {

  import context.dispatcher

  override def receive = super.receive orElse {
    case GetPolicies => pipe(getPolicies) to sender

    case SystemReady => // ignore
  }

  protected def getPolicies : Future[Map[String, Policy]] = {
    Future { PolicyManager.getPolicies.get }
  }

}

object PolicyManager {
  private val externalLogger = LoggerFactory.getLogger(this.getClass)

  // map that stores the name of the command with the actor it references
  val policyMap = mutable.Map[String, Policy]()

  def props = Props[PolicyManager]

  def addPolicy[T<:Policy](name:String, ref:T) = {
    ref.addCommands
    externalLogger.debug(s"Policy $name inserted into Policy Manager map.")
    policyMap += (name -> ref)
  }

  protected def removePolicy(name:String) : Boolean = {
    policyMap.get(name) match {
      case Some(n) =>
        externalLogger.debug(s"Policy $name removed from Policy Manager map.")
        policyMap -= name
        true
      case None => false
    }
  }

  def getPolicy(name:String) : Option[Policy] = policyMap.get(name)

  def getPolicies : Option[Map[String, Policy]] = Some(policyMap.toMap)
} 
Example 31
Source File: Command.scala    From wookiee   with Apache License 2.0 5 votes vote down vote up
package com.webtrends.harness.command

import akka.pattern.pipe
import com.webtrends.harness.app.HActor
import scala.util.{Try, Success, Failure}

import scala.concurrent.Future


  def matchPath(commandPath:String, requestPath:String) : Option[CommandBean] = {

    import com.webtrends.harness.utils.StringPathUtil._

    val bean = new CommandBean()
    val urlPath = requestPath.splitPath()

    val matched = urlPath.corresponds(commandPath.splitPath()) {
      // Convert the segment into an Integer if possible, otherwise leave it as a String
      case (uri, test) if test.head == '$' =>
        val key = test.substring(1)
        Try(uri.toInt) match {
          case Success(v) => bean.addValue(key, v.asInstanceOf[Integer])
          case Failure(_) => bean.addValue(key, uri)
        }
        true

      // Treat the value as a string
      case (uri, test) if test.head == '%' =>
        bean.addValue(test.drop(1), uri)
        true

      // Only match if the value is an INT
      case (uri, test) if test.head == '#' =>
        Try(uri.toInt) match {
          case Success(v) =>
            bean.addValue(test.drop(1), v.asInstanceOf[Integer])
            true
          case Failure(_) =>
            false
        }

      case (uri, test) =>
        test.toLowerCase.split('|').contains(uri.toLowerCase)
    }

    if (matched) Some(bean)
    else None
  }
} 
Example 32
Source File: KafkaConsumerProxy.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.kafka.consumer

import akka.actor.Actor
import akka.pattern.pipe
import hydra.kafka.consumer.KafkaConsumerProxy._
import hydra.kafka.util.KafkaUtils
import org.apache.kafka.clients.consumer.Consumer
import org.apache.kafka.common.{PartitionInfo, TopicPartition}

import scala.collection.JavaConverters._
import scala.collection.immutable.Map
import scala.concurrent.Future

class KafkaConsumerProxy extends Actor {

  private var _defaultConsumer: Consumer[String, String] = _

  private implicit val ec = context.dispatcher

  override def preStart(): Unit = {
    _defaultConsumer = KafkaUtils.stringConsumerSettings.createKafkaConsumer()
  }

  override def receive: Receive = {
    case GetLatestOffsets(topic) =>
      val requestor = sender
      pipe(latestOffsets(topic).map(LatestOffsetsResponse(topic, _))) to requestor

    case GetPartitionInfo(topic) =>
      val requestor = sender
      pipe(partitionInfo(topic).map(PartitionInfoResponse(topic, _))) to requestor

    case ListTopics =>
      val requestor = sender
      pipe(listTopics().map(ListTopicsResponse(_))) to requestor
  }

  override def postStop(): Unit = {
    _defaultConsumer.close()
  }

  private def latestOffsets(
      topic: String
  ): Future[Map[TopicPartition, Long]] = {
    Future {
      val ts = _defaultConsumer
        .partitionsFor(topic)
        .asScala
        .map(pi => new TopicPartition(topic, pi.partition()))
      _defaultConsumer
        .endOffsets(ts.asJava)
        .asScala
        .map(tp => tp._1 -> tp._2.toLong)
        .toMap
    }
  }

  private def partitionInfo(topic: String): Future[Seq[PartitionInfo]] =
    Future(_defaultConsumer.partitionsFor(topic).asScala)

  private def listTopics(): Future[Map[String, Seq[PartitionInfo]]] = {
    Future(_defaultConsumer.listTopics().asScala.toMap)
      .map(res => res.mapValues(_.asScala.toSeq))
  }

}

object KafkaConsumerProxy {

  case class GetLatestOffsets(topic: String)

  case class LatestOffsetsResponse(
      topic: String,
      offsets: Map[TopicPartition, Long]
  )

  case class GetPartitionInfo(topic: String)

  case class PartitionInfoResponse(
      topic: String,
      partitionInfo: Seq[PartitionInfo]
  )

  case object ListTopics

  case class ListTopicsResponse(topics: Map[String, Seq[PartitionInfo]])

} 
Example 33
Source File: Ingestor.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.core.ingest

import akka.actor.{Actor, OneForOneStrategy, SupervisorStrategy}
import akka.pattern.pipe
import hydra.core.akka.InitializingActor
import hydra.core.monitor.HydraMetrics
import hydra.core.protocol._
import hydra.core.transport.{AckStrategy, HydraRecord, RecordFactory, Transport}
import org.apache.commons.lang3.ClassUtils

import scala.concurrent.Future
import scala.util.{Success, Try}


  def validateRequest(request: HydraRequest): Try[HydraRequest] =
    Success(request)

  def doValidate(request: HydraRequest): Future[MessageValidationResult] = {
    Future
      .fromTry(validateRequest(request))
      .flatMap[MessageValidationResult] { r =>
        recordFactory.build(r).map { r =>
          val destination = r.destination

          val ackStrategy = r.ackStrategy.toString

          HydraMetrics.incrementGauge(
            lookupKey =
              ReconciliationMetricName + s"_${destination}_$ackStrategy",
            metricName = ReconciliationMetricName,
            tags = Seq(
              "ingestor" -> name,
              "destination" -> destination,
              "ackStrategy" -> ackStrategy
            )
          )

          HydraMetrics.incrementCounter(
            lookupKey =
              IngestCounterMetricName + s"_${destination}_$ackStrategy",
            metricName = IngestCounterMetricName,
            tags = Seq(
              "ingestor" -> name,
              "destination" -> destination,
              "ackStrategy" -> ackStrategy
            )
          )

          ValidRequest(r)
        }
      }
      .recover { case e => InvalidRequest(e) }
  }

  override def initializationError(ex: Throwable): Receive = {
    case Publish(req) =>
      sender ! IngestorError(ex)
      context.system.eventStream
        .publish(IngestorUnavailable(thisActorName, ex, req))
    case _ =>
      sender ! IngestorError(ex)
  }

  def ingest(next: Actor.Receive) = compose(next)

  override val supervisorStrategy = OneForOneStrategy() {
    case _ => SupervisorStrategy.Restart
  }

  def decrementGaugeOnReceipt(
      destination: String,
      ackStrategy: String
  ): Future[Unit] = {
    Future {
      HydraMetrics.decrementGauge(
        lookupKey =
          Ingestor.ReconciliationMetricName + s"_${destination}_$ackStrategy",
        metricName = Ingestor.ReconciliationMetricName,
        tags = Seq(
          "ingestor" -> name,
          "destination" -> destination,
          "ackStrategy" -> ackStrategy
        )
      )
    }
  }
}

object Ingestor {

  val ReconciliationMetricName = "hydra_ingest_reconciliation"

  val IngestCounterMetricName = "hydra_ingest_message_counter"
} 
Example 34
Source File: InitializingActor.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.core.akka

import akka.actor.{Actor, ActorRef, ReceiveTimeout, Stash}
import akka.pattern.pipe
import hydra.common.config.ActorConfigSupport
import hydra.common.logging.LoggingAdapter
import hydra.core.HydraException
import hydra.core.akka.InitializingActor.{InitializationError, Initialized}
import hydra.core.protocol.HydraMessage
import retry.Success

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.control.NonFatal

trait InitializingActor
    extends Actor
    with ActorConfigSupport
    with Stash
    with LoggingAdapter {

  
  def initializationError(ex: Throwable): Receive
}

object InitializingActor {

  case object Initialized extends HydraMessage

  case class InitializationError(cause: Throwable) extends HydraMessage

}

@SerialVersionUID(1L)
class ActorInitializationException(
    ingestor: ActorRef,
    message: String,
    cause: Throwable
) extends HydraException(
      ActorInitializationException.enrichedMessage(ingestor, message),
      cause
    ) {
  def getActor: ActorRef = ingestor
}

object ActorInitializationException {

  private def enrichedMessage(actor: ActorRef, message: String) =
    Option(actor).map(a => s"${a.path}: $message").getOrElse(message)

  private[hydra] def apply(
      actor: ActorRef,
      message: String,
      cause: Throwable = null
  ) =
    new ActorInitializationException(actor, message, cause)

  def unapply(
      ex: ActorInitializationException
  ): Option[(ActorRef, String, Throwable)] =
    Some((ex.getActor, ex.getMessage, ex.getCause))
} 
Example 35
Source File: VertxSingleConfirmationSender.scala    From eventuate   with Apache License 2.0 5 votes vote down vote up
package com.rbmhtechnology.eventuate.adapter.vertx

import akka.actor.{ ActorLogging, ActorRef, Props }
import akka.pattern.pipe
import com.rbmhtechnology.eventuate.adapter.vertx.api.EndpointRouter
import com.rbmhtechnology.eventuate.{ ConfirmedDelivery, EventsourcedActor }
import io.vertx.core.Vertx

import scala.concurrent.duration.FiniteDuration
import scala.util.{ Failure, Success }

private[vertx] object VertxSingleConfirmationSender {

  case class DeliverEvent(evt: EventEnvelope, deliveryId: String)
  case class Confirm(deliveryId: String)
  case class DeliverFailed(evt: EventEnvelope, deliveryId: String, err: Throwable)
  case object Redeliver

  case class DeliveryConfirmed()

  def props(id: String, eventLog: ActorRef, endpointRouter: EndpointRouter, vertx: Vertx, confirmationTimeout: FiniteDuration): Props =
    Props(new VertxSingleConfirmationSender(id, eventLog, endpointRouter, vertx, confirmationTimeout))
}

private[vertx] class VertxSingleConfirmationSender(val id: String, val eventLog: ActorRef, val endpointRouter: EndpointRouter, val vertx: Vertx, confirmationTimeout: FiniteDuration)
  extends EventsourcedActor with ConfirmedDelivery with VertxSender with ActorLogging {

  import VertxSingleConfirmationSender._
  import context.dispatcher

  context.system.scheduler.schedule(confirmationTimeout, confirmationTimeout, self, Redeliver)

  override def onCommand: Receive = {
    case DeliverEvent(envelope, deliveryId) =>
      send[Any](envelope.address, envelope.evt, confirmationTimeout)
        .map(_ => Confirm(deliveryId))
        .recover {
          case err => DeliverFailed(envelope, deliveryId, err)
        }
        .pipeTo(self)

    case Confirm(deliveryId) if unconfirmed.contains(deliveryId) =>
      persistConfirmation(DeliveryConfirmed(), deliveryId) {
        case Success(evt) =>
        case Failure(err) => log.error(s"Confirmation for delivery with id '$deliveryId' could not be persisted.", err)
      }

    case Redeliver =>
      redeliverUnconfirmed()

    case DeliverFailed(evt, deliveryId, err) =>
      log.warning(s"Delivery with id '$deliveryId' for event [$evt] failed with $err. The delivery will be retried.")
  }

  override def onEvent: Receive = {
    case DeliveryConfirmed() =>
    // confirmations should not be published
    case ev =>
      endpointRouter.endpoint(ev) match {
        case Some(endpoint) =>
          val deliveryId = lastSequenceNr.toString
          deliver(deliveryId, DeliverEvent(EventEnvelope(endpoint, lastHandledEvent), deliveryId), self.path)
        case None =>
      }
  }
} 
Example 36
Source File: ServiceAvailabilityActor.scala    From reactive-consul   with MIT License 5 votes vote down vote up
package stormlantern.consul.client
package discovery

import scala.concurrent.{ ExecutionContext, Future }
import akka.actor._
import akka.pattern.pipe
import dao._
import ServiceAvailabilityActor._

class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef) extends Actor {

  implicit val ec: ExecutionContext = context.dispatcher

  // Actor state
  var initialized = false
  var serviceAvailabilityState: IndexedServiceInstances = IndexedServiceInstances.empty

  def receive: Receive = {
    case Start ⇒
      self ! UpdateServiceAvailability(None)
    case UpdateServiceAvailability(services: Option[IndexedServiceInstances]) ⇒
      val (update, serviceChange) = updateServiceAvailability(services.getOrElse(IndexedServiceInstances.empty))
      update.foreach(listener ! _)
      if (!initialized && services.isDefined) {
        initialized = true
        listener ! Started
      }
      serviceChange.map(changes ⇒ UpdateServiceAvailability(Some(changes))) pipeTo self
  }

  def updateServiceAvailability(services: IndexedServiceInstances): (Option[ServiceAvailabilityUpdate], Future[IndexedServiceInstances]) = {
    val update = if (serviceAvailabilityState.index != services.index) {
      val oldServices = serviceAvailabilityState
      serviceAvailabilityState = services.filterForTags(serviceDefinition.serviceTags)
      Some(createServiceAvailabilityUpdate(oldServices, serviceAvailabilityState))
    } else {
      None
    }
    (update, httpClient.getService(
      serviceDefinition.serviceName,
      serviceDefinition.serviceTags.headOption,
      Some(services.index),
      Some("1s")
    ))
  }

  def createServiceAvailabilityUpdate(oldState: IndexedServiceInstances, newState: IndexedServiceInstances): ServiceAvailabilityUpdate = {
    val deleted = oldState.resource.diff(newState.resource)
    val added = newState.resource.diff(oldState.resource)
    ServiceAvailabilityUpdate(serviceDefinition.key, added, deleted)
  }

}

object ServiceAvailabilityActor {

  def props(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef): Props = Props(new ServiceAvailabilityActor(httpClient, serviceDefinition, listener))

  // Messages
  case object Start
  case object Started
  case object Initialized
  private case class UpdateServiceAvailability(services: Option[IndexedServiceInstances])
  private[client] case class ServiceAvailabilityUpdate(key: String, added: Set[ServiceInstance] = Set.empty,
    removed: Set[ServiceInstance] = Set.empty)
} 
Example 37
Source File: LoadBalancerActor.scala    From reactive-consul   with MIT License 5 votes vote down vote up
package stormlantern.consul.client.loadbalancers

import akka.actor.Status.Failure
import akka.actor.{ Props, Actor, ActorLogging }
import LoadBalancerActor._
import stormlantern.consul.client.discovery.{ ConnectionProvider, ConnectionHolder }
import stormlantern.consul.client.ServiceUnavailableException
import scala.concurrent.ExecutionContext.Implicits.global
import scala.collection.mutable

class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor with ActorLogging {

  import akka.pattern.pipe

  // Actor state
  val connectionProviders = mutable.Map.empty[String, ConnectionProvider]

  override def postStop(): Unit = {
    log.debug(s"LoadBalancerActor for $key stopped, destroying all connection providers")
    connectionProviders.values.foreach(_.destroy())
  }

  def receive: PartialFunction[Any, Unit] = {

    case GetConnection ⇒
      selectConnection match {
        case Some((id, connectionProvider)) ⇒ connectionProvider.getConnectionHolder(id, self) pipeTo sender
        case None                           ⇒ sender ! Failure(ServiceUnavailableException(key))
      }
    case ReturnConnection(connection)        ⇒ returnConnection(connection)
    case AddConnectionProvider(id, provider) ⇒ addConnectionProvider(id, provider)
    case RemoveConnectionProvider(id)        ⇒ removeConnectionProvider(id)
    case HasAvailableConnectionProvider      ⇒ sender ! connectionProviders.nonEmpty
  }

  def selectConnection: Option[(String, ConnectionProvider)] =
    loadBalancer.selectConnection.flatMap(id ⇒ connectionProviders.get(id).map(id → _))

  def returnConnection(connection: ConnectionHolder): Unit = {
    connectionProviders.get(connection.id).foreach(_.returnConnection(connection))
    loadBalancer.connectionReturned(connection.id)
  }

  def addConnectionProvider(id: String, provider: ConnectionProvider): Unit = {
    connectionProviders.put(id, provider)
    loadBalancer.connectionProviderAdded(id)
  }

  def removeConnectionProvider(id: String): Unit = {
    connectionProviders.remove(id).foreach(_.destroy())
    loadBalancer.connectionProviderRemoved(id)
  }
}

object LoadBalancerActor {
  // Props
  def props(loadBalancer: LoadBalancer, key: String) = Props(new LoadBalancerActor(loadBalancer, key))
  // Messsages
  case object GetConnection
  case class ReturnConnection(connection: ConnectionHolder)
  case class AddConnectionProvider(id: String, provider: ConnectionProvider)
  case class RemoveConnectionProvider(id: String)
  case object HasAvailableConnectionProvider
} 
Example 38
Source File: PublishActor.scala    From sns   with Apache License 2.0 5 votes vote down vote up
package me.snov.sns.actor

import akka.actor.Status.{Failure, Success}
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.pattern.ask
import akka.pattern.pipe
import akka.util.Timeout
import me.snov.sns.actor.SubscribeActor.CmdFanOut
import me.snov.sns.model.{Message, MessageAttribute}

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

object PublishActor {
  def props(actor: ActorRef) = Props(classOf[PublishActor], actor)

  case class CmdPublish(topicArn: String, bodies: Map[String, String], messageAttributes: Map[String, MessageAttribute])
}

class PublishActor(subscribeActor: ActorRef) extends Actor with ActorLogging {
  import me.snov.sns.actor.PublishActor._

  private implicit val timeout = Timeout(1.second)
  private implicit val ec = context.dispatcher

  private def publish(topicArn: String, bodies: Map[String, String], messageAttributes: Map[String, MessageAttribute])(implicit ec: ExecutionContext) = {
    val message = Message(bodies, messageAttributes = messageAttributes)

    (subscribeActor ? CmdFanOut(topicArn, message)).map {
      case Failure(e) => Failure(e)
      case Success => message
    }
  }

  override def receive = {
    case CmdPublish(topicArn, bodies, attributes) => publish(topicArn, bodies, attributes) pipeTo sender
  }
} 
Example 39
Source File: DropRepeatedSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.server.api

import akka.actor.ActorSystem
import akka.pattern.pipe
import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import akka.testkit.{TestKit, TestProbe}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

import scala.collection.immutable
import scala.concurrent.ExecutionContext

final class DropRepeatedSpec
    extends TestKit(ActorSystem(classOf[DropRepeatedSpec].getSimpleName))
    with WordSpecLike
    with Matchers
    with BeforeAndAfterAll {

  private[this] implicit val materializer: Materializer = Materializer(system)
  private[this] implicit val executionContext: ExecutionContext = materializer.executionContext

  override def afterAll: Unit = {
    TestKit.shutdownActorSystem(system)
  }

  "DropRepeated" should {
    "drop repeated elements" in {
      val probe = TestProbe()
      val input = immutable.Seq(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5)

      val _ = Source(input)
        .via(DropRepeated())
        .runWith(Sink.seq)
        .pipeTo(probe.ref)
        .failed
        .foreach(fail(_))

      probe.expectMsg(Vector(1, 2, 3, 4, 5))
    }

    "does not drop duplicate elements that are not repeated" in {
      val probe = TestProbe()
      val input = immutable.Seq(1, 1, 2, 2, 1, 1, 2, 2)

      val _ = Source(input)
        .via(DropRepeated())
        .runWith(Sink.seq)
        .pipeTo(probe.ref)
        .failed
        .foreach(fail(_))

      probe.expectMsg(Vector(1, 2, 1, 2))
    }
  }
} 
Example 40
Source File: DistServiceAppMaster.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.distributeservice

import java.io.File
import org.apache.gearpump.cluster.MasterToAppMaster.WorkerList

import scala.concurrent.Future

import akka.actor.{Deploy, Props}
import akka.pattern.{ask, pipe}
import akka.remote.RemoteScope
import com.typesafe.config.Config
import org.slf4j.Logger

import org.apache.gearpump.cluster.ClientToMaster.ShutdownApplication
import org.apache.gearpump.cluster.appmaster.ExecutorSystemScheduler.{ExecutorSystemJvmConfig, ExecutorSystemStarted, StartExecutorSystemTimeout}
import org.apache.gearpump.cluster.{AppDescription, AppMasterContext, ApplicationMaster, ExecutorContext}
import org.apache.gearpump.experiments.distributeservice.DistServiceAppMaster.{FileContainer, GetFileContainer, InstallService}
import org.apache.gearpump.util._

class DistServiceAppMaster(appContext: AppMasterContext, app: AppDescription)
  extends ApplicationMaster {
  import appContext._
  import context.dispatcher
  implicit val timeout = Constants.FUTURE_TIMEOUT
  private val LOG: Logger = LogUtil.getLogger(getClass, app = appId)
  private var currentExecutorId = 0
  private var workerNum: Option[Int] = None
  private var fileServerPort = -1

  val rootDirectory = new File("/")
  val host = context.system.settings.config.getString(Constants.GEARPUMP_HOSTNAME)
  val server = context.actorOf(Props(classOf[FileServer], rootDirectory, host, 0))

  override def preStart(): Unit = {
    LOG.info(s"Distribute Service AppMaster started")
    ActorUtil.launchExecutorOnEachWorker(masterProxy, getExecutorJvmConfig, self)
  }

  (server ? FileServer.GetPort).asInstanceOf[Future[FileServer.Port]] pipeTo self

  override def receive: Receive = {
    case ExecutorSystemStarted(executorSystem, _) =>
      import executorSystem.{address, resource => executorResource, worker}
      val executorContext = ExecutorContext(currentExecutorId, worker,
        appId, app.name, self, executorResource)
      // start executor
      val executor = context.actorOf(Props(classOf[DistServiceExecutor],
        executorContext, app.userConfig).withDeploy(
        Deploy(scope = RemoteScope(address))), currentExecutorId.toString)
      executorSystem.bindLifeCycleWith(executor)
      currentExecutorId += 1
      ActorUtil.tellMasterIfApplicationReady(workerNum, currentExecutorId, appContext)
    case WorkerList(workers) =>
      workerNum = Some(workers.length)
      ActorUtil.tellMasterIfApplicationReady(workerNum, currentExecutorId, appContext)
    case StartExecutorSystemTimeout =>
      LOG.error(s"Failed to allocate resource in time")
      masterProxy ! ShutdownApplication(appId)
      context.stop(self)
    case FileServer.Port(port) =>
      this.fileServerPort = port
    case GetFileContainer =>
      val name = Math.abs(new java.util.Random().nextLong()).toString
      sender ! new FileContainer(s"http://$host:$fileServerPort/$name")
    case installService: InstallService =>
      context.children.foreach(_ ! installService)
  }

  private def getExecutorJvmConfig: ExecutorSystemJvmConfig = {
    val config: Config = app.clusterConfig
    val jvmSetting = Util.resolveJvmSetting(
      config.withFallback(context.system.settings.config)).executor
    ExecutorSystemJvmConfig(jvmSetting.classPath, jvmSetting.vmargs,
      appJar, username, config)
  }
}

object DistServiceAppMaster {
  case object GetFileContainer

  case class FileContainer(url: String)

  case class InstallService(
      url: String,
      zipFileName: String,
      targetPath: String,
      script: Array[Byte],
      serviceName: String,
      serviceSettings: Map[String, Any])
} 
Example 41
Source File: JarStoreServer.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.jarstore

import akka.actor.{Actor, Stash}
import akka.pattern.pipe

import org.apache.gearpump.cluster.ClientToMaster.{GetJarStoreServer, JarStoreServerAddress}
import org.apache.gearpump.util._

class JarStoreServer(jarStoreRootPath: String) extends Actor with Stash {
  private val host = context.system.settings.config.getString(Constants.GEARPUMP_HOSTNAME)
  private val jarStore = JarStore.get(jarStoreRootPath)
  jarStore.init(context.system.settings.config)
  private val server = new FileServer(context.system, host, 0, jarStore)
  implicit val timeout = Constants.FUTURE_TIMEOUT
  implicit val executionContext = context.dispatcher

  server.start pipeTo self

  def receive: Receive = {
    case FileServer.Port(port) =>
      context.become(listen(port))
      unstashAll()
    case _ =>
      stash()
  }

  def listen(port: Int): Receive = {
    case GetJarStoreServer =>
      sender ! JarStoreServerAddress(s"http://$host:$port/")
  }

  override def postStop(): Unit = {
    server.stop
  }
} 
Example 42
Source File: SignatureManagerActor.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.communication.security

import akka.actor.{Props, ActorRef, Actor}
import akka.util.Timeout
import org.apache.toree.communication.utils.OrderedSupport
import org.apache.toree.kernel.protocol.v5.KernelMessage
import org.apache.toree.utils.LogLike

import scala.concurrent.duration._
import akka.pattern.ask
import akka.pattern.pipe

class SignatureManagerActor(
  key: String, scheme: String
) extends Actor with LogLike with OrderedSupport {
  private val hmac = Hmac(key, HmacAlgorithm(scheme))

  def this(key: String) = this(key, HmacAlgorithm.SHA256.toString)

  // NOTE: Required to provide the execution context for futures with akka
  import context._

  // NOTE: Required for ask (?) to function... maybe can define elsewhere?
  implicit val timeout = Timeout(5.seconds)

  //
  // List of child actors that the signature manager contains
  //
  private var signatureChecker: ActorRef = _
  private var signatureProducer: ActorRef = _

  
  override def orderedTypes(): Seq[Class[_]] = Seq(
    classOf[(String, Seq[_])],
    classOf[KernelMessage]
  )
} 
Example 43
Source File: BoxOffice.scala    From 006877   with MIT License 5 votes vote down vote up
package com.goticks

import scala.concurrent.Future

import akka.actor._
import akka.util.Timeout

object BoxOffice {
  def props(implicit timeout: Timeout) = Props(new BoxOffice)
  def name = "boxOffice"


  case class CreateEvent(name: String, tickets: Int)
  case class GetEvent(name: String)
  case object GetEvents
  case class GetTickets(event: String, tickets: Int)
  case class CancelEvent(name: String)

  case class Event(name: String, tickets: Int)
  case class Events(events: Vector[Event])

  sealed trait EventResponse
  case class EventCreated(event: Event) extends EventResponse
  case object EventExists extends EventResponse

}

class BoxOffice(implicit timeout: Timeout) extends Actor {
  import BoxOffice._
  import context._


  def createTicketSeller(name: String) =
    context.actorOf(TicketSeller.props(name), name)

  def receive = {
    case CreateEvent(name, tickets) =>
      def create() = {
        val eventTickets = createTicketSeller(name)
        val newTickets = (1 to tickets).map { ticketId =>
          TicketSeller.Ticket(ticketId)
        }.toVector
        eventTickets ! TicketSeller.Add(newTickets)
        sender() ! EventCreated(Event(name, tickets))
      }
      context.child(name).fold(create())(_ => sender() ! EventExists)

    case GetTickets(event, tickets) =>
      def notFound() = sender() ! TicketSeller.Tickets(event)
      def buy(child: ActorRef) =
        child.forward(TicketSeller.Buy(tickets))

      context.child(event).fold(notFound())(buy)

    case GetEvent(event) =>
      def notFound() = sender() ! None
      def getEvent(child: ActorRef) = child forward TicketSeller.GetEvent
      
	  context.child(event).fold(notFound())(getEvent)

    case GetEvents =>
      import akka.pattern.ask
      import akka.pattern.pipe

      def getEvents = context.children.map { child =>
        self.ask(GetEvent(child.path.name)).mapTo[Option[Event]]
      }
      def convertToEvents(f: Future[Iterable[Option[Event]]]) =
        f.map(_.flatten).map(l=> Events(l.toVector))

      pipe(convertToEvents(Future.sequence(getEvents))) to sender()


    case CancelEvent(event) =>
      def notFound() = sender() ! None
      def cancelEvent(child: ActorRef) = child forward TicketSeller.Cancel
      context.child(event).fold(notFound())(cancelEvent)
  }
} 
Example 44
Source File: BoxOffice.scala    From 006877   with MIT License 5 votes vote down vote up
package com.goticks

import scala.concurrent.Future

import akka.actor._
import akka.util.Timeout

object BoxOffice {
  def props(implicit timeout: Timeout) = Props(new BoxOffice)
  def name = "boxOffice"

  case class CreateEvent(name: String, tickets: Int)
  case class GetEvent(name: String)
  case object GetEvents
  case class GetTickets(event: String, tickets: Int)
  case class CancelEvent(name: String)

  case class Event(name: String, tickets: Int)
  case class Events(events: Vector[Event])

  sealed trait EventResponse
  case class EventCreated(event: Event) extends EventResponse
  case object EventExists extends EventResponse
}

class BoxOffice(implicit timeout: Timeout) extends Actor {
  import BoxOffice._
  import context._

  def createTicketSeller(name: String) =
    context.actorOf(TicketSeller.props(name), name)

  def receive = {
    case CreateEvent(name, tickets) =>
      def create() = {
        val eventTickets = createTicketSeller(name)
        val newTickets = (1 to tickets).map { ticketId =>
          TicketSeller.Ticket(ticketId)
        }.toVector
        eventTickets ! TicketSeller.Add(newTickets)
        sender() ! EventCreated(Event(name, tickets))
      }
      context.child(name).fold(create())(_ => sender() ! EventExists)

    case GetTickets(event, tickets) =>
      def notFound() = sender() ! TicketSeller.Tickets(event)
      def buy(child: ActorRef) =
        child.forward(TicketSeller.Buy(tickets))

      context.child(event).fold(notFound())(buy)

    case GetEvent(event) =>
      def notFound() = sender() ! None
      def getEvent(child: ActorRef) = child forward TicketSeller.GetEvent
      context.child(event).fold(notFound())(getEvent)

    case GetEvents =>
      import akka.pattern.{ ask, pipe }

      def getEvents = context.children.map { child =>
        self.ask(GetEvent(child.path.name)).mapTo[Option[Event]]
      }
      def convertToEvents(f: Future[Iterable[Option[Event]]]) =
        f.map(_.flatten).map(l=> Events(l.toVector))

      pipe(convertToEvents(Future.sequence(getEvents))) to sender()

    case CancelEvent(event) =>
      def notFound() = sender() ! None
      def cancelEvent(child: ActorRef) = child forward TicketSeller.Cancel
      context.child(event).fold(notFound())(cancelEvent)
  }
} 
Example 45
Source File: ChatClient.scala    From Akka-Cookbook   with MIT License 5 votes vote down vote up
package com.packt.chapter7

import akka.actor.{Actor, ActorRef, Props}
import com.packt.chapter7.ChatServer.{Connect, Disconnect, Disconnected, Message}
import akka.pattern.ask
import akka.pattern.pipe

import scala.concurrent.duration._
import akka.util.Timeout

object ChatClient {
  def props(chatServer: ActorRef) = Props(new ChatClient(chatServer))
}

class ChatClient(chatServer: ActorRef) extends Actor {
  import context.dispatcher

  implicit val timeout = Timeout(5 seconds)

  override def preStart = {
    chatServer ! Connect
  }

  def receive = {
    case Disconnect =>
      (chatServer ? Disconnect).pipeTo(self)
    case Disconnected =>
      context.stop(self)
    case body : String =>
      chatServer ! Message(self, body)
    case msg : Message =>
      println(s"Message from [${msg.author}] at [${msg.creationTimestamp}]: ${msg.body}")
  }
} 
Example 46
Source File: UnfoldPullerAsync.scala    From akka-stream-extensions   with Apache License 2.0 5 votes vote down vote up
package com.mfglabs.stream.internals.source

import akka.pattern.pipe
import akka.actor.{Props, Status, ActorLogging}
import akka.stream.actor.ActorPublisher

import scala.concurrent.Future

class UnfoldPullerAsync[A, B](zero: => B)(f: B => Future[(Option[A], Option[B])]) extends ActorPublisher[A] with ActorLogging {
  import akka.stream.actor.ActorPublisherMessage._
  implicit val ec = context.dispatcher

  def receive = waitingForDownstreamReq(zero)

  case object Pull

  def waitingForDownstreamReq(s: B): Receive = {
    case Request(_) | Pull =>
      if (totalDemand > 0 && isActive) {
        f(s).pipeTo(self)
        context.become(waitingForFut(s))
      }

    case Cancel => context.stop(self)
  }

  def waitingForFut(s: B): Receive = {
    case (maybeA: Option[A], maybeB: Option[B]) =>
      maybeA.foreach(onNext)
      maybeB match {
        case Some(b) =>
          if (totalDemand > 0) self ! Pull
          context.become(waitingForDownstreamReq(b))
        case None =>
          onComplete()
      }

    case Request(_) | Pull => // ignoring until we receive the future response

    case Status.Failure(err) =>
      context.become(waitingForDownstreamReq(s))
      onError(err)

    case Cancel => context.stop(self)
  }

}

object UnfoldPullerAsync {
  def props[A, B](zero: => B)(f: B => Future[(Option[A], Option[B])]) = Props(new UnfoldPullerAsync[A, B](zero)(f))
} 
Example 47
Source File: BatchingClient.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.blockchain.bitcoind.rpc

import akka.actor.{Actor, ActorLogging, ActorRef, Status}
import akka.pattern.pipe
import fr.acinq.eclair.blockchain.bitcoind.rpc.BatchingClient.Pending

import scala.collection.immutable.Queue

class BatchingClient(rpcClient: BasicBitcoinJsonRPCClient) extends Actor with ActorLogging {

  import scala.concurrent.ExecutionContext.Implicits.global

  override def receive: Receive = {
    case request: JsonRPCRequest =>
      // immediately process isolated request
      process(queue = Queue(Pending(request, sender)))
  }

  def waiting(queue: Queue[Pending], processing: Seq[Pending]): Receive = {
    case request: JsonRPCRequest =>
      // there is already a batch in flight, just add this request to the queue
      context become waiting(queue :+ Pending(request, sender), processing)

    case responses: Seq[JsonRPCResponse]@unchecked =>
      log.debug("got {} responses", responses.size)
      // let's send back answers to the requestors
      require(responses.size == processing.size, s"responses=${responses.size} != processing=${processing.size}")
      responses.zip(processing).foreach {
        case (JsonRPCResponse(result, None, _), Pending(_, requestor)) => requestor ! result
        case (JsonRPCResponse(_, Some(error), _), Pending(_, requestor)) => requestor ! Status.Failure(JsonRPCError(error))
      }
      process(queue)

    case [email protected](t) =>
      log.error(t, s"got exception for batch of ${processing.size} requests")
      // let's fail all requests
      processing.foreach { case Pending(_, requestor) => requestor ! s }
      process(queue)
  }

  def process(queue: Queue[Pending]) = {
    // do we have queued requests?
    if (queue.isEmpty) {
      log.debug("no more requests, going back to idle")
      context become receive
    } else {
      val (batch, rest) = queue.splitAt(BatchingClient.BATCH_SIZE)
      log.debug(s"sending {} request(s): {} (queue={})", batch.size, batch.groupBy(_.request.method).map(e => e._1 + "=" + e._2.size).mkString(" "), queue.size)
      rpcClient.invoke(batch.map(_.request)) pipeTo self
      context become waiting(rest, batch)
    }
  }

}

object BatchingClient {

  val BATCH_SIZE = 50

  case class Pending(request: JsonRPCRequest, requestor: ActorRef)

} 
Example 48
Source File: LeagueProjection.scala    From eventsourcing-intro   with Apache License 2.0 5 votes vote down vote up
package eu.reactivesystems.league.impl

import akka.actor.{Actor, ActorLogging, Props, Status}
import akka.pattern.pipe
import akka.persistence.cassandra.query.scaladsl.CassandraReadJournal
import akka.persistence.query.{EventEnvelope2, PersistenceQuery}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
import com.lightbend.lagom.scaladsl.persistence.jdbc.JdbcSession

class LeagueProjection(jdbcSession: JdbcSession)
    extends Actor
    with ActorLogging {

  import DBOperations._

  override def receive: Receive = {
    case Status.Failure(ex) =>
      log.error(ex, "read side generation terminated")
      context.stop(self)
  }

  override def preStart(): Unit = {
    val materializer = ActorMaterializer.create(context.system)
    val readJournal = PersistenceQuery
      .get(context.system)
      .readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier)
    import context.dispatcher

    val result = getOffset(jdbcSession)
      .flatMap(
        offset =>
          readJournal
            .eventsByTag(LeagueEvent.Tag.tag, offset)
            .mapAsync(1)(e => projectEvent(e))
            .runWith(Sink.ignore)(materializer))

    result pipeTo self
    ()
  }

  private def projectEvent(event: EventEnvelope2) =
    event.event match {
      case ClubRegistered(club) => addClub(jdbcSession, event.offset, club)
      case GamePlayed(game) => addGame(jdbcSession, event.offset, game)
      case ResultRevoked(game) => revokeResult(jdbcSession, event.offset, game)
    }
}

object LeagueProjection {
  val readSideId = "leagueProjection"

  def props(jdbcSession: JdbcSession) =
    Props(new LeagueProjection(jdbcSession))
} 
Example 49
Source File: BlogEntity.scala    From akka-blog-example   with Apache License 2.0 5 votes vote down vote up
package com.spr.blog

import akka.actor.Props
import akka.pattern.pipe
import akka.persistence.{PersistentActor, SnapshotOffer}

import scala.concurrent.{Future, Promise}


class BlogEntity extends PersistentActor {

  import BlogEntity._
  import context._

  private var state = BlogState()

  // in order to really match the Lagom example properly, we'd create an actor for every blog post, though that
  // sounds a bit overkill to me. then our persistenceId would be something like s"blog-$id".
  override def persistenceId: String = "blog"

  override def receiveCommand: Receive = {
    case GetPost(id) =>
      sender() ! state(id)
    case AddPost(content) =>
      handleEvent(PostAdded(PostId(), content)) pipeTo sender()
      ()
    case UpdatePost(id, content) =>
      state(id) match {
        case response @ Left(_) => sender() ! response
        case Right(_) =>
          handleEvent(PostUpdated(id, content)) pipeTo sender()
          ()
      }
  }

  private def handleEvent[E <: BlogEvent](e: => E): Future[E] = {
    val p = Promise[E]
    persist(e) { event =>
      p.success(event)
      state += event
      system.eventStream.publish(event)
      if (lastSequenceNr != 0 && lastSequenceNr % 1000 == 0) saveSnapshot(state)
    }
    p.future
  }

  override def receiveRecover: Receive = {
    case event: BlogEvent =>
      state += event
    case SnapshotOffer(_, snapshot: BlogState) =>
      state = snapshot
  }

}

object BlogEntity {
  def props = Props(new BlogEntity)

  sealed trait BlogCommand

  final case class GetPost(id: PostId) extends BlogCommand

  final case class AddPost(content: PostContent) extends BlogCommand

  final case class UpdatePost(id: PostId, content: PostContent) extends BlogCommand

  sealed trait BlogEvent {
    val id: PostId
    val content: PostContent
  }

  final case class PostAdded(id: PostId, content: PostContent) extends BlogEvent

  final case class PostUpdated(id: PostId, content: PostContent) extends BlogEvent

  final case class PostNotFound(id: PostId) extends RuntimeException(s"Blog post not found with id $id")

  type MaybePost[+A] = Either[PostNotFound, A]

  final case class BlogState(posts: Map[PostId, PostContent]) {
    def apply(id: PostId): MaybePost[PostContent] = posts.get(id).toRight(PostNotFound(id))

    def +(event: BlogEvent): BlogState = BlogState(posts.updated(event.id, event.content))
  }

  object BlogState {
    def apply(): BlogState = BlogState(Map.empty)
  }

} 
Example 50
Source File: KafkaClientActor.scala    From remora   with MIT License 5 votes vote down vote up
import KafkaClientActor.{DescribeKafkaCluster, DescribeKafkaConsumerGroup, ListConsumers}
import akka.actor.{Actor, ActorLogging, Props}
import akka.pattern.pipe
import kafka.admin.RemoraKafkaConsumerGroupService
import nl.grons.metrics.scala.{ActorInstrumentedLifeCycle, ReceiveCounterActor, ReceiveExceptionMeterActor, ReceiveTimerActor}

object KafkaClientActor {

  sealed trait Command

  case class DescribeKafkaConsumerGroup(consumerGroupName: String) extends Command
  object DescribeKafkaCluster extends Command
  object ListConsumers extends Command

  def props(kafkaConsumerGroupService: RemoraKafkaConsumerGroupService) = Props(classOf[KafkaClientActor], kafkaConsumerGroupService)

}

class BaseKafkaClientActor(kafkaConsumerGroupService: RemoraKafkaConsumerGroupService) extends Actor with ActorLogging
  with nl.grons.metrics.scala.DefaultInstrumented with ActorInstrumentedLifeCycle {

  import context.dispatcher

  def receive: Receive = {
    case DescribeKafkaCluster =>
      log.info(s"Received request for cluster description")
      kafkaConsumerGroupService.describeCluster() pipeTo sender
    case DescribeKafkaConsumerGroup(consumerGroupName) =>
      log.info(s"Received request for $consumerGroupName")
      kafkaConsumerGroupService.describeConsumerGroup(consumerGroupName) pipeTo sender
    case ListConsumers =>
      log.info(s"Received request for consumer list")
      kafkaConsumerGroupService.list() pipeTo sender
  }
}

class KafkaClientActor(kafkaConsumerGroupService: RemoraKafkaConsumerGroupService)
  extends BaseKafkaClientActor(kafkaConsumerGroupService) with ReceiveCounterActor with ReceiveTimerActor with ReceiveExceptionMeterActor