0

I am new to scala and have been trying to implement the directory watcher using https://index.scala-lang.org/gmethvin/directory-watcher/directory-watcher-better-files/0.13.0 library, but I am facing watching a directory recursively. I am running in Windows system.

Code

import better.files._
import io.methvin.better.files._
import scala.concurrent.ExecutionContext.Implicits.global

val myDir = File("/directory/to/watch/")
val watcher = new RecursiveFileMonitor(myDir) {
  override def onCreate(file: File, count: Int) = println(s"$file got created")
  override def onModify(file: File, count: Int) = println(s"$file got modified $count times")
  override def onDelete(file: File, count: Int) = println(s"$file got deleted")
}

//process is not blocked here - END is printing
watcher.start()

println("END")

Problem

The process is not getting blocked at watcher.start() not sure what I am missing, as per the library spec, it is specified that for Windows use ExtendedWatchEventModifier.FILE_TREE.

How to use this ExtendedWatchEventModifier.FILE_TREE parameter??

am I missing something in the above code??

Findings

  1. I tried to run the start() method in separate thread but no luck.

  2. I debugged and found that start() method is internally calling DirectoryWatcher.watch() but in a different ExecutionContext, I am not able to debug that watch() method, basically the debug point is not going to DirectoryWatcher. Not sure how it works.

Please let me know what am I am missing? Please let me know if more details are required.

  • Everything is acting as expected, as you can see, watch.start requires an implicit execution context, which means kind of like an ExecutorService in java, which defines how tasks should be processed in multiple threads, so if they are run in separate threads, they do not block the main thread, that's the basic concept of threads. – AminMal Apr 20 '22 at 12:47
  • ok, but if I want to block that thread / wait on a future object for that implicit execution context which **watch.start** uses, how should I go about, basically I want to watch a directory continuously. – Vijaykumar Arumugam Apr 20 '22 at 12:52
  • It's not recommended at all, but I understand that sometimes you might need to block concurrent process, so maybe watcher.start().onComplete(_ => global.shutDownNow()) would help, you can also use Await. (Again, It's not recommended at all) – AminMal Apr 20 '22 at 13:00
  • unfortunately, no it did not work there is no method like onComplete in watcher.start().... – Vijaykumar Arumugam Apr 20 '22 at 14:08
  • Oh so it doesn't return a `Future` instance, since onComplete is a method of future, anyway, you can block your main thread using various solutions, my suggestion is to expect some signal from your console (using a scanner which scans an input stream like your console), so before `println("END")`, do this: `new java.util.Scanner(System.in).nextLine()`. this will wait for a string from terminal, so you can exit your application writing anything into your console. – AminMal Apr 20 '22 at 17:35

0 Answers0