Created
January 27, 2022 02:23
-
-
Save taketora26/e10a42b25c6c1430202db303dff0fe7f to your computer and use it in GitHub Desktop.
FollowerActor.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample | |
import akka.actor.typed.{ActorRef, Behavior} | |
import akka.actor.typed.scaladsl.Behaviors | |
import sample.LeaderActor.{Failed, Finish} | |
import scala.concurrent.Future | |
import scala.util.{Failure, Success} | |
object FollowerActor { | |
sealed trait Command | |
case class ExecTask(taskId: String, replyTo: ActorRef[LeaderActor.Command]) | |
extends Command | |
sealed trait ExecResult { | |
val taskId: String | |
} | |
case class ExecSuccess(taskId: String) extends ExecResult | |
case class ExecFailure(taskId: String, reason: String) extends ExecResult | |
case class WrappedExecResult(execResult: ExecResult, | |
reply: ActorRef[LeaderActor.Command]) | |
extends Command | |
def apply(operation: FollowerOperation): Behavior[Command] = | |
Behaviors.receive[Command] { (context, message) => | |
message match { | |
case ExecTask(taskId, replyTo) => | |
context.log.info(s"receive ExecTask. start task taskId: ${taskId}") | |
context.pipeToSelf(operation.exec(taskId)) { | |
case Success(_) => WrappedExecResult(ExecSuccess(taskId), replyTo) | |
case Failure(e) => | |
WrappedExecResult(ExecFailure(taskId, e.getMessage), replyTo) | |
} | |
Behaviors.same | |
case WrappedExecResult(result, replyTo) => | |
result match { | |
case ExecSuccess(taskId) => replyTo ! Finish(taskId) | |
case ExecFailure(taskId, reason) => replyTo ! Failed(taskId, reason) | |
} | |
Behaviors.same | |
} | |
} | |
} | |
trait FollowerOperation { | |
def exec(taskId: String): Future[Unit] | |
} | |
class FollowerOperationImpl extends FollowerOperation { | |
// ダミーの実装です | |
def exec(taskId: String): Future[Unit] = Future.successful(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment