Class: Envoi::Mam::Cantemo::Agent::WatchFolderHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb,
lib/envoi/mam/cantemo/agent/watch_folder_handler-working.rb

Constant Summary collapse

AWF =
Envoi::Aspera::WatchService::WatchFolder
LWF =
Envoi::WatchFolderUtility::WatchFolder::Listen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ WatchFolderHandler

Returns a new instance of WatchFolderHandler.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 15

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

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



13
14
15
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 13

def agent
  @agent
end

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 13

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 13

def logger
  @logger
end

#watch_foldersObject

Returns the value of attribute watch_folders.



13
14
15
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 13

def watch_folders
  @watch_folders
end

Class Method Details

.run(args) ⇒ Object

Initialize then run



99
100
101
102
# 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) ⇒ Object



102
103
104
105
106
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 102

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

Instance Method Details

#add_to_ignore(wf, file) ⇒ Object



38
39
40
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 38

def add_to_ignore(wf, file)
  @ignored_file_paths_by_watch_folder[wf] << file.path
end

#initialize_logger(args = { }) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 28

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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.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) }
        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

#runObject



88
89
90
91
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 88

def run
  # AWF.run_once(watch_folders) { |wf| pp wf }
  AWF.run(watch_folders) { |wf| process_watch_folder(wf) }
end

#run_onceObject



93
94
95
# File 'lib/envoi/mam/cantemo/agent/watch_folder_handler_aspera.rb', line 93

def run_once
  AWF.run_once(watch_folders) { |wf| process_watch_folder(wf) }
end