class Watcher

Constants

AWF

Attributes

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

Public Class Methods

new(args = { }) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 137
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)
  pp watch_folders
end
run(args) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 224
def self.run(args)
  w = self.new(args)
  w.run
end
run_as_daemon(args, options = { }) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 229
def self.run_as_daemon(args, options = { })
  # ARGV.unshift 'run' unless %w(start stop restart run zap killall status).include? ARGV.first
  require 'daemons'
  Daemons.run_proc('cantemo-portal-agent-watch-folders', options) { self.run(args) }
end

Public Instance Methods

add_to_ignore(wf, file) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 161
def add_to_ignore(wf, file)
  @ignored_file_paths_by_watch_folder[wf] << file.path
end
initialize_logger(args = { }) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 151
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, full_file_path, storage_id = nil, quarantine_directory_path = nil) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 165
def process_file(watch_folder, full_file_path, storage_id = nil, quarantine_directory_path = nil)
  return unless storage_id

  if agent.upload(file_path: full_file_path, storage_id: storage_id)
    FileUtils.rm full_file_path
  else
    FileUtils.mv full_file_path, quarantine_directory_path
  end
end
process_watch_folder(wf) click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 175
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) }
        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
      if storage_id
        if agent.upload(file_path: full_file_path, storage_id: storage_id)
          FileUtils.rm full_file_path
        else
          FileUtils.mv full_file_path, quarantine_directory_path
        end
      end

    end
  end

  # process_watch_folder
end
run() click to toggle source
# File lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 215
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/cantemo/portal/agent/cli/commands/watch_folders-working.rb, line 220
def run_once
  AWF.run_once(watch_folders) { |wf| process_watch_folder(wf) }
end