Code Monkey home page Code Monkey logo

Comments (3)

gmethvin avatar gmethvin commented on August 15, 2024

Thanks for the report @J-N-K. I'm guessing that's actually because when you call watch() it still needs to traverse the directory to get initial hashes of all the files before it can start watching, and there's no way to know when it finishes the initialization phase and starts the actual listening phase.

You may be able to use the watchAsync method instead, which actually does the initialization synchronously and returns a future as soon as the watcher starts watching. I think something like this would be roughly equivalent to the code you have:

dirWatcher = DirectoryWatcher.builder().listener(this).path(basePath).build();
executor = Executors.newSingleThreadExecutor();
dirWatcher.watchAsync(executor);

then you should be able to shut it down with:

dirWatcher.close();
executor.shutdown();

Of course that has the downside of having to block during the initialization phase, but you could wrap that in another future perhaps and provide some async method of seeing when it starts watching. You might also consider disabling hashing, which makes this initialization phase much faster, though it increases the likelihood of duplicate events.

Nevertheless I think it's an issue that watch() provides no way of knowing when the initialization phase ends and the actual watching starts. We'd have to think about the best way to communicate this. Perhaps it'd make sense to provide a signal to the listener in that case. Let me know if you have any ideas on the best approach here.

from directory-watcher.

J-N-K avatar J-N-K commented on August 15, 2024

I have refactored a bit to use .watchAsync (which was not so easy, because we can't block in service activation) and it seems to avoid these issues. Unfortunately I don't have good idea how to signal that.

from directory-watcher.

gmethvin avatar gmethvin commented on August 15, 2024

I can't think of any way around this problem if you want hashing. I guess we could start watching while we're doing the initial hashing of the files, then just emit the events as-is if we haven't hashed the file yet. Does your use case require the hashing functionality?

from directory-watcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.