# Ruby fs watcher?

**URL:** <https://rubytalk.org/t/ruby-fs-watcher/32360>\
**Category:** ruby-talk\
**Created:** [26 October 2006 18:10 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360 "2006-10-26T18:10:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Etienne](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Jean-Etienne](https://rubytalk.org/u/Jean-Etienne)\
**Post date:** [26 October 2006 18:10 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/1 "2006-10-26T18:10:42Z")

</div>

Hi,

I wrote a script processing some files in a directory: each time there  
is a new file in a given directory, I do something. So, my problem here  
is to know when a new file arrived in the dir.  
What I do now is to try to open the file in exclusive mode and do not  
process the file if it fails, but polling just kills the cpu.

I am wondering if somebody could explain me an elegant notification  
solution?

Thank you,  
Jean-Etienne

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [26 October 2006 18:14 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/2 "2006-10-26T18:14:55Z")

</div>

This is fairly operating-system specific. Which one are you using?

> **···**
>
> On 10/26/06, Jean-Etienne Durand \<etienne.durand@woa.hu\> wrote:
> 
> > Hi,
> > 
> > I wrote a script processing some files in a directory: each time there  
> > is a new file in a given directory, I do something. So, my problem here  
> > is to know when a new file arrived in the dir.  
> > What I do now is to try to open the file in exclusive mode and do not  
> > process the file if it fails, but polling just kills the cpu.
> > 
> > I am wondering if somebody could explain me an elegant notification  
> > solution?

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [26 October 2006 18:20 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/3 "2006-10-26T18:20:05Z")

</div>

Take a look at Ara's dirwatch solution. Does exactly what you want.

[http://raa.ruby-lang.org/project/dirwatch/](http://raa.ruby-lang.org/project/dirwatch/)

Blessings,  
TwP

> **···**
>
> On 10/26/06, Jean-Etienne Durand \<etienne.durand@woa.hu\> wrote:
> 
> > Hi,
> > 
> > I wrote a script processing some files in a directory: each time there  
> > is a new file in a given directory, I do something. So, my problem here  
> > is to know when a new file arrived in the dir.  
> > What I do now is to try to open the file in exclusive mode and do not  
> > process the file if it fails, but polling just kills the cpu.
> > 
> > I am wondering if somebody could explain me an elegant notification  
> > solution?

---

<div class="post-metadata">

**Author:** ![Thomas\_Adam](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@Thomas\_Adam](https://rubytalk.org/u/Thomas_Adam)\
**Post date:** [26 October 2006 18:22 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/4 "2006-10-26T18:22:23Z")

</div>

If you're using Linux, you can use dnotify (and the newer 'anotify'  
where applicable). I have no idea how Windows would handle such a  
thing, if at all.

-- Thomas Adam

> **···**
>
> On Fri, Oct 27, 2006 at 03:10:42AM +0900, Jean-Etienne Durand wrote:
> 
> > Hi,
> > 
> > I wrote a script processing some files in a directory: each time there  
> > is a new file in a given directory, I do something. So, my problem here  
> > is to know when a new file arrived in the dir.  
> > What I do now is to try to open the file in exclusive mode and do not  
> > process the file if it fails, but polling just kills the cpu.
> > 
> > I am wondering if somebody could explain me an elegant notification  
> > solution?
> 
> --  
> "Wanting to feel; to know what is real. Living is a lie." -- Purpoise  
> Song, by The Monkees.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [26 October 2006 18:44 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/5 "2006-10-26T18:44:20Z")

</div>

[http://codeforpeople.com/lib/ruby/dirwatch/](http://codeforpeople.com/lib/ruby/dirwatch/)  
[http://codeforpeople.com/lib/ruby/dirwatch/dirwatch-0.9.0/README](http://codeforpeople.com/lib/ruby/dirwatch/dirwatch-0.9.0/README)

-a

> **···**
>
> On Fri, 27 Oct 2006, Jean-Etienne Durand wrote:
> 
> > Hi,
> > 
> > I wrote a script processing some files in a directory: each time there  
> > is a new file in a given directory, I do something. So, my problem here  
> > is to know when a new file arrived in the dir.  
> > What I do now is to try to open the file in exclusive mode and do not  
> > process the file if it fails, but polling just kills the cpu.
> > 
> > I am wondering if somebody could explain me an elegant notification  
> > solution?
> > 
> > Thank you,  
> > Jean-Etienne
> 
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [26 October 2006 18:50 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/6 "2006-10-26T18:50:23Z")

</div>

Jean-Etienne Durand wrote:

> Hi,
> 
> I wrote a script processing some files in a directory: each time there  
> is a new file in a given directory, I do something. So, my problem here  
> is to know when a new file arrived in the dir.  
> What I do now is to try to open the file in exclusive mode and do not  
> process the file if it fails, but polling just kills the cpu.

This problem is easily solved, and in a portable way. You create a list of  
the files and their modification times, then sleep for some interval, then  
wake up and compare the stored modification times with the new ones, also  
test for any new files. Take action on any new or modified files. Maybe 25  
lines of Ruby code.

Be sure to sleep for an interval between tests, otherwise your script will  
hog the processor.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Sam\_Smoot](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Sam\_Smoot](https://rubytalk.org/u/Sam_Smoot)\
**Post date:** [26 October 2006 18:30 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/7 "2006-10-26T18:30:14Z")

</div>

Thomas Adam wrote:

> **···**
>
> > On Fri, Oct 27, 2006 at 03:10:42AM +0900, Jean-Etienne Durand wrote:  
> > \> Hi,  
> > \>  
> > \> I wrote a script processing some files in a directory: each time there  
> > \> is a new file in a given directory, I do something. So, my problem here  
> > \> is to know when a new file arrived in the dir.  
> > \> What I do now is to try to open the file in exclusive mode and do not  
> > \> process the file if it fails, but polling just kills the cpu.  
> > \>  
> > \> I am wondering if somebody could explain me an elegant notification  
> > \> solution?
> > 
> > If you're using Linux, you can use dnotify (and the newer 'anotify'  
> > where applicable). I have no idea how Windows would handle such a  
> > thing, if at all.
> > 
> > -- Thomas Adam
> > 
> > --  
> > "Wanting to feel; to know what is real. Living is a lie." -- Purpoise  
> > Song, by The Monkees.
> 
> You'd used WMI in Windows, potentially through the WIN32OLE library, to  
> monitor for file events. The FileSystemWatcher class in .NET is just a  
> wrapper around this functionality.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [26 October 2006 19:10 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/8 "2006-10-26T19:10:25Z")

</div>

> This problem is easily solved, and in a portable way. You create a list of  
> the files and their modification times, then sleep for some interval, then  
> wake up and compare the stored modification times with the new ones, also  
> test for any new files. Take action on any new or modified files. Maybe 25  
> lines of Ruby code.

you'd think - until your script stops, restarts, and you re-fire actions for  
all previous actions. if your action happens to have been something like

&nbsp;&nbsp;&nbsp;system "something\_which\_should\_only\_happen\_for\_new\_files #{ file }"

you're screwed. that approach is simply not that much more durable that  
cron'ing a script to process every file every minute since, logically the  
system can degrade to that.

i think a transactional db is an absolute requirement of such a system.

another, absoulute must, for such a system, is the ability to deal with  
batches up updated files. the reason is that this:

&nbsp;&nbsp;&nbsp;while(true)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get\_new\_files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;process\_new\_files  
&nbsp;&nbsp;&nbsp;end

is terrifically flawed of 100,000 new files arrive at once - since it requires  
you to spawn 100,000 new processes. ideally the processing can be batched in  
chunks. dirwatch allows this by providing a config option to pass all  
files to be processed to the script on stdin.

> Be sure to sleep for an interval between tests, otherwise your script will  
> hog the processor.

also a flaw. if you simply sleep, say 200s, between loops you waste time when  
the actions you just took required more than that time. basically you want to  
ensure at least n seconds elapses between scans of the directory, but if the  
system is very busy you will not need to sleep since simply processing may  
require this amount of time.

in summary, having written three or four such systems and ultimately arriving  
at the code for dirwatch, which we've used in production for 24x7 satellite  
ingest systems for several year, i can assure you the task isn't quite that  
trivial.

regards.

-a

> **···**
>
> On Fri, 27 Oct 2006, Paul Lutus wrote:  
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 October 2006 19:25 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/9 "2006-10-26T19:25:16Z")

</div>

Paul Lutus wrote:

> This problem is easily solved, and in a portable way. You create a list of  
> the files and their modification times, then sleep for some interval, then  
> wake up and compare the stored modification times with the new ones, also  
> test for any new files. Take action on any new or modified files. Maybe 25  
> lines of Ruby code.
> 
> Be sure to sleep for an interval between tests, otherwise your script will  
> hog the processor.

You rather need the mtime of the directory and only that - at least if you are interested in /new/ files only:

require 'set'

last = nil  
set = Set.new

loop do  
&nbsp;&nbsp;&nbsp;current = File.mtime "."  
&nbsp;&nbsp;&nbsp;if last.nil? || last \< current  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = Dir["\*"].to\_set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p s - set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set = s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last = current  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;sleep 1  
end

Test run:

$ !ru  
ruby /cygdrive/c/Temp/watch.rb &  
[1] 1432

robert@fussel ~  
$ #\<Set: {"xx", "x", "bin", "a.1234"}\>  
touch foo

robert@fussel ~  
$ #\<Set: {"foo"}\>  
touch bar

robert@fussel ~  
$ #\<Set: {"bar"}\>  
touch bar

robert@fussel ~  
$ touch baz

robert@fussel ~  
$ #\<Set: {"baz"}\>

Kind regards

&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [26 October 2006 19:30 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/10 "2006-10-26T19:30:17Z")

</div>

/ ...

> in summary, having written three or four such systems and ultimately  
> arriving at the code for dirwatch, which we've used in production for 24x7  
> satellite ingest systems for several year, i can assure you the task isn't  
> quite that trivial.

Yes, but the OP wants to know how to do it, not produce a mature, robust  
version. He may want to learn the programming aspects on his own, make his  
own mistakes. From the content of his post, he didn't bother to yield any  
time during execution, therefore his script ate up the CPU and consequently  
failed.

At that level, a simple solution really is simple.

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [26 October 2006 19:40 UTC](https://rubytalk.org/t/ruby-fs-watcher/32360/11 "2006-10-26T19:40:03Z")

</div>

i guess you're right. i get defensive at the mere suggestion of simply event  
or cron based processing systems without mutual exclusion - i've debugged way  
too many of them!

cheers.

-a

> **···**
>
> On Fri, 27 Oct 2006, Paul Lutus wrote:
> 
> > ara.t.howard@noaa.gov wrote:
> > 
> > / ...
> > 
> > > in summary, having written three or four such systems and ultimately  
> > > arriving at the code for dirwatch, which we've used in production for 24x7  
> > > satellite ingest systems for several year, i can assure you the task isn't  
> > > quite that trivial.
> > 
> > Yes, but the OP wants to know how to do it, not produce a mature, robust  
> > version. He may want to learn the programming aspects on his own, make his  
> > own mistakes. From the content of his post, he didn't bother to yield any  
> > time during execution, therefore his script ate up the CPU and consequently  
> > failed.
> > 
> > At that level, a simple solution really is simple.
> 
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama
