wdm
wdm copied to clipboard
Run when started
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?
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