module Daengine class DigitalAssetProcessor @@last_read_time = 2.days.ago def self.process_tuple_directory path = Daengine.config[:assets_path] raise "ERROR: DigitalAssetProcessor: Invalid assets_path provided. Unable to read from #{path}" unless File::directory?(path) # ensure only one instance running at a time lockfile_name = Daengine.config[:daengine_yml_file] + '.lck' begin lockfile = File.new(lockfile_name, File::CREAT|File::EXCL) # read the given directory, process each file in date order starting 2 days ago if last_read_time is nil time = self.read_last_process_time last_run_time = 10.years.ago if not time.blank? @@last_read_time = time last_run_time = time end Daengine.log("DigitalAssetProcessor: Last process time was #{@@last_read_time}", "info") deploy_files= [] deploy_files = Dir.entries(path).select { |f| File.file?("#{path}/#{f}") and File.mtime("#{path}/#{f}") > @@last_read_time }.sort_by { |f| File.mtime("#{path}/#{f}") } if deploy_files.empty? Daengine.log("DigitalAssetProcessor: No digital asset deployment files found to process under #{path}", "warn") else Daengine.log("DigitalAssetProcessor: Reading digital asset deployment files from #{path}", "info") deploy_files.each do |filename| begin #parse the file and add metadata content to database. file = File.expand_path(filename, path) Daengine.log("DigitalAssetProcessor: Processing file #{filename} --- #{File.mtime(file)}", "info") open_file = File.open(file, 'rb') Daengine::TeamsiteMetadataParser.parse_tuple_file(open_file, last_run_time) Daengine.log("DigitalAssetProcessor: Finished processing #{filename}", "info") @@last_read_time = File.mtime(file) + 1.second self.save_last_read_time Daengine.log("DigitalAssetProcessor: Last process time set to #{@@last_read_time}", "info") rescue Daengine.log("Unable to process file #{filename}, #{$!.message}", "error") end end rescue Errno::EEXIST Daengine.log("DigitalAssetProcessor: Already in progress. Lockfile exists at #{lockfile_name}. Aborting!", "error") ensure unless lockfile.nil? lockfile.close() File.delete(lockfile_name) end end end def self.save_last_read_time begin target = Daengine.config[:daengine_yml_file] content = "last_read_time: #{@@last_read_time}" File.open(target, "w+") do |f| f.write(content) end rescue Exception => e Daengine.log("ERROR: DigitalAssetProcessor: Failed to write to #{target}.", "error") end end def self.read_last_process_time time = 2.days.ago begin target = Daengine.config[:daengine_yml_file] property = YAML.load_file(target) time = property['last_read_time'] rescue Exception => e Daengine.log("ERROR: DigitalAssetProcessor: Failed to read from #{target} - #{e.message}", "error") end time end end end