Thanks to visit codestin.com
Credit goes to github.com

Skip to content

allow effectBlockingCancelable to take a environment/clock for the cancel function #5714

@domdorn

Description

@domdorn

I have the following code, where I have to repeat the cancel operation because for some reason the underlying code (which is invoked via JNI) doesn't properly kill the process, when I just do it once.

    def runJob(job: GAMSJob, opt: GAMSOptions): ZIO[Blocking with Clock with Has[GamsPathSettings], Throwable, Unit] =
      {
        import zio.duration._
        for {
          p <- ZIO.environment[Has[GamsPathSettings]]
          _ <- ZIO.effectTotal(p.get.licenseFilePath.foreach(v => opt.setLicense(v)))
          _ <- zio.blocking
            .effectBlockingCancelable(job.run(opt))((
              ZIO.debug("trying to interrupt the GAMS process") *> ZIO
                .effect(job.interrupt())).repeat(Schedule.recurs(20) && Schedule.spaced(4.seconds)).ignore) // <-- this complains that the cancel function needs
            .catchAll(t => ZIO.fail(GamsErrorHandler.handleError(t)))
        } yield ()
      }

at the moment, I have refactored this to manually provide the clock to the cancel effect. However, this seems kind of wrong and unintuitive.

    def runJob(job: GAMSJob, opt: GAMSOptions): ZIO[Blocking with Clock with Has[GamsPathSettings], Throwable, Unit] =
      {
        import zio.duration._
        for {
          p <- ZIO.environment[Has[GamsPathSettings]]
          _ <- ZIO.effectTotal(p.get.licenseFilePath.foreach(v => opt.setLicense(v)))
          clock <- ZIO.environment[Clock]
          _ <- zio.blocking
            .effectBlockingCancelable(job.run(opt))((
              ZIO.debug("trying to interrupt the GAMS process") *> ZIO
                .effect(job.interrupt())).repeat(Schedule.recurs(20) && Schedule.spaced(4.seconds)).provide(clock).ignore)
            .catchAll(t => ZIO.fail(GamsErrorHandler.handleError(t)))
        } yield ()
      }

should the cancel operation be really a UIO?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions