wdm icon indicating copy to clipboard operation
wdm copied to clipboard

Run when started

Open jmthomas opened this issue 10 years ago • 1 comments

My use case is to monitor a directory for files to process. There may or may not be files there already. Therefore I'd like to have WDM initially run the change block whether or not there are changes so I can process any files that are there initially. From that point on it would work as usual. Is this possible already or is this a feature request?

jmthomas avatar Nov 06 '14 23:11 jmthomas

After investigating further it didn't look like this was supported. So I achieved the same result by doing this:

Dir.chdir(@current_dir)
# Create a temp file to be deleted later which kicks off the first monitor
File.open("temp.txt",'w') {|file| file.puts "Temp" }

# Monitor directory using WDM
monitor = WDM::Monitor.new
monitor.watch(@current_dir, :files, :attributes) do |change|
  list = []
  Dir.glob('*').grep(@regexp).each do |file|
    list << file unless File.writable?(file) # only process readonly
  end
  process_files(list) unless list.empty?
end
worker = Thread.new { monitor.run! }
sleep 1
# Delete the temporary file to kick off the initial change
File.delete "temp.txt"
# Join on the monitor thread to allow it to continue processing
worker.join

jmthomas avatar Nov 07 '14 20:11 jmthomas