From 9ed3965be8213054fa8edafb2e4e65bdeeb9ba74 Mon Sep 17 00:00:00 2001 From: Alexey Rykhalskiy Date: Tue, 27 Aug 2024 15:59:22 +0300 Subject: [PATCH] retry added --- ce3/src/main/scala/_sandbox/Sandbox.scala | 34 +++++++++++++++++ ce3/src/main/scala/el_meter/Sandbox.scala | 45 ++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/ce3/src/main/scala/_sandbox/Sandbox.scala b/ce3/src/main/scala/_sandbox/Sandbox.scala index 60a2acda..c97d0956 100644 --- a/ce3/src/main/scala/_sandbox/Sandbox.scala +++ b/ce3/src/main/scala/_sandbox/Sandbox.scala @@ -4,6 +4,10 @@ import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration.DurationInt +import scala.concurrent.{Await, Future} + object Sandbox { def id[A](a: A): A = a @@ -15,6 +19,36 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck import Sandbox._ + test("123") { + case class Person(age: Int, name: String) + def getAge = Future(123) + def getName = Future("Jim") + + val fp: Future[Person] = + getName.flatMap(n => + getAge.map(a => + Person(a, n) + ) + ) + val p: Person = Await.result(fp, 10.seconds) + + + + + } + + test("11") { + pprint.log(math.acos(0.96)) + pprint.log(math.sin(math.acos(0.96))) + pprint.log(math.acos(1)) + pprint.log(math.sin(math.acos(1))) + pprint.log(math.acos(0.95)) + pprint.log(math.sin(math.acos(0.95))) + pprint.log(math.acos(0.65)) + pprint.log(math.sin(math.acos(0.65))) + } + + test("0") { def isScala212a(v: Option[(Long, Long)]): Boolean = v.exists(_._1 == 2) && v.exists(_._2 == 12) diff --git a/ce3/src/main/scala/el_meter/Sandbox.scala b/ce3/src/main/scala/el_meter/Sandbox.scala index c42a6962..5e97d9f4 100644 --- a/ce3/src/main/scala/el_meter/Sandbox.scala +++ b/ce3/src/main/scala/el_meter/Sandbox.scala @@ -4,9 +4,12 @@ import cats.effect.IO import cats.effect.unsafe.implicits.global import cats.implicits._ import io.scalaland.chimney.Transformer + import java.time.Instant import java.time.LocalDateTime import java.time.ZoneOffset +import java.time.format.DateTimeFormatter +import javax.swing.text.DateFormatter import org.http4s.Method import org.http4s.Request import org.http4s.Status.BadRequest @@ -19,6 +22,10 @@ import org.http4s.implicits.http4sLiteralsSyntax import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks +import retry.{RetryDetails, RetryPolicy} +import retry.implicits.retrySyntaxError +import retryideas.RetryApp.x + import scala.concurrent.duration.DurationInt object Http { @@ -48,6 +55,25 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck import model._ // import io.scalaland.chimney.dsl._ + def leftPad(x: Int, width: Int): String = s"%${width}d".formatted(x) + def leftPad(x: Double, width: Int): String = leftPad(x.toInt, width) + + test("leftPad") { + val x = leftPad(1.123, 6) + pprint.log(x) + } + + test("date formatted") { + val ldt = LocalDateTime.parse("2024-08-25T13:52") + val rep = ldt.format(DateTimeFormatter.ISO_DATE_TIME) + pprint.log(rep) + } + + test("pprint") { + pprint + .log("Hello") + } + test("round") { pprint.log(round1(1.23456)) } @@ -72,9 +98,26 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck } test("streamed") { + + val policy: RetryPolicy[IO] = { + import retry.RetryPolicies._ + // 6 retries starting from 1 gives us +1 +2 +4 +8 +16 +32 = 63 sec ~= 1 min + val growing: RetryPolicy[IO] = limitRetries[IO](5) join exponentialBackoff[IO](1.second) + // constant never terminating retry + val constant: RetryPolicy[IO] = constantDelay[IO](10.seconds) + + growing followedBy constant + } + + val onError = (t: Throwable, d: RetryDetails) => + IO { println(LocalDateTime.now -> "device inaccessible retrying...") } + + val getDataWithRetry = getData + .retryingOnAllErrors(policy, onError) + fs2.Stream .awakeEvery[IO](5000.millis) - .evalMap(_ => getData) + .evalMap(_ => getDataWithRetry) .unNone .map(_.into[DataLine].transform) .map(_.into[DataLineWattOnlyDetailed].transform)