Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
supervise
Browse files Browse the repository at this point in the history
  • Loading branch information
LiGuoBin committed May 13, 2020
1 parent 8ba81ab commit 9d3f36f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object FileDownloadActorMain extends LazyLogging with App {

lazy val system: ActorSystem[FileTask] = ActorSystem(FileDownloadActorMain(), "file-download-system-actor")

//主actor
//主actor,不做恢复
def apply(): Behavior[FileTask] =
Behaviors.setup { context =>
Behaviors.receiveMessage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.dreamylost.impl.actor

import akka.actor.typed.Behavior
import akka.actor.typed.{ Behavior, SupervisorStrategy }
import akka.actor.typed.scaladsl.Behaviors
import com.typesafe.scalalogging.LazyLogging
import io.github.dreamylost.Constants._
Expand Down Expand Up @@ -30,46 +30,49 @@ object FileDownloadAuditActor extends LazyLogging {

//计算速度和处理结果actor
def apply(): Behavior[Result] = {
Behaviors.receive { (context, message) =>
message match {
case FileDownloadActor.DownloadDoingResult(actorId, processPercent, _) =>
logger.info(s"actorId $actorId - $processPercent complete now")
case FileDownloadActor.DownloadResult(actorId, blockSize, startTime, endTime, msg, _) =>
printSpeed(blockSize, startTime, endTime, prefix = s"actorId $actorId - speed", suffix = msg.getOrElse(s"actorId $actorId finished"))
case FileDownloadActor.DownloadErrorResult(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, error, replyTo) =>
//简单重试
if (retryMap.containsKey(actorId)) {
var count = retryMap.get(actorId)
if (count < retryTimes) {
logger.warn(s"error, retry $count times")
replyTo ! DownloadTask(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, context.self)
count += 1
retryMap.put(actorId, count)
Behaviors.supervise[Result] {
Behaviors.receive { (context, message) =>
message match {
case FileDownloadActor.DownloadDoingResult(actorId, processPercent, _) =>
logger.info(s"actorId $actorId - $processPercent complete now")
case FileDownloadActor.DownloadResult(actorId, blockSize, startTime, endTime, msg, _) =>
printSpeed(blockSize, startTime, endTime, prefix = s"actorId $actorId - speed", suffix = msg.getOrElse(s"actorId $actorId finished"))
case FileDownloadActor.DownloadErrorResult(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, error, replyTo) =>
//简单重试
if (retryMap.containsKey(actorId)) {
var count = retryMap.get(actorId)
if (count < retryTimes) {
logger.warn(s"error, retry $count times")
replyTo ! DownloadTask(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, context.self)
count += 1
retryMap.put(actorId, count)
} else {
FileDownloadActorMain.system ! ShutdownSystem(Some(s"error, retry more than $retryTimes times: $error"))
}
} else {
FileDownloadActorMain.system ! ShutdownSystem(Some(s"error, retry more than $retryTimes times: $error"))
retryMap.put(actorId, 1)
replyTo ! DownloadTask(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, context.self)
}
} else {
retryMap.put(actorId, 1)
replyTo ! DownloadTask(actorId, taskBeginTime, fileTotalLength, startPos, endPos, url, context.self)
}
case FileDownloadActor.DownloadDoneResult(taskBeginTime, fileTotalLength, _) =>
val success =
"""
| .-') ('-. .-') .-')
| ( OO ). _( OO) ( OO ). ( OO ).
|(_)---\_) ,--. ,--. .-----. .-----. (,------.(_)---\_)(_)---\_)
|/ _ | | | | | ' .--./ ' .--./ | .---'/ _ | / _ |
|\ :` `. | | | .-') | |('-. | |('-. | | \ :` `. \ :` `.
| '..`''.) | |_|( OO )/_) |OO )/_) |OO )(| '--. '..`''.) '..`''.)
|.-._) \ | | | `-' /|| |`-'| || |`-'| | .--' .-._) \.-._) \
|\ /(' '-'(_.-'(_' '--'\(_' '--'\ | `---.\ /\ /
| `-----' `-----' `-----' `-----' `------' `-----' `-----'
|""".stripMargin
printSpeed(fileTotalLength, taskBeginTime, System.currentTimeMillis(), prefix = s"total speed: ", suffix = s"all task finished, download successfully\n $success")
clearTempFiles()
FileDownloadActorMain.system ! ShutdownSystem(None)
case FileDownloadActor.DownloadDoneResult(taskBeginTime, fileTotalLength, _) =>
val success =
"""
| .-') ('-. .-') .-')
| ( OO ). _( OO) ( OO ). ( OO ).
|(_)---\_) ,--. ,--. .-----. .-----. (,------.(_)---\_)(_)---\_)
|/ _ | | | | | ' .--./ ' .--./ | .---'/ _ | / _ |
|\ :` `. | | | .-') | |('-. | |('-. | | \ :` `. \ :` `.
| '..`''.) | |_|( OO )/_) |OO )/_) |OO )(| '--. '..`''.) '..`''.)
|.-._) \ | | | `-' /|| |`-'| || |`-'| | .--' .-._) \.-._) \
|\ /(' '-'(_.-'(_' '--'\(_' '--'\ | `---.\ /\ /
| `-----' `-----' `-----' `-----' `------' `-----' `-----'
|""".stripMargin
printSpeed(fileTotalLength, taskBeginTime, System.currentTimeMillis(), prefix = s"total speed: ", suffix = s"all task finished, download successfully\n $success")
clearTempFiles()
FileDownloadActorMain.system ! ShutdownSystem(None)
}
Behaviors.same
}
Behaviors.same
}
//不影响主要功能,忽略错误
}.onFailure[RuntimeException](SupervisorStrategy.resume)
}
}

0 comments on commit 9d3f36f

Please sign in to comment.