2

What I'm really trying to do is monitor multiple files and when any of them is modified I'd like to update some state and produce a side effect using this state. I imagine what I want is a scan over a Traversable that produces a Traversable[IO[_]]. But I don't see the path there.

as a minimal attempt to produce this I wrote

package example

import better.files.{File, FileMonitor}
import cats.implicits._
import com.monovore.decline._
import cats.effect.IO
import java.nio.file.{Files, Path}
import scala.concurrent.ExecutionContext.Implicits.global

object Hello extends CommandApp(
  name = "cats-effects-playground",
  header = "welcome",
  main = {
    val filesOpts = Opts.options[Path]("input", help = "input files") 
    filesOpts.map { files =>
      IO.async[File] { cb  =>
        val watchers = files.map { path =>
          new FileMonitor(path, recursive = false) {
            override def onModify(file: File, count: Int) = cb(Right(file))
          }
        }
        watchers.toList.foreach(_.start)
      }
        .flatMap(f => IO { println(f) })
        .unsafeRunSync
    }
  }
)

but this has two major flaws. One it creates a thread for each file I'm watching, which is a little heavy. But more importantly the program finishes as soon as a single file is modified, even though onModify would be called more times if the program stayed running.

I'm not married to using better-files, it just seemed like the path of least resistance. But I do require using Cats IO.

1 Answer 1

0

This solution doesn't solve the issue of creating a bunch of threads, and it doesn't strictly produce a Traversable, but it solves the underlying use case. I'm very open to this being critiqued and a better solution provided.

package example

import better.files.{File, FileMonitor}
import cats.implicits._
import com.monovore.decline._
import cats.effect.IO
import java.nio.file.{Files, Path}
import java.util.concurrent.LinkedBlockingQueue
import scala.concurrent.ExecutionContext.Implicits.global

object Hello extends CommandApp(
  name = "cats-effects-playground",
  header = "welcome",
  main = {
    val filesOpts = Opts.options[Path]("input", help = "input files")
    filesOpts.map { files =>
      val bq: LinkedBlockingQueue[IO[File]] = new LinkedBlockingQueue()
      val watchers = files.map { path =>
        new FileMonitor(path, recursive = false) {
          override def onModify(file: File, count: Int) = bq.put(IO(file))
        }
      }
      def ioLoop(): IO[Unit] = bq.take()
          .flatMap(f => IO(println(f)))
          .flatMap(_ => ioLoop())

      watchers.toList.foreach(_.start)
      ioLoop.unsafeRunSync
    }

  }
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.