Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 16:08
    adamgfraser commented #2463
  • 15:53
    adamgfraser review_requested #2464
  • 15:39
    adamgfraser synchronize #2464
  • 15:27
    adamgfraser synchronize #2464
  • 15:16
    adamgfraser synchronize #2464
  • 15:07
    adamgfraser synchronize #2464
  • 14:43
    adamgfraser opened #2464
  • 14:13
    saraiva132 edited #2463
  • 14:10
    saraiva132 opened #2463
  • 13:02
    regiskuckaertz commented #2453
  • 12:53
    vasilmkd commented #2461
  • 12:52
    saraiva132 commented #2461
  • 12:48
    saraiva132 synchronize #2461
  • 12:00
    mijicd closed #2368
  • 12:00
    mijicd commented #2368
  • 08:49
    saraiva132 edited #2453
  • 00:35

    ghostdogpr on master

    Added my 2 Blogs the Blog Artic… (compare)

  • 00:35
    ghostdogpr closed #2460
  • Dec 10 23:46
    saraiva132 commented #2462
  • Dec 10 23:21
    adamgfraser commented #2462
Yuriy Badalyantc
@LMnet
def acquire: URIO[Clock, String] = {
  for {
    _ <- UIO(logger.info("Resource acquiring..."))
    _ <- ZIO.sleep(zio.duration.Duration.fromScala(5.seconds))
    res <- UIO {
      logger.info("Resource acquired")
      "some resource"
    }
  } yield res
}

def release(resource: String): UIO[Unit] = {
  UIO(logger.info(s"Releasing $resource"))
}

val action: RIO[Clock, Unit] = acquire.bracket(release, { r =>
  Task(logger.info(s"Using $r"))
})

val timedOut: RIO[Clock, Option[Unit]] = action.timeoutTo(None)(Some(_))(zio.duration.Duration.fromScala(100.millis))

val result: RIO[Clock, Unit] = timedOut
  .flatMap[Clock, Throwable, Unit] {
    case Some(value) => UIO.succeed(value)
    case None => UIO(logger.info("Resource acquiring timed out"))
  }

UIO(logger.info("Started")).flatMap { _ =>
  result.either.map { _ =>
    0
  }
}

Hi everybody! With the code above I see this in my console:

21:32:17.543 Started
21:32:17.741 Resource acquiring...
21:32:22.839 Resource acquired
21:32:22.874 Releasing some resource
21:32:22.896 Resource acquiring timed out

My timeout is only 100 millis. Why I'm still waiting for 5 seconds before timeout?

Matthias Langer
@mlangc
@LMnet I guess it's because what is in acquire is marked as uninterruptible by default in bracket (this is for your own safety to avoid unintended resource leaks). Try ZIO.sleep(1.second).interruptible instead in acquire if you really want the sleep to be interruptible.
Note that you don't need to convert to Scala durations for convenient syntax - just import zio.duration.durationInt or zio.duration._
Yuriy Badalyantc
@LMnet
@mlangc wow, that works, thanks!
But in that case my release function will not be called in case of error?
I'm trying to simulate situation when I have a long running resource acquisition and I want to have a time out for this, but with the bracket functionality at the same time: if resource acqusition timed out, I want to close it when it eventually be ready.
Matthias Langer
@mlangc
@LMnet Take a look at bracketExit: It should allow exactly that if I'm not mistaken
Yuriy Badalyantc
@LMnet
Scaladoc looks promising at least :)
Yuriy Badalyantc
@LMnet
Well, bracketExit didn't help. Looks like I can have a nice timeouts or nice resource handling, but not both :(
Matthias Langer
@mlangc
I think the reasoning behind bracket is that there is nothing to release if aquire failed. If this is not true in your case, you should split your acquire into two parts and/or look into ZIO.ensuring
Yuriy Badalyantc
@LMnet
@mlangc How ZIO.ensuring could help me? It looks like a helper for if
Matthias Langer
@mlangc
@LMnet ensuring is the ZIO equivalent to try finally
Or maybe ZIO.onError...
Yuriy Badalyantc
@LMnet
Oh, ok. Ensuring looks good
Looks like I can create my own operation on top of it
Matthias Langer
@mlangc
ZIO.sleep(10.seconds)
  .onError(_ => logger.errorIO(s"Cleaning up after error"))
  .bracket_(logger.infoIO("Cleaning up after normal exit")
might work too
^^ @LMnet
Yuriy Badalyantc
@LMnet
Thanks! I need to play with this a bit
Matthias Langer
@mlangc
Don't forget to look into zio.Managed - it's at a higher abstraction level than bracket and should usually be preferred in my opinion
You're welcome :-)
sken
@sken77
Hi, not sure if this is the place to ask. but you guys know of companies that do scala fp or better yet, zio in the sillicon valley area?
matfournier
@matfournier
Can anyone point me to a good blog post (or video, or github repo project to glance through) on leveraging the environment parameter to constrain my context?
I'm perhaps using the wrong search terms, but my google-fu is coming up without much here!
Piotr Gawryś
@Avasil
@matfournier There are some blog posts on John's blog, e.g. http://degoes.net/articles/zio-environment but it mostly compares to Tagless Final. I would be personally interested in comparison to plain traits/classes with fixed IO
John A. De Goes
@jdegoes
@sken77 Credit Karma, Netflix, & surely others.
Btw everyone has moved to Discord!!!
@matfournier Take a look at zio.dev resources for learning more, there are a number of blog posts on ZIO environment.
sken
@sken77
thanks whats the discord link?
oh i see its on the title nvm
matfournier
@matfournier
Thanks!
@ugobourdon
@ubourdon
Hi all. How can we compose to ZIO with different Env ?
I mean i have ZIO[Console, ..., ...) and ZIO[Any, ..., ...] and i dont find how to compose them in a for {} yield () for exemple ....
@ugobourdon
@ubourdon
Other question :D
How can i flatten a List[ZIO[Console, Nothing, Unit]] ? I find reduceAll but i can't find how to use it properly. Each ZIO is some putStrLn, and i just want to play them sequentially
Pierre Ricadat
@ghostdogpr
@ugobourdon everyone migrated to discord, you’ll probably have a response there
You can use ZIO with different environments in a for comprehension
And I think you’re looking for ZIO.collectAll
Stanislav Palatnik
@stanpalatnik
Hey guys. Trying to move some code from akka streams to zio streams. Trying to fetch unbounded paginated results from a db. I think Stream.repeatEffect.takeWhile is the best approach?
the akka code used unfolding instead, but i wonder if it makes sense to just have a data structure that just keeps an internal counter for the next page?
private def asyncPageToPageSource[T](transactionSearchRequest: TransactionSearchRequest, pageFunc: TransactionSearchRequest => Future[Seq[T]]): Source[Seq[T], NotUsed] = { Source.unfoldAsync(transactionSearchRequest) { page => val pageData = pageFunc(page) pageData.map(data => if (data.isEmpty) None else Some(page.next, data) ) } }
for reference to the old approach
Stanislav Palatnik
@stanpalatnik
actually now that I type it out, unfold sounds more FP
anyone got a basic example for that?
Adam Fraser
@adamgfraser
@stanpalatnik I would also take a look at ZStream#paginate for that.
If you're up for it come on over to Discord. There are a lot more folks over there.
Guo Liang Oon
@sarkologist
hi guys, how do I retry for some E in ZIO[R,E,A] but give up on other E?

effect.retry(schedule.whileInput(e => ...))

whoops, found it! awesome!

Adam Fraser
@adamgfraser
:thumbsup:
Guo Liang Oon
@sarkologist

final def retry[R1 <: R, E1 >: E, S](policy: Schedule[R1, E1, S])(implicit ev: CanFail[E]): ZIO[R1, E, A] = retryOrElse(policy, (e: E, _: S) => ZIO.fail(e))

type inference is not available for the orElse param because it is not part of a different parameter list of retryOrElse?

Akhtiam Sakaev
@asakaev
What is the good way to deal with Managed release that can fail?