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 Attribute Summary collapse
-
#cleanup_on_exit ⇒ Object
Returns the value of attribute cleanup_on_exit.
-
#global_temp ⇒ Object
Returns the value of attribute global_temp.
Class Method Summary collapse
-
.instance ⇒ TempFileManager
Returns the singleton instance of TempFileManager.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Call this on process exit.
-
#cleanup_expired(temp_folder) ⇒ Object
Garbage collect undeleted files.
- #delete_file(filepath) ⇒ Object
-
#initialize ⇒ TempFileManager
constructor
A new instance of TempFileManager.
-
#new_file_path_global(prefix = nil, suffix: nil) ⇒ Object
Same as above but in global temp folder, with user's name.
-
#new_file_path_in_folder(temp_folder, prefix: nil, suffix: nil) ⇒ Object
Ensure that provided folder exists, or create it, generate a unique filename.
Constructor Details
#initialize ⇒ TempFileManager
Returns a new instance of TempFileManager.
26 27 28 29 30 |
# File 'lib/aspera/temp_file_manager.rb', line 26 def initialize @created_files = [] @cleanup_on_exit = true @global_temp = Etc.systmpdir end |
Instance Attribute Details
#cleanup_on_exit ⇒ Object
Returns the value of attribute cleanup_on_exit.
23 24 25 |
# File 'lib/aspera/temp_file_manager.rb', line 23 def cleanup_on_exit @cleanup_on_exit end |
#global_temp ⇒ Object
Returns the value of attribute global_temp.
24 25 26 |
# File 'lib/aspera/temp_file_manager.rb', line 24 def global_temp @global_temp end |
Class Method Details
.instance ⇒ TempFileManager
Returns the singleton instance of TempFileManager
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 87 |
# File 'lib/aspera/temp_file_manager.rb', line 14 class TempFileManager include Singleton SEC_IN_DAY = 86_400 # assume no transfer last longer than this # (garbage collect file list which were not deleted after transfer) FILE_LIST_AGE_MAX_SEC = SEC_IN_DAY * 5 private_constant :SEC_IN_DAY, :FILE_LIST_AGE_MAX_SEC attr_accessor :cleanup_on_exit attr_reader :global_temp def initialize @created_files = [] @cleanup_on_exit = true @global_temp = Etc.systmpdir end def global_temp=(value) @global_temp = case value when '@env' then Dir.tmpdir when '@sys' then Etc.systmpdir else value end end def delete_file(filepath) File.delete(filepath) if @cleanup_on_exit rescue => e Log.log.warn{"Problem deleting file: #{filepath}: #{e.}"} end # Call this on process exit def cleanup @created_files.each do |filepath| delete_file(filepath) if File.file?(filepath) end @created_files = [] end # Ensure that provided folder exists, or create it, generate a unique filename # @return path to that unique file def new_file_path_in_folder(temp_folder, prefix: nil, suffix: nil) FileUtils.mkdir_p(temp_folder) new_file = File.join(temp_folder, [prefix, SecureRandom.uuid, suffix].compact.join('-')) @created_files.push(new_file) new_file end # Same as above but in global temp folder, with user's name def new_file_path_global(prefix = nil, suffix: nil) username = begin Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue StandardError 'unknown_user' end prefix = [prefix, username].compact.join('-') new_file_path_in_folder(@global_temp, prefix: prefix, suffix: suffix) end # Garbage collect undeleted files def cleanup_expired(temp_folder) 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}"} delete_file(file_path) end end end end |
Instance Method Details
#cleanup ⇒ Object
Call this on process exit
47 48 49 50 51 52 |
# File 'lib/aspera/temp_file_manager.rb', line 47 def cleanup @created_files.each do |filepath| delete_file(filepath) if File.file?(filepath) end @created_files = [] end |
#cleanup_expired(temp_folder) ⇒ Object
Garbage collect undeleted files
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/aspera/temp_file_manager.rb', line 76 def cleanup_expired(temp_folder) 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}"} delete_file(file_path) end end end |
#delete_file(filepath) ⇒ Object
40 41 42 43 44 |
# File 'lib/aspera/temp_file_manager.rb', line 40 def delete_file(filepath) File.delete(filepath) if @cleanup_on_exit rescue => e Log.log.warn{"Problem deleting file: #{filepath}: #{e.}"} end |
#new_file_path_global(prefix = nil, suffix: nil) ⇒ Object
Same as above but in global temp folder, with user's name
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/aspera/temp_file_manager.rb', line 64 def new_file_path_global(prefix = nil, suffix: nil) username = begin Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue StandardError 'unknown_user' end prefix = [prefix, username].compact.join('-') new_file_path_in_folder(@global_temp, prefix: prefix, suffix: suffix) end |
#new_file_path_in_folder(temp_folder, prefix: nil, suffix: nil) ⇒ Object
Ensure that provided folder exists, or create it, generate a unique filename
56 57 58 59 60 61 |
# File 'lib/aspera/temp_file_manager.rb', line 56 def new_file_path_in_folder(temp_folder, prefix: nil, suffix: nil) FileUtils.mkdir_p(temp_folder) new_file = File.join(temp_folder, [prefix, SecureRandom.uuid, suffix].compact.join('-')) @created_files.push(new_file) new_file end |