Class: Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb

Constant Summary collapse

AWF =
Envoi::Aspera::WatchService::WatchFolder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ Watcher

Returns a new instance of Watcher.



137
138
139
140
141
142
143
144
145
146
147
148
149
# 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

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



135
136
137
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb', line 135

def agent
  @agent
end

#configObject

Returns the value of attribute config.



135
136
137
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb', line 135

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



135
136
137
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb', line 135

def logger
  @logger
end

#watch_foldersObject

Returns the value of attribute watch_folders.



135
136
137
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders-working.rb', line 135

def watch_folders
  @watch_folders
end

Class Method Details

.run(args) ⇒ Object



224
225
226
227
# 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 = { }) ⇒ Object



229
230
231
232
233
# 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

Instance Method Details

#add_to_ignore(wf, file) ⇒ Object



161
162
163
# 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 = { }) ⇒ Object



151
152
153
154
155
156
157
158
159
# 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) ⇒ Object



165
166
167
168
169
170
171
172
173
# 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) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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

#runObject



215
216
217
218
# 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_onceObject



220
221
222
# 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