module ASF::DocumentUtils

Constants

MAX_AGE

Public Class Methods

check_cache(type, cache_dir: ASF::Config.get(:cache), warn: true) click to toggle source

N.B. must check :cache config each time to allow for test overrides check cache age and get settings

# File lib/whimsy/asf/documents.rb, line 13
def self.check_cache(type, cache_dir: ASF::Config.get(:cache), warn: true)
  file, _ = ASF::SVN.listingNames(type, cache_dir)
  mtime = begin
    File.mtime(file)
  rescue Errno::ENOENT
    0
  end
  age = (Time.now - mtime).to_i
  stale = age > MAX_AGE
  if warn && stale
    Wunderbar.warn "Cache for #{type} is older than #{MAX_AGE} seconds"
    # Wunderbar.warn caller(0, 10).join("\n")
  end
  return [cache_dir, stale, file, age]
end
update_cache(type, env, cache_dir: ASF::Config.get(:cache)) click to toggle source

N.B. must check :cache config each time to allow for test overrides create/update cache file

# File lib/whimsy/asf/documents.rb, line 31
def self.update_cache(type, env, cache_dir: ASF::Config.get(:cache))
  _cache_dir, stale, file, age = check_cache(type, cache_dir: cache_dir, warn: false)
  if stale
    require 'whimsy/asf/rack'
    ASF::Auth.decode(env)
    # TODO: Downdate to info
    Wunderbar.warn "Updating listing #{file} #{age} as #{env.user}"
    filerev, svnrev = ASF::SVN.updatelisting(type, env.user, env.password, false, cache_dir)
    if filerev && svnrev # it worked
      FileUtils.touch file # last time it was checked
    else
      # raise IOError.new("Failed to fetch iclas.txt: #{svnrev}")
      Wunderbar.warn("User #{env.user}: failed to update #{type}: #{svnrev}")
    end
  end
end