Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poll a file for change?

Tags:

clojure

It is often a pattern that I wish to poll a file for changes (when it was last written). When the file does change from its previous value, I wish to execute some function. Something of the form.

(poll-for-changes file-str on-change-fx current-value)

where

  1. file-str is just a string that specifies the files location
  2. on-change-fx is the function that should be called when the file at file-str changes. Let us say that the on-change-fx should take the File object pointing to file-str as a argument.
  3. current-value the current value of the file in milliseconds. You might set to 0 to guarantee that this function will run at least once, or to the actual value to only run this function when you actually detect a change.

I would just like this function implemented in the clearest, most concise, Clojurist way possible. Thank you.

like image 235
Stephen Cagle Avatar asked Oct 18 '12 01:10

Stephen Cagle


People also ask

Can I poll for changes to my team using the API?

Team-linked applications using the Business API are able to poll for a variety of changes to the team - including changes to team content, membership, sharing, and more.

How does a polling trigger acquire new data?

The following example provides a basic overview of how a polling trigger acquires new data. The flow runtime invokes an initial call on the trigger to the API in the connector. The connector will then call the backend service. The backend service then returns all of the current data back to the connector.

How do I implement a file change notification?

To implement this functionality, called file change notification, a program must be able to detect what is happening to the relevant directory on the file system. One way to do so is to poll the file system looking for changes, but this approach is inefficient.

Why does my Dropbox app want to poll for changes?

As you build your Dropbox application, it’s important to remember that users may edit, move, and delete content at any time on Dropbox. As a result, your app may wish to poll for or be notified of these changes as they occur.


1 Answers

If you're looking to poll a directory or files and act on it, I think watchtower is pretty good to look at.

Java 7 has a WatchService, which uses file system events to react to changes. In this case, you don't poll at all, but block on a future file event. I don't think there are any projects in Clojure that are out there leveraging that, although I spent some time toying with it to write a small library. The source for it is here

I don't claim my library is even complete, but it does use the Java 7 service, so you could use that for inspiration on your own project.

like image 179
Nick Klauer Avatar answered Sep 18 '22 12:09

Nick Klauer