Module: Datadog::CI::Utils::FileStorage

Defined in:
lib/datadog/ci/utils/file_storage.rb

Overview

FileStorage module provides functionality for storing and retrieving arbitrary Ruby objects in a temp file to share them between processes.

Constant Summary collapse

TEMP_DIR =
File.join(Dir.tmpdir, "datadog-ci-storage")

Class Method Summary collapse

Class Method Details

.cleanupObject



36
37
38
39
40
41
42
43
44
# File 'lib/datadog/ci/utils/file_storage.rb', line 36

def self.cleanup
  return false unless Dir.exist?(TEMP_DIR)

  FileUtils.rm_rf(TEMP_DIR)
  true
rescue => e
  Datadog.logger.error("Failed to cleanup storage directory: #{e.class} - #{e.message}")
  false
end

.ensure_temp_dir_existsObject



46
47
48
# File 'lib/datadog/ci/utils/file_storage.rb', line 46

def self.ensure_temp_dir_exists
  FileUtils.mkdir_p(TEMP_DIR) unless Dir.exist?(TEMP_DIR)
end

.file_path_for(key) ⇒ Object



50
51
52
53
# File 'lib/datadog/ci/utils/file_storage.rb', line 50

def self.file_path_for(key)
  sanitized_key = key.to_s.gsub(/[^a-zA-Z0-9_-]/, "_")
  File.join(TEMP_DIR, "dd-ci-#{sanitized_key}.dat")
end

.retrieve(key) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/datadog/ci/utils/file_storage.rb', line 26

def self.retrieve(key)
  file_path = file_path_for(key)
  return nil unless File.exist?(file_path)

  Marshal.load(File.binread(file_path))
rescue => e
  Datadog.logger.error("Failed to retrieve data for key '#{key}': #{e.class} - #{e.message}")
  nil
end

.store(key, value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/datadog/ci/utils/file_storage.rb', line 14

def self.store(key, value)
  ensure_temp_dir_exists
  file_path = file_path_for(key)

  File.binwrite(file_path, Marshal.dump(value))

  true
rescue => e
  Datadog.logger.error("Failed to store data for key '#{key}': #{e.class} - #{e.message}")
  false
end