-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1960 from GiganticMinecraft/forceRetrySave
Finalizerの処理に失敗した場合にリトライするようにする
- Loading branch information
Showing
6 changed files
with
100 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
src/main/scala/com/github/unchama/generic/effect/MonadThrowExtra.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.unchama.generic.effect | ||
|
||
import cats.MonadError | ||
import cats.effect.Sync | ||
|
||
object MonadThrowExtra { | ||
|
||
import cats.implicits._ | ||
|
||
def retryUntilSucceeds[F[_]: Sync, A](fa: F[A])(limit: Int): F[A] = { | ||
require(limit >= 1) | ||
def go(currentIterationCount: Int, occurredException: Option[Throwable]): F[A] = { | ||
if (currentIterationCount <= limit) { | ||
fa.attempt.flatMap { | ||
case Right(a) => | ||
Sync[F].delay(occurredException.foreach(_.printStackTrace())).as(a) | ||
case Left(error) => | ||
go(currentIterationCount + 1, Some(error)) | ||
} | ||
} else { | ||
// このelse節に入っている時点で1度は失敗しているので、`occurredExceptions`が`None`であることはありえない。 | ||
case object LimitReached | ||
extends Exception(s"Limit $limit reached!", occurredException.last) | ||
|
||
MonadError[F, Throwable].raiseError(LimitReached) | ||
} | ||
} | ||
|
||
go(0, None) | ||
} | ||
|
||
} |
This file contains 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