Class: Aspera::TempFileManager
- Inherits:
-
Object
- Object
- Aspera::TempFileManager
- 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
-
#cleanup ⇒ Object
call this on process exit.
- #cleanup_expired(temp_folder) ⇒ Object
-
#initialize ⇒ TempFileManager
constructor
A new instance of TempFileManager.
-
#new_file_path_global(base_name) ⇒ Object
same as above but in global temp folder.
-
#new_file_path_in_folder(temp_folder, add_base = '') ⇒ Object
ensure that provided folder exists, or create it, generate a unique filename.
Constructor Details
#initialize ⇒ TempFileManager
Returns a new instance of TempFileManager.
17 18 19 |
# File 'lib/aspera/temp_file_manager.rb', line 17 def initialize @created_files = [] end |
Instance Method Details
#cleanup ⇒ Object
call this on process exit
22 23 24 25 26 27 |
# File 'lib/aspera/temp_file_manager.rb', line 22 def cleanup @created_files.each do |filepath| File.delete(filepath) if File.file?(filepath) end @created_files = [] end |
#cleanup_expired(temp_folder) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aspera/temp_file_manager.rb', line 49 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) && (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
39 40 41 42 43 44 45 46 47 |
# File 'lib/aspera/temp_file_manager.rb', line 39 def new_file_path_global(base_name) username = begin Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue StandardError 'unknown_user' end 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
31 32 33 34 35 36 |
# File 'lib/aspera/temp_file_manager.rb', line 31 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) new_file end |