module Daengine class DigitalAssetProcessor @@last_read_time = 2.days.ago def self.trap_signals sigtrap = proc { puts "DigitalAssetProcessor: caught trapped signal, shutting down" @@run = false } signals = ["SIGTERM", "SIGINT"] signals.push("SIGHUP") unless is_windows? signals.each do |signal| trap signal, sigtrap end end def self.is_windows? processor, platform, *rest = RUBY_PLATFORM.split("-") platform =~ /mswin/ || platform =~ /mingw/ end def self.execute @@run = true trap_signals @@wthread = Thread.new { worker() } return @@wthread end def self.worker puts "DigitalAssetProcessor: start processing digital assets!" while @@run begin self.process_tuple_directory process_tuple_directory rescue => e puts e.message puts e.backtrace end sleep(5) end @@wthread.exit end 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) # 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 if not time.blank? @@last_read_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| #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) Daengine.log("DigitalAssetProcessor: Finished processing #{filename}", "info") @@last_read_time = File.mtime(file) self.save_last_read_time Daengine.log("DigitalAssetProcessor: Last process time set to #{@@last_read_time}", "info") 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("DigitalAssetProcessor: Failed to write to #{target}.", "warn") end end def self.read_last_process_time time = "" begin target = Daengine.config[:daengine_yml_file] property = YAML.load_file(target) time = property['last_read_time'] rescue Exception => e Daengine.log("DigitalAssetProcessor: Failed to read from #{target}", "warn") end time end end end