Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import akka.actor._
- import akka.pattern.{AskTimeoutException, ask}
- import akka.util.Timeout
- import com.ctilogic.cvd.common.aux.StackTraceHelper
- import com.ctilogic.cvd.common.messaging.{ErrorResponse, InternalRequest}
- import com.github.levkhomich.akka.tracing.{ActorTracing, TracingSupport}
- import scala.concurrent.{Future, ExecutionContext}
- import scala.util.control.ControlThrowable
- /**
- * This trait provides inheritor ability to communicate with services in distributed by cluster using `askService`
- * and `tellService` methods.
- */
- trait DistributedServicesSupport { this: Actor with ActorTracing ⇒
- val apiRouter = CommonActorSelections.apiRouterSelection(context)
- /**
- * This method sends the `request` using the ask pattern to the service on one of nodes with `role`.
- *
- * This method provides request tracing.
- *
- * @param role the role of the node that should process the request.
- * @param request the request message.
- * @param parentRequest parent request should be provided, if the `request` generated by other request, for example
- * by REST request.
- * @param t ask timeout
- * @param ec ask executionContext
- * @return future with response.
- */
- def askService(role: NodeRole.Value, request: InternalRequest, parentRequest: TracingSupport)
- (implicit t: Timeout, ec: ExecutionContext): Future[Any] = {
- val future = apiRouter ? request.asChildOf(parentRequest)
- future map {
- case msg =>
- trace.record(request, "Ask service response: " + msg)
- msg
- } recover {
- case e: ControlThrowable =>
- throw e
- case e: AskTimeoutException =>
- trace.record(request, StackTraceHelper.format(e))
- ErrorResponse.ServiceUnavailable
- case e: Throwable =>
- trace.record(request, StackTraceHelper.format(e))
- ErrorResponse.InternalServerError("error asking " + role + " for " + request)
- }
- }
- /**
- * This method sends the `request` using tell to the appropriate service on one of nodes with `role`.
- *
- * This method provides request tracing.
- *
- * @param role the role of the node that should process the request.
- * @param request the request message.
- * @param parentRequest parent request should be provided, if the `request` generated by other request, for example
- * by REST request.
- */
- def tellService(role: NodeRole.Value, request: InternalRequest, parentRequest: TracingSupport): Unit = {
- apiRouter ! request.asChildOf(parentRequest)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement