Class: Aspera::TempFileManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/temp_file_manager.rb

Overview

create a temp file name for a given folder files can be deleted on process exit by calling cleanup

Instance Method Summary collapse

Constructor Details

#initializeTempFileManager

Returns a new instance of TempFileManager.



15
16
17
# File 'lib/aspera/temp_file_manager.rb', line 15

def initialize
  @created_files=[]
end

Instance Method Details

#cleanupObject

call this on process exit



20
21
22
23
24
25
# File 'lib/aspera/temp_file_manager.rb', line 20

def cleanup
  @created_files.each do |filepath|
    File.delete(filepath) if File.file?(filepath)
  end
  @created_files=[]
end

#cleanup_expired(temp_folder) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aspera/temp_file_manager.rb', line 42

def cleanup_expired(temp_folder)
  # garbage collect undeleted files
  Dir.entries(temp_folder).each do |name|
    file_path=File.join(temp_folder,name)
    age_sec=(Time.now - File.stat(file_path).mtime).to_i
    # check age of file, delete too old
    if File.file?(file_path) and age_sec > FILE_LIST_AGE_MAX_SEC
      Log.log.debug("garbage collecting #{name}")
      File.delete(file_path)
    end
  end

end

#new_file_path_global(base_name) ⇒ Object

same as above but in global temp folder



37
38
39
40
# File 'lib/aspera/temp_file_manager.rb', line 37

def new_file_path_global(base_name)
  username = Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue 'unknown_user'
  return new_file_path_in_folder(Etc.systmpdir,base_name+'_'+username+'_')
end

#new_file_path_in_folder(temp_folder, add_base = '') ⇒ Object

ensure that provided folder exists, or create it, generate a unique filename

Returns:

  • path to that unique file



29
30
31
32
33
34
# File 'lib/aspera/temp_file_manager.rb', line 29

def new_file_path_in_folder(temp_folder,add_base='')
  FileUtils.mkdir_p(temp_folder) unless Dir.exist?(temp_folder)
  new_file=File.join(temp_folder,add_base+SecureRandom.uuid)
  @created_files.push(new_file)
  return new_file
end