class Envoi::Mam::Cantemo::Agent::WatchFolderHandler

Constants

AWF
LWF

Attributes

agent[RW]
config[RW]
logger[RW]
watch_folders[RW]

Public Class Methods

new(args = { }) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 14
def initialize(args = { })
  initialize_logger(args)

  @agent = Envoi::Mam::Cantemo::Agent.load_from_config_file(args)
  @config = agent.config
  cantemo_config = config[:cantemo] || config['cantemo']
  watch_folder_defs = cantemo_config[:watch_folders] || cantemo_config['watch_folders']

  @ignored_file_paths_by_watch_folder = Hash.new { |h, k| h[k] = [] }

  @watch_folders = AWF.process_watch_folder_defs(watch_folder_defs)
end
run(args) click to toggle source

Initialize then run

# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 99
def self.run(args)
  w = self.new(args)
  w.run
end
run_as_daemon(args) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 104
def self.run_as_daemon(args)
  # ARGV.unshift 'run' unless %w(start stop restart run zap killall status).include? ARGV.first
  require 'daemons'
  Daemons.run_proc('cantemo-portal-watch-folders') { self.run(args) }
end

Public Instance Methods

add_to_ignore(wf, file) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 37
def add_to_ignore(wf, file)
  logger.debug { "Adding path to ignore cache: '#{file.path}'" }
  @ignored_file_paths_by_watch_folder[wf] << file.path
end
initialize_logger(args = { }) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 27
def initialize_logger(args = { })
  @logger = args[:logger] ||= Logger.new(args[:log_to] || STDOUT)
  log_level = args[:log_level]
  if log_level
    @logger.level = log_level
    args[:logger] = @logger
  end
  @logger
end
process_file(watch_folder, file, storage_id = nil, quarantine_directory_path = nil) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 42
def process_file(watch_folder, file, storage_id = nil, quarantine_directory_path = nil)
  full_file_path = File.join(watch_folder.path, file.path)
  if storage_id && agent.upload(file_path: full_file_path, storage_id: storage_id)
    FileUtils.rm full_file_path
  else
    if Dir.exist?(quarantine_directory_path)
      FileUtils.mv full_file_path, quarantine_directory_path
    else
      add_to_ignore(watch_folder, file)
    end
  end
end
process_watch_folder(wf) click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 55
def process_watch_folder(wf)
  storage_id = wf.definition['upload_to_storage_id'] || wf.definition['storage_id']
  quarantine_directory_path = wf.definition['quarantine_path']
  exclude = wf.definition['exclude']
  min_stable_poll_count = wf.definition['stable_poll_count'] || 3

  maps = wf.state.details[:maps]
  stable_paths = maps[:stable]
  ignored_files = @ignored_file_paths_by_watch_folder[wf]
  stable_paths.each do |fp, file|
    if exclude
      next if ignored_files.include?(file.path)
      if [*exclude].find { |ep| File.fnmatch(ep, file.path) }
        add_to_ignore(wf, file)
        # logger.debug { "Adding File to Ignore Cache: '#{file.path}'"}
        # ignored_files << file.path
        next
      end
    end

    # full_file_path = File.join(wf.path, file.path)

    # pp file
    # puts file_path
    stable_poll_count = file[:stable_poll_count]

    if stable_poll_count && stable_poll_count > min_stable_poll_count
      process_file(wf, file, storage_id, quarantine_directory_path)
    end
  end

  # process_watch_folder
end
run() click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 89
def run
  # AWF.run_once(watch_folders) { |wf| pp wf }
  AWF.run(watch_folders) { |wf| process_watch_folder(wf) }
end
run_once() click to toggle source
# File lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb, line 94
def run_once
  AWF.run_once(watch_folders) { |wf| process_watch_folder(wf) }
end